Merge "Deferred setup banner updates intermittently" into oc-mr1-dev

This commit is contained in:
TreeHugger Robot
2017-09-19 22:13:28 +00:00
committed by Android (Google) Code Review
3 changed files with 118 additions and 52 deletions

View File

@@ -26,7 +26,6 @@ import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.RemoteException;
import android.os.UserHandle;
@@ -327,6 +326,15 @@ public class TileUtils {
Context context, UserHandle user, Intent intent,
Map<Pair<String, String>, Tile> addedCache, String defaultCategory, List<Tile> outTiles,
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();
List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,
PackageManager.GET_META_DATA, user.getIdentifier());
@@ -364,9 +372,11 @@ public class TileUtils {
updateTileData(context, tile, activityInfo, activityInfo.applicationInfo,
pm, providerMap, forceTintExternalIcon);
if (DEBUG) Log.d(LOG_TAG, "Adding tile " + tile.title);
addedCache.put(key, tile);
} else if (shouldUpdateTiles) {
updateSummaryAndTitle(context, providerMap, tile);
}
if (!tile.userHandle.contains(user)) {
tile.userHandle.add(user);
}
@@ -387,7 +397,6 @@ public class TileUtils {
String summary = null;
String keyHint = null;
boolean isIconTintable = false;
RemoteViews remoteViews = null;
// Get the activity's meta-data
try {
@@ -442,7 +451,8 @@ public class TileUtils {
}
if (metaData.containsKey(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) {
@@ -474,7 +484,6 @@ public class TileUtils {
// Suggest a key for this tile
tile.key = keyHint;
tile.isIconTintable = isIconTintable;
tile.remoteViews = remoteViews;
return true;
}
@@ -482,6 +491,26 @@ public class TileUtils {
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.
* @param Context context
@@ -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) {
return bundle == null ? null : bundle.getString(key);
}

View File

@@ -195,7 +195,7 @@ public class SuggestionParser {
intent.setPackage(category.pkg);
}
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);
if (!category.multiple && suggestions.size() > (countBefore + 1)) {
// If there are too many, remove them all and only re-add the one with the highest

View File

@@ -22,12 +22,10 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.robolectric.RuntimeEnvironment.application;
@@ -66,7 +64,9 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
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.Collections;
@@ -75,7 +75,8 @@ import java.util.Map;
@RunWith(RobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH,
sdk = TestConfig.SDK_VERSION)
sdk = TestConfig.SDK_VERSION,
shadows = {TileUtilsTest.TileUtilsShadowRemoteViews.class})
public class TileUtilsTest {
@Mock
@@ -420,12 +421,24 @@ public class TileUtilsTest {
}
@Test
public void updateTileUsingSummaryUri_summaryUriSpecified_shouldOverrideRemoteViewSummary()
public void getTilesForIntent_summaryUriSpecified_shouldOverrideRemoteViewSummary()
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();
String expectedSummary = "new summary text";
bundle.putString(TileUtils.META_DATA_PREFERENCE_SUMMARY, expectedSummary);
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);
@@ -434,14 +447,57 @@ public class TileUtilsTest {
when(mContentResolver.acquireUnstableProvider(any(Uri.class)))
.thenReturn(mIContentProvider);
Tile tile = new Tile();
tile.metaData = new Bundle();
tile.metaData.putString(TileUtils.META_DATA_PREFERENCE_SUMMARY_URI, URI_GET_SUMMARY);
tile.remoteViews = mock(RemoteViews.class);
TileUtils.updateTileUsingSummaryUri(mContext, tile);
ShadowApplication.runBackgroundTasks();
TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache,
null /* defaultCategory */, outTiles, false /* usePriority */,
false /* checkCategory */, true /* forceTintExternalIcon */);
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) {
@@ -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;
}
}
}