Merge "Allow injected Settings tiles to specify a key."

This commit is contained in:
Shahriyar Amini
2016-11-28 21:21:07 +00:00
committed by Android (Google) Code Review
3 changed files with 62 additions and 1 deletions

View File

@@ -79,6 +79,11 @@ public class Tile implements Parcelable {
*/
public Bundle metaData;
/**
* Optional key to use for this tile.
*/
public String key;
public Tile() {
// Empty
}
@@ -113,6 +118,7 @@ public class Tile implements Parcelable {
dest.writeString(category);
dest.writeInt(priority);
dest.writeBundle(metaData);
dest.writeString(key);
}
public void readFromParcel(Parcel in) {
@@ -132,6 +138,7 @@ public class Tile implements Parcelable {
category = in.readString();
priority = in.readInt();
metaData = in.readBundle();
key = in.readString();
}
Tile(Parcel in) {

View File

@@ -100,6 +100,12 @@ public class TileUtils {
*/
private static final String EXTRA_CATEGORY_KEY = "com.android.settings.category";
/**
* Name of the meta-data item that should be set in the AndroidManifest.xml
* to specify the key that should be used for the preference.
*/
public static final String META_DATA_PREFERENCE_KEYHINT = "com.android.settings.keyhint";
/**
* Name of the meta-data item that should be set in the AndroidManifest.xml
* to specify the icon that should be displayed for the preference.
@@ -292,6 +298,7 @@ public class TileUtils {
int icon = 0;
CharSequence title = null;
String summary = null;
String keyHint = null;
// Get the activity's meta-data
try {
@@ -317,6 +324,13 @@ public class TileUtils {
summary = metaData.getString(META_DATA_PREFERENCE_SUMMARY);
}
}
if (metaData.containsKey(META_DATA_PREFERENCE_KEYHINT)) {
if (metaData.get(META_DATA_PREFERENCE_KEYHINT) instanceof Integer) {
keyHint = res.getString(metaData.getInt(META_DATA_PREFERENCE_KEYHINT));
} else {
keyHint = metaData.getString(META_DATA_PREFERENCE_KEYHINT);
}
}
}
} catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
if (DEBUG) Log.d(LOG_TAG, "Couldn't find info", e);
@@ -338,6 +352,8 @@ public class TileUtils {
// Replace the intent with this specific activity
tile.intent = new Intent().setClassName(activityInfo.packageName,
activityInfo.name);
// Suggest a key for this tile
tile.key = keyHint;
return true;
}

View File

@@ -21,7 +21,9 @@ import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.UserHandle;
import android.util.ArrayMap;
@@ -43,6 +45,7 @@ import java.util.Map;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;
@@ -54,11 +57,14 @@ public class TileUtilsTest {
private Context mContext;
@Mock
private PackageManager mPackageManager;
@Mock
private Resources mResources;
@Before
public void setUp() {
public void setUp() throws NameNotFoundException {
MockitoAnnotations.initMocks(this);
when(mContext.getPackageManager()).thenReturn(mPackageManager);
when(mPackageManager.getResourcesForApplication(anyString())).thenReturn(mResources);
}
@Test
@@ -81,6 +87,27 @@ public class TileUtilsTest {
assertThat(outTiles.get(0).category).isEqualTo(testCategory);
}
@Test
public void getTilesForIntent_shouldParseKeyHintForSystemApp() {
String keyHint = "key";
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 */, keyHint);
info.add(resolveInfo);
when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt()))
.thenReturn(info);
TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache,
null /* defaultCategory */, outTiles, false /* usePriority */,
false /* checkCategory */);
assertThat(outTiles.size()).isEqualTo(1);
assertThat(outTiles.get(0).key).isEqualTo(keyHint);
}
@Test
public void getTilesForIntent_shouldSkipNonSystemApp() {
final String testCategory = "category1";
@@ -100,7 +127,12 @@ public class TileUtilsTest {
assertThat(outTiles.isEmpty()).isTrue();
}
private ResolveInfo newInfo(boolean systemApp, String category) {
return newInfo(systemApp, category, null);
}
private ResolveInfo newInfo(boolean systemApp, String category, String keyHint) {
ResolveInfo info = new ResolveInfo();
info.system = systemApp;
info.activityInfo = new ActivityInfo();
@@ -108,7 +140,13 @@ public class TileUtilsTest {
info.activityInfo.name = "123";
info.activityInfo.metaData = new Bundle();
info.activityInfo.metaData.putString("com.android.settings.category", category);
if (keyHint != null) {
info.activityInfo.metaData.putString("com.android.settings.keyhint", keyHint);
}
info.activityInfo.applicationInfo = new ApplicationInfo();
if (systemApp) {
info.activityInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
}
return info;
}
}