From 4dd81467e33a694138da6916fc68ca79501a9429 Mon Sep 17 00:00:00 2001 From: Jason Monk Date: Mon, 8 Sep 2014 10:13:59 -0400 Subject: [PATCH] Move zen mode constants and code to framework This allows the strings and code to be shared with settings. Bug: 15454830 Change-Id: I9300af4f8b6d80042452b75db3418b20c5c5cc81 --- .../service/notification/ZenModeConfig.java | 23 +++++++++++ core/res/res/values/strings.xml | 15 ++++++++ core/res/res/values/symbols.xml | 3 ++ packages/SystemUI/res/values/strings.xml | 15 -------- .../android/systemui/volume/ZenModePanel.java | 38 +++++-------------- 5 files changed, 51 insertions(+), 43 deletions(-) diff --git a/core/java/android/service/notification/ZenModeConfig.java b/core/java/android/service/notification/ZenModeConfig.java index 872f911c03eff..9cbedabd98df3 100644 --- a/core/java/android/service/notification/ZenModeConfig.java +++ b/core/java/android/service/notification/ZenModeConfig.java @@ -17,6 +17,7 @@ package android.service.notification; import android.content.ComponentName; +import android.content.res.Resources; import android.net.Uri; import android.os.Parcel; import android.os.Parcelable; @@ -55,6 +56,11 @@ public class ZenModeConfig implements Parcelable { public static final int[] WEEKNIGHT_DAYS = { Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY, Calendar.WEDNESDAY, Calendar.THURSDAY }; + public static final int[] MINUTE_BUCKETS = new int[] { 15, 30, 45, 60, 120, 180, 240, 480 }; + private static final int SECONDS_MS = 1000; + private static final int MINUTES_MS = 60 * SECONDS_MS; + private static final int ZERO_VALUE_MS = 20 * SECONDS_MS; + private static final int XML_VERSION = 1; private static final String ZEN_TAG = "zen"; private static final String ZEN_ATT_VERSION = "version"; @@ -445,6 +451,23 @@ public class ZenModeConfig implements Parcelable { return downtime; } + public static Condition toTimeCondition(int minutesFromNow) { + final long now = System.currentTimeMillis(); + final long millis = minutesFromNow == 0 ? ZERO_VALUE_MS : minutesFromNow * MINUTES_MS; + return toTimeCondition(now + millis, minutesFromNow); + } + + public static Condition toTimeCondition(long time, int minutes) { + final int num = minutes < 60 ? minutes : Math.round(minutes / 60f); + final int resId = minutes < 60 + ? com.android.internal.R.plurals.zen_mode_duration_minutes + : com.android.internal.R.plurals.zen_mode_duration_hours; + final String caption = Resources.getSystem().getQuantityString(resId, num, num); + final Uri id = toCountdownConditionId(time); + return new Condition(id, caption, "", "", 0, Condition.STATE_TRUE, + Condition.FLAG_RELEVANT_NOW); + } + // For built-in conditions private static final String SYSTEM_AUTHORITY = "android"; diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml index 5c932fd73acfa..50da1fa9a15ca 100644 --- a/core/res/res/values/strings.xml +++ b/core/res/res/values/strings.xml @@ -4877,4 +4877,19 @@ Until your downtime ends at %1$s + + + + For one minute + For %d minutes + + + + + For one hour + For %d hours + + + + Indefinitely diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml index c28f3a693c7a2..556c07fd03221 100644 --- a/core/res/res/values/symbols.xml +++ b/core/res/res/values/symbols.xml @@ -1918,6 +1918,9 @@ + + + diff --git a/packages/SystemUI/res/values/strings.xml b/packages/SystemUI/res/values/strings.xml index 147efaf10d47b..0445fe816af63 100644 --- a/packages/SystemUI/res/values/strings.xml +++ b/packages/SystemUI/res/values/strings.xml @@ -736,9 +736,6 @@ Swipe left for camera - - Indefinitely - None @@ -805,18 +802,6 @@ When you add a new user, that person needs to set up their space.\n\nAny user can update apps for all other users. - - - For one minute - For %d minutes - - - - - For one hour - For %d hours - - Battery saver is on diff --git a/packages/SystemUI/src/com/android/systemui/volume/ZenModePanel.java b/packages/SystemUI/src/com/android/systemui/volume/ZenModePanel.java index ac7fc251cc691..ac1563dfda587 100644 --- a/packages/SystemUI/src/com/android/systemui/volume/ZenModePanel.java +++ b/packages/SystemUI/src/com/android/systemui/volume/ZenModePanel.java @@ -60,7 +60,7 @@ public class ZenModePanel extends LinearLayout { private static final int[] MINUTE_BUCKETS = DEBUG ? new int[] { 0, 1, 2, 5, 15, 30, 45, 60, 120, 180, 240, 480 } - : new int[] { 15, 30, 45, 60, 120, 180, 240, 480 }; + : ZenModeConfig.MINUTE_BUCKETS; private static final int MIN_BUCKET_MINUTES = MINUTE_BUCKETS[0]; private static final int MAX_BUCKET_MINUTES = MINUTE_BUCKETS[MINUTE_BUCKETS.length - 1]; private static final int DEFAULT_BUCKET_INDEX = Arrays.binarySearch(MINUTE_BUCKETS, 60); @@ -68,7 +68,6 @@ public class ZenModePanel extends LinearLayout { private static final int TIME_CONDITION_INDEX = 1; private static final int FIRST_CONDITION_INDEX = 2; private static final float SILENT_HINT_PULSE_SCALE = 1.1f; - private static final int ZERO_VALUE_MS = 20 * SECONDS_MS; public static final Intent ZEN_SETTINGS = new Intent(Settings.ACTION_ZEN_MODE_SETTINGS); @@ -213,7 +212,7 @@ public class ZenModePanel extends LinearLayout { mBucketIndex = -1; } else { mBucketIndex = DEFAULT_BUCKET_INDEX; - mTimeCondition = newTimeCondition(MINUTE_BUCKETS[mBucketIndex]); + mTimeCondition = ZenModeConfig.toTimeCondition(MINUTE_BUCKETS[mBucketIndex]); } if (DEBUG) Log.d(mTag, "Initial bucket index: " + mBucketIndex); mConditions = null; // reset conditions @@ -254,7 +253,7 @@ public class ZenModePanel extends LinearLayout { } private void refreshExitConditionText() { - final String forever = mContext.getString(R.string.zen_mode_forever); + final String forever = mContext.getString(com.android.internal.R.string.zen_mode_forever); if (mExitCondition == null) { mExitConditionText = forever; } else if (ZenModeConfig.isValidCountdownConditionId(mExitCondition.id)) { @@ -330,24 +329,7 @@ public class ZenModePanel extends LinearLayout { if (time == 0) return null; final long span = time - System.currentTimeMillis(); if (span <= 0 || span > MAX_BUCKET_MINUTES * MINUTES_MS) return null; - return timeCondition(time, Math.round(span / (float)MINUTES_MS)); - } - - private Condition newTimeCondition(int minutesFromNow) { - final long now = System.currentTimeMillis(); - final long millis = minutesFromNow == 0 ? ZERO_VALUE_MS : minutesFromNow * MINUTES_MS; - return timeCondition(now + millis, minutesFromNow); - } - - private Condition timeCondition(long time, int minutes) { - final int num = minutes < 60 ? minutes : Math.round(minutes / 60f); - final int resId = minutes < 60 - ? R.plurals.zen_mode_duration_minutes - : R.plurals.zen_mode_duration_hours; - final String caption = mContext.getResources().getQuantityString(resId, num, num); - final Uri id = ZenModeConfig.toCountdownConditionId(time); - return new Condition(id, caption, "", "", 0, Condition.STATE_TRUE, - Condition.FLAG_RELEVANT_NOW); + return ZenModeConfig.toTimeCondition(time, Math.round(span / (float) MINUTES_MS)); } private void handleUpdateConditions(Condition[] conditions) { @@ -401,7 +383,7 @@ public class ZenModePanel extends LinearLayout { if (favoriteIndex == -1) { getConditionTagAt(FOREVER_CONDITION_INDEX).rb.setChecked(true); } else { - mTimeCondition = newTimeCondition(MINUTE_BUCKETS[favoriteIndex]); + mTimeCondition = ZenModeConfig.toTimeCondition(MINUTE_BUCKETS[favoriteIndex]); mBucketIndex = favoriteIndex; bind(mTimeCondition, mZenConditions.getChildAt(TIME_CONDITION_INDEX)); getConditionTagAt(TIME_CONDITION_INDEX).rb.setChecked(true); @@ -457,7 +439,7 @@ public class ZenModePanel extends LinearLayout { }); final TextView title = (TextView) row.findViewById(android.R.id.title); if (condition == null) { - title.setText(R.string.zen_mode_forever); + title.setText(mContext.getString(com.android.internal.R.string.zen_mode_forever)); } else { title.setText(condition.summary); } @@ -494,7 +476,7 @@ public class ZenModePanel extends LinearLayout { } else { final long span = time - System.currentTimeMillis(); button1.setEnabled(span > MIN_BUCKET_MINUTES * MINUTES_MS); - final Condition maxCondition = newTimeCondition(MAX_BUCKET_MINUTES); + final Condition maxCondition = ZenModeConfig.toTimeCondition(MAX_BUCKET_MINUTES); button2.setEnabled(!Objects.equals(condition.summary, maxCondition.summary)); } @@ -520,18 +502,18 @@ public class ZenModePanel extends LinearLayout { final long bucketTime = now + bucketMinutes * MINUTES_MS; if (up && bucketTime > time || !up && bucketTime < time) { mBucketIndex = j; - newCondition = timeCondition(bucketTime, bucketMinutes); + newCondition = ZenModeConfig.toTimeCondition(bucketTime, bucketMinutes); break; } } if (newCondition == null) { mBucketIndex = DEFAULT_BUCKET_INDEX; - newCondition = newTimeCondition(MINUTE_BUCKETS[mBucketIndex]); + newCondition = ZenModeConfig.toTimeCondition(MINUTE_BUCKETS[mBucketIndex]); } } else { // on a known index, simply increment or decrement mBucketIndex = Math.max(0, Math.min(N - 1, mBucketIndex + (up ? 1 : -1))); - newCondition = newTimeCondition(MINUTE_BUCKETS[mBucketIndex]); + newCondition = ZenModeConfig.toTimeCondition(MINUTE_BUCKETS[mBucketIndex]); } mTimeCondition = newCondition; bind(mTimeCondition, row);