From 752a96b7afc311ff24be0d069434fb78698ccc00 Mon Sep 17 00:00:00 2001 From: Michael Rapp Date: Wed, 25 Oct 2017 23:27:13 +0200 Subject: [PATCH] Changed the theme, the attributes of a PreferenceFragment are obtained from. --- .../activity/PreferenceFragment.java | 94 +++++-------------- .../fragment/AbstractPreferenceFragment.java | 63 +++---------- 2 files changed, 39 insertions(+), 118 deletions(-) diff --git a/library/src/main/java/de/mrapp/android/preference/activity/PreferenceFragment.java b/library/src/main/java/de/mrapp/android/preference/activity/PreferenceFragment.java index 06a94d4..ab6e334 100644 --- a/library/src/main/java/de/mrapp/android/preference/activity/PreferenceFragment.java +++ b/library/src/main/java/de/mrapp/android/preference/activity/PreferenceFragment.java @@ -26,7 +26,6 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.StringRes; -import android.support.annotation.StyleRes; import android.support.v4.content.ContextCompat; import android.text.TextUtils; import android.view.LayoutInflater; @@ -129,39 +128,33 @@ public abstract class PreferenceFragment extends AbstractPreferenceFragment { private int buttonBarElevation; /** - * The color of dividers, which are contained by the fragment. + * Obtains all relevant attributes from the activity's current theme. */ - private int dividerColor; + private void obtainStyledAttributes() { + obtainShowRestoreDefaultsButton(); + obtainRestoreDefaultsButtonText(); + obtainButtonBarBackground(); + obtainButtonBarElevation(); + } /** * Obtains, whether the button, which allows to restore the preferences' default values, should - * be shown, or not, from a specific theme. - * - * @param themeResourceId - * The resource id of the theme, the boolean value should be obtained from, as an {@link - * Integer} value - */ - private void obtainShowRestoreDefaultsButton(@StyleRes final int themeResourceId) { - boolean show = ThemeUtil - .getBoolean(getActivity(), themeResourceId, R.attr.showRestoreDefaultsButton, - false); + * be shown, or not, from the activity's current theme. + */ + private void obtainShowRestoreDefaultsButton() { + boolean show = ThemeUtil.getBoolean(getActivity(), R.attr.showRestoreDefaultsButton, false); showRestoreDefaultsButton(show); } /** * Obtains the text of the button, which allows to restore the preferences' default values, from - * a specific theme. - * - * @param themeResourceId - * The resource id of the theme, the text should be obtained from, as an {@link Integer} - * value + * the activity's current theme. */ - private void obtainRestoreDefaultsButtonText(@StyleRes final int themeResourceId) { + private void obtainRestoreDefaultsButtonText() { CharSequence text; try { - text = ThemeUtil - .getText(getActivity(), themeResourceId, R.attr.restoreDefaultsButtonText); + text = ThemeUtil.getText(getActivity(), R.attr.restoreDefaultsButtonText); } catch (NotFoundException e) { text = getText(R.string.restore_defaults_button_text); } @@ -170,20 +163,16 @@ private void obtainRestoreDefaultsButtonText(@StyleRes final int themeResourceId } /** - * Obtains the background of the button bar from a specific theme. - * - * @param themeResourceId - * The resource id of the theme, the background should be obtained from, as an {@link - * Integer} value + * Obtains the background of the button bar from the activity's current theme. */ - private void obtainButtonBarBackground(@StyleRes final int themeResourceId) { + private void obtainButtonBarBackground() { try { - int color = ThemeUtil.getColor(getActivity(), themeResourceId, - R.attr.restoreDefaultsButtonBarBackground); + int color = + ThemeUtil.getColor(getActivity(), R.attr.restoreDefaultsButtonBarBackground); setButtonBarBackgroundColor(color); } catch (NotFoundException e) { - int resourceId = ThemeUtil.getResId(getActivity(), themeResourceId, - R.attr.restoreDefaultsButtonBarBackground, -1); + int resourceId = ThemeUtil + .getResId(getActivity(), R.attr.restoreDefaultsButtonBarBackground, -1); if (resourceId != -1) { setButtonBarBackground(resourceId); @@ -195,18 +184,14 @@ private void obtainButtonBarBackground(@StyleRes final int themeResourceId) { } /** - * Obtains the elevation of the button bar from a specific theme. - * - * @param themeResourceId - * The resource id of the theme, the elevation should be obtained from, as an {@link - * Integer} value + * Obtains the elevation of the button bar from the activity's current theme. */ - private void obtainButtonBarElevation(@StyleRes final int themeResourceId) { + private void obtainButtonBarElevation() { int elevation; try { - elevation = ThemeUtil.getDimensionPixelSize(getActivity(), themeResourceId, - R.attr.restoreDefaultsButtonBarElevation); + elevation = ThemeUtil + .getDimensionPixelSize(getActivity(), R.attr.restoreDefaultsButtonBarElevation); } catch (NotFoundException e) { elevation = getResources().getDimensionPixelSize(R.dimen.button_bar_elevation); } @@ -214,26 +199,6 @@ private void obtainButtonBarElevation(@StyleRes final int themeResourceId) { setButtonBarElevation(pixelsToDp(getActivity(), elevation)); } - /** - * Obtains the color of the dividers, which are contained by the fragment, from a specific - * theme. - * - * @param themeResourceId - * The resource id of the theme, the color should be obtained from, as an {@link - * Integer} value - */ - private void obtainDividerColor(@StyleRes final int themeResourceId) { - int color; - - try { - color = ThemeUtil.getColor(getActivity(), themeResourceId, R.attr.dividerColor); - } catch (NotFoundException e) { - color = ContextCompat.getColor(getActivity(), R.color.preference_divider_color_light); - } - - setDividerColor(color); - } - /** * Handles the arguments, which have been passed to the fragment. */ @@ -654,6 +619,7 @@ public final void setButtonBarElevation(final int elevation) { @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); + obtainStyledAttributes(); handleArguments(); } @@ -684,14 +650,4 @@ protected final View onInflateView(@NonNull final LayoutInflater inflater, return frameLayout; } - @Override - protected final void onObtainStyledAttributes(@StyleRes final int themeResourceId) { - super.onObtainStyledAttributes(themeResourceId); - obtainShowRestoreDefaultsButton(themeResourceId); - obtainRestoreDefaultsButtonText(themeResourceId); - obtainButtonBarBackground(themeResourceId); - obtainButtonBarElevation(themeResourceId); - obtainDividerColor(themeResourceId); - } - } \ No newline at end of file diff --git a/library/src/main/java/de/mrapp/android/preference/activity/fragment/AbstractPreferenceFragment.java b/library/src/main/java/de/mrapp/android/preference/activity/fragment/AbstractPreferenceFragment.java index 4479de7..cba412b 100644 --- a/library/src/main/java/de/mrapp/android/preference/activity/fragment/AbstractPreferenceFragment.java +++ b/library/src/main/java/de/mrapp/android/preference/activity/fragment/AbstractPreferenceFragment.java @@ -13,16 +13,13 @@ */ package de.mrapp.android.preference.activity.fragment; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager; -import android.content.pm.PackageManager.NameNotFoundException; -import android.content.res.TypedArray; +import android.content.res.Resources.NotFoundException; import android.os.Bundle; import android.support.annotation.CallSuper; import android.support.annotation.ColorInt; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import android.support.annotation.StyleRes; +import android.support.v4.content.ContextCompat; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -30,6 +27,7 @@ import de.mrapp.android.preference.activity.R; import de.mrapp.android.preference.activity.view.PreferenceListView; +import de.mrapp.android.util.ThemeUtil; /** * An abstract base class for all fragments, which show multiple preferences. @@ -47,48 +45,29 @@ public abstract class AbstractPreferenceFragment extends android.preference.Pref /** * The color of the dividers which are shown above preference categories. */ - private int dividerColor; + private int dividerColor = -1; /** * Obtains all relevant attributes from the activity's current theme. */ private void obtainStyledAttributes() { - int themeResourceId = obtainThemeResourceId(); - - if (themeResourceId != -1) { - onObtainStyledAttributes(themeResourceId); - } + obtainDividerColor(); } /** - * Obtains the resource id of the activity's current theme. - * - * @return The resource id of the activity's current theme as an {@link Integer} value or 0, if - * an error occurred while obtaining the theme + * Obtains the color of the dividers, which are shown above preference categories, from the + * activity's theme. */ - private int obtainThemeResourceId() { + private void obtainDividerColor() { + int color; + try { - String packageName = getActivity().getClass().getPackage().getName(); - PackageInfo packageInfo = getActivity().getPackageManager() - .getPackageInfo(packageName, PackageManager.GET_META_DATA); - return packageInfo.applicationInfo.theme; - } catch (NameNotFoundException e) { - return -1; + color = ThemeUtil.getColor(getActivity(), R.attr.dividerColor); + } catch (NotFoundException e) { + color = ContextCompat.getColor(getActivity(), R.color.preference_divider_color_light); } - } - /** - * Obtains the color of the dividers, which are shown above preference categories, from a - * specific theme. - * - * @param themeResourceId - * The resource id of the theme, the color should be obtained from, as an {@link - * Integer} value - */ - private void obtainDividerColor(@StyleRes final int themeResourceId) { - TypedArray typedArray = getActivity().getTheme() - .obtainStyledAttributes(themeResourceId, new int[]{R.attr.dividerColor}); - setDividerColor(typedArray.getColor(0, -1)); + setDividerColor(color); } /** @@ -121,20 +100,6 @@ protected abstract View onInflateView(@NonNull final LayoutInflater inflater, @Nullable final ViewGroup parent, @Nullable final Bundle savedInstanceState); - /** - * The method, which is invoked in order to obtain relevant attributes from the activity's - * current theme. This method may be overriden by subclasses in order to obtain additional - * attributes. - * - * @param themeResourceId - * The resource id of the theme, the attributes should be obtained from, as an {@link - * Integer} value - */ - @CallSuper - protected void onObtainStyledAttributes(@StyleRes final int themeResourceId) { - obtainDividerColor(themeResourceId); - } - /** * Returns the list view, which is used to show the fragment's preferences. *