content: /*
   * Copyright (c) 2014 Amahi
   *
   * This file is part of Amahi.
   *
   * Amahi is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * Amahi is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with Amahi. If not, see <http ://www.gnu.org/licenses/>.
   */
  
  package org.amahi.anywhere.activity;
  
  import android.content.ComponentName;
  import android.content.Context;
  import android.net.Uri;
  import android.os.Bundle;
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.browser.customtabs.CustomTabsClient;
  import androidx.browser.customtabs.CustomTabsIntent;
  import androidx.browser.customtabs.CustomTabsServiceConnection;
  import androidx.browser.customtabs.CustomTabsSession;
  
  import org.amahi.anywhere.AmahiApplication;
  import org.amahi.anywhere.R;
  import org.amahi.anywhere.util.Constants;
  import org.amahi.anywhere.util.LocaleHelper;
  
  public class WebViewActivity extends AppCompatActivity {
  
      CustomTabsClient mCustomTabsClient;
      CustomTabsSession mCustomTabsSession;
      CustomTabsServiceConnection mCustomTabsServiceConnection;
      CustomTabsIntent mCustomTabsIntent;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.activity_web_view);
  
          setUpInjections();
  
          setUpCustomTabs(Constants.amahiAndroidUrl);
  
      }
  
      private void setUpInjections() {
          AmahiApplication.from(this).inject(this);
      }
  
      private void setUpCustomTabs(String url) {
  
          mCustomTabsServiceConnection = new CustomTabsServiceConnection() {
              @Override
              public void onCustomTabsServiceConnected(ComponentName componentName, CustomTabsClient customTabsClient) {
                  mCustomTabsClient = customTabsClient;
                  mCustomTabsClient.warmup(0L);
                  mCustomTabsSession = mCustomTabsClient.newSession(null);
              }
  
              @Override
              public void onServiceDisconnected(ComponentName name) {
                  mCustomTabsClient = null;
              }
          };
  
          CustomTabsClient.bindCustomTabsService(this, getPackageName(), mCustomTabsServiceConnection);
  
          mCustomTabsIntent = new CustomTabsIntent.Builder(mCustomTabsSession)
              .setShowTitle(true)
              .setToolbarColor(getResources().getColor(R.color.primary))
              .build();
  
          mCustomTabsIntent.launchUrl(this, Uri.parse(url));
      }
  
      @Override
      protected void onResume() {
          super.onResume();
          onBackPressed();
      }
  
      @Override
      protected void attachBaseContext(Context newBase) {
          super.attachBaseContext(LocaleHelper.onAttach(newBase));
      }
  }
  