1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/preferences/PanelsPreferenceCategory.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,228 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.preferences; 1.9 + 1.10 +import org.mozilla.gecko.Telemetry; 1.11 +import org.mozilla.gecko.TelemetryContract; 1.12 +import org.mozilla.gecko.home.HomeConfig; 1.13 +import org.mozilla.gecko.home.HomeConfig.PanelConfig; 1.14 +import org.mozilla.gecko.home.HomeConfig.State; 1.15 +import org.mozilla.gecko.util.ThreadUtils; 1.16 +import org.mozilla.gecko.util.UiAsyncTask; 1.17 + 1.18 +import android.content.Context; 1.19 +import android.text.TextUtils; 1.20 +import android.util.AttributeSet; 1.21 + 1.22 +public class PanelsPreferenceCategory extends CustomListCategory { 1.23 + public static final String LOGTAG = "PanelsPrefCategory"; 1.24 + 1.25 + protected HomeConfig mHomeConfig; 1.26 + protected HomeConfig.Editor mConfigEditor; 1.27 + 1.28 + protected UiAsyncTask<Void, Void, HomeConfig.State> mLoadTask; 1.29 + 1.30 + public PanelsPreferenceCategory(Context context) { 1.31 + super(context); 1.32 + initConfig(context); 1.33 + } 1.34 + 1.35 + public PanelsPreferenceCategory(Context context, AttributeSet attrs) { 1.36 + super(context, attrs); 1.37 + initConfig(context); 1.38 + } 1.39 + 1.40 + public PanelsPreferenceCategory(Context context, AttributeSet attrs, int defStyle) { 1.41 + super(context, attrs, defStyle); 1.42 + initConfig(context); 1.43 + } 1.44 + 1.45 + protected void initConfig(Context context) { 1.46 + mHomeConfig = HomeConfig.getDefault(context); 1.47 + } 1.48 + 1.49 + @Override 1.50 + public void onAttachedToActivity() { 1.51 + super.onAttachedToActivity(); 1.52 + 1.53 + loadHomeConfig(null); 1.54 + } 1.55 + 1.56 + /** 1.57 + * Load the Home Panels config and populate the preferences screen and maintain local state. 1.58 + */ 1.59 + private void loadHomeConfig(final String animatePanelId) { 1.60 + mLoadTask = new UiAsyncTask<Void, Void, HomeConfig.State>(ThreadUtils.getBackgroundHandler()) { 1.61 + @Override 1.62 + public HomeConfig.State doInBackground(Void... params) { 1.63 + return mHomeConfig.load(); 1.64 + } 1.65 + 1.66 + @Override 1.67 + public void onPostExecute(HomeConfig.State configState) { 1.68 + mConfigEditor = configState.edit(); 1.69 + displayHomeConfig(configState, animatePanelId); 1.70 + } 1.71 + }; 1.72 + mLoadTask.execute(); 1.73 + } 1.74 + 1.75 + /** 1.76 + * Simplified refresh of Home Panels when there is no state to be persisted. 1.77 + */ 1.78 + public void refresh() { 1.79 + refresh(null, null); 1.80 + } 1.81 + 1.82 + /** 1.83 + * Refresh the Home Panels list and animate a panel, if specified. 1.84 + * If null, load from HomeConfig. 1.85 + * 1.86 + * @param State HomeConfig.State to rebuild Home Panels list from. 1.87 + * @param String panelId of panel to be animated. 1.88 + */ 1.89 + public void refresh(State state, String animatePanelId) { 1.90 + // Clear all the existing home panels. 1.91 + removeAll(); 1.92 + 1.93 + if (state == null) { 1.94 + loadHomeConfig(animatePanelId); 1.95 + } else { 1.96 + displayHomeConfig(state, animatePanelId); 1.97 + } 1.98 + } 1.99 + 1.100 + private void displayHomeConfig(HomeConfig.State configState, String animatePanelId) { 1.101 + int index = 0; 1.102 + for (PanelConfig panelConfig : configState) { 1.103 + final boolean isRemovable = panelConfig.isDynamic(); 1.104 + 1.105 + // Create and add the pref. 1.106 + final String panelId = panelConfig.getId(); 1.107 + final boolean animate = TextUtils.equals(animatePanelId, panelId); 1.108 + 1.109 + final PanelsPreference pref = new PanelsPreference(getContext(), PanelsPreferenceCategory.this, isRemovable, index, animate); 1.110 + pref.setTitle(panelConfig.getTitle()); 1.111 + pref.setKey(panelConfig.getId()); 1.112 + // XXX: Pull icon from PanelInfo. 1.113 + addPreference(pref); 1.114 + 1.115 + if (panelConfig.isDisabled()) { 1.116 + pref.setHidden(true); 1.117 + } 1.118 + 1.119 + index++; 1.120 + } 1.121 + 1.122 + setPositionState(); 1.123 + setDefaultFromConfig(); 1.124 + } 1.125 + 1.126 + private void setPositionState() { 1.127 + final int prefCount = getPreferenceCount(); 1.128 + 1.129 + // Pass in position state to first and last preference. 1.130 + final PanelsPreference firstPref = (PanelsPreference) getPreference(0); 1.131 + firstPref.setIsFirst(); 1.132 + 1.133 + final PanelsPreference lastPref = (PanelsPreference) getPreference(prefCount - 1); 1.134 + lastPref.setIsLast(); 1.135 + } 1.136 + 1.137 + private void setDefaultFromConfig() { 1.138 + final String defaultPanelId = mConfigEditor.getDefaultPanelId(); 1.139 + if (defaultPanelId == null) { 1.140 + mDefaultReference = null; 1.141 + return; 1.142 + } 1.143 + 1.144 + final int prefCount = getPreferenceCount(); 1.145 + 1.146 + for (int i = 0; i < prefCount; i++) { 1.147 + final PanelsPreference pref = (PanelsPreference) getPreference(i); 1.148 + 1.149 + if (defaultPanelId.equals(pref.getKey())) { 1.150 + super.setDefault(pref); 1.151 + break; 1.152 + } 1.153 + } 1.154 + } 1.155 + 1.156 + @Override 1.157 + public void setDefault(CustomListPreference pref) { 1.158 + super.setDefault(pref); 1.159 + 1.160 + final String id = pref.getKey(); 1.161 + 1.162 + final String defaultPanelId = mConfigEditor.getDefaultPanelId(); 1.163 + if (defaultPanelId != null && defaultPanelId.equals(id)) { 1.164 + return; 1.165 + } 1.166 + 1.167 + mConfigEditor.setDefault(id); 1.168 + mConfigEditor.apply(); 1.169 + 1.170 + Telemetry.sendUIEvent(TelemetryContract.Event.PANEL_SET_DEFAULT, null, id); 1.171 + } 1.172 + 1.173 + @Override 1.174 + protected void onPrepareForRemoval() { 1.175 + if (mLoadTask != null) { 1.176 + mLoadTask.cancel(true); 1.177 + } 1.178 + } 1.179 + 1.180 + @Override 1.181 + public void uninstall(CustomListPreference pref) { 1.182 + mConfigEditor.uninstall(pref.getKey()); 1.183 + mConfigEditor.apply(); 1.184 + 1.185 + super.uninstall(pref); 1.186 + } 1.187 + 1.188 + public void moveUp(PanelsPreference pref) { 1.189 + final int panelIndex = pref.getIndex(); 1.190 + if (panelIndex > 0) { 1.191 + final String panelKey = pref.getKey(); 1.192 + mConfigEditor.moveTo(panelKey, panelIndex - 1); 1.193 + final State state = mConfigEditor.apply(); 1.194 + refresh(state, panelKey); 1.195 + } 1.196 + } 1.197 + 1.198 + public void moveDown(PanelsPreference pref) { 1.199 + final int panelIndex = pref.getIndex(); 1.200 + if (panelIndex < getPreferenceCount() - 1) { 1.201 + final String panelKey = pref.getKey(); 1.202 + mConfigEditor.moveTo(panelKey, panelIndex + 1); 1.203 + final State state = mConfigEditor.apply(); 1.204 + refresh(state, panelKey); 1.205 + } 1.206 + } 1.207 + 1.208 + /** 1.209 + * Update the hide/show state of the preference and save the HomeConfig 1.210 + * changes. 1.211 + * 1.212 + * @param pref Preference to update 1.213 + * @param toHide New hidden state of the preference 1.214 + */ 1.215 + protected void setHidden(PanelsPreference pref, boolean toHide) { 1.216 + mConfigEditor.setDisabled(pref.getKey(), toHide); 1.217 + mConfigEditor.apply(); 1.218 + 1.219 + pref.setHidden(toHide); 1.220 + setDefaultFromConfig(); 1.221 + } 1.222 + 1.223 + /** 1.224 + * When the default panel is removed or disabled, find an enabled panel 1.225 + * if possible and set it as mDefaultReference. 1.226 + */ 1.227 + @Override 1.228 + protected void setFallbackDefault() { 1.229 + setDefaultFromConfig(); 1.230 + } 1.231 +}