Merge "Add method to update tile's remoteview using summaryUri" into oc-mr1-dev

This commit is contained in:
TreeHugger Robot
2017-09-08 06:36:44 +00:00
committed by Android (Google) Code Review
2 changed files with 37 additions and 93 deletions

View File

@@ -442,15 +442,6 @@ 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);
if (metaData.containsKey(META_DATA_PREFERENCE_SUMMARY_URI)) {
String uriString = metaData.getString(
META_DATA_PREFERENCE_SUMMARY_URI);
String overrideSummary = getTextFromUri(context, uriString, providerMap,
META_DATA_PREFERENCE_SUMMARY);
if (overrideSummary != null) {
remoteViews.setTextViewText(android.R.id.summary, overrideSummary);
}
}
}
}
} catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
@@ -555,6 +546,30 @@ public class TileUtils {
}
}
public static void updateTileUsingSummaryUri(Context context, Tile tile) {
if (tile == null || tile.metaData == null ||
!tile.metaData.containsKey(META_DATA_PREFERENCE_SUMMARY_URI)) {
return;
}
final Map<String, IContentProvider> providerMap = new HashMap<>();
final String uriString = tile.metaData.getString(META_DATA_PREFERENCE_SUMMARY_URI);
final Bundle bundle = getBundleFromUri(context, uriString, providerMap);
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);
}
}
private static String getString(Bundle bundle, String key) {
return bundle == null ? null : bundle.getString(key);
}
private static IContentProvider getProviderFromUri(Context context, Uri uri,
Map<String, IContentProvider> providerMap) {
if (uri == null) {

View File

@@ -22,10 +22,12 @@ 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;
@@ -64,9 +66,6 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.internal.ShadowExtractor;
import java.util.ArrayList;
import java.util.Collections;
@@ -75,8 +74,7 @@ import java.util.Map;
@RunWith(RobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH,
sdk = TestConfig.SDK_VERSION,
shadows = {TileUtilsTest.TileUtilsShadowRemoteViews.class})
sdk = TestConfig.SDK_VERSION)
public class TileUtilsTest {
@Mock
@@ -421,24 +419,12 @@ public class TileUtilsTest {
}
@Test
public void getTilesForIntent_summaryUriSpecified_shouldOverrideRemoteViewSummary()
public void updateTileUsingSummaryUri_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();
bundle.putString(TileUtils.META_DATA_PREFERENCE_SUMMARY, "new summary text");
String expectedSummary = "new summary text";
bundle.putString(TileUtils.META_DATA_PREFERENCE_SUMMARY, expectedSummary);
when(mIContentProvider.call(anyString(),
eq(TileUtils.getMethodFromUri(Uri.parse(URI_GET_SUMMARY))), eq(URI_GET_SUMMARY),
any())).thenReturn(bundle);
@@ -447,57 +433,12 @@ public class TileUtilsTest {
when(mContentResolver.acquireUnstableProvider(any(Uri.class)))
.thenReturn(mIContentProvider);
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 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();
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);
verify(tile.remoteViews, times(1)).setTextViewText(anyInt(), eq(expectedSummary));
}
public static ResolveInfo newInfo(boolean systemApp, String category) {
@@ -562,16 +503,4 @@ 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;
}
}
}