diff options
| author | 2019-11-25 02:36:08 +0000 | |
|---|---|---|
| committer | 2019-11-25 02:36:08 +0000 | |
| commit | 039a30fbd7de3de86d074f0e0af0362da78167a9 (patch) | |
| tree | 1b1eee44891593b85ef00981de8949f56a3768c2 | |
| parent | bc56ea3551814da5617d2a4eccc833f30af325f5 (diff) | |
| parent | 58eda53014f62972fb5b62f205fefff28c7fe761 (diff) | |
Merge "[SettingsLib] Support master switch of inline toggle of Settings Injection v2"
9 files changed, 171 insertions, 36 deletions
diff --git a/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/ActivityTile.java b/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/ActivityTile.java index 8cd33a5b3f6c..b739ee62174d 100644 --- a/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/ActivityTile.java +++ b/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/ActivityTile.java @@ -18,6 +18,7 @@ package com.android.settingslib.drawer; import android.content.Context; import android.content.Intent; +import android.content.pm.ActivityInfo; import android.content.pm.ComponentInfo; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; @@ -33,7 +34,7 @@ import java.util.Objects; public class ActivityTile extends Tile { private static final String TAG = "ActivityTile"; - public ActivityTile(ComponentInfo info, String category) { + public ActivityTile(ActivityInfo info, String category) { super(info, category); setMetaData(info.metaData); } diff --git a/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/MasterSwitchController.java b/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/MasterSwitchController.java new file mode 100644 index 000000000000..a12aa83e9d4b --- /dev/null +++ b/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/MasterSwitchController.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settingslib.drawer; + +import android.os.Bundle; + +/** + * A controller that manages event for master switch. + */ +public abstract class MasterSwitchController extends SwitchController { + + @Override + protected final MetaData getMetaData() { + throw new UnsupportedOperationException(); + } + + @Override + final Bundle getBundle() { + throw new UnsupportedOperationException(); + } +} diff --git a/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/ProviderTile.java b/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/ProviderTile.java index b2ba5deee731..312d30e2f2ad 100644 --- a/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/ProviderTile.java +++ b/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/ProviderTile.java @@ -42,10 +42,10 @@ public class ProviderTile extends Tile { private String mAuthority; private String mKey; - public ProviderTile(ComponentInfo info, String category, Bundle metaData) { + public ProviderTile(ProviderInfo info, String category, Bundle metaData) { super(info, category); setMetaData(metaData); - mAuthority = ((ProviderInfo) info).authority; + mAuthority = info.authority; mKey = metaData.getString(META_DATA_PREFERENCE_KEYHINT); } diff --git a/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/SwitchController.java b/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/SwitchController.java index e48da8608780..23669b2743ce 100644 --- a/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/SwitchController.java +++ b/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/SwitchController.java @@ -108,7 +108,7 @@ public abstract class SwitchController { Bundle getBundle() { final MetaData metaData = getMetaData(); if (metaData == null) { - throw new IllegalArgumentException("Should not return null in getMetaData()"); + throw new NullPointerException("Should not return null in getMetaData()"); } final Bundle bundle = metaData.build(); diff --git a/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/SwitchesProvider.java b/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/SwitchesProvider.java index a05c7d5d3b51..73f1a904b04b 100644 --- a/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/SwitchesProvider.java +++ b/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/SwitchesProvider.java @@ -55,7 +55,7 @@ public abstract class SwitchesProvider extends ContentProvider { private String mAuthority; private final Map<String, SwitchController> mControllerMap = new LinkedHashMap<>(); - private final List<Bundle> mSwitchList = new ArrayList<>(); + private final List<Bundle> mSwitchDataList = new ArrayList<>(); /** * Get a list of {@link SwitchController} for this provider. @@ -88,7 +88,9 @@ public abstract class SwitchesProvider extends ContentProvider { controller.setAuthority(mAuthority); mControllerMap.put(key, controller); - mSwitchList.add(controller.getBundle()); + if (!(controller instanceof MasterSwitchController)) { + mSwitchDataList.add(controller.getBundle()); + } }); return true; } @@ -101,7 +103,7 @@ public abstract class SwitchesProvider extends ContentProvider { : null; if (TextUtils.isEmpty(key)) { if (METHOD_GET_SWITCH_DATA.equals(method)) { - bundle.putParcelableList(EXTRA_SWITCH_DATA, mSwitchList); + bundle.putParcelableList(EXTRA_SWITCH_DATA, mSwitchDataList); return bundle; } return null; @@ -114,7 +116,10 @@ public abstract class SwitchesProvider extends ContentProvider { switch (method) { case METHOD_GET_SWITCH_DATA: - return controller.getBundle(); + if (!(controller instanceof MasterSwitchController)) { + return controller.getBundle(); + } + break; case METHOD_GET_PROVIDER_ICON: if (controller instanceof ProviderIcon) { return ((ProviderIcon) controller).getProviderIcon(); diff --git a/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/TileUtils.java b/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/TileUtils.java index 71ffff780b21..f93faeb0c7ae 100644 --- a/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/TileUtils.java +++ b/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/TileUtils.java @@ -244,15 +244,15 @@ public class TileUtils { // TODO: Needs much optimization, too many PM queries going on here. if (user.getIdentifier() == ActivityManager.getCurrentUser()) { // Only add Settings for this user. - getTilesForAction(context, user, SETTINGS_ACTION, cache, null, tiles, true); - getTilesForAction(context, user, OPERATOR_SETTINGS, cache, + loadTilesForAction(context, user, SETTINGS_ACTION, cache, null, tiles, true); + loadTilesForAction(context, user, OPERATOR_SETTINGS, cache, OPERATOR_DEFAULT_CATEGORY, tiles, false); - getTilesForAction(context, user, MANUFACTURER_SETTINGS, cache, + loadTilesForAction(context, user, MANUFACTURER_SETTINGS, cache, MANUFACTURER_DEFAULT_CATEGORY, tiles, false); } if (setup) { - getTilesForAction(context, user, EXTRA_SETTINGS_ACTION, cache, null, tiles, false); - getTilesForAction(context, user, IA_SETTINGS_ACTION, cache, null, tiles, false); + loadTilesForAction(context, user, EXTRA_SETTINGS_ACTION, cache, null, tiles, false); + loadTilesForAction(context, user, IA_SETTINGS_ACTION, cache, null, tiles, false); } } @@ -284,18 +284,18 @@ public class TileUtils { } @VisibleForTesting - static void getTilesForAction(Context context, + static void loadTilesForAction(Context context, UserHandle user, String action, Map<Pair<String, String>, Tile> addedCache, String defaultCategory, List<Tile> outTiles, boolean requireSettings) { final Intent intent = new Intent(action); if (requireSettings) { intent.setPackage(SETTING_PKG); } - getActivityTiles(context, user, addedCache, defaultCategory, outTiles, intent); - getProviderTiles(context, user, addedCache, defaultCategory, outTiles, intent); + loadActivityTiles(context, user, addedCache, defaultCategory, outTiles, intent); + loadProviderTiles(context, user, addedCache, defaultCategory, outTiles, intent); } - private static void getActivityTiles(Context context, + private static void loadActivityTiles(Context context, UserHandle user, Map<Pair<String, String>, Tile> addedCache, String defaultCategory, List<Tile> outTiles, Intent intent) { final PackageManager pm = context.getPackageManager(); @@ -308,11 +308,11 @@ public class TileUtils { } final ActivityInfo activityInfo = resolved.activityInfo; final Bundle metaData = activityInfo.metaData; - getTile(user, addedCache, defaultCategory, outTiles, intent, metaData, activityInfo); + loadTile(user, addedCache, defaultCategory, outTiles, intent, metaData, activityInfo); } } - private static void getProviderTiles(Context context, + private static void loadProviderTiles(Context context, UserHandle user, Map<Pair<String, String>, Tile> addedCache, String defaultCategory, List<Tile> outTiles, Intent intent) { final PackageManager pm = context.getPackageManager(); @@ -330,13 +330,13 @@ public class TileUtils { continue; } for (Bundle metaData : switchData) { - getTile(user, addedCache, defaultCategory, outTiles, intent, metaData, + loadTile(user, addedCache, defaultCategory, outTiles, intent, metaData, providerInfo); } } } - private static void getTile(UserHandle user, Map<Pair<String, String>, Tile> addedCache, + private static void loadTile(UserHandle user, Map<Pair<String, String>, Tile> addedCache, String defaultCategory, List<Tile> outTiles, Intent intent, Bundle metaData, ComponentInfo componentInfo) { String categoryKey = defaultCategory; @@ -359,8 +359,8 @@ public class TileUtils { Tile tile = addedCache.get(key); if (tile == null) { tile = isProvider - ? new ProviderTile(componentInfo, categoryKey, metaData) - : new ActivityTile(componentInfo, categoryKey); + ? new ProviderTile((ProviderInfo) componentInfo, categoryKey, metaData) + : new ActivityTile((ActivityInfo) componentInfo, categoryKey); addedCache.put(key, tile); } else { tile.setMetaData(metaData); @@ -516,7 +516,7 @@ public class TileUtils { * @param value Boolean associated with the key * @return Bundle associated with the action, if returned by the content provider */ - public static Bundle putBooleanToUri(Context context, Uri uri, + public static Bundle putBooleanToUriAndGetResult(Context context, Uri uri, Map<String, IContentProvider> providerMap, String key, boolean value) { final Bundle bundle = new Bundle(); bundle.putBoolean(key, value); diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/MasterSwitchControllerTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/MasterSwitchControllerTest.java new file mode 100644 index 000000000000..69d0f2e71c17 --- /dev/null +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/MasterSwitchControllerTest.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.settingslib.drawer; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +@RunWith(RobolectricTestRunner.class) +public class MasterSwitchControllerTest { + + @Rule + public final ExpectedException thrown = ExpectedException.none(); + + private MasterSwitchController mController; + + @Before + public void setUp() { + mController = new TestMasterSwitchController("123"); + } + + @Test + public void getMetaData_shouldThrowUnsupportedOperationException() { + thrown.expect(UnsupportedOperationException.class); + + mController.getMetaData(); + } + + @Test + public void getBundle_shouldThrowUnsupportedOperationException() { + thrown.expect(UnsupportedOperationException.class); + + mController.getBundle(); + } + + static class TestMasterSwitchController extends MasterSwitchController { + + private String mKey; + + TestMasterSwitchController(String key) { + mKey = key; + } + + @Override + public String getSwitchKey() { + return mKey; + } + + @Override + protected boolean isChecked() { + return true; + } + + @Override + protected boolean onCheckedChanged(boolean checked) { + return true; + } + + @Override + protected String getErrorMessage(boolean attemptedChecked) { + return null; + } + } +} diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/SwitchesProviderTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/SwitchesProviderTest.java index 27b3697f54ea..a740e683642a 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/SwitchesProviderTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/SwitchesProviderTest.java @@ -35,6 +35,7 @@ import android.content.Context; import android.content.pm.ProviderInfo; import android.os.Bundle; +import com.android.settingslib.drawer.MasterSwitchControllerTest.TestMasterSwitchController; import com.android.settingslib.drawer.SwitchController.MetaData; import org.junit.Before; @@ -84,8 +85,8 @@ public class SwitchesProviderTest { } @Test - public void attachInfo_NoMetaDataInController_shouldThrowIllegalArgumentException() { - thrown.expect(IllegalArgumentException.class); + public void attachInfo_NoMetaDataInController_shouldThrowNullPointerException() { + thrown.expect(NullPointerException.class); final TestSwitchController controller = new TestSwitchController(); controller.setKey("123"); mSwitchesProvider.addSwitchController(controller); @@ -123,6 +124,19 @@ public class SwitchesProviderTest { } @Test + public void getSwitchData_shouldNotReturnMasterSwitchData() { + final SwitchController controller = new TestMasterSwitchController("123"); + mSwitchesProvider.addSwitchController(controller); + mSwitchesProvider.attachInfo(mContext, mProviderInfo); + + final Bundle switchData = mSwitchesProvider.call(METHOD_GET_SWITCH_DATA, "uri" , + null /* extras*/); + + final ArrayList<Bundle> dataList = switchData.getParcelableArrayList(EXTRA_SWITCH_DATA); + assertThat(dataList).isEmpty(); + } + + @Test public void getSwitchData_shouldReturnDataList() { final TestSwitchController controller = new TestSwitchController(); controller.setKey("123"); diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileUtilsTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileUtilsTest.java index b36eb4950b11..9b4b97e7f55d 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileUtilsTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileUtilsTest.java @@ -113,7 +113,7 @@ public class TileUtilsTest { when(mPackageManager.queryIntentContentProvidersAsUser(any(Intent.class), anyInt(), anyInt())).thenReturn(info); - TileUtils.getTilesForAction(mContext, UserHandle.CURRENT, IA_SETTINGS_ACTION, addedCache, + TileUtils.loadTilesForAction(mContext, UserHandle.CURRENT, IA_SETTINGS_ACTION, addedCache, null /* defaultCategory */, outTiles, false /* usePriority */); assertThat(outTiles).hasSize(2); @@ -135,7 +135,7 @@ public class TileUtilsTest { when(mPackageManager.queryIntentContentProvidersAsUser(any(Intent.class), anyInt(), anyInt())).thenReturn(info); - TileUtils.getTilesForAction(mContext, UserHandle.CURRENT, IA_SETTINGS_ACTION, addedCache, + TileUtils.loadTilesForAction(mContext, UserHandle.CURRENT, IA_SETTINGS_ACTION, addedCache, null /* defaultCategory */, outTiles, false /* requiresSettings */); assertThat(outTiles).hasSize(2); @@ -156,7 +156,7 @@ public class TileUtilsTest { when(mPackageManager.queryIntentContentProvidersAsUser(any(Intent.class), anyInt(), anyInt())).thenReturn(info); - TileUtils.getTilesForAction(mContext, UserHandle.CURRENT, IA_SETTINGS_ACTION, + TileUtils.loadTilesForAction(mContext, UserHandle.CURRENT, IA_SETTINGS_ACTION, addedCache, null /* defaultCategory */, outTiles, false /* requiresSettings */); assertThat(outTiles).isEmpty(); @@ -197,7 +197,7 @@ public class TileUtilsTest { when(mPackageManager.queryIntentContentProvidersAsUser(any(Intent.class), anyInt(), anyInt())).thenReturn(info); - TileUtils.getTilesForAction(mContext, UserHandle.CURRENT, IA_SETTINGS_ACTION, addedCache, + TileUtils.loadTilesForAction(mContext, UserHandle.CURRENT, IA_SETTINGS_ACTION, addedCache, null /* defaultCategory */, outTiles, false /* usePriority */); assertThat(outTiles).hasSize(2); @@ -222,7 +222,7 @@ public class TileUtilsTest { when(mResources.getString(eq(123))) .thenReturn("my localized title"); - TileUtils.getTilesForAction(mContext, UserHandle.CURRENT, IA_SETTINGS_ACTION, addedCache, + TileUtils.loadTilesForAction(mContext, UserHandle.CURRENT, IA_SETTINGS_ACTION, addedCache, null /* defaultCategory */, outTiles, false /* usePriority */); assertThat(outTiles).hasSize(2); assertThat(outTiles.get(0).getTitle(mContext)).isEqualTo("my localized title"); @@ -245,7 +245,7 @@ public class TileUtilsTest { when(mPackageManager.queryIntentContentProvidersAsUser(any(Intent.class), anyInt(), anyInt())).thenReturn(info); - TileUtils.getTilesForAction(mContext, UserHandle.CURRENT, IA_SETTINGS_ACTION, addedCache, + TileUtils.loadTilesForAction(mContext, UserHandle.CURRENT, IA_SETTINGS_ACTION, addedCache, null /* defaultCategory */, outTiles, false /* usePriority */); assertThat(outTiles.get(0).isIconTintable(mContext)).isFalse(); @@ -266,7 +266,7 @@ public class TileUtilsTest { when(mPackageManager.queryIntentActivitiesAsUser(any(Intent.class), anyInt(), anyInt())) .thenReturn(info); - TileUtils.getTilesForAction(mContext, UserHandle.CURRENT, IA_SETTINGS_ACTION, addedCache, + TileUtils.loadTilesForAction(mContext, UserHandle.CURRENT, IA_SETTINGS_ACTION, addedCache, null /* defaultCategory */, outTiles, false /* usePriority */); assertThat(outTiles).hasSize(1); @@ -276,7 +276,7 @@ public class TileUtilsTest { resolveInfo.activityInfo.metaData.putInt(META_DATA_PREFERENCE_ICON, com.android.internal.R.drawable.ic_phone); outTiles.clear(); - TileUtils.getTilesForAction(mContext, UserHandle.CURRENT, IA_SETTINGS_ACTION, addedCache, + TileUtils.loadTilesForAction(mContext, UserHandle.CURRENT, IA_SETTINGS_ACTION, addedCache, null /* defaultCategory */, outTiles, false /* usePriority */); assertThat(outTiles).hasSize(1); @@ -300,7 +300,7 @@ public class TileUtilsTest { when(mPackageManager.queryIntentContentProvidersAsUser(any(Intent.class), anyInt(), anyInt())).thenReturn(info); - TileUtils.getTilesForAction(mContext, UserHandle.CURRENT, IA_SETTINGS_ACTION, addedCache, + TileUtils.loadTilesForAction(mContext, UserHandle.CURRENT, IA_SETTINGS_ACTION, addedCache, null /* defaultCategory */, outTiles, false /* usePriority */); assertThat(outTiles.get(0).isIconTintable(mContext)).isTrue(); @@ -321,7 +321,7 @@ public class TileUtilsTest { when(mPackageManager.queryIntentContentProvidersAsUser(any(Intent.class), anyInt(), anyInt())).thenReturn(info); - TileUtils.getTilesForAction(mContext, UserHandle.CURRENT, IA_SETTINGS_ACTION, addedCache, + TileUtils.loadTilesForAction(mContext, UserHandle.CURRENT, IA_SETTINGS_ACTION, addedCache, null /* defaultCategory */, outTiles, false /* usePriority */); assertThat(outTiles).hasSize(2); |