Skip to content

Commit f252553

Browse files
authored
Add Custom Tabs to WebView (#573)
* Add Custom Tabs to WebView * Add License and theme to CustomTabs
1 parent 6d1e357 commit f252553

File tree

6 files changed

+90
-49
lines changed

6 files changed

+90
-49
lines changed

src/main/java/org/amahi/anywhere/AmahiModule.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.amahi.anywhere.activity.ServerFileVideoActivity;
3434
import org.amahi.anywhere.activity.ServerFileWebActivity;
3535
import org.amahi.anywhere.activity.ServerFilesActivity;
36+
import org.amahi.anywhere.activity.WebViewActivity;
3637
import org.amahi.anywhere.cache.CacheModule;
3738
import org.amahi.anywhere.fragment.AudioListFragment;
3839
import org.amahi.anywhere.fragment.NavigationFragment;
@@ -99,6 +100,7 @@
99100
VideoService.class,
100101
MainTVFragment.class,
101102
TVWebViewActivity.class,
103+
WebViewActivity.class,
102104
ServerFileTvFragment.class,
103105
UploadService.class,
104106
DownloadService.class,

src/main/java/org/amahi/anywhere/activity/ServerFileWebActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public void onServiceDisconnected(ComponentName name) {
117117
CustomTabsClient.bindCustomTabsService(this, getPackageName(), mCustomTabsServiceConnection);
118118

119119
mCustomTabsIntent = new CustomTabsIntent.Builder(mCustomTabsSession)
120+
.setToolbarColor(getResources().getColor(R.color.primary))
120121
.setShowTitle(true)
121122
.build();
122123

src/main/java/org/amahi/anywhere/activity/WebViewActivity.java

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,92 @@
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+
120
package org.amahi.anywhere.activity;
221

22+
import android.content.ComponentName;
323
import android.content.Context;
24+
import android.net.Uri;
425
import android.os.Bundle;
526
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;
1031

32+
import org.amahi.anywhere.AmahiApplication;
1133
import org.amahi.anywhere.R;
34+
import org.amahi.anywhere.util.Constants;
1235
import org.amahi.anywhere.util.LocaleHelper;
1336

1437
public class WebViewActivity extends AppCompatActivity {
1538

16-
private WebView webView;
39+
CustomTabsClient mCustomTabsClient;
40+
CustomTabsSession mCustomTabsSession;
41+
CustomTabsServiceConnection mCustomTabsServiceConnection;
42+
CustomTabsIntent mCustomTabsIntent;
1743

1844
@Override
1945
protected void onCreate(Bundle savedInstanceState) {
2046
super.onCreate(savedInstanceState);
2147

22-
setUpHomeNavigation();
2348
setContentView(R.layout.activity_web_view);
24-
webView = findViewById(R.id.webview);
2549

26-
loadWebView("https://www.amahi.org/android");
50+
setUpInjections();
51+
52+
setUpCustomTabs(Constants.amahiAndroidUrl);
2753

2854
}
2955

30-
private void loadWebView(String url) {
31-
webView.setWebViewClient(new WebViewClient());
56+
private void setUpInjections() {
57+
AmahiApplication.from(this).inject(this);
58+
}
3259

33-
WebSettings settings = webView.getSettings();
60+
private void setUpCustomTabs(String url) {
3461

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+
}
3869

39-
webView.loadUrl(url);
70+
@Override
71+
public void onServiceDisconnected(ComponentName name) {
72+
mCustomTabsClient = null;
73+
}
74+
};
4075

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();
4282

43-
private void setUpHomeNavigation() {
44-
getSupportActionBar().setHomeButtonEnabled(true);
83+
mCustomTabsIntent.launchUrl(this, Uri.parse(url));
4584
}
4685

4786
@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();
5790
}
5891

5992
@Override

src/main/java/org/amahi/anywhere/util/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,6 @@ public class Constants {
4242
public static final String successIntentExtra = "success";
4343

4444
public static final String sharesPathAppendLoc = "shares";
45+
46+
public static final String amahiAndroidUrl = "https://www.amahi.org/android";
4547
}

src/main/res/layout/activity_server_file_web.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@
2222
android:layout_width="fill_parent"
2323
android:layout_height="fill_parent"
2424
android:inAnimation="@android:anim/fade_in"
25-
android:outAnimation="@android:anim/fade_out"></ViewAnimator>
25+
android:outAnimation="@android:anim/fade_out"/>
Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:tools="http://schemas.android.com/tools"
4-
android:id="@+id/activity_main"
5-
android:layout_width="match_parent"
6-
android:layout_height="match_parent"
7-
android:background="@color/background_secondary"
8-
tools:context="org.amahi.anywhere.activity.WebViewActivity">
1+
<?xml version="1.0" encoding="utf-8"?><!--
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+
-->
919

10-
<WebView
11-
android:id="@+id/webview"
12-
android:layout_width="fill_parent"
13-
android:layout_height="fill_parent"
14-
android:background="@color/background_secondary" />
1520

16-
<ProgressBar
17-
android:id="@+id/progress_webview"
18-
android:layout_width="wrap_content"
19-
android:layout_height="wrap_content"
20-
android:layout_centerInParent="true"
21-
android:layout_gravity="center"
22-
android:visibility="gone" />
23-
</RelativeLayout>
21+
<ViewAnimator xmlns:android="http://schemas.android.com/apk/res/android"
22+
android:id="@+id/animator"
23+
android:layout_width="fill_parent"
24+
android:layout_height="fill_parent"
25+
android:inAnimation="@android:anim/fade_in"
26+
android:outAnimation="@android:anim/fade_out"/>

0 commit comments

Comments
 (0)