From 67c416f8703450fa2710f84cffddf7cf5ca0bced Mon Sep 17 00:00:00 2001 From: Gregor Santner Date: Sun, 18 Sep 2016 23:17:18 +0200 Subject: [PATCH] Preference option - Chrome Custom Tab --- .../diaspora_android/data/AppSettings.java | 4 ++ .../receivers/OpenExternalLinkReceiver.java | 49 +++++++++++------ .../dfa/diaspora_android/util/Helpers.java | 29 ++++++---- .../drawable-hdpi/chrome_custom_tab__back.png | Bin 0 -> 148 bytes .../drawable-mdpi/chrome_custom_tab__back.png | Bin 0 -> 115 bytes .../chrome_custom_tab__back.png | Bin 0 -> 131 bytes .../chrome_custom_tab__back.png | Bin 0 -> 191 bytes .../chrome_custom_tab__back.png | Bin 0 -> 194 bytes .../main/res/values/strings-preferences.xml | 5 ++ app/src/main/res/values/strings.xml | 1 - app/src/main/res/xml/preferences.xml | 50 ++++++++++-------- 11 files changed, 89 insertions(+), 49 deletions(-) create mode 100644 app/src/main/res/drawable-hdpi/chrome_custom_tab__back.png create mode 100644 app/src/main/res/drawable-mdpi/chrome_custom_tab__back.png create mode 100644 app/src/main/res/drawable-xhdpi/chrome_custom_tab__back.png create mode 100644 app/src/main/res/drawable-xxhdpi/chrome_custom_tab__back.png create mode 100644 app/src/main/res/drawable-xxxhdpi/chrome_custom_tab__back.png diff --git a/app/src/main/java/com/github/dfa/diaspora_android/data/AppSettings.java b/app/src/main/java/com/github/dfa/diaspora_android/data/AppSettings.java index 6c25f930..e4ba46a7 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/data/AppSettings.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/data/AppSettings.java @@ -256,6 +256,10 @@ public class AppSettings { return getBoolean(prefApp, R.string.pref_key__intellihide_toolbars, true); } + public boolean isChromeCustomTabsEnabled() { + return getBoolean(prefApp, R.string.pref_key__chrome_custom_tabs_enabled, true); + } + public boolean isVisibleInNavExit() { return getBoolean(prefApp, R.string.pref_key__visibility_nav__exit, false); } diff --git a/app/src/main/java/com/github/dfa/diaspora_android/receivers/OpenExternalLinkReceiver.java b/app/src/main/java/com/github/dfa/diaspora_android/receivers/OpenExternalLinkReceiver.java index b9cd46bf..3bc7d492 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/receivers/OpenExternalLinkReceiver.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/receivers/OpenExternalLinkReceiver.java @@ -7,14 +7,15 @@ import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; -import android.os.Build; import android.support.customtabs.CustomTabsIntent; import com.github.dfa.diaspora_android.App; import com.github.dfa.diaspora_android.R; import com.github.dfa.diaspora_android.activity.MainActivity; +import com.github.dfa.diaspora_android.data.AppSettings; import com.github.dfa.diaspora_android.util.CustomTabHelpers.BrowserFallback; import com.github.dfa.diaspora_android.util.CustomTabHelpers.CustomTabActivityHelper; +import com.github.dfa.diaspora_android.util.Helpers; import com.github.dfa.diaspora_android.util.Log; /** @@ -29,22 +30,38 @@ public class OpenExternalLinkReceiver extends BroadcastReceiver { } @Override - public void onReceive(Context context, Intent intent) { - String url = intent.getStringExtra(MainActivity.EXTRA_URL); + public void onReceive(Context c, Intent receiveIntent) { + AppSettings settings = new AppSettings(c); + Log.v(App.TAG, "OpenExternalLinkReceiver.onReceive(): url"); - if(url != null) { - CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder(); - if(Build.VERSION.SDK_INT >= 23) { - intentBuilder.setToolbarColor(context.getResources().getColor(R.color.colorPrimary, context.getTheme())); - } else { - intentBuilder.setToolbarColor(context.getResources().getColor(R.color.colorPrimary)); - } - intentBuilder.setStartAnimations(context, android.R.anim.slide_in_left, android.R.anim.fade_out); - intentBuilder.setExitAnimations(context, android.R.anim.fade_in, android.R.anim.slide_out_right); - Bitmap backButtonIcon = BitmapFactory.decodeResource(context.getResources(), - R.drawable.ic_arrow_back_white_24px); - intentBuilder.setCloseButtonIcon(backButtonIcon); - CustomTabActivityHelper.openCustomTab(parent, intentBuilder.build(), Uri.parse(url), new BrowserFallback()); + + Uri url = null; + try { + String sUrl = receiveIntent.getStringExtra(MainActivity.EXTRA_URL); + url = Uri.parse(sUrl); + } catch (Exception _ignored) { + Log.v(App.TAG, "Could not open Chrome Custom Tab (bad URL)"); + return; + } + + if (settings.isChromeCustomTabsEnabled()) { + // Setup Chrome Custom Tab + CustomTabsIntent.Builder customTab = new CustomTabsIntent.Builder(); + customTab.setToolbarColor(Helpers.getColorFromRessource(c, R.color.colorPrimary)); + customTab.setStartAnimations(c, android.R.anim.slide_in_left, android.R.anim.fade_out); + customTab.setExitAnimations(c, android.R.anim.fade_in, android.R.anim.slide_out_right); + customTab.addDefaultShareMenuItem(); + + Bitmap backButtonIcon = BitmapFactory.decodeResource(c.getResources(), R.drawable.chrome_custom_tab__back); + customTab.setCloseButtonIcon(backButtonIcon); + + // Launch Chrome Custom Tab + CustomTabActivityHelper.openCustomTab(parent, customTab.build(), url, new BrowserFallback()); + } else { + // Open in normal browser (via intent) + Intent openBrowserIntent = new Intent(Intent.ACTION_VIEW, url); + openBrowserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + c.startActivity(openBrowserIntent); } } } diff --git a/app/src/main/java/com/github/dfa/diaspora_android/util/Helpers.java b/app/src/main/java/com/github/dfa/diaspora_android/util/Helpers.java index 2b296014..ea8137b4 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/util/Helpers.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/util/Helpers.java @@ -21,10 +21,11 @@ package com.github.dfa.diaspora_android.util; import android.app.Activity; import android.content.Context; import android.content.Intent; +import android.content.res.Resources; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.os.Environment; -import com.github.dfa.diaspora_android.util.Log; import com.github.dfa.diaspora_android.App; import com.github.dfa.diaspora_android.R; @@ -37,7 +38,6 @@ import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; import java.util.Locale; -import java.util.Objects; public class Helpers { @@ -51,6 +51,15 @@ public class Helpers { } } + public static int getColorFromRessource(Context context, int ressourceId) { + Resources res = context.getResources(); + if (Build.VERSION.SDK_INT >= 23) { + return res.getColor(ressourceId, context.getTheme()); + } else { + return res.getColor(ressourceId); + } + } + public static void loadUrlInExternalBrowser(Context context, String url) { try { Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); @@ -66,9 +75,9 @@ public class Helpers { Log.d(App.TAG, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath()); File storageDir = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES); - return new File ( + return new File( imageFileName + /* prefix */ - ".jpg", /* suffix */ + ".jpg", /* suffix */ storageDir.getAbsolutePath() /* directory */ ); } @@ -100,22 +109,22 @@ public class Helpers { return sb.toString(); } - public static String hexColorFromRessourceColor(Context context, int idColor){ + public static String hexColorFromRessourceColor(Context context, int idColor) { return "#" + Integer.toHexString(context.getResources().getColor(idColor) & 0x00ffffff); } public static void printBundle(Bundle savedInstanceState, String k) { - if(savedInstanceState != null) { + if (savedInstanceState != null) { for (String key : savedInstanceState.keySet()) { - Log.d("SAVED", key + " is a key in the bundle "+k); + Log.d("SAVED", key + " is a key in the bundle " + k); Object bun = savedInstanceState.get(key); - if(bun != null) { + if (bun != null) { if (bun instanceof Bundle) { printBundle((Bundle) bun, k + "." + key); } else if (bun instanceof byte[]) { - Log.d("SAVED", "Key: "+k + "." + key+": "+ Arrays.toString((byte[])bun)); + Log.d("SAVED", "Key: " + k + "." + key + ": " + Arrays.toString((byte[]) bun)); } else { - Log.d("SAVED", "Key: "+k + "." + key+": "+ bun.toString()); + Log.d("SAVED", "Key: " + k + "." + key + ": " + bun.toString()); } } } diff --git a/app/src/main/res/drawable-hdpi/chrome_custom_tab__back.png b/app/src/main/res/drawable-hdpi/chrome_custom_tab__back.png new file mode 100644 index 0000000000000000000000000000000000000000..cd1972677699802e4ef9723ea50fcb284f9a2d9e GIT binary patch literal 148 zcmeAS@N?(olHy`uVBq!ia0vp^Dj>|k0wldT1B8K8w5N+>NCjiE#JmKNKlwlIJIE$G zENCoBR!I0=pY}@Pupe*oHeRjdZG1q?qm{hxZ$A4bPKTOf6Ie1b1fIqz%iQ?l<@&`u s>vn;8mbGbb(!%VA{yVIsrX`2~&0ZQe`%OT@F`zXJp00i_>zopr05HKY)c^nh literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-mdpi/chrome_custom_tab__back.png b/app/src/main/res/drawable-mdpi/chrome_custom_tab__back.png new file mode 100644 index 0000000000000000000000000000000000000000..4ef72eec99423c5d4f83227e34b24835a79f324f GIT binary patch literal 115 zcmeAS@N?(olHy`uVBq!ia0vp^5+KaM0wlfaz7_*1D^C~4kP60RiG2_15C3<&H0A&Q z|Ihd9Wu7|h-d41yp@_F-ZcDVc#LB3;|NKk4rwLrznlW+N(!?KWaSTDLrmhl;G7;?6Tmes9(^C#@)&$XLAN2fmDK5_2;oXs^qKRrD? zT|a&wl=0u{XMX3o{EI(zZSFU;t5*oK`xGb|A7l~sF+e3G`eLnqRNcP#_|M_{efOCe b7~U|>UYh-J>#;zfqZmA0{an^LB{Ts5m>*B+ literal 0 HcmV?d00001 diff --git a/app/src/main/res/values/strings-preferences.xml b/app/src/main/res/values/strings-preferences.xml index e0074ca7..cf1d1fea 100644 --- a/app/src/main/res/values/strings-preferences.xml +++ b/app/src/main/res/values/strings-preferences.xml @@ -14,6 +14,7 @@ pref_key_category_network pref_key_load_images pref_key_clear_cache + pref_key__chrome_custom_tabs_enabled pref_key_append_shared_via_app pref_key_proxy_enabled @@ -82,6 +83,10 @@ Port + + Chrome Custom Tabs + Open external links with Chrome Custom Tabs. Chromium or Google Chrome needs to be installed for this feature + Personal settings Open your Diaspora account settings diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index dc345adf..aa23ef1f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -103,5 +103,4 @@ Diaspora. In the permissions section you can grant the \"write storage permission\". Permission denied. Permission granted. Please try again. - diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 843c5a2e..0caf75e3 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -16,43 +16,43 @@ + android:title="@string/nav_profile"/> + android:title="@string/nav_followed_tags"/> + android:title="@string/nav_aspects"/> + android:title="@string/nav_activities"/> + android:title="@string/nav_liked"/> + android:title="@string/nav_commented"/> + android:title="@string/nav_mentions"/> + android:title="@string/nav_public_activities"/> + android:title="@string/action_exit_app"/> + android:title="@string/nav_help_license"/> @@ -63,19 +63,25 @@ android:entryValues="@array/pref_entries_values__font_size" android:key="@string/pref_key__font_size" android:summary="%s" - android:title="@string/pref_title__font_size" /> + android:title="@string/pref_title__font_size"/> + android:title="@string/pref_title__intellihide_toolbars"/> + android:title="@string/pref_title__append_shared_via_app"/> + + @@ -86,22 +92,22 @@ + android:title="@string/pref_title__personal_settings"/> + android:title="@string/pref_title__manage_tags"/> + android:title="@string/pref_title__manage_contacts"/> + android:title="@string/pref_title__change_account"/> @@ -112,27 +118,27 @@ android:defaultValue="true" android:key="@string/pref_key__load_images" android:summary="@string/pref_desc__load_images" - android:title="@string/pref_title__load_images" /> + android:title="@string/pref_title__load_images"/> + android:title="@string/pref_title__clear_cache"/> + android:title="@string/pref_title__proxy_enabled"/> + android:title="@string/pref_title__proxy_host"/> + android:title="@string/pref_title__proxy_port"/> \ No newline at end of file