diff options
Diffstat (limited to 'packages/SettingsLib/src')
5 files changed, 433 insertions, 57 deletions
diff --git a/packages/SettingsLib/src/com/android/settingslib/animation/AppearAnimationCreator.java b/packages/SettingsLib/src/com/android/settingslib/animation/AppearAnimationCreator.java new file mode 100644 index 0000000..8a61e4e --- /dev/null +++ b/packages/SettingsLib/src/com/android/settingslib/animation/AppearAnimationCreator.java @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.settingslib.animation; + +import android.view.animation.Interpolator; + +/** + * An interface which can create animations when starting an appear animation with + * {@link AppearAnimationUtils} + */ +public interface AppearAnimationCreator<T> { + void createAnimation(T animatedObject, long delay, long duration, + float translationY, boolean appearing, Interpolator interpolator, + Runnable finishListener); +} diff --git a/packages/SettingsLib/src/com/android/settingslib/animation/AppearAnimationUtils.java b/packages/SettingsLib/src/com/android/settingslib/animation/AppearAnimationUtils.java new file mode 100644 index 0000000..441474d --- /dev/null +++ b/packages/SettingsLib/src/com/android/settingslib/animation/AppearAnimationUtils.java @@ -0,0 +1,206 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.settingslib.animation; + +import android.content.Context; +import android.view.View; +import android.view.animation.AnimationUtils; +import android.view.animation.Interpolator; + +import com.android.settingslib.R; + +/** + * A class to make nice appear transitions for views in a tabular layout. + */ +public class AppearAnimationUtils implements AppearAnimationCreator<View> { + + public static final long DEFAULT_APPEAR_DURATION = 220; + + private final Interpolator mInterpolator; + private final float mStartTranslation; + private final AppearAnimationProperties mProperties = new AppearAnimationProperties(); + protected final float mDelayScale; + private final long mDuration; + protected RowTranslationScaler mRowTranslationScaler; + protected boolean mAppearing; + + public AppearAnimationUtils(Context ctx) { + this(ctx, DEFAULT_APPEAR_DURATION, + 1.0f, 1.0f, + AnimationUtils.loadInterpolator(ctx, android.R.interpolator.linear_out_slow_in)); + } + + public AppearAnimationUtils(Context ctx, long duration, float translationScaleFactor, + float delayScaleFactor, Interpolator interpolator) { + mInterpolator = interpolator; + mStartTranslation = ctx.getResources().getDimensionPixelOffset( + R.dimen.appear_y_translation_start) * translationScaleFactor; + mDelayScale = delayScaleFactor; + mDuration = duration; + mAppearing = true; + } + + public void startAnimation2d(View[][] objects, final Runnable finishListener) { + startAnimation2d(objects, finishListener, this); + } + + public void startAnimation(View[] objects, final Runnable finishListener) { + startAnimation(objects, finishListener, this); + } + + public <T> void startAnimation2d(T[][] objects, final Runnable finishListener, + AppearAnimationCreator<T> creator) { + AppearAnimationProperties properties = getDelays(objects); + startAnimations(properties, objects, finishListener, creator); + } + + public <T> void startAnimation(T[] objects, final Runnable finishListener, + AppearAnimationCreator<T> creator) { + AppearAnimationProperties properties = getDelays(objects); + startAnimations(properties, objects, finishListener, creator); + } + + private <T> void startAnimations(AppearAnimationProperties properties, T[] objects, + final Runnable finishListener, AppearAnimationCreator<T> creator) { + if (properties.maxDelayRowIndex == -1 || properties.maxDelayColIndex == -1) { + finishListener.run(); + return; + } + for (int row = 0; row < properties.delays.length; row++) { + long[] columns = properties.delays[row]; + long delay = columns[0]; + Runnable endRunnable = null; + if (properties.maxDelayRowIndex == row && properties.maxDelayColIndex == 0) { + endRunnable = finishListener; + } + float translationScale = mRowTranslationScaler != null + ? mRowTranslationScaler.getRowTranslationScale(row, properties.delays.length) + : 1f; + float translation = translationScale * mStartTranslation; + creator.createAnimation(objects[row], delay, mDuration, + mAppearing ? translation : -translation, + mAppearing, mInterpolator, endRunnable); + } + } + + private <T> void startAnimations(AppearAnimationProperties properties, T[][] objects, + final Runnable finishListener, AppearAnimationCreator<T> creator) { + if (properties.maxDelayRowIndex == -1 || properties.maxDelayColIndex == -1) { + finishListener.run(); + return; + } + for (int row = 0; row < properties.delays.length; row++) { + long[] columns = properties.delays[row]; + float translationScale = mRowTranslationScaler != null + ? mRowTranslationScaler.getRowTranslationScale(row, properties.delays.length) + : 1f; + float translation = translationScale * mStartTranslation; + for (int col = 0; col < columns.length; col++) { + long delay = columns[col]; + Runnable endRunnable = null; + if (properties.maxDelayRowIndex == row && properties.maxDelayColIndex == col) { + endRunnable = finishListener; + } + creator.createAnimation(objects[row][col], delay, mDuration, + mAppearing ? translation : -translation, + mAppearing, mInterpolator, endRunnable); + } + } + } + + private <T> AppearAnimationProperties getDelays(T[] items) { + long maxDelay = -1; + mProperties.maxDelayColIndex = -1; + mProperties.maxDelayRowIndex = -1; + mProperties.delays = new long[items.length][]; + for (int row = 0; row < items.length; row++) { + mProperties.delays[row] = new long[1]; + long delay = calculateDelay(row, 0); + mProperties.delays[row][0] = delay; + if (items[row] != null && delay > maxDelay) { + maxDelay = delay; + mProperties.maxDelayColIndex = 0; + mProperties.maxDelayRowIndex = row; + } + } + return mProperties; + } + + private <T> AppearAnimationProperties getDelays(T[][] items) { + long maxDelay = -1; + mProperties.maxDelayColIndex = -1; + mProperties.maxDelayRowIndex = -1; + mProperties.delays = new long[items.length][]; + for (int row = 0; row < items.length; row++) { + T[] columns = items[row]; + mProperties.delays[row] = new long[columns.length]; + for (int col = 0; col < columns.length; col++) { + long delay = calculateDelay(row, col); + mProperties.delays[row][col] = delay; + if (items[row][col] != null && delay > maxDelay) { + maxDelay = delay; + mProperties.maxDelayColIndex = col; + mProperties.maxDelayRowIndex = row; + } + } + } + return mProperties; + } + + protected long calculateDelay(int row, int col) { + return (long) ((row * 40 + col * (Math.pow(row, 0.4) + 0.4) * 20) * mDelayScale); + } + + public Interpolator getInterpolator() { + return mInterpolator; + } + + public float getStartTranslation() { + return mStartTranslation; + } + + @Override + public void createAnimation(View view, long delay, long duration, float translationY, + boolean appearing, Interpolator interpolator, Runnable endRunnable) { + if (view != null) { + view.setAlpha(appearing ? 0f : 1.0f); + view.setTranslationY(appearing ? translationY : 0); + view.animate() + .alpha(appearing ? 1f : 0f) + .translationY(appearing ? 0 : translationY) + .setInterpolator(interpolator) + .setDuration(duration) + .setStartDelay(delay); + if (view.hasOverlappingRendering()) { + view.animate().withLayer(); + } + if (endRunnable != null) { + view.animate().withEndAction(endRunnable); + } + } + } + + public class AppearAnimationProperties { + public long[][] delays; + public int maxDelayRowIndex; + public int maxDelayColIndex; + } + + public interface RowTranslationScaler { + float getRowTranslationScale(int row, int numRows); + } +} diff --git a/packages/SettingsLib/src/com/android/settingslib/animation/DisappearAnimationUtils.java b/packages/SettingsLib/src/com/android/settingslib/animation/DisappearAnimationUtils.java new file mode 100644 index 0000000..a444ff0 --- /dev/null +++ b/packages/SettingsLib/src/com/android/settingslib/animation/DisappearAnimationUtils.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.settingslib.animation; + +import android.content.Context; +import android.view.animation.AnimationUtils; +import android.view.animation.Interpolator; + +/** + * A class to make nice disappear transitions for views in a tabular layout. + */ +public class DisappearAnimationUtils extends AppearAnimationUtils { + + public DisappearAnimationUtils(Context ctx) { + this(ctx, DEFAULT_APPEAR_DURATION, + 1.0f, 1.0f, + AnimationUtils.loadInterpolator(ctx, android.R.interpolator.fast_out_linear_in)); + } + + public DisappearAnimationUtils(Context ctx, long duration, float translationScaleFactor, + float delayScaleFactor, Interpolator interpolator) { + this(ctx, duration, translationScaleFactor, delayScaleFactor, interpolator, + ROW_TRANSLATION_SCALER); + } + + public DisappearAnimationUtils(Context ctx, long duration, float translationScaleFactor, + float delayScaleFactor, Interpolator interpolator, RowTranslationScaler rowScaler) { + super(ctx, duration, translationScaleFactor, delayScaleFactor, interpolator); + mRowTranslationScaler = rowScaler; + mAppearing = false; + } + + protected long calculateDelay(int row, int col) { + return (long) ((row * 60 + col * (Math.pow(row, 0.4) + 0.4) * 10) * mDelayScale); + } + + private static final RowTranslationScaler ROW_TRANSLATION_SCALER = new RowTranslationScaler() { + + @Override + public float getRowTranslationScale(int row, int numRows) { + return (float) (Math.pow((numRows - row), 2) / numRows); + } + }; +} diff --git a/packages/SettingsLib/src/com/android/settingslib/applications/ApplicationsState.java b/packages/SettingsLib/src/com/android/settingslib/applications/ApplicationsState.java index b5e53c0..6561512 100644 --- a/packages/SettingsLib/src/com/android/settingslib/applications/ApplicationsState.java +++ b/packages/SettingsLib/src/com/android/settingslib/applications/ApplicationsState.java @@ -784,6 +784,21 @@ public class ApplicationsState { numDone++; getEntryLocked(info); } + if (userId != 0 && mEntriesMap.indexOfKey(0) >= 0) { + // If this app is for a profile and we are on the owner, remove + // the owner entry if it isn't installed. This will prevent + // duplicates of work only apps showing up as 'not installed + // for this user'. + // Note: This depends on us traversing the users in order, which + // happens because of the way we generate the list in + // doResumeIfNeededLocked. + AppEntry entry = mEntriesMap.get(0).get(info.packageName); + if (entry != null && + (entry.info.flags & ApplicationInfo.FLAG_INSTALLED) == 0) { + mEntriesMap.get(0).remove(info.packageName); + mAppEntries.remove(entry); + } + } } if (DEBUG_LOCKING) Log.v(TAG, "MSG_LOAD_ENTRIES releasing lock"); } diff --git a/packages/SettingsLib/src/com/android/settingslib/datetime/ZoneGetter.java b/packages/SettingsLib/src/com/android/settingslib/datetime/ZoneGetter.java index 12ead4e..7b5bfb5 100644 --- a/packages/SettingsLib/src/com/android/settingslib/datetime/ZoneGetter.java +++ b/packages/SettingsLib/src/com/android/settingslib/datetime/ZoneGetter.java @@ -32,13 +32,14 @@ import org.xmlpull.v1.XmlPullParserException; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Calendar; import java.util.Date; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Locale; +import java.util.Map; +import java.util.Set; import java.util.TimeZone; +import java.util.TreeSet; public class ZoneGetter { private static final String TAG = "ZoneGetter"; @@ -50,17 +51,120 @@ public class ZoneGetter { public static final String KEY_GMT = "gmt"; // value: String public static final String KEY_OFFSET = "offset"; // value: int (Integer) - private final List<HashMap<String, Object>> mZones = new ArrayList<>(); - private final HashSet<String> mLocalZones = new HashSet<>(); - private final Date mNow = Calendar.getInstance().getTime(); - private final SimpleDateFormat mZoneNameFormatter = new SimpleDateFormat("zzzz"); + private ZoneGetter() {} - public List<HashMap<String, Object>> getZones(Context context) { - for (String olsonId : TimeZoneNames.forLocale(Locale.getDefault())) { - mLocalZones.add(olsonId); + public static String getTimeZoneOffsetAndName(TimeZone tz, Date now) { + Locale locale = Locale.getDefault(); + String gmtString = getGmtOffsetString(locale, tz, now); + String zoneNameString = getZoneLongName(locale, tz, now); + if (zoneNameString == null) { + return gmtString; + } + + // We don't use punctuation here to avoid having to worry about localizing that too! + return gmtString + " " + zoneNameString; + } + + public static List<Map<String, Object>> getZonesList(Context context) { + final Locale locale = Locale.getDefault(); + final Date now = new Date(); + + // The display name chosen for each zone entry depends on whether the zone is one associated + // with the country of the user's chosen locale. For "local" zones we prefer the "long name" + // (e.g. "Europe/London" -> "British Summer Time" for people in the UK). For "non-local" + // zones we prefer the exemplar location (e.g. "Europe/London" -> "London" for English + // speakers from outside the UK). This heuristic is based on the fact that people are + // typically familiar with their local timezones and exemplar locations don't always match + // modern-day expectations for people living in the country covered. Large countries like + // China that mostly use a single timezone (olson id: "Asia/Shanghai") may not live near + // "Shanghai" and prefer the long name over the exemplar location. The only time we don't + // follow this policy for local zones is when Android supplies multiple olson IDs to choose + // from and the use of a zone's long name leads to ambiguity. For example, at the time of + // writing Android lists 5 olson ids for Australia which collapse to 2 different zone names + // in winter but 4 different zone names in summer. The ambiguity leads to the users + // selecting the wrong olson ids. + + // Get the list of olson ids to display to the user. + List<String> olsonIdsToDisplay = readTimezonesToDisplay(context); + + // Create a lookup of local zone IDs. + Set<String> localZoneIds = new TreeSet<String>(); + for (String olsonId : TimeZoneNames.forLocale(locale)) { + localZoneIds.add(olsonId); + } + + // Work out whether the long names for the local entries that we would show by default would + // be ambiguous. + Set<String> localZoneNames = new TreeSet<String>(); + boolean localLongNamesAreAmbiguous = false; + for (String olsonId : olsonIdsToDisplay) { + if (localZoneIds.contains(olsonId)) { + TimeZone tz = TimeZone.getTimeZone(olsonId); + String zoneLongName = getZoneLongName(locale, tz, now); + boolean longNameIsUnique = localZoneNames.add(zoneLongName); + if (!longNameIsUnique) { + localLongNamesAreAmbiguous = true; + break; + } + } + } + + // Generate the list of zone entries to return. + List<Map<String, Object>> zones = new ArrayList<Map<String, Object>>(); + for (String olsonId : olsonIdsToDisplay) { + final TimeZone tz = TimeZone.getTimeZone(olsonId); + // Exemplar location display is the default. The only time we intend to display the long + // name is when the olsonId is local AND long names are not ambiguous. + boolean isLocalZoneId = localZoneIds.contains(olsonId); + boolean preferLongName = isLocalZoneId && !localLongNamesAreAmbiguous; + String displayName = getZoneDisplayName(locale, tz, now, preferLongName); + + String gmtOffsetString = getGmtOffsetString(locale, tz, now); + int offsetMillis = tz.getOffset(now.getTime()); + Map<String, Object> displayEntry = + createDisplayEntry(tz, gmtOffsetString, displayName, offsetMillis); + zones.add(displayEntry); + } + return zones; + } + + private static Map<String, Object> createDisplayEntry( + TimeZone tz, String gmtOffsetString, String displayName, int offsetMillis) { + Map<String, Object> map = new HashMap<String, Object>(); + map.put(KEY_ID, tz.getID()); + map.put(KEY_DISPLAYNAME, displayName); + map.put(KEY_GMT, gmtOffsetString); + map.put(KEY_OFFSET, offsetMillis); + return map; + } + + /** + * Returns a name for the specific zone. If {@code preferLongName} is {@code true} then the + * long display name for the timezone will be used, otherwise the exemplar location will be + * preferred. + */ + private static String getZoneDisplayName(Locale locale, TimeZone tz, Date now, + boolean preferLongName) { + String zoneNameString; + if (preferLongName) { + zoneNameString = getZoneLongName(locale, tz, now); + } else { + zoneNameString = getZoneExemplarLocation(locale, tz); + if (zoneNameString == null || zoneNameString.isEmpty()) { + // getZoneExemplarLocation can return null. + zoneNameString = getZoneLongName(locale, tz, now); + } } - try { - XmlResourceParser xrp = context.getResources().getXml(R.xml.timezones); + return zoneNameString; + } + + private static String getZoneExemplarLocation(Locale locale, TimeZone tz) { + return TimeZoneNames.getExemplarLocation(locale.toString(), tz.getID()); + } + + private static List<String> readTimezonesToDisplay(Context context) { + List<String> olsonIds = new ArrayList<String>(); + try (XmlResourceParser xrp = context.getResources().getXml(R.xml.timezones)) { while (xrp.next() != XmlResourceParser.START_TAG) { continue; } @@ -68,58 +172,34 @@ public class ZoneGetter { while (xrp.getEventType() != XmlResourceParser.END_TAG) { while (xrp.getEventType() != XmlResourceParser.START_TAG) { if (xrp.getEventType() == XmlResourceParser.END_DOCUMENT) { - return mZones; + return olsonIds; } xrp.next(); } if (xrp.getName().equals(XMLTAG_TIMEZONE)) { String olsonId = xrp.getAttributeValue(0); - addTimeZone(olsonId); + olsonIds.add(olsonId); } while (xrp.getEventType() != XmlResourceParser.END_TAG) { xrp.next(); } xrp.next(); } - xrp.close(); } catch (XmlPullParserException xppe) { Log.e(TAG, "Ill-formatted timezones.xml file"); } catch (java.io.IOException ioe) { Log.e(TAG, "Unable to read timezones.xml file"); } - return mZones; + return olsonIds; } - private void addTimeZone(String olsonId) { - // We always need the "GMT-07:00" string. - final TimeZone tz = TimeZone.getTimeZone(olsonId); - - // For the display name, we treat time zones within the country differently - // from other countries' time zones. So in en_US you'd get "Pacific Daylight Time" - // but in de_DE you'd get "Los Angeles" for the same time zone. - String displayName; - if (mLocalZones.contains(olsonId)) { - // Within a country, we just use the local name for the time zone. - mZoneNameFormatter.setTimeZone(tz); - displayName = mZoneNameFormatter.format(mNow); - } else { - // For other countries' time zones, we use the exemplar location. - final String localeName = Locale.getDefault().toString(); - displayName = TimeZoneNames.getExemplarLocation(localeName, olsonId); - } - - final HashMap<String, Object> map = new HashMap<>(); - map.put(KEY_ID, olsonId); - map.put(KEY_DISPLAYNAME, displayName); - map.put(KEY_GMT, getTimeZoneText(tz, false)); - map.put(KEY_OFFSET, tz.getOffset(mNow.getTime())); - - mZones.add(map); + private static String getZoneLongName(Locale locale, TimeZone tz, Date now) { + boolean daylight = tz.inDaylightTime(now); + // This returns a name if it can, or will fall back to GMT+0X:00 format. + return tz.getDisplayName(daylight, TimeZone.LONG, locale); } - public static String getTimeZoneText(TimeZone tz, boolean includeName) { - Date now = new Date(); - + private static String getGmtOffsetString(Locale locale, TimeZone tz, Date now) { // Use SimpleDateFormat to format the GMT+00:00 string. SimpleDateFormat gmtFormatter = new SimpleDateFormat("ZZZZ"); gmtFormatter.setTimeZone(tz); @@ -127,21 +207,9 @@ public class ZoneGetter { // Ensure that the "GMT+" stays with the "00:00" even if the digits are RTL. BidiFormatter bidiFormatter = BidiFormatter.getInstance(); - Locale l = Locale.getDefault(); - boolean isRtl = TextUtils.getLayoutDirectionFromLocale(l) == View.LAYOUT_DIRECTION_RTL; + boolean isRtl = TextUtils.getLayoutDirectionFromLocale(locale) == View.LAYOUT_DIRECTION_RTL; gmtString = bidiFormatter.unicodeWrap(gmtString, isRtl ? TextDirectionHeuristics.RTL : TextDirectionHeuristics.LTR); - - if (!includeName) { - return gmtString; - } - - // Optionally append the time zone name. - SimpleDateFormat zoneNameFormatter = new SimpleDateFormat("zzzz"); - zoneNameFormatter.setTimeZone(tz); - String zoneNameString = zoneNameFormatter.format(now); - - // We don't use punctuation here to avoid having to worry about localizing that too! - return gmtString + " " + zoneNameString; + return gmtString; } } |
