From 1261d93a179ee414df6630e33948de9bf9f9b9d7 Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Sun, 11 Sep 2016 15:22:16 +0200 Subject: [PATCH] Moved TitleUpdateReceiver in own class and added some debug logs --- .../activity/MainActivity.java | 55 ++++--------- .../receivers/OpenExternalLinkReceiver.java | 15 ++-- .../receivers/UpdateTitleReceiver.java | 79 +++++++++++++++++++ 3 files changed, 102 insertions(+), 47 deletions(-) create mode 100644 app/src/main/java/com/github/dfa/diaspora_android/receivers/UpdateTitleReceiver.java diff --git a/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java b/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java index 1c36d6f2..d9b7999e 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java @@ -80,6 +80,7 @@ import com.github.dfa.diaspora_android.data.AppSettings; import com.github.dfa.diaspora_android.data.PodUserProfile; import com.github.dfa.diaspora_android.listener.WebUserProfileChangedListener; import com.github.dfa.diaspora_android.receivers.OpenExternalLinkReceiver; +import com.github.dfa.diaspora_android.receivers.UpdateTitleReceiver; import com.github.dfa.diaspora_android.ui.ContextMenuWebView; import com.github.dfa.diaspora_android.ui.CustomWebViewClient; import com.github.dfa.diaspora_android.util.CustomTabHelpers.BrowserFallback; @@ -137,6 +138,7 @@ public class MainActivity extends AppCompatActivity private final Handler uiHandler = new Handler(); private CustomWebViewClient webViewClient; private OpenExternalLinkReceiver brOpenExternalLink; + private BroadcastReceiver brSetTitle; private Snackbar snackbarExitApp; private Snackbar snackbarNewNotification; private Snackbar snackbarNoInternet; @@ -208,6 +210,17 @@ public class MainActivity extends AppCompatActivity } brOpenExternalLink = new OpenExternalLinkReceiver(this); + brSetTitle = new UpdateTitleReceiver(app, urls, new UpdateTitleReceiver.TitleCallback() { + @Override + public void setTitle(int rId) { + MainActivity.this.setTitle(rId); + } + + @Override + public void setTitle(String title) { + MainActivity.this.setTitle(title); + } + }); } private void setupUI(Bundle savedInstanceState) { @@ -219,6 +232,7 @@ public class MainActivity extends AppCompatActivity } else { Log.v(App.TAG, "webViewPlaceholder had no child views"); } + boolean newWebView = (webView == null); if(newWebView) { Log.v(App.TAG, "WebView was null. Create new one."); @@ -701,47 +715,6 @@ public class MainActivity extends AppCompatActivity } } - /** - * BroadcastReceiver that updates the title of the activity based on which url is currently loaded - */ - private final BroadcastReceiver brSetTitle = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - String url = intent.getStringExtra(EXTRA_URL); - if (url != null && url.startsWith(urls.getPodUrl())) { - String subUrl = url.substring((urls.getPodUrl()).length()); - Log.v(App.TAG, "MainActivity.brSetTitle.onReceive(): Set title for subUrl "+subUrl); - if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_STREAM)) { - setTitle(R.string.nav_stream); - } else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_POSTS)) { - setTitle(R.string.diaspora); - } else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_NOTIFICATIONS)) { - setTitle(R.string.notifications); - } else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_CONVERSATIONS)) { - setTitle(R.string.conversations); - } else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_NEW_POST)) { - setTitle(R.string.new_post); - } else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_PEOPLE + appSettings.getProfileId())) { - setTitle(R.string.nav_profile); - } else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_ACTIVITY)) { - setTitle(R.string.nav_activities); - } else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_LIKED)) { - setTitle(R.string.nav_liked); - } else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_COMMENTED)) { - setTitle(R.string.nav_commented); - } else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_MENTIONS)) { - setTitle(R.string.nav_mentions); - } else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_PUBLIC)) { - setTitle(R.string.public_); - } else if (urls.isAspectUrl(url)){ - setTitle(urls.getAspectNameFromUrl(url, app)); - } - } else { - Log.w(App.TAG, "MainActivity.brSetTitle.onReceive(): Invalid url: "+url); - } - } - }; - @Override protected void onStart() { super.onStart(); 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 f3faf58a..b9cd46bf 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 @@ -10,13 +10,15 @@ 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.util.CustomTabHelpers.BrowserFallback; import com.github.dfa.diaspora_android.util.CustomTabHelpers.CustomTabActivityHelper; +import com.github.dfa.diaspora_android.util.Log; /** - * BroadcastReceiver that opens + * BroadcastReceiver that opens links in a Chrome CustomTab * Created by vanitas on 11.09.16. */ public class OpenExternalLinkReceiver extends BroadcastReceiver { @@ -29,16 +31,17 @@ public class OpenExternalLinkReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String url = intent.getStringExtra(MainActivity.EXTRA_URL); + Log.v(App.TAG, "OpenExternalLinkReceiver.onReceive(): url"); if(url != null) { CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder(); if(Build.VERSION.SDK_INT >= 23) { - intentBuilder.setToolbarColor(parent.getResources().getColor(R.color.colorPrimary, parent.getTheme())); + intentBuilder.setToolbarColor(context.getResources().getColor(R.color.colorPrimary, context.getTheme())); } else { - intentBuilder.setToolbarColor(parent.getResources().getColor(R.color.colorPrimary)); + intentBuilder.setToolbarColor(context.getResources().getColor(R.color.colorPrimary)); } - intentBuilder.setStartAnimations(parent, android.R.anim.slide_in_left, android.R.anim.fade_out); - intentBuilder.setExitAnimations(parent, android.R.anim.fade_in, android.R.anim.slide_out_right); - Bitmap backButtonIcon = BitmapFactory.decodeResource(parent.getResources(), + 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()); diff --git a/app/src/main/java/com/github/dfa/diaspora_android/receivers/UpdateTitleReceiver.java b/app/src/main/java/com/github/dfa/diaspora_android/receivers/UpdateTitleReceiver.java new file mode 100644 index 00000000..9a00599b --- /dev/null +++ b/app/src/main/java/com/github/dfa/diaspora_android/receivers/UpdateTitleReceiver.java @@ -0,0 +1,79 @@ +package com.github.dfa.diaspora_android.receivers; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + +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.DiasporaUrlHelper; +import com.github.dfa.diaspora_android.util.Log; + +/** + * BroadcastReceiver used to update the title of the MainActivity depending on the url of the webview + * Created by vanitas on 11.09.16. + */ +public class UpdateTitleReceiver extends BroadcastReceiver { + private DiasporaUrlHelper urls; + private AppSettings appSettings; + private App app; + private TitleCallback callback; + + public UpdateTitleReceiver(App app, DiasporaUrlHelper urls, TitleCallback callback) { + this.urls = urls; + this.app = app; + this.appSettings = app.getSettings(); + this.callback = callback; + } + + @Override + public void onReceive(Context context, Intent intent) { + String url = intent.getStringExtra(MainActivity.EXTRA_URL); + if (url != null && url.startsWith(urls.getPodUrl())) { + String subUrl = url.substring((urls.getPodUrl()).length()); + Log.v(App.TAG, "UpdateTitleReceiver.onReceive(): Set title for subUrl "+subUrl); + if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_STREAM)) { + setTitle(R.string.nav_stream); + } else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_POSTS)) { + setTitle(R.string.diaspora); + } else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_NOTIFICATIONS)) { + setTitle(R.string.notifications); + } else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_CONVERSATIONS)) { + setTitle(R.string.conversations); + } else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_NEW_POST)) { + setTitle(R.string.new_post); + } else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_PEOPLE + appSettings.getProfileId())) { + setTitle(R.string.nav_profile); + } else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_ACTIVITY)) { + setTitle(R.string.nav_activities); + } else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_LIKED)) { + setTitle(R.string.nav_liked); + } else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_COMMENTED)) { + setTitle(R.string.nav_commented); + } else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_MENTIONS)) { + setTitle(R.string.nav_mentions); + } else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_PUBLIC)) { + setTitle(R.string.public_); + } else if (urls.isAspectUrl(url)){ + setTitle(urls.getAspectNameFromUrl(url, app)); + } + } else { + Log.w(App.TAG, "UpdateTitleReceiver.onReceive(): Invalid url: "+url); + } + } + + private void setTitle(int rId) { + callback.setTitle(rId); + } + + private void setTitle(String title) { + callback.setTitle(title); + } + + public interface TitleCallback { + void setTitle(int Rid); + void setTitle(String title); + } +}