From b6dc5e31a0beb8b47a40e4c6f634fe9e26e72298 Mon Sep 17 00:00:00 2001 From: tom79 Date: Sun, 23 Jul 2017 17:09:23 +0200 Subject: [PATCH] Fixes a small bad behavior when changing the theme. --- .../mastodon/activities/MainActivity.java | 33 ++++++++----------- .../mastodon/activities/WebviewActivity.java | 4 +++ .../mastodon/fragments/SettingsFragment.java | 9 +++++ .../gouv/etalab/mastodon/helper/Helper.java | 16 ++++++++- .../webview/MastalabWebViewClient.java | 9 ++++- 5 files changed, 50 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/MainActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/MainActivity.java index 5ecdba9c8..5d53f04ba 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/MainActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/MainActivity.java @@ -29,6 +29,7 @@ import android.support.v4.app.FragmentManager; import android.support.v4.view.ViewPager; import android.support.v7.app.AlertDialog; import android.support.v7.widget.SearchView; +import android.util.Log; import android.view.View; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; @@ -73,6 +74,7 @@ import fr.gouv.etalab.mastodon.fragments.TabLayoutSettingsFragment; import fr.gouv.etalab.mastodon.sqlite.AccountDAO; import mastodon.etalab.gouv.fr.mastodon.R; +import static fr.gouv.etalab.mastodon.helper.Helper.CHANGE_THEME_INTENT; import static fr.gouv.etalab.mastodon.helper.Helper.HOME_TIMELINE_INTENT; import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION; import static fr.gouv.etalab.mastodon.helper.Helper.NOTIFICATION_INTENT; @@ -81,6 +83,7 @@ import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor; import static fr.gouv.etalab.mastodon.helper.Helper.changeUser; import static fr.gouv.etalab.mastodon.helper.Helper.loadPPInActionBar; import static fr.gouv.etalab.mastodon.helper.Helper.menuAccounts; +import static fr.gouv.etalab.mastodon.helper.Helper.unCheckAllMenuItems; import static fr.gouv.etalab.mastodon.helper.Helper.updateHeaderAccountInfo; import android.support.v4.app.FragmentStatePagerAdapter; @@ -335,17 +338,7 @@ public class MainActivity extends AppCompatActivity } } - private void unCheckAllMenuItems(@NonNull final Menu menu) { - int size = menu.size(); - for (int i = 0; i < size; i++) { - final MenuItem item = menu.getItem(i); - if(item.hasSubMenu()) { - unCheckAllMenuItems(item.getSubMenu()); - } else { - item.setChecked(false); - } - } - } + @Override @@ -372,18 +365,20 @@ public class MainActivity extends AppCompatActivity final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); userIdIntent = extras.getString(PREF_KEY_ID); //Id of the account in the intent if (extras.getInt(INTENT_ACTION) == NOTIFICATION_INTENT){ - changeUser(MainActivity.this, userIdIntent, false); //Connects the account which is related to the notification unCheckAllMenuItems(navigationView.getMenu()); - navigationView.getMenu().performIdentifierAction(R.id.nav_notification, 0); - if( navigationView.getMenu().findItem(R.id.nav_notification) != null) - navigationView.getMenu().findItem(R.id.nav_notification).setChecked(true); + changeUser(MainActivity.this, userIdIntent, false); //Connects the account which is related to the notification + tabLayout.getTabAt(3).select(); matchingIntent = true; }else if( extras.getInt(INTENT_ACTION) == HOME_TIMELINE_INTENT){ - changeUser(MainActivity.this, userIdIntent, false); //Connects the account which is related to the notification unCheckAllMenuItems(navigationView.getMenu()); - navigationView.getMenu().performIdentifierAction(R.id.nav_home, 0); - if( navigationView.getMenu().findItem(R.id.nav_home) != null) - navigationView.getMenu().findItem(R.id.nav_home).setChecked(true); + changeUser(MainActivity.this, userIdIntent, false); //Connects the account which is related to the notification + tabLayout.getTabAt(0).select(); + matchingIntent = true; + }else if( extras.getInt(INTENT_ACTION) == CHANGE_THEME_INTENT){ + unCheckAllMenuItems(navigationView.getMenu()); + navigationView.setCheckedItem(R.id.nav_settings); + navigationView.getMenu().performIdentifierAction(R.id.nav_settings, 0); + toolbarTitle.setText(R.string.settings); matchingIntent = true; } }else if( Intent.ACTION_SEND.equals(action) && type != null ){ diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/WebviewActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/WebviewActivity.java index cef7813f5..dac368945 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/WebviewActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/WebviewActivity.java @@ -142,6 +142,10 @@ public class WebviewActivity extends AppCompatActivity { } } + public void setUrl(String newUrl){ + this.url = newUrl; + } + @Override public void onDestroy(){ super.onDestroy(); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/SettingsFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/SettingsFragment.java index 430ffbdf6..42261a35f 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/SettingsFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/SettingsFragment.java @@ -36,10 +36,15 @@ import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.LinearLayout; import android.widget.TextView; + +import fr.gouv.etalab.mastodon.activities.MainActivity; import fr.gouv.etalab.mastodon.helper.Helper; import mastodon.etalab.gouv.fr.mastodon.R; import static android.app.Activity.RESULT_OK; +import static fr.gouv.etalab.mastodon.helper.Helper.CHANGE_THEME_INTENT; +import static fr.gouv.etalab.mastodon.helper.Helper.HOME_TIMELINE_INTENT; +import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION; /** @@ -201,6 +206,10 @@ public class SettingsFragment extends Fragment { getActivity().setTheme(R.style.AppTheme); } getActivity().recreate(); + Intent intent = new Intent(context, MainActivity.class); + intent.putExtra(INTENT_ACTION, CHANGE_THEME_INTENT); + startActivity(intent); + } }); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java index 24cf3282b..d66d84d49 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java @@ -42,6 +42,7 @@ import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.os.Environment; +import android.support.annotation.NonNull; import android.support.design.widget.NavigationView; import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationManagerCompat; @@ -56,6 +57,7 @@ import android.text.method.LinkMovementMethod; import android.text.style.ClickableSpan; import android.util.DisplayMetrics; import android.util.Log; +import android.view.Menu; import android.view.MenuItem; import android.view.SubMenu; import android.view.View; @@ -168,7 +170,7 @@ public class Helper { //Notifications public static final int NOTIFICATION_INTENT = 1; public static final int HOME_TIMELINE_INTENT = 2; - + public static final int CHANGE_THEME_INTENT = 3; //Settings public static final String SET_TOOTS_PER_PAGE = "set_toots_per_page"; public static final String SET_ACCOUNTS_PER_PAGE = "set_accounts_per_page"; @@ -1311,4 +1313,16 @@ public class Helper { } return false; } + + public static void unCheckAllMenuItems(@NonNull final Menu menu) { + int size = menu.size(); + for (int i = 0; i < size; i++) { + final MenuItem item = menu.getItem(i); + if(item.hasSubMenu()) { + unCheckAllMenuItems(item.getSubMenu()); + } else { + item.setChecked(false); + } + } + } } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/webview/MastalabWebViewClient.java b/app/src/main/java/fr/gouv/etalab/mastodon/webview/MastalabWebViewClient.java index e269434aa..43bb26bd0 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/webview/MastalabWebViewClient.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/webview/MastalabWebViewClient.java @@ -22,6 +22,8 @@ import android.view.View; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.TextView; + +import fr.gouv.etalab.mastodon.activities.WebviewActivity; import mastodon.etalab.gouv.fr.mastodon.R; /** @@ -57,7 +59,12 @@ public class MastalabWebViewClient extends WebViewClient { }else { activity.setTitle(url); } + //Changes the url in webview activity so that it can be opened with an external app + try{ + if( activity instanceof WebviewActivity) + ((WebviewActivity)activity).setUrl(url); + }catch (Exception ignore){} + } - }