|
| 1 | +/* |
| 2 | + * Copyright (c) 2014 Amahi |
| 3 | + * |
| 4 | + * This file is part of Amahi. |
| 5 | + * |
| 6 | + * Amahi is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * Amahi is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with Amahi. If not, see <http ://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
1 | 20 | package org.amahi.anywhere.activity; |
2 | 21 |
|
| 22 | +import android.content.ComponentName; |
3 | 23 | import android.content.Context; |
| 24 | +import android.net.Uri; |
4 | 25 | import android.os.Bundle; |
5 | 26 | import androidx.appcompat.app.AppCompatActivity; |
6 | | -import android.view.MenuItem; |
7 | | -import android.webkit.WebSettings; |
8 | | -import android.webkit.WebView; |
9 | | -import android.webkit.WebViewClient; |
| 27 | +import androidx.browser.customtabs.CustomTabsClient; |
| 28 | +import androidx.browser.customtabs.CustomTabsIntent; |
| 29 | +import androidx.browser.customtabs.CustomTabsServiceConnection; |
| 30 | +import androidx.browser.customtabs.CustomTabsSession; |
10 | 31 |
|
| 32 | +import org.amahi.anywhere.AmahiApplication; |
11 | 33 | import org.amahi.anywhere.R; |
| 34 | +import org.amahi.anywhere.util.Constants; |
12 | 35 | import org.amahi.anywhere.util.LocaleHelper; |
13 | 36 |
|
14 | 37 | public class WebViewActivity extends AppCompatActivity { |
15 | 38 |
|
16 | | - private WebView webView; |
| 39 | + CustomTabsClient mCustomTabsClient; |
| 40 | + CustomTabsSession mCustomTabsSession; |
| 41 | + CustomTabsServiceConnection mCustomTabsServiceConnection; |
| 42 | + CustomTabsIntent mCustomTabsIntent; |
17 | 43 |
|
18 | 44 | @Override |
19 | 45 | protected void onCreate(Bundle savedInstanceState) { |
20 | 46 | super.onCreate(savedInstanceState); |
21 | 47 |
|
22 | | - setUpHomeNavigation(); |
23 | 48 | setContentView(R.layout.activity_web_view); |
24 | | - webView = findViewById(R.id.webview); |
25 | 49 |
|
26 | | - loadWebView("https://www.amahi.org/android"); |
| 50 | + setUpInjections(); |
| 51 | + |
| 52 | + setUpCustomTabs(Constants.amahiAndroidUrl); |
27 | 53 |
|
28 | 54 | } |
29 | 55 |
|
30 | | - private void loadWebView(String url) { |
31 | | - webView.setWebViewClient(new WebViewClient()); |
| 56 | + private void setUpInjections() { |
| 57 | + AmahiApplication.from(this).inject(this); |
| 58 | + } |
32 | 59 |
|
33 | | - WebSettings settings = webView.getSettings(); |
| 60 | + private void setUpCustomTabs(String url) { |
34 | 61 |
|
35 | | - settings.setLoadWithOverviewMode(true); |
36 | | - settings.setBuiltInZoomControls(true); |
37 | | - settings.setUseWideViewPort(true); |
| 62 | + mCustomTabsServiceConnection = new CustomTabsServiceConnection() { |
| 63 | + @Override |
| 64 | + public void onCustomTabsServiceConnected(ComponentName componentName, CustomTabsClient customTabsClient) { |
| 65 | + mCustomTabsClient = customTabsClient; |
| 66 | + mCustomTabsClient.warmup(0L); |
| 67 | + mCustomTabsSession = mCustomTabsClient.newSession(null); |
| 68 | + } |
38 | 69 |
|
39 | | - webView.loadUrl(url); |
| 70 | + @Override |
| 71 | + public void onServiceDisconnected(ComponentName name) { |
| 72 | + mCustomTabsClient = null; |
| 73 | + } |
| 74 | + }; |
40 | 75 |
|
41 | | - } |
| 76 | + CustomTabsClient.bindCustomTabsService(this, getPackageName(), mCustomTabsServiceConnection); |
| 77 | + |
| 78 | + mCustomTabsIntent = new CustomTabsIntent.Builder(mCustomTabsSession) |
| 79 | + .setShowTitle(true) |
| 80 | + .setToolbarColor(getResources().getColor(R.color.primary)) |
| 81 | + .build(); |
42 | 82 |
|
43 | | - private void setUpHomeNavigation() { |
44 | | - getSupportActionBar().setHomeButtonEnabled(true); |
| 83 | + mCustomTabsIntent.launchUrl(this, Uri.parse(url)); |
45 | 84 | } |
46 | 85 |
|
47 | 86 | @Override |
48 | | - public boolean onOptionsItemSelected(MenuItem menuItem) { |
49 | | - switch (menuItem.getItemId()) { |
50 | | - case android.R.id.home: |
51 | | - finish(); |
52 | | - return true; |
53 | | - |
54 | | - default: |
55 | | - return super.onOptionsItemSelected(menuItem); |
56 | | - } |
| 87 | + protected void onResume() { |
| 88 | + super.onResume(); |
| 89 | + onBackPressed(); |
57 | 90 | } |
58 | 91 |
|
59 | 92 | @Override |
|
0 commit comments