From dadfd50325d8d9fbfabf52db46cb68b586697234 Mon Sep 17 00:00:00 2001 From: Fan Zhang Date: Wed, 26 Jul 2017 11:00:51 -0700 Subject: [PATCH] Misc change to support loading dynamic summary/icon in app - Add ThreadUtils.postOnBackground helper method - Don't fallback to app.icon if tile's metadata contains icon uri - Deprecate TITLE_RES_ID in favor of TITLE metadata Bug: 63758074 Test: robotests Change-Id: Ie60fb4677cdb12c3ff313ffdfdb4a4f4960fac0f --- .../android/settingslib/drawer/TileUtils.java | 32 ++++++------------- .../settingslib/utils/ThreadUtils.java | 14 ++++++++ .../settingslib/drawer/TileUtilsTest.java | 6 +--- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/packages/SettingsLib/src/com/android/settingslib/drawer/TileUtils.java b/packages/SettingsLib/src/com/android/settingslib/drawer/TileUtils.java index 9620a9153f80f..fc0a444915c3e 100644 --- a/packages/SettingsLib/src/com/android/settingslib/drawer/TileUtils.java +++ b/packages/SettingsLib/src/com/android/settingslib/drawer/TileUtils.java @@ -148,13 +148,6 @@ public class TileUtils { */ public static final String META_DATA_PREFERENCE_TITLE = "com.android.settings.title"; - /** - * @deprecated Use {@link #META_DATA_PREFERENCE_TITLE} with {@code android:resource} - */ - @Deprecated - public static final String META_DATA_PREFERENCE_TITLE_RES_ID = - "com.android.settings.title.resid"; - /** * Name of the meta-data item that should be set in the AndroidManifest.xml * to specify the summary text that should be displayed for the preference. @@ -176,7 +169,7 @@ public class TileUtils { * custom view which should be displayed for the preference. The custom view will be inflated * as a remote view. * - * This also can be used with {@link META_DATA_PREFERENCE_SUMMARY_URI} above, by setting the id + * This also can be used with {@link #META_DATA_PREFERENCE_SUMMARY_URI}, by setting the id * of the summary TextView to '@android:id/summary'. */ public static final String META_DATA_PREFERENCE_CUSTOM_VIEW = @@ -411,14 +404,7 @@ public class TileUtils { metaData.getBoolean(META_DATA_PREFERENCE_ICON_TINTABLE); } } - int resId = 0; - if (metaData.containsKey(META_DATA_PREFERENCE_TITLE_RES_ID)) { - resId = metaData.getInt(META_DATA_PREFERENCE_TITLE_RES_ID); - if (resId != 0) { - title = res.getString(resId); - } - } - if ((resId == 0) && metaData.containsKey(META_DATA_PREFERENCE_TITLE)) { + if (metaData.containsKey(META_DATA_PREFERENCE_TITLE)) { if (metaData.get(META_DATA_PREFERENCE_TITLE) instanceof Integer) { title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE)); } else { @@ -464,12 +450,14 @@ public class TileUtils { } // Set the icon - if (iconFromUri != null) { - tile.icon = Icon.createWithResource(iconFromUri.first, iconFromUri.second); - } else { - if (icon == 0) { + if (icon == 0) { + // Only fallback to activityinfo.icon if metadata does not contain ICON_URI. + // ICON_URI should be loaded in app UI when need the icon object. + if (!tile.metaData.containsKey(META_DATA_PREFERENCE_ICON_URI)) { icon = activityInfo.icon; } + } + if (icon != 0) { tile.icon = Icon.createWithResource(activityInfo.packageName, icon); } @@ -492,7 +480,7 @@ public class TileUtils { /** * Gets the icon package name and resource id from content provider. - * @param Context context + * @param context context * @param packageName package name of the target activity * @param uriString URI for the content provider * @param providerMap Maps URI authorities to providers @@ -522,7 +510,7 @@ public class TileUtils { /** * Gets text associated with the input key from the content provider. - * @param Context context + * @param context context * @param uriString URI for the content provider * @param providerMap Maps URI authorities to providers * @param key Key mapping to the text in bundle returned by the content provider diff --git a/packages/SettingsLib/src/com/android/settingslib/utils/ThreadUtils.java b/packages/SettingsLib/src/com/android/settingslib/utils/ThreadUtils.java index 6eeb59394f425..88adcdb16edcb 100644 --- a/packages/SettingsLib/src/com/android/settingslib/utils/ThreadUtils.java +++ b/packages/SettingsLib/src/com/android/settingslib/utils/ThreadUtils.java @@ -18,10 +18,14 @@ package com.android.settingslib.utils; import android.os.Handler; import android.os.Looper; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + public class ThreadUtils { private static volatile Thread sMainThread; private static volatile Handler sMainThreadHandler; + private static volatile ExecutorService sSingleThreadExecutor; /** * Returns true if the current thread is the UI thread. @@ -53,6 +57,16 @@ public class ThreadUtils { } } + /** + * Posts runnable in background using shared background thread pool. + */ + public static void postOnBackgroundThread(Runnable runnable) { + if (sSingleThreadExecutor == null) { + sSingleThreadExecutor = Executors.newSingleThreadExecutor(); + } + sSingleThreadExecutor.execute(runnable); + } + /** * Posts the runnable on the main thread. */ diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileUtilsTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileUtilsTest.java index e9ca753fa6221..3e90435b21d79 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileUtilsTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileUtilsTest.java @@ -535,14 +535,10 @@ public class TileUtilsTest { info.activityInfo.metaData.putString("com.android.settings.summary_uri", summaryUri); } if (titleResId != 0) { - info.activityInfo.metaData.putString(TileUtils.META_DATA_PREFERENCE_TITLE, title); + info.activityInfo.metaData.putInt(TileUtils.META_DATA_PREFERENCE_TITLE, titleResId); } else if (title != null) { info.activityInfo.metaData.putString(TileUtils.META_DATA_PREFERENCE_TITLE, title); } - if (titleResId != 0) { - info.activityInfo.metaData.putInt( - TileUtils.META_DATA_PREFERENCE_TITLE_RES_ID, titleResId); - } info.activityInfo.applicationInfo = new ApplicationInfo(); if (systemApp) { info.activityInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM;