2013-12-16 20:37:16 -08:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.gecko.preferences;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.mozilla.gecko.home.HomeConfig;
|
|
|
|
import org.mozilla.gecko.home.HomeConfig.PanelConfig;
|
|
|
|
import org.mozilla.gecko.util.ThreadUtils;
|
|
|
|
import org.mozilla.gecko.util.UiAsyncTask;
|
|
|
|
|
2014-02-22 19:53:02 -08:00
|
|
|
import android.content.Context;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
|
2013-12-16 20:37:16 -08:00
|
|
|
public class PanelsPreferenceCategory extends CustomListCategory {
|
|
|
|
public static final String LOGTAG = "PanelsPrefCategory";
|
|
|
|
|
|
|
|
protected HomeConfig mHomeConfig;
|
2014-03-07 09:15:31 -08:00
|
|
|
protected HomeConfig.Editor mConfigEditor;
|
2013-12-16 20:37:16 -08:00
|
|
|
|
2014-03-07 09:15:31 -08:00
|
|
|
protected UiAsyncTask<Void, Void, HomeConfig.State> mLoadTask;
|
2013-12-16 20:37:16 -08:00
|
|
|
|
|
|
|
public PanelsPreferenceCategory(Context context) {
|
|
|
|
super(context);
|
|
|
|
initConfig(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
public PanelsPreferenceCategory(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
initConfig(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
public PanelsPreferenceCategory(Context context, AttributeSet attrs, int defStyle) {
|
|
|
|
super(context, attrs, defStyle);
|
|
|
|
initConfig(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void initConfig(Context context) {
|
|
|
|
mHomeConfig = HomeConfig.getDefault(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAttachedToActivity() {
|
|
|
|
super.onAttachedToActivity();
|
|
|
|
|
|
|
|
loadHomeConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the Home Panels config and populate the preferences screen and maintain local state.
|
|
|
|
*/
|
|
|
|
private void loadHomeConfig() {
|
2014-03-07 09:15:31 -08:00
|
|
|
mLoadTask = new UiAsyncTask<Void, Void, HomeConfig.State>(ThreadUtils.getBackgroundHandler()) {
|
2013-12-16 20:37:16 -08:00
|
|
|
@Override
|
2014-03-07 09:15:31 -08:00
|
|
|
public HomeConfig.State doInBackground(Void... params) {
|
2013-12-16 20:37:16 -08:00
|
|
|
return mHomeConfig.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-03-07 09:15:31 -08:00
|
|
|
public void onPostExecute(HomeConfig.State configState) {
|
|
|
|
mConfigEditor = configState.edit();
|
|
|
|
displayHomeConfig(configState);
|
2013-12-16 20:37:16 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
mLoadTask.execute();
|
|
|
|
}
|
|
|
|
|
2014-03-10 09:37:36 -07:00
|
|
|
/**
|
|
|
|
* Reload the Home Panels list from HomeConfig.
|
|
|
|
*/
|
|
|
|
public void refresh() {
|
|
|
|
// Clear all the existing home panels, but leave the
|
|
|
|
// first item (Add panels).
|
|
|
|
int prefCount = getPreferenceCount();
|
|
|
|
while (prefCount > 1) {
|
|
|
|
removePreference(getPreference(1));
|
|
|
|
prefCount--;
|
|
|
|
}
|
|
|
|
|
|
|
|
loadHomeConfig();
|
|
|
|
}
|
|
|
|
|
2014-03-07 09:15:31 -08:00
|
|
|
private void displayHomeConfig(HomeConfig.State configState) {
|
|
|
|
for (PanelConfig panelConfig : configState) {
|
2014-02-13 15:33:46 -08:00
|
|
|
final boolean isRemovable = panelConfig.isDynamic();
|
|
|
|
|
2013-12-16 20:37:16 -08:00
|
|
|
// Create and add the pref.
|
2014-02-13 15:33:46 -08:00
|
|
|
final PanelsPreference pref = new PanelsPreference(getContext(), PanelsPreferenceCategory.this, isRemovable);
|
2013-12-16 20:37:16 -08:00
|
|
|
pref.setTitle(panelConfig.getTitle());
|
|
|
|
pref.setKey(panelConfig.getId());
|
|
|
|
// XXX: Pull icon from PanelInfo.
|
|
|
|
addPreference(pref);
|
|
|
|
|
|
|
|
if (panelConfig.isDisabled()) {
|
|
|
|
pref.setHidden(true);
|
|
|
|
}
|
|
|
|
}
|
2014-03-07 09:15:31 -08:00
|
|
|
|
|
|
|
setDefaultFromConfig();
|
2013-12-16 20:37:16 -08:00
|
|
|
}
|
|
|
|
|
2014-03-07 09:15:31 -08:00
|
|
|
private void setDefaultFromConfig() {
|
|
|
|
final String defaultPanelId = mConfigEditor.getDefaultPanelId();
|
|
|
|
if (defaultPanelId == null) {
|
|
|
|
mDefaultReference = null;
|
2014-02-05 06:14:52 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-07 09:15:31 -08:00
|
|
|
final int prefCount = getPreferenceCount();
|
2013-12-16 20:37:16 -08:00
|
|
|
|
2014-03-07 09:15:31 -08:00
|
|
|
// First preference (index 0) is Preference to add panels.
|
|
|
|
for (int i = 1; i < prefCount; i++) {
|
|
|
|
final PanelsPreference pref = (PanelsPreference) getPreference(i);
|
|
|
|
|
|
|
|
if (defaultPanelId.equals(pref.getKey())) {
|
|
|
|
super.setDefault(pref);
|
|
|
|
break;
|
|
|
|
}
|
2013-12-16 20:37:16 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setDefault(CustomListPreference pref) {
|
|
|
|
super.setDefault(pref);
|
2014-03-07 09:15:31 -08:00
|
|
|
|
|
|
|
final String id = pref.getKey();
|
|
|
|
|
|
|
|
final String defaultPanelId = mConfigEditor.getDefaultPanelId();
|
|
|
|
if (defaultPanelId != null && defaultPanelId.equals(id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mConfigEditor.setDefault(id);
|
|
|
|
mConfigEditor.apply();
|
2013-12-16 20:37:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPrepareForRemoval() {
|
|
|
|
if (mLoadTask != null) {
|
|
|
|
mLoadTask.cancel(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void uninstall(CustomListPreference pref) {
|
2014-03-07 09:15:31 -08:00
|
|
|
mConfigEditor.uninstall(pref.getKey());
|
|
|
|
mConfigEditor.apply();
|
2013-12-16 20:37:16 -08:00
|
|
|
|
2014-03-07 09:15:31 -08:00
|
|
|
super.uninstall(pref);
|
2013-12-16 20:37:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the hide/show state of the preference and save the HomeConfig
|
|
|
|
* changes.
|
|
|
|
*
|
|
|
|
* @param pref Preference to update
|
|
|
|
* @param toHide New hidden state of the preference
|
|
|
|
*/
|
|
|
|
protected void setHidden(PanelsPreference pref, boolean toHide) {
|
2014-03-07 09:15:31 -08:00
|
|
|
mConfigEditor.setDisabled(pref.getKey(), toHide);
|
|
|
|
mConfigEditor.apply();
|
2013-12-16 20:37:16 -08:00
|
|
|
|
2014-03-07 09:15:31 -08:00
|
|
|
pref.setHidden(toHide);
|
|
|
|
setDefaultFromConfig();
|
2013-12-16 20:37:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When the default panel is removed or disabled, find an enabled panel
|
|
|
|
* if possible and set it as mDefaultReference.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
protected void setFallbackDefault() {
|
2014-03-07 09:15:31 -08:00
|
|
|
setDefaultFromConfig();
|
2013-12-16 20:37:16 -08:00
|
|
|
}
|
|
|
|
}
|