Merge "Overload TileUtils.getCategories() with an additional parameteri."

This commit is contained in:
Doris Ling
2016-12-19 23:53:34 +00:00
committed by Android (Google) Code Review
3 changed files with 55 additions and 3 deletions

View File

@@ -50,19 +50,25 @@ public class CategoryManager {
private final Map<String, DashboardCategory> mCategoryByKeyMap;
private List<DashboardCategory> mCategories;
private String mExtraAction;
public static CategoryManager get(Context context) {
return get(context, null);
}
public static CategoryManager get(Context context, String action) {
if (sInstance == null) {
sInstance = new CategoryManager(context);
sInstance = new CategoryManager(context, action);
}
return sInstance;
}
CategoryManager(Context context) {
CategoryManager(Context context, String action) {
mTileByComponentCache = new ArrayMap<>();
mCategoryByKeyMap = new ArrayMap<>();
mInterestingConfigChanges = new InterestingConfigChanges();
mInterestingConfigChanges.applyNewConfig(context.getResources());
mExtraAction = action;
}
public synchronized DashboardCategory getTilesByCategory(Context context, String categoryKey) {
@@ -111,7 +117,7 @@ public class CategoryManager {
}
mCategoryByKeyMap.clear();
mCategories = TileUtils.getCategories(context, mTileByComponentCache,
false /* categoryDefinedInManifest */);
false /* categoryDefinedInManifest */, mExtraAction);
for (DashboardCategory category : mCategories) {
mCategoryByKeyMap.put(category.key, category);
}

View File

@@ -144,6 +144,19 @@ public class TileUtils {
*/
public static List<DashboardCategory> getCategories(Context context,
Map<Pair<String, String>, Tile> cache, boolean categoryDefinedInManifest) {
return getCategories(context, cache, categoryDefinedInManifest, null);
}
/**
* Build a list of DashboardCategory.
* @param categoryDefinedInManifest If true, an dummy activity must exists in manifest to
* represent this category (eg: .Settings$DeviceSettings)
* @param extraAction additional intent filter action to be used to build the dashboard
* categories
*/
public static List<DashboardCategory> getCategories(Context context,
Map<Pair<String, String>, Tile> cache, boolean categoryDefinedInManifest,
String extraAction) {
final long startTime = System.currentTimeMillis();
boolean setup = Global.getInt(context.getContentResolver(), Global.DEVICE_PROVISIONED, 0)
!= 0;
@@ -162,6 +175,9 @@ public class TileUtils {
if (setup) {
getTilesForAction(context, user, EXTRA_SETTINGS_ACTION, cache, null, tiles, false);
getTilesForAction(context, user, IA_SETTINGS_ACTION, cache, null, tiles, false);
if (extraAction != null) {
getTilesForAction(context, user, extraAction, cache, null, tiles, false);
}
}
}

View File

@@ -26,6 +26,8 @@ import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings.Global;
import android.util.ArrayMap;
import android.util.Pair;
@@ -34,6 +36,7 @@ import com.android.settingslib.TestConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatcher;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
@@ -46,6 +49,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.argThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;
@@ -59,6 +63,8 @@ public class TileUtilsTest {
private PackageManager mPackageManager;
@Mock
private Resources mResources;
@Mock
private UserManager mUserManager;
@Before
public void setUp() throws NameNotFoundException {
@@ -127,6 +133,30 @@ public class TileUtilsTest {
assertThat(outTiles.isEmpty()).isTrue();
}
@Test
public void getCategories_shouldHandleExtraIntentAction() {
final String testCategory = "category1";
final String testAction = "action1";
Map<Pair<String, String>, Tile> cache = new ArrayMap<>();
List<ResolveInfo> info = new ArrayList<>();
info.add(newInfo(true, testCategory));
Global.putInt(mContext.getContentResolver(), Global.DEVICE_PROVISIONED, 1);
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
List<UserHandle> userHandleList = new ArrayList<>();
userHandleList.add(UserHandle.CURRENT);
when(mUserManager.getUserProfiles()).thenReturn(userHandleList);
when(mPackageManager.queryIntentActivitiesAsUser(argThat(new ArgumentMatcher<Intent>() {
public boolean matches(Object event) {
return testAction.equals(((Intent) event).getAction());
}
}), anyInt(), anyInt())).thenReturn(info);
List<DashboardCategory> categoryList = TileUtils.getCategories(
mContext, cache, false /* categoryDefinedInManifest */, testAction);
assertThat(categoryList.get(0).tiles.get(0).category).isEqualTo(testCategory);
}
private ResolveInfo newInfo(boolean systemApp, String category) {
return newInfo(systemApp, category, null);