Settings: Display corp profile calendars in event rule selection.
And sort the accounts by display name. Bug: 21155107 Change-Id: I8789022535ba7c66d950f5ec6f93c40ffb8106d3
This commit is contained in:
@@ -23,6 +23,7 @@ import android.content.Intent;
|
|||||||
import android.content.pm.ServiceInfo;
|
import android.content.pm.ServiceInfo;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.UserHandle;
|
||||||
import android.preference.Preference;
|
import android.preference.Preference;
|
||||||
import android.preference.Preference.OnPreferenceClickListener;
|
import android.preference.Preference.OnPreferenceClickListener;
|
||||||
import android.preference.PreferenceScreen;
|
import android.preference.PreferenceScreen;
|
||||||
@@ -214,14 +215,11 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
|
|||||||
|
|
||||||
private String computeCalendarName(EventInfo event) {
|
private String computeCalendarName(EventInfo event) {
|
||||||
if (event.calendar != EventInfo.ANY_CALENDAR) {
|
if (event.calendar != EventInfo.ANY_CALENDAR) {
|
||||||
final CalendarInfo[] calendars = ZenModeEventRuleSettings.getCalendars(mContext);
|
final CalendarInfo calendar = ZenModeEventRuleSettings.findCalendar(mContext, event);
|
||||||
for (int i = 0; i < calendars.length; i++) {
|
if (calendar != null) {
|
||||||
final CalendarInfo calendar = calendars[i];
|
|
||||||
if (calendar.id == event.calendar) {
|
|
||||||
return calendar.name;
|
return calendar.name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return getString(R.string.zen_mode_event_rule_summary_any_calendar);
|
return getString(R.string.zen_mode_event_rule_summary_any_calendar);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -16,8 +16,13 @@
|
|||||||
|
|
||||||
package com.android.settings.notification;
|
package com.android.settings.notification;
|
||||||
|
|
||||||
|
import static android.service.notification.ZenModeConfig.EventInfo.ANY_CALENDAR;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.pm.PackageManager.NameNotFoundException;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
|
import android.os.UserHandle;
|
||||||
|
import android.os.UserManager;
|
||||||
import android.preference.PreferenceScreen;
|
import android.preference.PreferenceScreen;
|
||||||
import android.provider.CalendarContract.Calendars;
|
import android.provider.CalendarContract.Calendars;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
@@ -29,6 +34,11 @@ import com.android.internal.logging.MetricsLogger;
|
|||||||
import com.android.settings.DropDownPreference;
|
import com.android.settings.DropDownPreference;
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class ZenModeEventRuleSettings extends ZenModeRuleSettingsBase {
|
public class ZenModeEventRuleSettings extends ZenModeRuleSettingsBase {
|
||||||
private static final String KEY_CALENDAR = "calendar";
|
private static final String KEY_CALENDAR = "calendar";
|
||||||
private static final String KEY_REPLY = "reply";
|
private static final String KEY_REPLY = "reply";
|
||||||
@@ -39,7 +49,8 @@ public class ZenModeEventRuleSettings extends ZenModeRuleSettingsBase {
|
|||||||
private DropDownPreference mReply;
|
private DropDownPreference mReply;
|
||||||
|
|
||||||
private EventInfo mEvent;
|
private EventInfo mEvent;
|
||||||
private CalendarInfo[] mCalendars;
|
private List<CalendarInfo> mCalendars;
|
||||||
|
private boolean mCreate;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean setRule(ZenRule rule) {
|
protected boolean setRule(ZenRule rule) {
|
||||||
@@ -61,20 +72,24 @@ public class ZenModeEventRuleSettings extends ZenModeRuleSettingsBase {
|
|||||||
@Override
|
@Override
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
if (!mCreate) {
|
||||||
reloadCalendar();
|
reloadCalendar();
|
||||||
}
|
}
|
||||||
|
mCreate = false;
|
||||||
|
}
|
||||||
|
|
||||||
private void reloadCalendar() {
|
private void reloadCalendar() {
|
||||||
mCalendars = getCalendars(mContext);
|
mCalendars = getCalendars(mContext);
|
||||||
mCalendar.clearItems();
|
mCalendar.clearItems();
|
||||||
mCalendar.addItem(R.string.zen_mode_event_rule_calendar_any, 0L);
|
mCalendar.addItem(R.string.zen_mode_event_rule_calendar_any, key(0, ANY_CALENDAR));
|
||||||
for (int i = 0; i < mCalendars.length; i++) {
|
for (CalendarInfo calendar : mCalendars) {
|
||||||
mCalendar.addItem(mCalendars[i].name, mCalendars[i].id);
|
mCalendar.addItem(calendar.name, key(calendar));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreateInternal() {
|
protected void onCreateInternal() {
|
||||||
|
mCreate = true;
|
||||||
addPreferencesFromResource(R.xml.zen_mode_event_rule_settings);
|
addPreferencesFromResource(R.xml.zen_mode_event_rule_settings);
|
||||||
final PreferenceScreen root = getPreferenceScreen();
|
final PreferenceScreen root = getPreferenceScreen();
|
||||||
|
|
||||||
@@ -82,9 +97,11 @@ public class ZenModeEventRuleSettings extends ZenModeRuleSettingsBase {
|
|||||||
mCalendar.setCallback(new DropDownPreference.Callback() {
|
mCalendar.setCallback(new DropDownPreference.Callback() {
|
||||||
@Override
|
@Override
|
||||||
public boolean onItemSelected(int pos, Object value) {
|
public boolean onItemSelected(int pos, Object value) {
|
||||||
final long calendar = (Long) value;
|
final String calendarKey = (String) value;
|
||||||
if (calendar == mEvent.calendar) return true;
|
if (calendarKey.equals(key(mEvent))) return true;
|
||||||
mEvent.calendar = calendar;
|
final int i = calendarKey.indexOf(':');
|
||||||
|
mEvent.userId = Integer.parseInt(calendarKey.substring(0, i));
|
||||||
|
mEvent.calendar = Long.parseLong(calendarKey.substring(i + 1));
|
||||||
updateRule(ZenModeConfig.toEventConditionId(mEvent));
|
updateRule(ZenModeConfig.toEventConditionId(mEvent));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -114,7 +131,7 @@ public class ZenModeEventRuleSettings extends ZenModeRuleSettingsBase {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void updateControlsInternal() {
|
protected void updateControlsInternal() {
|
||||||
mCalendar.setSelectedValue(mEvent.calendar);
|
mCalendar.setSelectedValue(key(mEvent));
|
||||||
mReply.setSelectedValue(mEvent.reply);
|
mReply.setSelectedValue(mEvent.reply);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,7 +140,38 @@ public class ZenModeEventRuleSettings extends ZenModeRuleSettingsBase {
|
|||||||
return MetricsLogger.NOTIFICATION_ZEN_MODE_EVENT_RULE;
|
return MetricsLogger.NOTIFICATION_ZEN_MODE_EVENT_RULE;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CalendarInfo[] getCalendars(Context context) {
|
public static CalendarInfo findCalendar(Context context, EventInfo event) {
|
||||||
|
if (context == null || event == null) return null;
|
||||||
|
final String eventKey = key(event);
|
||||||
|
for (CalendarInfo calendar : getCalendars(context)) {
|
||||||
|
if (eventKey.equals(key(calendar))) {
|
||||||
|
return calendar;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<CalendarInfo> getCalendars(Context context) {
|
||||||
|
final List<CalendarInfo> calendars = new ArrayList<>();
|
||||||
|
for (UserHandle user : UserManager.get(context).getUserProfiles()) {
|
||||||
|
final Context userContext = getContextForUser(context, user);
|
||||||
|
if (userContext != null) {
|
||||||
|
addCalendars(userContext, calendars);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Collections.sort(calendars, CALENDAR_NAME);
|
||||||
|
return calendars;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Context getContextForUser(Context context, UserHandle user) {
|
||||||
|
try {
|
||||||
|
return context.createPackageContextAsUser(context.getPackageName(), 0, user);
|
||||||
|
} catch (NameNotFoundException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void addCalendars(Context context, List<CalendarInfo> outCalendars) {
|
||||||
final String primary = "\"primary\"";
|
final String primary = "\"primary\"";
|
||||||
final String[] projection = { Calendars._ID, Calendars.CALENDAR_DISPLAY_NAME,
|
final String[] projection = { Calendars._ID, Calendars.CALENDAR_DISPLAY_NAME,
|
||||||
"(" + Calendars.ACCOUNT_NAME + "=" + Calendars.OWNER_ACCOUNT + ") AS " + primary };
|
"(" + Calendars.ACCOUNT_NAME + "=" + Calendars.OWNER_ACCOUNT + ") AS " + primary };
|
||||||
@@ -133,17 +181,15 @@ public class ZenModeEventRuleSettings extends ZenModeRuleSettingsBase {
|
|||||||
cursor = context.getContentResolver().query(Calendars.CONTENT_URI, projection,
|
cursor = context.getContentResolver().query(Calendars.CONTENT_URI, projection,
|
||||||
selection, null, null);
|
selection, null, null);
|
||||||
if (cursor == null) {
|
if (cursor == null) {
|
||||||
return new CalendarInfo[0];
|
return;
|
||||||
}
|
}
|
||||||
final CalendarInfo[] rt = new CalendarInfo[cursor.getCount()];
|
|
||||||
int i = 0;
|
|
||||||
while (cursor.moveToNext()) {
|
while (cursor.moveToNext()) {
|
||||||
final CalendarInfo ci = new CalendarInfo();
|
final CalendarInfo ci = new CalendarInfo();
|
||||||
ci.id = cursor.getLong(0);
|
ci.id = cursor.getLong(0);
|
||||||
ci.name = cursor.getString(1);
|
ci.name = cursor.getString(1);
|
||||||
rt[i++] = ci;
|
ci.userId = context.getUserId();
|
||||||
|
outCalendars.add(ci);
|
||||||
}
|
}
|
||||||
return rt;
|
|
||||||
} finally {
|
} finally {
|
||||||
if (cursor != null) {
|
if (cursor != null) {
|
||||||
cursor.close();
|
cursor.close();
|
||||||
@@ -151,9 +197,29 @@ public class ZenModeEventRuleSettings extends ZenModeRuleSettingsBase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String key(CalendarInfo calendar) {
|
||||||
|
return key(calendar.userId, calendar.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String key(EventInfo event) {
|
||||||
|
return key(event.userId, event.calendar);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String key(int userId, long calendarId) {
|
||||||
|
return EventInfo.resolveUserId(userId) + ":" + calendarId;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Comparator<CalendarInfo> CALENDAR_NAME = new Comparator<CalendarInfo>() {
|
||||||
|
@Override
|
||||||
|
public int compare(CalendarInfo lhs, CalendarInfo rhs) {
|
||||||
|
return lhs.name.compareTo(rhs.name);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
public static class CalendarInfo {
|
public static class CalendarInfo {
|
||||||
public long id;
|
public long id;
|
||||||
public String name;
|
public String name;
|
||||||
|
public int userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user