Update sorting for rule types and instances.

Bug: 22977552
Change-Id: I671f45f156f8f3faa4ce55608dd1d61b9f07d16a
This commit is contained in:
Julia Reynolds
2015-10-14 17:19:35 -04:00
parent 562e40f66a
commit 8fe6efc8e1
3 changed files with 39 additions and 14 deletions

View File

@@ -61,11 +61,11 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
public void onCreate(Bundle icicle) { public void onCreate(Bundle icicle) {
super.onCreate(icicle); super.onCreate(icicle);
addPreferencesFromResource(R.xml.zen_mode_automation_settings); addPreferencesFromResource(R.xml.zen_mode_automation_settings);
mPm = mContext.getPackageManager();
mServiceListing = new ServiceListing(mContext, CONFIG); mServiceListing = new ServiceListing(mContext, CONFIG);
mServiceListing.addCallback(mServiceListingCallback); mServiceListing.addCallback(mServiceListingCallback);
mServiceListing.reload(); mServiceListing.reload();
mServiceListing.setListening(true); mServiceListing.setListening(true);
mPm = mContext.getPackageManager();
} }
@Override @Override
@@ -223,7 +223,7 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
@Override @Override
public void onServicesReloaded(List<ServiceInfo> services) { public void onServicesReloaded(List<ServiceInfo> services) {
for (ServiceInfo service : services) { for (ServiceInfo service : services) {
final ZenRuleInfo ri = getRuleInfo(service); final ZenRuleInfo ri = getRuleInfo(mPm, service);
if (ri != null && ri.serviceComponent != null if (ri != null && ri.serviceComponent != null
&& Objects.equals(ri.settingsAction, && Objects.equals(ri.settingsAction,
Settings.ACTION_ZEN_MODE_EXTERNAL_RULE_SETTINGS)) { Settings.ACTION_ZEN_MODE_EXTERNAL_RULE_SETTINGS)) {
@@ -236,7 +236,7 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
} }
}; };
public static ZenRuleInfo getRuleInfo(ServiceInfo si) { public static ZenRuleInfo getRuleInfo(PackageManager pm, ServiceInfo si) {
if (si == null || si.metaData == null) return null; if (si == null || si.metaData == null) return null;
final String ruleType = si.metaData.getString(ConditionProviderService.META_DATA_RULE_TYPE); final String ruleType = si.metaData.getString(ConditionProviderService.META_DATA_RULE_TYPE);
final ComponentName configurationActivity = getSettingsActivity(si); final ComponentName configurationActivity = getSettingsActivity(si);
@@ -246,6 +246,7 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
ri.title = ruleType; ri.title = ruleType;
ri.packageName = si.packageName; ri.packageName = si.packageName;
ri.configurationActivity = getSettingsActivity(si); ri.configurationActivity = getSettingsActivity(si);
ri.packageLabel = si.applicationInfo.loadLabel(pm);
return ri; return ri;
} }
@@ -262,12 +263,16 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
return null; return null;
} }
// TODO: Sort by creation date, once that data is available.
private static final Comparator<AutomaticZenRule> RULE_COMPARATOR = private static final Comparator<AutomaticZenRule> RULE_COMPARATOR =
new Comparator<AutomaticZenRule>() { new Comparator<AutomaticZenRule>() {
@Override @Override
public int compare(AutomaticZenRule lhs, AutomaticZenRule rhs) { public int compare(AutomaticZenRule lhs, AutomaticZenRule rhs) {
return key(lhs).compareTo(key(rhs)); int byDate = Long.compare(lhs.getCreationTime(), rhs.getCreationTime());
if (byDate != 0) {
return byDate;
} else {
return key(lhs).compareTo(key(rhs));
}
} }
private String key(AutomaticZenRule rule) { private String key(AutomaticZenRule rule) {

View File

@@ -11,4 +11,5 @@ public class ZenRuleInfo {
public Uri defaultConditionId; public Uri defaultConditionId;
public ComponentName serviceComponent; public ComponentName serviceComponent;
public boolean isSystem; public boolean isSystem;
public CharSequence packageLabel;
} }

View File

@@ -17,6 +17,7 @@
package com.android.settings.notification; package com.android.settings.notification;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.AutomaticZenRule;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener; import android.content.DialogInterface.OnDismissListener;
@@ -36,7 +37,10 @@ import android.widget.TextView;
import com.android.settings.R; import com.android.settings.R;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.text.Collator;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List; import java.util.List;
public abstract class ZenRuleSelectionDialog { public abstract class ZenRuleSelectionDialog {
@@ -48,7 +52,6 @@ public abstract class ZenRuleSelectionDialog {
private final AlertDialog mDialog; private final AlertDialog mDialog;
private final LinearLayout mRuleContainer; private final LinearLayout mRuleContainer;
private final ServiceListing mServiceListing; private final ServiceListing mServiceListing;
private final List<ZenRuleInfo> mExternalRuleTypes = new ArrayList<ZenRuleInfo>();
public ZenRuleSelectionDialog(Context context, ServiceListing serviceListing) { public ZenRuleSelectionDialog(Context context, ServiceListing serviceListing) {
mContext = context; mContext = context;
@@ -59,9 +62,8 @@ public abstract class ZenRuleSelectionDialog {
mRuleContainer = (LinearLayout) v.findViewById(R.id.rule_container); mRuleContainer = (LinearLayout) v.findViewById(R.id.rule_container);
if (mServiceListing != null) { if (mServiceListing != null) {
bindType(defaultNewSchedule());
bindType(defaultNewEvent()); bindType(defaultNewEvent());
bindExternalRules(); bindType(defaultNewSchedule());
mServiceListing.addCallback(mServiceListingCallback); mServiceListing.addCallback(mServiceListingCallback);
mServiceListing.reload(); mServiceListing.reload();
} }
@@ -147,8 +149,9 @@ public abstract class ZenRuleSelectionDialog {
return rt; return rt;
} }
private void bindExternalRules() { private void bindExternalRules(ZenRuleInfo[] externalRuleTypes) {
for (ZenRuleInfo ri : mExternalRuleTypes) { Arrays.sort(externalRuleTypes, RULE_TYPE_COMPARATOR);
for (ZenRuleInfo ri : externalRuleTypes) {
bindType(ri); bindType(ri);
} }
} }
@@ -157,16 +160,32 @@ public abstract class ZenRuleSelectionDialog {
@Override @Override
public void onServicesReloaded(List<ServiceInfo> services) { public void onServicesReloaded(List<ServiceInfo> services) {
if (DEBUG) Log.d(TAG, "Services reloaded: count=" + services.size()); if (DEBUG) Log.d(TAG, "Services reloaded: count=" + services.size());
for (ServiceInfo si : services) { ZenRuleInfo[] externalRuleTypes = new ZenRuleInfo[services.size()];
final ZenRuleInfo ri = ZenModeAutomationSettings.getRuleInfo(si); for (int i = 0; i < services.size(); i++) {
final ZenRuleInfo ri = ZenModeAutomationSettings.getRuleInfo(mPm, services.get(i));
if (ri != null && ri.configurationActivity != null) { if (ri != null && ri.configurationActivity != null) {
mExternalRuleTypes.add(ri); externalRuleTypes[i] = ri;
} }
} }
bindExternalRules(); bindExternalRules(externalRuleTypes);
} }
}; };
private static final Comparator<ZenRuleInfo> RULE_TYPE_COMPARATOR =
new Comparator<ZenRuleInfo>() {
private final Collator mCollator = Collator.getInstance();
@Override
public int compare(ZenRuleInfo lhs, ZenRuleInfo rhs) {
int byAppName = mCollator.compare(lhs.packageLabel, rhs.packageLabel);
if (byAppName != 0) {
return byAppName;
} else {
return mCollator.compare(lhs.title, rhs.title);
}
}
};
private class LoadIconTask extends AsyncTask<ApplicationInfo, Void, Drawable> { private class LoadIconTask extends AsyncTask<ApplicationInfo, Void, Drawable> {
private final WeakReference<ImageView> viewReference; private final WeakReference<ImageView> viewReference;