Remove more wrappers

- WallpaperManagerWrapper
- UserManagerWrapper

Bug: 76167422
Test: robotests
Change-Id: I3ca866089082e01f1e9d7a4554387962f45a5437
This commit is contained in:
Fan Zhang
2018-04-02 11:33:16 -07:00
parent e1549f0af1
commit 947a673f74
16 changed files with 113 additions and 200 deletions

View File

@@ -40,7 +40,6 @@ import com.android.settings.R;
import com.android.settings.applications.AppInfoBase;
import com.android.settings.notification.EmptyTextSettings;
import com.android.settings.widget.AppPreference;
import com.android.settings.wrapper.UserManagerWrapper;
import com.android.settingslib.wrapper.PackageManagerWrapper;
import java.text.Collator;
@@ -88,7 +87,7 @@ public class PictureInPictureSettings extends EmptyTextSettings {
private Context mContext;
private PackageManagerWrapper mPackageManager;
private UserManagerWrapper mUserManager;
private UserManager mUserManager;
private IconDrawableFactory mIconDrawableFactory;
/**
@@ -119,7 +118,7 @@ public class PictureInPictureSettings extends EmptyTextSettings {
// Do nothing
}
public PictureInPictureSettings(PackageManagerWrapper pm, UserManagerWrapper um) {
public PictureInPictureSettings(PackageManagerWrapper pm, UserManager um) {
mPackageManager = pm;
mUserManager = um;
}
@@ -130,7 +129,7 @@ public class PictureInPictureSettings extends EmptyTextSettings {
mContext = getActivity();
mPackageManager = new PackageManagerWrapper(mContext.getPackageManager());
mUserManager = new UserManagerWrapper(mContext.getSystemService(UserManager.class));
mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
mIconDrawableFactory = IconDrawableFactory.newInstance(mContext);
}

View File

@@ -22,11 +22,10 @@ import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbPort;
import android.hardware.usb.UsbPortStatus;
import android.net.ConnectivityManager;
import android.os.UserHandle;
import android.os.UserManager;
import android.support.annotation.VisibleForTesting;
import com.android.settings.wrapper.UserManagerWrapper;
/**
* Provides access to underlying system USB functionality.
*/
@@ -50,17 +49,17 @@ public class UsbBackend {
private UsbPortStatus mPortStatus;
public UsbBackend(Context context) {
this(context, new UserManagerWrapper(UserManager.get(context)));
this(context, (UserManager) context.getSystemService(Context.USER_SERVICE));
}
@VisibleForTesting
public UsbBackend(Context context, UserManagerWrapper userManagerWrapper) {
public UsbBackend(Context context, UserManager userManager) {
mUsbManager = context.getSystemService(UsbManager.class);
mFileTransferRestricted = userManagerWrapper.isUsbFileTransferRestricted();
mFileTransferRestrictedBySystem = userManagerWrapper.isUsbFileTransferRestrictedBySystem();
mTetheringRestricted = userManagerWrapper.isUsbTetheringRestricted();
mTetheringRestrictedBySystem = userManagerWrapper.isUsbTetheringRestrictedBySystem();
mFileTransferRestricted = isUsbFileTransferRestricted(userManager);
mFileTransferRestrictedBySystem = isUsbFileTransferRestrictedBySystem(userManager);
mTetheringRestricted = isUsbTetheringRestricted(userManager);
mTetheringRestrictedBySystem = isUsbTetheringRestrictedBySystem(userManager);
mMidiSupported = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_MIDI);
ConnectivityManager cm =
@@ -172,6 +171,24 @@ public class UsbBackend {
return Integer.parseInt(role);
}
private static boolean isUsbFileTransferRestricted(UserManager userManager) {
return userManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER);
}
private static boolean isUsbTetheringRestricted(UserManager userManager) {
return userManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING);
}
private static boolean isUsbFileTransferRestrictedBySystem(UserManager userManager) {
return userManager.hasBaseUserRestriction(
UserManager.DISALLOW_USB_FILE_TRANSFER, UserHandle.of(UserHandle.myUserId()));
}
private static boolean isUsbTetheringRestrictedBySystem(UserManager userManager) {
return userManager.hasBaseUserRestriction(
UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.of(UserHandle.myUserId()));
}
private boolean areFunctionDisallowed(long functions) {
return (mFileTransferRestricted && ((functions & UsbManager.FUNCTION_MTP) != 0
|| (functions & UsbManager.FUNCTION_PTP) != 0))

View File

@@ -47,7 +47,6 @@ import com.android.settings.deviceinfo.storage.VolumeSizesLoader;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
import com.android.settings.widget.EntityHeaderController;
import com.android.settings.wrapper.UserManagerWrapper;
import com.android.settingslib.applications.StorageStatsSource;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.deviceinfo.PrivateStorageInfo;
@@ -185,8 +184,7 @@ public class StorageDashboardFragment extends DashboardFragment
mVolume, new StorageManagerVolumeProvider(sm));
controllers.add(mPreferenceController);
UserManagerWrapper userManager =
new UserManagerWrapper(context.getSystemService(UserManager.class));
final UserManager userManager = context.getSystemService(UserManager.class);
mSecondaryUsers = SecondaryUserController.getSecondaryUserControllers(context, userManager);
controllers.addAll(mSecondaryUsers);
@@ -235,8 +233,7 @@ public class StorageDashboardFragment extends DashboardFragment
public List<AbstractPreferenceController> createPreferenceControllers(
Context context) {
final StorageManager sm = context.getSystemService(StorageManager.class);
final UserManagerWrapper userManager =
new UserManagerWrapper(context.getSystemService(UserManager.class));
final UserManager userManager = context.getSystemService(UserManager.class);
final List<AbstractPreferenceController> controllers = new ArrayList<>();
controllers.add(new StorageSummaryDonutPreferenceController(context));
controllers.add(new StorageItemPreferenceController(context, null /* host */,
@@ -251,9 +248,8 @@ public class StorageDashboardFragment extends DashboardFragment
@Override
public Loader<SparseArray<StorageAsyncLoader.AppsStorageResult>> onCreateLoader(int id,
Bundle args) {
Context context = getContext();
return new StorageAsyncLoader(context,
new UserManagerWrapper(context.getSystemService(UserManager.class)),
final Context context = getContext();
return new StorageAsyncLoader(context, context.getSystemService(UserManager.class),
mVolume.fsUuid,
new StorageStatsSource(context),
new PackageManagerWrapper(context.getPackageManager()));

View File

@@ -34,7 +34,6 @@ import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.deviceinfo.storage.StorageAsyncLoader;
import com.android.settings.deviceinfo.storage.StorageAsyncLoader.AppsStorageResult;
import com.android.settings.deviceinfo.storage.StorageItemPreferenceController;
import com.android.settings.wrapper.UserManagerWrapper;
import com.android.settingslib.applications.StorageStatsSource;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.deviceinfo.StorageManagerVolumeProvider;
@@ -114,9 +113,9 @@ public class StorageProfileFragment extends DashboardFragment
@Override
public Loader<SparseArray<AppsStorageResult>> onCreateLoader(int id, Bundle args) {
Context context = getContext();
final Context context = getContext();
return new StorageAsyncLoader(context,
new UserManagerWrapper(context.getSystemService(UserManager.class)),
context.getSystemService(UserManager.class),
mVolume.fsUuid,
new StorageStatsSource(context),
new PackageManagerWrapper(context.getPackageManager()));

View File

@@ -19,6 +19,7 @@ package com.android.settings.deviceinfo.storage;
import android.content.Context;
import android.content.pm.UserInfo;
import android.graphics.drawable.Drawable;
import android.os.UserManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
@@ -29,7 +30,6 @@ import android.util.SparseArray;
import com.android.settings.Utils;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.deviceinfo.StorageItemPreference;
import com.android.settings.wrapper.UserManagerWrapper;
import com.android.settingslib.core.AbstractPreferenceController;
import java.util.ArrayList;
@@ -61,7 +61,7 @@ public class SecondaryUserController extends AbstractPreferenceController implem
* @param userManager UserManagerWrapper for figuring out which controllers to add.
*/
public static List<AbstractPreferenceController> getSecondaryUserControllers(
Context context, UserManagerWrapper userManager) {
Context context, UserManager userManager) {
List<AbstractPreferenceController> controllers = new ArrayList<>();
UserInfo primaryUser = userManager.getPrimaryUser();
boolean addedUser = false;
@@ -74,8 +74,7 @@ public class SecondaryUserController extends AbstractPreferenceController implem
if (info == null || Utils.isProfileOf(primaryUser, info)) {
controllers.add(
new UserProfileController(
context, info, userManager, USER_PROFILE_INSERTION_LOCATION));
new UserProfileController(context, info, USER_PROFILE_INSERTION_LOCATION));
continue;
}

View File

@@ -26,11 +26,11 @@ import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.UserInfo;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.ArraySet;
import android.util.Log;
import android.util.SparseArray;
import com.android.settings.wrapper.UserManagerWrapper;
import com.android.settingslib.applications.StorageStatsSource;
import com.android.settingslib.utils.AsyncLoader;
import com.android.settingslib.wrapper.PackageManagerWrapper;
@@ -46,7 +46,7 @@ import java.util.List;
*/
public class StorageAsyncLoader
extends AsyncLoader<SparseArray<StorageAsyncLoader.AppsStorageResult>> {
private UserManagerWrapper mUserManager;
private UserManager mUserManager;
private static final String TAG = "StorageAsyncLoader";
private String mUuid;
@@ -54,7 +54,7 @@ public class StorageAsyncLoader
private PackageManagerWrapper mPackageManager;
private ArraySet<String> mSeenPackages;
public StorageAsyncLoader(Context context, UserManagerWrapper userManager,
public StorageAsyncLoader(Context context, UserManager userManager,
String uuid, StorageStatsSource source, PackageManagerWrapper pm) {
super(context);
mUserManager = userManager;

View File

@@ -32,7 +32,6 @@ import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.core.SubSettingLauncher;
import com.android.settings.deviceinfo.StorageItemPreference;
import com.android.settings.deviceinfo.StorageProfileFragment;
import com.android.settings.wrapper.UserManagerWrapper;
import com.android.settingslib.core.AbstractPreferenceController;
/**
@@ -44,16 +43,13 @@ public class UserProfileController extends AbstractPreferenceController implemen
UserIconLoader.UserIconHandler {
private static final String PREFERENCE_KEY_BASE = "pref_profile_";
private StorageItemPreference mStoragePreference;
private UserManagerWrapper mUserManager;
private UserInfo mUser;
private long mTotalSizeBytes;
private final int mPreferenceOrder;
public UserProfileController(
Context context, UserInfo info, UserManagerWrapper userManager, int preferenceOrder) {
public UserProfileController(Context context, UserInfo info, int preferenceOrder) {
super(context);
mUser = Preconditions.checkNotNull(info);
mUserManager = userManager;
mPreferenceOrder = preferenceOrder;
}

View File

@@ -27,7 +27,6 @@ import android.support.annotation.VisibleForTesting;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.core.SubSettingLauncher;
import com.android.settings.wrapper.WallpaperManagerWrapper;
public class WallpaperSuggestionActivity extends Activity {
@@ -61,9 +60,16 @@ public class WallpaperSuggestionActivity extends Activity {
@VisibleForTesting
public static boolean isSuggestionComplete(Context context) {
final WallpaperManagerWrapper manager = new WallpaperManagerWrapper(context);
return manager.isWallpaperServiceEnabled() ? manager.getWallpaperId(
WallpaperManager.FLAG_SYSTEM) > 0 : false;
if (!isWallpaperServiceEnabled(context)) {
return true;
}
final WallpaperManager manager = (WallpaperManager) context.getSystemService(
WALLPAPER_SERVICE);
return manager.getWallpaperId(WallpaperManager.FLAG_SYSTEM) > 0;
}
private static boolean isWallpaperServiceEnabled(Context context) {
return context.getResources().getBoolean(
com.android.internal.R.bool.config_enableWallpaperService);
}
}

View File

@@ -1,67 +0,0 @@
/*
* Copyright (C) 2017 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.settings.wrapper;
import android.content.pm.UserInfo;
import android.os.UserHandle;
import android.os.UserManager;
import java.util.List;
/**
* This class replicates a subset of the android.os.UserManager. The class
* exists so that we can use a thin wrapper around the UserManager in production code and a mock in
* tests. We cannot directly mock or shadow the UserManager, because some of the methods we rely on
* are newer than the API version supported by Robolectric or are hidden.
*/
public class UserManagerWrapper {
private UserManager mUserManager;
public UserManagerWrapper(UserManager userManager) {
mUserManager = userManager;
}
public UserInfo getPrimaryUser() {
return mUserManager.getPrimaryUser();
}
public List<UserInfo> getUsers() {
return mUserManager.getUsers();
}
public List<UserInfo> getProfiles(int userHandle) {
return mUserManager.getProfiles(userHandle);
}
public boolean isUsbFileTransferRestricted() {
return mUserManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER);
}
public boolean isUsbTetheringRestricted() {
return mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING);
}
public boolean isUsbFileTransferRestrictedBySystem() {
return mUserManager.hasBaseUserRestriction(
UserManager.DISALLOW_USB_FILE_TRANSFER, UserHandle.of(UserHandle.myUserId()));
}
public boolean isUsbTetheringRestrictedBySystem() {
return mUserManager.hasBaseUserRestriction(
UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.of(UserHandle.myUserId()));
}
}

View File

@@ -1,44 +0,0 @@
/*
* Copyright (C) 2017 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.settings.wrapper;
import android.app.WallpaperManager;
import android.content.Context;
public class WallpaperManagerWrapper {
private final WallpaperManager mWallpaperManager;
private final boolean mWallpaperServiceEnabled;
public WallpaperManagerWrapper(Context context) {
mWallpaperServiceEnabled = context.getResources().getBoolean(
com.android.internal.R.bool.config_enableWallpaperService);
mWallpaperManager = mWallpaperServiceEnabled ? (WallpaperManager) context.getSystemService(
Context.WALLPAPER_SERVICE) : null;
}
public boolean isWallpaperServiceEnabled() {
return mWallpaperServiceEnabled;
}
public int getWallpaperId(int which) {
if (!mWallpaperServiceEnabled) {
throw new RuntimeException("This device does not have wallpaper service enabled.");
}
return mWallpaperManager.getWallpaperId(which);
}
}