Loading packages/SettingsLib/src/com/android/settingslib/drawer/TileUtils.java +34 −36 Original line number Original line Diff line number Diff line Loading @@ -26,7 +26,6 @@ import android.content.pm.ResolveInfo; import android.content.res.Resources; import android.content.res.Resources; import android.graphics.drawable.Icon; import android.graphics.drawable.Icon; import android.net.Uri; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.os.Bundle; import android.os.RemoteException; import android.os.RemoteException; import android.os.UserHandle; import android.os.UserHandle; Loading Loading @@ -327,6 +326,15 @@ public class TileUtils { Context context, UserHandle user, Intent intent, Context context, UserHandle user, Intent intent, Map<Pair<String, String>, Tile> addedCache, String defaultCategory, List<Tile> outTiles, Map<Pair<String, String>, Tile> addedCache, String defaultCategory, List<Tile> outTiles, boolean usePriority, boolean checkCategory, boolean forceTintExternalIcon) { boolean usePriority, boolean checkCategory, boolean forceTintExternalIcon) { getTilesForIntent(context, user, intent, addedCache, defaultCategory, outTiles, usePriority, checkCategory, forceTintExternalIcon, false /* shouldUpdateTiles */); } public static void getTilesForIntent( Context context, UserHandle user, Intent intent, Map<Pair<String, String>, Tile> addedCache, String defaultCategory, List<Tile> outTiles, boolean usePriority, boolean checkCategory, boolean forceTintExternalIcon, boolean shouldUpdateTiles) { PackageManager pm = context.getPackageManager(); PackageManager pm = context.getPackageManager(); List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent, List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent, PackageManager.GET_META_DATA, user.getIdentifier()); PackageManager.GET_META_DATA, user.getIdentifier()); Loading Loading @@ -364,9 +372,11 @@ public class TileUtils { updateTileData(context, tile, activityInfo, activityInfo.applicationInfo, updateTileData(context, tile, activityInfo, activityInfo.applicationInfo, pm, providerMap, forceTintExternalIcon); pm, providerMap, forceTintExternalIcon); if (DEBUG) Log.d(LOG_TAG, "Adding tile " + tile.title); if (DEBUG) Log.d(LOG_TAG, "Adding tile " + tile.title); addedCache.put(key, tile); addedCache.put(key, tile); } else if (shouldUpdateTiles) { updateSummaryAndTitle(context, providerMap, tile); } } if (!tile.userHandle.contains(user)) { if (!tile.userHandle.contains(user)) { tile.userHandle.add(user); tile.userHandle.add(user); } } Loading @@ -387,7 +397,6 @@ public class TileUtils { String summary = null; String summary = null; String keyHint = null; String keyHint = null; boolean isIconTintable = false; boolean isIconTintable = false; RemoteViews remoteViews = null; // Get the activity's meta-data // Get the activity's meta-data try { try { Loading Loading @@ -442,7 +451,8 @@ public class TileUtils { } } if (metaData.containsKey(META_DATA_PREFERENCE_CUSTOM_VIEW)) { if (metaData.containsKey(META_DATA_PREFERENCE_CUSTOM_VIEW)) { int layoutId = metaData.getInt(META_DATA_PREFERENCE_CUSTOM_VIEW); int layoutId = metaData.getInt(META_DATA_PREFERENCE_CUSTOM_VIEW); remoteViews = new RemoteViews(applicationInfo.packageName, layoutId); tile.remoteViews = new RemoteViews(applicationInfo.packageName, layoutId); updateSummaryAndTitle(context, providerMap, tile); } } } } } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) { } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) { Loading Loading @@ -474,7 +484,6 @@ public class TileUtils { // Suggest a key for this tile // Suggest a key for this tile tile.key = keyHint; tile.key = keyHint; tile.isIconTintable = isIconTintable; tile.isIconTintable = isIconTintable; tile.remoteViews = remoteViews; return true; return true; } } Loading @@ -482,6 +491,26 @@ public class TileUtils { return false; return false; } } private static void updateSummaryAndTitle( Context context, Map<String, IContentProvider> providerMap, Tile tile) { if (tile == null || tile.metaData == null || !tile.metaData.containsKey(META_DATA_PREFERENCE_SUMMARY_URI)) { return; } String uriString = tile.metaData.getString(META_DATA_PREFERENCE_SUMMARY_URI); Bundle bundle = getBundleFromUri(context, uriString, providerMap); String overrideSummary = getString(bundle, META_DATA_PREFERENCE_SUMMARY); String overrideTitle = getString(bundle, META_DATA_PREFERENCE_TITLE); if (overrideSummary != null) { tile.remoteViews.setTextViewText(android.R.id.summary, overrideSummary); } if (overrideTitle != null) { tile.remoteViews.setTextViewText(android.R.id.title, overrideTitle); } } /** /** * Gets the icon package name and resource id from content provider. * Gets the icon package name and resource id from content provider. * @param Context context * @param Context context Loading Loading @@ -547,37 +576,6 @@ public class TileUtils { } } } } public static void updateTileUsingSummaryUri(Context context, final Tile tile) { if (tile == null || tile.metaData == null || !tile.metaData.containsKey(META_DATA_PREFERENCE_SUMMARY_URI)) { return; } new AsyncTask<Void, Void, Bundle>() { @Override protected Bundle doInBackground(Void... params) { return getBundleFromUri(context, tile.metaData.getString(META_DATA_PREFERENCE_SUMMARY_URI), new HashMap<>()); } @Override protected void onPostExecute(Bundle bundle) { if (bundle == null) { return; } final String overrideSummary = getString(bundle, META_DATA_PREFERENCE_SUMMARY); final String overrideTitle = getString(bundle, META_DATA_PREFERENCE_TITLE); if (overrideSummary != null) { tile.remoteViews.setTextViewText(android.R.id.summary, overrideSummary); } if (overrideTitle != null) { tile.remoteViews.setTextViewText(android.R.id.title, overrideTitle); } } }.execute(); } private static String getString(Bundle bundle, String key) { private static String getString(Bundle bundle, String key) { return bundle == null ? null : bundle.getString(key); return bundle == null ? null : bundle.getString(key); } } Loading packages/SettingsLib/src/com/android/settingslib/suggestions/SuggestionParser.java +1 −1 Original line number Original line Diff line number Diff line Loading @@ -195,7 +195,7 @@ public class SuggestionParser { intent.setPackage(category.pkg); intent.setPackage(category.pkg); } } TileUtils.getTilesForIntent(mContext, new UserHandle(UserHandle.myUserId()), intent, TileUtils.getTilesForIntent(mContext, new UserHandle(UserHandle.myUserId()), intent, mAddCache, null, suggestions, true, false, false); mAddCache, null, suggestions, true, false, false, true /* shouldUpdateTiles */); filterSuggestions(suggestions, countBefore, isSmartSuggestionEnabled); filterSuggestions(suggestions, countBefore, isSmartSuggestionEnabled); if (!category.multiple && suggestions.size() > (countBefore + 1)) { if (!category.multiple && suggestions.size() > (countBefore + 1)) { // If there are too many, remove them all and only re-add the one with the highest // If there are too many, remove them all and only re-add the one with the highest Loading packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileUtilsTest.java +83 −15 Original line number Original line Diff line number Diff line Loading @@ -22,12 +22,10 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.mockito.Mockito.when; import static org.robolectric.RuntimeEnvironment.application; import static org.robolectric.RuntimeEnvironment.application; Loading Loading @@ -66,7 +64,9 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations; import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowApplication; import org.robolectric.annotation.Implementation; import org.robolectric.annotation.Implements; import org.robolectric.internal.ShadowExtractor; import java.util.ArrayList; import java.util.ArrayList; import java.util.Collections; import java.util.Collections; Loading @@ -75,7 +75,8 @@ import java.util.Map; @RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class) @Config(manifest = TestConfig.MANIFEST_PATH, @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) sdk = TestConfig.SDK_VERSION, shadows = {TileUtilsTest.TileUtilsShadowRemoteViews.class}) public class TileUtilsTest { public class TileUtilsTest { @Mock @Mock Loading Loading @@ -420,12 +421,24 @@ public class TileUtilsTest { } } @Test @Test public void updateTileUsingSummaryUri_summaryUriSpecified_shouldOverrideRemoteViewSummary() public void getTilesForIntent_summaryUriSpecified_shouldOverrideRemoteViewSummary() throws RemoteException { throws RemoteException { Intent intent = new Intent(); Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); List<Tile> outTiles = new ArrayList<>(); List<ResolveInfo> info = new ArrayList<>(); ResolveInfo resolveInfo = newInfo(true, null /* category */, null, null, URI_GET_SUMMARY); resolveInfo.activityInfo.metaData.putInt("com.android.settings.custom_view", R.layout.user_preference); info.add(resolveInfo); when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) .thenReturn(info); // Mock the content provider interaction. // Mock the content provider interaction. Bundle bundle = new Bundle(); Bundle bundle = new Bundle(); String expectedSummary = "new summary text"; bundle.putString(TileUtils.META_DATA_PREFERENCE_SUMMARY, "new summary text"); bundle.putString(TileUtils.META_DATA_PREFERENCE_SUMMARY, expectedSummary); when(mIContentProvider.call(anyString(), when(mIContentProvider.call(anyString(), eq(TileUtils.getMethodFromUri(Uri.parse(URI_GET_SUMMARY))), eq(URI_GET_SUMMARY), eq(TileUtils.getMethodFromUri(Uri.parse(URI_GET_SUMMARY))), eq(URI_GET_SUMMARY), any())).thenReturn(bundle); any())).thenReturn(bundle); Loading @@ -434,14 +447,57 @@ public class TileUtilsTest { when(mContentResolver.acquireUnstableProvider(any(Uri.class))) when(mContentResolver.acquireUnstableProvider(any(Uri.class))) .thenReturn(mIContentProvider); .thenReturn(mIContentProvider); Tile tile = new Tile(); TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, tile.metaData = new Bundle(); null /* defaultCategory */, outTiles, false /* usePriority */, tile.metaData.putString(TileUtils.META_DATA_PREFERENCE_SUMMARY_URI, URI_GET_SUMMARY); false /* checkCategory */, true /* forceTintExternalIcon */); tile.remoteViews = mock(RemoteViews.class); TileUtils.updateTileUsingSummaryUri(mContext, tile); ShadowApplication.runBackgroundTasks(); verify(tile.remoteViews, times(1)).setTextViewText(anyInt(), eq(expectedSummary)); assertThat(outTiles.size()).isEqualTo(1); Tile tile = outTiles.get(0); assertThat(tile.remoteViews).isNotNull(); assertThat(tile.remoteViews.getLayoutId()).isEqualTo(R.layout.user_preference); // Make sure the summary TextView got a new text string. TileUtilsShadowRemoteViews shadowRemoteViews = (TileUtilsShadowRemoteViews) ShadowExtractor.extract(tile.remoteViews); assertThat(shadowRemoteViews.overrideViewId).isEqualTo(android.R.id.summary); assertThat(shadowRemoteViews.overrideText).isEqualTo("new summary text"); } @Test public void getTilesForIntent_providerUnavailable_shouldNotOverrideRemoteViewSummary() throws RemoteException { Intent intent = new Intent(); Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); List<Tile> outTiles = new ArrayList<>(); List<ResolveInfo> info = new ArrayList<>(); ResolveInfo resolveInfo = newInfo(true, null /* category */, null, null, URI_GET_SUMMARY); resolveInfo.activityInfo.metaData.putInt("com.android.settings.custom_view", R.layout.user_preference); info.add(resolveInfo); when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) .thenReturn(info); // Mock the content provider interaction. Bundle bundle = new Bundle(); bundle.putString(TileUtils.META_DATA_PREFERENCE_SUMMARY, "new summary text"); when(mIContentProvider.call(anyString(), eq(TileUtils.getMethodFromUri(Uri.parse(URI_GET_SUMMARY))), eq(URI_GET_SUMMARY), any())).thenReturn(bundle); TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, null /* defaultCategory */, outTiles, false /* usePriority */, false /* checkCategory */, true /* forceTintExternalIcon */); assertThat(outTiles.size()).isEqualTo(1); Tile tile = outTiles.get(0); assertThat(tile.remoteViews).isNotNull(); assertThat(tile.remoteViews.getLayoutId()).isEqualTo(R.layout.user_preference); // Make sure the summary TextView didn't get any text view updates. TileUtilsShadowRemoteViews shadowRemoteViews = (TileUtilsShadowRemoteViews) ShadowExtractor.extract(tile.remoteViews); assertThat(shadowRemoteViews.overrideViewId).isNull(); assertThat(shadowRemoteViews.overrideText).isNull(); } } public static ResolveInfo newInfo(boolean systemApp, String category) { public static ResolveInfo newInfo(boolean systemApp, String category) { Loading Loading @@ -506,4 +562,16 @@ public class TileUtilsTest { } } } } @Implements(RemoteViews.class) public static class TileUtilsShadowRemoteViews { private Integer overrideViewId; private CharSequence overrideText; @Implementation public void setTextViewText(int viewId, CharSequence text) { overrideViewId = viewId; overrideText = text; } } } } Loading
packages/SettingsLib/src/com/android/settingslib/drawer/TileUtils.java +34 −36 Original line number Original line Diff line number Diff line Loading @@ -26,7 +26,6 @@ import android.content.pm.ResolveInfo; import android.content.res.Resources; import android.content.res.Resources; import android.graphics.drawable.Icon; import android.graphics.drawable.Icon; import android.net.Uri; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.os.Bundle; import android.os.RemoteException; import android.os.RemoteException; import android.os.UserHandle; import android.os.UserHandle; Loading Loading @@ -327,6 +326,15 @@ public class TileUtils { Context context, UserHandle user, Intent intent, Context context, UserHandle user, Intent intent, Map<Pair<String, String>, Tile> addedCache, String defaultCategory, List<Tile> outTiles, Map<Pair<String, String>, Tile> addedCache, String defaultCategory, List<Tile> outTiles, boolean usePriority, boolean checkCategory, boolean forceTintExternalIcon) { boolean usePriority, boolean checkCategory, boolean forceTintExternalIcon) { getTilesForIntent(context, user, intent, addedCache, defaultCategory, outTiles, usePriority, checkCategory, forceTintExternalIcon, false /* shouldUpdateTiles */); } public static void getTilesForIntent( Context context, UserHandle user, Intent intent, Map<Pair<String, String>, Tile> addedCache, String defaultCategory, List<Tile> outTiles, boolean usePriority, boolean checkCategory, boolean forceTintExternalIcon, boolean shouldUpdateTiles) { PackageManager pm = context.getPackageManager(); PackageManager pm = context.getPackageManager(); List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent, List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent, PackageManager.GET_META_DATA, user.getIdentifier()); PackageManager.GET_META_DATA, user.getIdentifier()); Loading Loading @@ -364,9 +372,11 @@ public class TileUtils { updateTileData(context, tile, activityInfo, activityInfo.applicationInfo, updateTileData(context, tile, activityInfo, activityInfo.applicationInfo, pm, providerMap, forceTintExternalIcon); pm, providerMap, forceTintExternalIcon); if (DEBUG) Log.d(LOG_TAG, "Adding tile " + tile.title); if (DEBUG) Log.d(LOG_TAG, "Adding tile " + tile.title); addedCache.put(key, tile); addedCache.put(key, tile); } else if (shouldUpdateTiles) { updateSummaryAndTitle(context, providerMap, tile); } } if (!tile.userHandle.contains(user)) { if (!tile.userHandle.contains(user)) { tile.userHandle.add(user); tile.userHandle.add(user); } } Loading @@ -387,7 +397,6 @@ public class TileUtils { String summary = null; String summary = null; String keyHint = null; String keyHint = null; boolean isIconTintable = false; boolean isIconTintable = false; RemoteViews remoteViews = null; // Get the activity's meta-data // Get the activity's meta-data try { try { Loading Loading @@ -442,7 +451,8 @@ public class TileUtils { } } if (metaData.containsKey(META_DATA_PREFERENCE_CUSTOM_VIEW)) { if (metaData.containsKey(META_DATA_PREFERENCE_CUSTOM_VIEW)) { int layoutId = metaData.getInt(META_DATA_PREFERENCE_CUSTOM_VIEW); int layoutId = metaData.getInt(META_DATA_PREFERENCE_CUSTOM_VIEW); remoteViews = new RemoteViews(applicationInfo.packageName, layoutId); tile.remoteViews = new RemoteViews(applicationInfo.packageName, layoutId); updateSummaryAndTitle(context, providerMap, tile); } } } } } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) { } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) { Loading Loading @@ -474,7 +484,6 @@ public class TileUtils { // Suggest a key for this tile // Suggest a key for this tile tile.key = keyHint; tile.key = keyHint; tile.isIconTintable = isIconTintable; tile.isIconTintable = isIconTintable; tile.remoteViews = remoteViews; return true; return true; } } Loading @@ -482,6 +491,26 @@ public class TileUtils { return false; return false; } } private static void updateSummaryAndTitle( Context context, Map<String, IContentProvider> providerMap, Tile tile) { if (tile == null || tile.metaData == null || !tile.metaData.containsKey(META_DATA_PREFERENCE_SUMMARY_URI)) { return; } String uriString = tile.metaData.getString(META_DATA_PREFERENCE_SUMMARY_URI); Bundle bundle = getBundleFromUri(context, uriString, providerMap); String overrideSummary = getString(bundle, META_DATA_PREFERENCE_SUMMARY); String overrideTitle = getString(bundle, META_DATA_PREFERENCE_TITLE); if (overrideSummary != null) { tile.remoteViews.setTextViewText(android.R.id.summary, overrideSummary); } if (overrideTitle != null) { tile.remoteViews.setTextViewText(android.R.id.title, overrideTitle); } } /** /** * Gets the icon package name and resource id from content provider. * Gets the icon package name and resource id from content provider. * @param Context context * @param Context context Loading Loading @@ -547,37 +576,6 @@ public class TileUtils { } } } } public static void updateTileUsingSummaryUri(Context context, final Tile tile) { if (tile == null || tile.metaData == null || !tile.metaData.containsKey(META_DATA_PREFERENCE_SUMMARY_URI)) { return; } new AsyncTask<Void, Void, Bundle>() { @Override protected Bundle doInBackground(Void... params) { return getBundleFromUri(context, tile.metaData.getString(META_DATA_PREFERENCE_SUMMARY_URI), new HashMap<>()); } @Override protected void onPostExecute(Bundle bundle) { if (bundle == null) { return; } final String overrideSummary = getString(bundle, META_DATA_PREFERENCE_SUMMARY); final String overrideTitle = getString(bundle, META_DATA_PREFERENCE_TITLE); if (overrideSummary != null) { tile.remoteViews.setTextViewText(android.R.id.summary, overrideSummary); } if (overrideTitle != null) { tile.remoteViews.setTextViewText(android.R.id.title, overrideTitle); } } }.execute(); } private static String getString(Bundle bundle, String key) { private static String getString(Bundle bundle, String key) { return bundle == null ? null : bundle.getString(key); return bundle == null ? null : bundle.getString(key); } } Loading
packages/SettingsLib/src/com/android/settingslib/suggestions/SuggestionParser.java +1 −1 Original line number Original line Diff line number Diff line Loading @@ -195,7 +195,7 @@ public class SuggestionParser { intent.setPackage(category.pkg); intent.setPackage(category.pkg); } } TileUtils.getTilesForIntent(mContext, new UserHandle(UserHandle.myUserId()), intent, TileUtils.getTilesForIntent(mContext, new UserHandle(UserHandle.myUserId()), intent, mAddCache, null, suggestions, true, false, false); mAddCache, null, suggestions, true, false, false, true /* shouldUpdateTiles */); filterSuggestions(suggestions, countBefore, isSmartSuggestionEnabled); filterSuggestions(suggestions, countBefore, isSmartSuggestionEnabled); if (!category.multiple && suggestions.size() > (countBefore + 1)) { if (!category.multiple && suggestions.size() > (countBefore + 1)) { // If there are too many, remove them all and only re-add the one with the highest // If there are too many, remove them all and only re-add the one with the highest Loading
packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileUtilsTest.java +83 −15 Original line number Original line Diff line number Diff line Loading @@ -22,12 +22,10 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.mockito.Mockito.when; import static org.robolectric.RuntimeEnvironment.application; import static org.robolectric.RuntimeEnvironment.application; Loading Loading @@ -66,7 +64,9 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations; import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowApplication; import org.robolectric.annotation.Implementation; import org.robolectric.annotation.Implements; import org.robolectric.internal.ShadowExtractor; import java.util.ArrayList; import java.util.ArrayList; import java.util.Collections; import java.util.Collections; Loading @@ -75,7 +75,8 @@ import java.util.Map; @RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class) @Config(manifest = TestConfig.MANIFEST_PATH, @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) sdk = TestConfig.SDK_VERSION, shadows = {TileUtilsTest.TileUtilsShadowRemoteViews.class}) public class TileUtilsTest { public class TileUtilsTest { @Mock @Mock Loading Loading @@ -420,12 +421,24 @@ public class TileUtilsTest { } } @Test @Test public void updateTileUsingSummaryUri_summaryUriSpecified_shouldOverrideRemoteViewSummary() public void getTilesForIntent_summaryUriSpecified_shouldOverrideRemoteViewSummary() throws RemoteException { throws RemoteException { Intent intent = new Intent(); Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); List<Tile> outTiles = new ArrayList<>(); List<ResolveInfo> info = new ArrayList<>(); ResolveInfo resolveInfo = newInfo(true, null /* category */, null, null, URI_GET_SUMMARY); resolveInfo.activityInfo.metaData.putInt("com.android.settings.custom_view", R.layout.user_preference); info.add(resolveInfo); when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) .thenReturn(info); // Mock the content provider interaction. // Mock the content provider interaction. Bundle bundle = new Bundle(); Bundle bundle = new Bundle(); String expectedSummary = "new summary text"; bundle.putString(TileUtils.META_DATA_PREFERENCE_SUMMARY, "new summary text"); bundle.putString(TileUtils.META_DATA_PREFERENCE_SUMMARY, expectedSummary); when(mIContentProvider.call(anyString(), when(mIContentProvider.call(anyString(), eq(TileUtils.getMethodFromUri(Uri.parse(URI_GET_SUMMARY))), eq(URI_GET_SUMMARY), eq(TileUtils.getMethodFromUri(Uri.parse(URI_GET_SUMMARY))), eq(URI_GET_SUMMARY), any())).thenReturn(bundle); any())).thenReturn(bundle); Loading @@ -434,14 +447,57 @@ public class TileUtilsTest { when(mContentResolver.acquireUnstableProvider(any(Uri.class))) when(mContentResolver.acquireUnstableProvider(any(Uri.class))) .thenReturn(mIContentProvider); .thenReturn(mIContentProvider); Tile tile = new Tile(); TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, tile.metaData = new Bundle(); null /* defaultCategory */, outTiles, false /* usePriority */, tile.metaData.putString(TileUtils.META_DATA_PREFERENCE_SUMMARY_URI, URI_GET_SUMMARY); false /* checkCategory */, true /* forceTintExternalIcon */); tile.remoteViews = mock(RemoteViews.class); TileUtils.updateTileUsingSummaryUri(mContext, tile); ShadowApplication.runBackgroundTasks(); verify(tile.remoteViews, times(1)).setTextViewText(anyInt(), eq(expectedSummary)); assertThat(outTiles.size()).isEqualTo(1); Tile tile = outTiles.get(0); assertThat(tile.remoteViews).isNotNull(); assertThat(tile.remoteViews.getLayoutId()).isEqualTo(R.layout.user_preference); // Make sure the summary TextView got a new text string. TileUtilsShadowRemoteViews shadowRemoteViews = (TileUtilsShadowRemoteViews) ShadowExtractor.extract(tile.remoteViews); assertThat(shadowRemoteViews.overrideViewId).isEqualTo(android.R.id.summary); assertThat(shadowRemoteViews.overrideText).isEqualTo("new summary text"); } @Test public void getTilesForIntent_providerUnavailable_shouldNotOverrideRemoteViewSummary() throws RemoteException { Intent intent = new Intent(); Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); List<Tile> outTiles = new ArrayList<>(); List<ResolveInfo> info = new ArrayList<>(); ResolveInfo resolveInfo = newInfo(true, null /* category */, null, null, URI_GET_SUMMARY); resolveInfo.activityInfo.metaData.putInt("com.android.settings.custom_view", R.layout.user_preference); info.add(resolveInfo); when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) .thenReturn(info); // Mock the content provider interaction. Bundle bundle = new Bundle(); bundle.putString(TileUtils.META_DATA_PREFERENCE_SUMMARY, "new summary text"); when(mIContentProvider.call(anyString(), eq(TileUtils.getMethodFromUri(Uri.parse(URI_GET_SUMMARY))), eq(URI_GET_SUMMARY), any())).thenReturn(bundle); TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, null /* defaultCategory */, outTiles, false /* usePriority */, false /* checkCategory */, true /* forceTintExternalIcon */); assertThat(outTiles.size()).isEqualTo(1); Tile tile = outTiles.get(0); assertThat(tile.remoteViews).isNotNull(); assertThat(tile.remoteViews.getLayoutId()).isEqualTo(R.layout.user_preference); // Make sure the summary TextView didn't get any text view updates. TileUtilsShadowRemoteViews shadowRemoteViews = (TileUtilsShadowRemoteViews) ShadowExtractor.extract(tile.remoteViews); assertThat(shadowRemoteViews.overrideViewId).isNull(); assertThat(shadowRemoteViews.overrideText).isNull(); } } public static ResolveInfo newInfo(boolean systemApp, String category) { public static ResolveInfo newInfo(boolean systemApp, String category) { Loading Loading @@ -506,4 +562,16 @@ public class TileUtilsTest { } } } } @Implements(RemoteViews.class) public static class TileUtilsShadowRemoteViews { private Integer overrideViewId; private CharSequence overrideText; @Implementation public void setTextViewText(int viewId, CharSequence text) { overrideViewId = viewId; overrideText = text; } } } }