Bug 959917 - Part 3: Add nested reordering to context menu. r=lucasr

This commit is contained in:
Chenxia Liu 2014-03-26 16:07:14 -07:00
parent 875ac71893
commit ee752c0894
4 changed files with 164 additions and 9 deletions

View File

@ -178,6 +178,9 @@ size. -->
<!ENTITY pref_panels_show "Show">
<!ENTITY pref_panels_hide "Hide">
<!ENTITY pref_panels_reorder "Change order">
<!ENTITY pref_panels_move_up "Move up">
<!ENTITY pref_panels_move_down "Move down">
<!ENTITY datareporting_notification_title "&brandShortName; stats &amp; data">
<!ENTITY datareporting_notification_action_long "Choose what information to share">

View File

@ -6,7 +6,12 @@ package org.mozilla.gecko.preferences;
import org.mozilla.gecko.R;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnShowListener;
import android.content.res.Resources;
import android.util.Log;
import android.view.View;
@ -16,12 +21,21 @@ import android.widget.TextView;
public class PanelsPreference extends CustomListPreference {
protected String LOGTAG = "PanelsPreference";
// Position state of this Preference in enclosing category.
private static final int STATE_IS_FIRST = 0;
private static final int STATE_IS_LAST = 1;
/**
* Index of the context menu button for controlling display options.
* For (removable) Dynamic panels, this button removes the panel.
* For built-in panels, this button toggles showing or hiding the panel.
*/
private static final int INDEX_DISPLAY_BUTTON = 1;
private static final int INDEX_REORDER_BUTTON = 2;
// Indices of buttons in context menu for reordering.
private static final int INDEX_MOVE_UP_BUTTON = 0;
private static final int INDEX_MOVE_DOWN_BUTTON = 1;
private String LABEL_HIDE;
private String LABEL_SHOW;
@ -29,9 +43,14 @@ public class PanelsPreference extends CustomListPreference {
protected boolean mIsHidden = false;
private boolean mIsRemovable;
public PanelsPreference(Context context, CustomListCategory parentCategory, boolean isRemovable) {
// State for reordering.
private int mPositionState = -1;
private final int mIndex;
public PanelsPreference(Context context, CustomListCategory parentCategory, boolean isRemovable, int index) {
super(context, parentCategory);
mIsRemovable = isRemovable;
mIndex = index;
}
@Override
@ -56,16 +75,18 @@ public class PanelsPreference extends CustomListPreference {
@Override
protected String[] createDialogItems() {
final Resources res = getContext().getResources();
final String labelReorder = res.getString(R.string.pref_panels_reorder);
if (mIsRemovable) {
return new String[] { LABEL_SET_AS_DEFAULT, LABEL_REMOVE };
return new String[] { LABEL_SET_AS_DEFAULT, LABEL_REMOVE, labelReorder };
}
// Built-in panels can't be removed, so use show/hide options.
Resources res = getContext().getResources();
LABEL_HIDE = res.getString(R.string.pref_panels_hide);
LABEL_SHOW = res.getString(R.string.pref_panels_show);
return new String[] { LABEL_SET_AS_DEFAULT, LABEL_HIDE };
return new String[] { LABEL_SET_AS_DEFAULT, LABEL_HIDE, labelReorder };
}
@Override
@ -100,6 +121,12 @@ public class PanelsPreference extends CustomListPreference {
}
break;
case INDEX_REORDER_BUTTON:
// Display dialog for changing preference order.
final Dialog orderDialog = makeReorderDialog();
orderDialog.show();
break;
default:
Log.w(LOGTAG, "Selected index out of range: " + index);
}
@ -116,6 +143,77 @@ public class PanelsPreference extends CustomListPreference {
}
}
private Dialog makeReorderDialog() {
final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
final Resources res = getContext().getResources();
final String labelUp = res.getString(R.string.pref_panels_move_up);
final String labelDown = res.getString(R.string.pref_panels_move_down);
builder.setTitle(getTitle());
builder.setItems(new String[] { labelUp, labelDown }, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int index) {
dialog.dismiss();
switch (index) {
case INDEX_MOVE_UP_BUTTON:
((PanelsPreferenceCategory) mParentCategory).moveUp(PanelsPreference.this);
break;
case INDEX_MOVE_DOWN_BUTTON:
((PanelsPreferenceCategory) mParentCategory).moveDown(PanelsPreference.this);
break;
}
}
});
final Dialog dialog = builder.create();
dialog.setOnShowListener(new OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
setReorderItemsEnabled(dialog);
}
});
return dialog;
}
public void setIsFirst() {
mPositionState = STATE_IS_FIRST;
}
public void setIsLast() {
mPositionState = STATE_IS_LAST;
}
/**
* Configure enabled state of the reorder dialog, which must be done after the dialog is shown.
* @param dialog Dialog to configure
*/
private void setReorderItemsEnabled(DialogInterface dialog) {
// Update button enabled-ness for reordering.
switch (mPositionState) {
case STATE_IS_FIRST:
final TextView itemUp = (TextView) ((AlertDialog) dialog).getListView().getChildAt(INDEX_MOVE_UP_BUTTON);
itemUp.setEnabled(false);
// Disable clicks to this view.
itemUp.setOnClickListener(null);
break;
case STATE_IS_LAST:
final TextView itemDown = (TextView) ((AlertDialog) dialog).getListView().getChildAt(INDEX_MOVE_DOWN_BUTTON);
itemDown.setEnabled(false);
// Disable clicks to this view.
itemDown.setOnClickListener(null);
break;
default:
// Do nothing.
break;
}
}
public void setHidden(boolean toHide) {
if (toHide) {
setIsDefault(false);
@ -130,4 +228,8 @@ public class PanelsPreference extends CustomListPreference {
public boolean isHidden() {
return mIsHidden;
}
public int getIndex() {
return mIndex;
}
}

View File

@ -6,6 +6,7 @@ package org.mozilla.gecko.preferences;
import org.mozilla.gecko.home.HomeConfig;
import org.mozilla.gecko.home.HomeConfig.PanelConfig;
import org.mozilla.gecko.home.HomeConfig.State;
import org.mozilla.gecko.util.ThreadUtils;
import org.mozilla.gecko.util.UiAsyncTask;
@ -18,6 +19,9 @@ public class PanelsPreferenceCategory extends CustomListCategory {
protected HomeConfig mHomeConfig;
protected HomeConfig.Editor mConfigEditor;
// Account for the fake "Add Panel" preference in preference counting.
private static final int PANEL_PREFS_OFFSET = 1;
protected UiAsyncTask<Void, Void, HomeConfig.State> mLoadTask;
public PanelsPreferenceCategory(Context context) {
@ -65,10 +69,16 @@ public class PanelsPreferenceCategory extends CustomListCategory {
mLoadTask.execute();
}
/**
* Reload the Home Panels list from HomeConfig.
*/
public void refresh() {
refresh(null);
}
/**
* Reload the Home Panels list.
* @param State HomeConfig.State to rebuild Home Panels list from.
* If null, load from HomeConfig.
*/
public void refresh(State state) {
// Clear all the existing home panels, but leave the
// first item (Add panels).
int prefCount = getPreferenceCount();
@ -77,15 +87,20 @@ public class PanelsPreferenceCategory extends CustomListCategory {
prefCount--;
}
loadHomeConfig();
if (state == null) {
loadHomeConfig();
} else {
displayHomeConfig(state);
}
}
private void displayHomeConfig(HomeConfig.State configState) {
int index = 0;
for (PanelConfig panelConfig : configState) {
final boolean isRemovable = panelConfig.isDynamic();
// Create and add the pref.
final PanelsPreference pref = new PanelsPreference(getContext(), PanelsPreferenceCategory.this, isRemovable);
final PanelsPreference pref = new PanelsPreference(getContext(), PanelsPreferenceCategory.this, isRemovable, index);
pref.setTitle(panelConfig.getTitle());
pref.setKey(panelConfig.getId());
// XXX: Pull icon from PanelInfo.
@ -94,11 +109,25 @@ public class PanelsPreferenceCategory extends CustomListCategory {
if (panelConfig.isDisabled()) {
pref.setHidden(true);
}
index++;
}
setPositionState();
setDefaultFromConfig();
}
private void setPositionState() {
final int prefCount = getPreferenceCount();
// Pass in position state to first and last preference.
final PanelsPreference firstPref = (PanelsPreference) getPreference(PANEL_PREFS_OFFSET);
firstPref.setIsFirst();
final PanelsPreference lastPref = (PanelsPreference) getPreference(prefCount - 1);
lastPref.setIsLast();
}
private void setDefaultFromConfig() {
final String defaultPanelId = mConfigEditor.getDefaultPanelId();
if (defaultPanelId == null) {
@ -149,6 +178,24 @@ public class PanelsPreferenceCategory extends CustomListCategory {
super.uninstall(pref);
}
public void moveUp(PanelsPreference pref) {
final int panelIndex = pref.getIndex();
if (panelIndex > 0) {
mConfigEditor.moveTo(pref.getKey(), panelIndex - 1);
final State state = mConfigEditor.apply();
refresh(state);
}
}
public void moveDown(PanelsPreference pref) {
final int panelIndex = pref.getIndex();
if (panelIndex < getPreferenceCount() - 1) {
mConfigEditor.moveTo(pref.getKey(), panelIndex + 1);
final State state = mConfigEditor.apply();
refresh(state);
}
}
/**
* Update the hide/show state of the preference and save the HomeConfig
* changes.

View File

@ -193,6 +193,9 @@
<string name="pref_panels_show">&pref_panels_show;</string>
<string name="pref_panels_hide">&pref_panels_hide;</string>
<string name="pref_panels_reorder">&pref_panels_reorder;</string>
<string name="pref_panels_move_up">&pref_panels_move_up;</string>
<string name="pref_panels_move_down">&pref_panels_move_down;</string>
<string name="datareporting_notification_title">&datareporting_notification_title;</string>
<string name="datareporting_notification_action_long">&datareporting_notification_action_long;</string>