mobile/android/base/preferences/PanelsPreferenceCategory.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 package org.mozilla.gecko.preferences;
     7 import org.mozilla.gecko.Telemetry;
     8 import org.mozilla.gecko.TelemetryContract;
     9 import org.mozilla.gecko.home.HomeConfig;
    10 import org.mozilla.gecko.home.HomeConfig.PanelConfig;
    11 import org.mozilla.gecko.home.HomeConfig.State;
    12 import org.mozilla.gecko.util.ThreadUtils;
    13 import org.mozilla.gecko.util.UiAsyncTask;
    15 import android.content.Context;
    16 import android.text.TextUtils;
    17 import android.util.AttributeSet;
    19 public class PanelsPreferenceCategory extends CustomListCategory {
    20     public static final String LOGTAG = "PanelsPrefCategory";
    22     protected HomeConfig mHomeConfig;
    23     protected HomeConfig.Editor mConfigEditor;
    25     protected UiAsyncTask<Void, Void, HomeConfig.State> mLoadTask;
    27     public PanelsPreferenceCategory(Context context) {
    28         super(context);
    29         initConfig(context);
    30     }
    32     public PanelsPreferenceCategory(Context context, AttributeSet attrs) {
    33         super(context, attrs);
    34         initConfig(context);
    35     }
    37     public PanelsPreferenceCategory(Context context, AttributeSet attrs, int defStyle) {
    38         super(context, attrs, defStyle);
    39         initConfig(context);
    40     }
    42     protected void initConfig(Context context) {
    43         mHomeConfig = HomeConfig.getDefault(context);
    44     }
    46     @Override
    47     public void onAttachedToActivity() {
    48         super.onAttachedToActivity();
    50         loadHomeConfig(null);
    51     }
    53     /**
    54      * Load the Home Panels config and populate the preferences screen and maintain local state.
    55      */
    56     private void loadHomeConfig(final String animatePanelId) {
    57         mLoadTask = new UiAsyncTask<Void, Void, HomeConfig.State>(ThreadUtils.getBackgroundHandler()) {
    58             @Override
    59             public HomeConfig.State doInBackground(Void... params) {
    60                 return mHomeConfig.load();
    61             }
    63             @Override
    64             public void onPostExecute(HomeConfig.State configState) {
    65                 mConfigEditor = configState.edit();
    66                 displayHomeConfig(configState, animatePanelId);
    67             }
    68         };
    69         mLoadTask.execute();
    70     }
    72     /**
    73      * Simplified refresh of Home Panels when there is no state to be persisted.
    74      */
    75     public void refresh() {
    76         refresh(null, null);
    77     }
    79     /**
    80      * Refresh the Home Panels list and animate a panel, if specified.
    81      * If null, load from HomeConfig.
    82      *
    83      * @param State HomeConfig.State to rebuild Home Panels list from.
    84      * @param String panelId of panel to be animated.
    85      */
    86     public void refresh(State state, String animatePanelId) {
    87         // Clear all the existing home panels.
    88         removeAll();
    90         if (state == null) {
    91             loadHomeConfig(animatePanelId);
    92         } else {
    93             displayHomeConfig(state, animatePanelId);
    94         }
    95     }
    97     private void displayHomeConfig(HomeConfig.State configState, String animatePanelId) {
    98         int index = 0;
    99         for (PanelConfig panelConfig : configState) {
   100             final boolean isRemovable = panelConfig.isDynamic();
   102             // Create and add the pref.
   103             final String panelId = panelConfig.getId();
   104             final boolean animate = TextUtils.equals(animatePanelId, panelId);
   106             final PanelsPreference pref = new PanelsPreference(getContext(), PanelsPreferenceCategory.this, isRemovable, index, animate);
   107             pref.setTitle(panelConfig.getTitle());
   108             pref.setKey(panelConfig.getId());
   109             // XXX: Pull icon from PanelInfo.
   110             addPreference(pref);
   112             if (panelConfig.isDisabled()) {
   113                 pref.setHidden(true);
   114             }
   116             index++;
   117         }
   119         setPositionState();
   120         setDefaultFromConfig();
   121     }
   123     private void setPositionState() {
   124         final int prefCount = getPreferenceCount();
   126         // Pass in position state to first and last preference.
   127         final PanelsPreference firstPref = (PanelsPreference) getPreference(0);
   128         firstPref.setIsFirst();
   130         final PanelsPreference lastPref = (PanelsPreference) getPreference(prefCount - 1);
   131         lastPref.setIsLast();
   132     }
   134     private void setDefaultFromConfig() {
   135         final String defaultPanelId = mConfigEditor.getDefaultPanelId();
   136         if (defaultPanelId == null) {
   137             mDefaultReference = null;
   138             return;
   139         }
   141         final int prefCount = getPreferenceCount();
   143         for (int i = 0; i < prefCount; i++) {
   144             final PanelsPreference pref = (PanelsPreference) getPreference(i);
   146             if (defaultPanelId.equals(pref.getKey())) {
   147                 super.setDefault(pref);
   148                 break;
   149             }
   150         }
   151     }
   153     @Override
   154     public void setDefault(CustomListPreference pref) {
   155         super.setDefault(pref);
   157         final String id = pref.getKey();
   159         final String defaultPanelId = mConfigEditor.getDefaultPanelId();
   160         if (defaultPanelId != null && defaultPanelId.equals(id)) {
   161             return;
   162         }
   164         mConfigEditor.setDefault(id);
   165         mConfigEditor.apply();
   167         Telemetry.sendUIEvent(TelemetryContract.Event.PANEL_SET_DEFAULT, null, id);
   168     }
   170     @Override
   171     protected void onPrepareForRemoval() {
   172         if (mLoadTask != null) {
   173             mLoadTask.cancel(true);
   174         }
   175     }
   177     @Override
   178     public void uninstall(CustomListPreference pref) {
   179         mConfigEditor.uninstall(pref.getKey());
   180         mConfigEditor.apply();
   182         super.uninstall(pref);
   183     }
   185     public void moveUp(PanelsPreference pref) {
   186         final int panelIndex = pref.getIndex();
   187         if (panelIndex > 0) {
   188             final String panelKey = pref.getKey();
   189             mConfigEditor.moveTo(panelKey, panelIndex - 1);
   190             final State state = mConfigEditor.apply();
   191             refresh(state, panelKey);
   192         }
   193     }
   195     public void moveDown(PanelsPreference pref) {
   196         final int panelIndex = pref.getIndex();
   197         if (panelIndex < getPreferenceCount() - 1) {
   198             final String panelKey = pref.getKey();
   199             mConfigEditor.moveTo(panelKey, panelIndex + 1);
   200             final State state = mConfigEditor.apply();
   201             refresh(state, panelKey);
   202         }
   203     }
   205     /**
   206      * Update the hide/show state of the preference and save the HomeConfig
   207      * changes.
   208      *
   209      * @param pref Preference to update
   210      * @param toHide New hidden state of the preference
   211      */
   212     protected void setHidden(PanelsPreference pref, boolean toHide) {
   213         mConfigEditor.setDisabled(pref.getKey(), toHide);
   214         mConfigEditor.apply();
   216         pref.setHidden(toHide);
   217         setDefaultFromConfig();
   218     }
   220     /**
   221      * When the default panel is removed or disabled, find an enabled panel
   222      * if possible and set it as mDefaultReference.
   223      */
   224     @Override
   225     protected void setFallbackDefault() {
   226         setDefaultFromConfig();
   227     }
   228 }

mercurial