Merge "[Multi-user] Change Backup Settings page to support multi-user."

This commit is contained in:
Chandan Nath
2019-01-30 14:03:38 +00:00
committed by Android (Google) Code Review
13 changed files with 179 additions and 249 deletions

View File

@@ -2776,13 +2776,17 @@
android:value="com.android.settings.webview.WebViewAppPicker" />
</activity-alias>
<activity android:name=".backup.BackupSettingsActivity"
<provider
android:name=".backup.BackupSettingsContentProvider"
android:authorities="com.android.settings.backup.BackupSettingsContentProvider"
android:exported="true">
</provider>
<activity android:name=".backup.UserBackupSettingsActivity"
android:label="@string/privacy_settings_title"
android:icon="@drawable/ic_settings_backup"
android:parentActivityName="Settings">
android:icon="@drawable/ic_settings_backup">
<intent-filter android:priority="1">
<action android:name="android.settings.PRIVACY_SETTINGS" />
<action android:name="android.settings.BACKUP_AND_RESET_SETTINGS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
@@ -2790,10 +2794,16 @@
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.VOICE_LAUNCH" />
</intent-filter>
<meta-data android:name="com.android.settings.summary"
android:resource="@string/summary_empty"/>
<meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"
android:value="true" />
<!-- Mark the activity as a dynamic setting -->
<intent-filter>
<action android:name="com.android.settings.action.IA_SETTINGS" />
</intent-filter>
<!-- Tell Settings app which category it belongs to -->
<meta-data android:name="com.android.settings.category"
android:value="com.android.settings.category.ia.system" />
<meta-data android:name="com.android.settings.summary_uri"
android:value="content://com.android.settings.backup.BackupSettingsContentProvider/summary" />
<meta-data android:name="com.android.settings.order" android:value="-60"/>
</activity>
<activity

View File

@@ -29,17 +29,6 @@
android:fragment="com.android.settings.gestures.GestureSettings"
settings:controller="com.android.settings.gestures.GesturesSettingPreferenceController"/>
<!-- Backup -->
<Preference
android:key="backup_settings"
android:title="@string/privacy_settings_title"
android:summary="@string/summary_placeholder"
android:icon="@drawable/ic_settings_backup"
android:order="-60"
settings:controller="com.android.settings.backup.BackupSettingsActivityPreferenceController">
<intent android:action="android.settings.BACKUP_AND_RESET_SETTINGS" />
</Preference>
<Preference
android:key="reset_dashboard"
android:title="@string/reset_dashboard_title"

View File

@@ -51,7 +51,6 @@ import androidx.preference.PreferenceManager;
import com.android.internal.util.ArrayUtils;
import com.android.settings.Settings.WifiSettingsActivity;
import com.android.settings.applications.manageapplications.ManageApplications;
import com.android.settings.backup.BackupSettingsActivity;
import com.android.settings.core.OnActivityResultListener;
import com.android.settings.core.SettingsBaseActivity;
import com.android.settings.core.SubSettingLauncher;
@@ -651,11 +650,6 @@ public class SettingsActivity extends SettingsBaseActivity
showDev, isAdmin)
|| somethingChanged;
// Enable/disable backup settings depending on whether the user is admin.
somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
BackupSettingsActivity.class.getName()), true, isAdmin)
|| somethingChanged;
somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Settings.WifiDisplaySettingsActivity.class.getName()),
WifiDisplaySettings.isAvailable(this), isAdmin)

View File

@@ -1,54 +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.backup;
import android.app.backup.BackupManager;
import android.content.Context;
import android.os.UserManager;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
public class BackupSettingsActivityPreferenceController extends BasePreferenceController {
private static final String TAG = "BackupSettingActivityPC";
private final UserManager mUm;
private final BackupManager mBackupManager;
public BackupSettingsActivityPreferenceController(Context context, String key) {
super(context, key);
mUm = (UserManager) context.getSystemService(Context.USER_SERVICE);
mBackupManager = new BackupManager(context);
}
@Override
public int getAvailabilityStatus() {
return mUm.isAdminUser()
? AVAILABLE_UNSEARCHABLE
: UNSUPPORTED_ON_DEVICE;
}
@Override
public CharSequence getSummary() {
final boolean backupEnabled = mBackupManager.isBackupEnabled();
return backupEnabled
? mContext.getText(R.string.backup_summary_state_on)
: mContext.getText(R.string.backup_summary_state_off);
}
}

View File

@@ -0,0 +1,78 @@
/*
* 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.settings.backup;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_SUMMARY;
/** Provider stores and manages user interaction feedback for homepage contextual cards. */
public class BackupSettingsContentProvider extends ContentProvider {
private static final String AUTHORITY =
"com.android.settings.backup.BackupSettingsContentProvider";
private static final String SUMMARY = "summary";
private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
static {
URI_MATCHER.addURI(AUTHORITY, SUMMARY, 1);
}
@Override
public Bundle call(String method, String uri, Bundle extras) {
if (!SUMMARY.equals(method)) {
return null;
}
Bundle bundle = new Bundle();
bundle.putString(META_DATA_PREFERENCE_SUMMARY,
new BackupSettingsHelper(getContext()).getSummary());
return bundle;
}
@Override
public boolean onCreate() {
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
throw new UnsupportedOperationException();
}
@Override
public String getType(Uri uri) {
throw new UnsupportedOperationException();
}
@Override
public Uri insert(Uri uri, ContentValues values) {
throw new UnsupportedOperationException();
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
throw new UnsupportedOperationException();
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
throw new UnsupportedOperationException();
}
}

View File

@@ -68,7 +68,7 @@ public class BackupSettingsFragment extends DashboardFragment {
return controllers;
}
// The intention is to index {@link BackupSettingsActivity} instead of the fragments,
// The intention is to index {@link UserBackupSettingsActivity} instead of the fragments,
// therefore leaving this index provider empty.
public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider() {

View File

@@ -24,6 +24,7 @@ import android.content.Intent;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.Log;
import androidx.annotation.VisibleForTesting;
@@ -34,7 +35,7 @@ import com.android.settings.Settings.PrivacySettingsActivity;
import java.net.URISyntaxException;
/**
* Helper class for {@link BackupSettingsActivity} that interacts with {@link IBackupManager}.
* Helper class for {@link UserBackupSettingsActivity} that interacts with {@link IBackupManager}.
*/
public class BackupSettingsHelper {
private static final String TAG = "BackupSettingsHelper";
@@ -48,6 +49,24 @@ public class BackupSettingsHelper {
mContext = context;
}
/**
* If there is only one profile, show whether the backup is on or off.
* Otherwise, show nothing.
*/
String getSummary() {
UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
if (userManager.getUserProfiles().size() == 1) {
try {
int resId = mBackupManager.isBackupEnabled() ? R.string.backup_summary_state_on
: R.string.backup_summary_state_off;
return mContext.getText(resId).toString();
} catch (RemoteException e) {
Log.e(TAG, "Error getting isBackupEnabled", e);
}
}
return null;
}
/**
* Returns an intent to launch backup settings from backup transport if the intent was provided
* by the transport. Otherwise returns the intent to launch the default backup settings screen.

View File

@@ -20,7 +20,6 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.UserHandle;
import android.util.Log;
import androidx.annotation.VisibleForTesting;
@@ -40,9 +39,14 @@ import java.util.List;
/**
* The activity used to launch the configured Backup activity or the preference screen
* if the manufacturer provided their backup settings.
* Pre-Q, BackupSettingsActivity was disabled for non-system users. Therefore, for phones which
* upgrade to Q, BackupSettingsActivity is disabled for those users. However, we cannot simply
* enable it in Q since component enable can only be done by the user itself; which is not
* enough in Q we want it to be enabled for all profile users of the user.
* Therefore, as a simple workaround, we use a new class which is enabled by default.
*/
@SearchIndexable
public class BackupSettingsActivity extends FragmentActivity implements Indexable {
public class UserBackupSettingsActivity extends FragmentActivity implements Indexable {
private static final String TAG = "BackupSettingsActivity";
private FragmentManager mFragmentManager;
@@ -108,29 +112,13 @@ public class BackupSettingsActivity extends FragmentActivity implements Indexabl
data.screenTitle = context.getString(R.string.settings_label);
data.keywords = context.getString(R.string.keywords_backup);
data.intentTargetPackage = context.getPackageName();
data.intentTargetClass = BackupSettingsActivity.class.getName();
data.intentTargetClass = com.android.settings.backup.UserBackupSettingsActivity.class.getName();
data.intentAction = Intent.ACTION_MAIN;
data.key = BACKUP_SEARCH_INDEX_KEY;
result.add(data);
return result;
}
@Override
public List<String> getNonIndexableKeys(Context context) {
final List<String> keys = super.getNonIndexableKeys(context);
// For non-primary user, no backup is available, so don't show it in search
// TODO: http://b/22388012
if (UserHandle.myUserId() != UserHandle.USER_SYSTEM) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Not a system user, not indexing the screen");
}
keys.add(BACKUP_SEARCH_INDEX_KEY);
}
return keys;
}
};
@VisibleForTesting

View File

@@ -49,6 +49,7 @@ import com.android.settings.applications.specialaccess.deviceadmin.DeviceAdminSe
import com.android.settings.applications.specialaccess.pictureinpicture.PictureInPictureDetails;
import com.android.settings.applications.specialaccess.pictureinpicture.PictureInPictureSettings;
import com.android.settings.applications.specialaccess.vrlistener.VrListenerSettings;
import com.android.settings.backup.UserBackupSettingsActivity;
import com.android.settings.backup.PrivacySettings;
import com.android.settings.backup.ToggleBackupSettingFragment;
import com.android.settings.biometrics.face.FaceSettings;
@@ -298,5 +299,6 @@ public class SettingsGateway {
Settings.DateTimeSettingsActivity.class.getName(),
Settings.EnterprisePrivacySettingsActivity.class.getName(),
Settings.MyDeviceInfoActivity.class.getName(),
UserBackupSettingsActivity.class.getName(),
};
}

View File

@@ -1,110 +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.backup;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import android.app.backup.BackupManager;
import android.content.Context;
import android.os.UserManager;
import androidx.preference.Preference;
import com.android.settings.R;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.shadows.ShadowUserManager;
@RunWith(RobolectricTestRunner.class)
public class BackupSettingsActivityPreferenceControllerTest {
private static final String KEY_BACKUP_SETTINGS = "backup_settings";
private Context mContext;
private BackupManager mBackupManager;
private UserManager mUserManager;
@Mock
private Preference mBackupPreference;
private BackupSettingsActivityPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mBackupManager = new BackupManager(mContext);
final ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.grantPermissions(android.Manifest.permission.BACKUP);
shadowApplication.setSystemService(Context.BACKUP_SERVICE, mBackupManager);
mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
mController = new BackupSettingsActivityPreferenceController(mContext, KEY_BACKUP_SETTINGS);
}
@Test
public void updateState_backupOn() {
mBackupManager.setBackupEnabled(true);
mController.updateState(mBackupPreference);
String summaryString = mContext.getString(R.string.backup_summary_state_on);
verify(mBackupPreference).setSummary(summaryString);
}
@Test
public void updateState_backupOff() {
mBackupManager.setBackupEnabled(false);
mController.updateState(mBackupPreference);
String summaryString = mContext.getString(R.string.backup_summary_state_off);
verify(mBackupPreference).setSummary(summaryString);
}
@Test
public void isAvailable_systemUser() {
final ShadowUserManager sum = Shadow.extract(mUserManager);
sum.setIsAdminUser(true);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void isAvailable_nonSystemUser() {
final ShadowUserManager sum = Shadow.extract(mUserManager);
sum.setIsAdminUser(false);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void getPreferenceKey() {
assertThat(mController.getPreferenceKey()).isEqualTo(KEY_BACKUP_SETTINGS);
}
}

View File

@@ -31,12 +31,16 @@ import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
import android.content.res.Resources;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.UserHandle;
import android.os.UserManager;
import com.android.settings.R;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -47,11 +51,12 @@ import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowUserManager;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = BackupSettingsHelperTest.ShadowBackupManagerStub.class)
public class BackupSettingsHelperTest {
private static final String DEFAULT_SETTINGS_CLASSNAME =
"com.android.settings.Settings$PrivacySettingsActivity";
@@ -72,12 +77,46 @@ public class BackupSettingsHelperTest {
@Mock
private static IBackupManager mBackupManager;
private ShadowUserManager mUserManager;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application.getApplicationContext());
when(mBackupManager.getCurrentTransport()).thenReturn("test_transport");
mBackupSettingsHelper = new BackupSettingsHelper(mContext);
mUserManager = Shadow.extract(mContext.getSystemService(Context.USER_SERVICE));
}
@Test
public void testGetSummary_backupEnabledOnlyOneProfile_showsOn() throws Exception {
mUserManager.addUserProfile(new UserHandle(0));
when(mBackupManager.isBackupEnabled()).thenReturn(true);
String backupSummary = mBackupSettingsHelper.getSummary();
assertThat(backupSummary).isEqualTo(mContext.getString(R.string.backup_summary_state_on));
}
@Test
public void testGetSummary_backupDisabledOnlyOneProfile_showsOff() throws Exception {
mUserManager.addUserProfile(new UserHandle(0));
when(mBackupManager.isBackupEnabled()).thenReturn(false);
String backupSummary = mBackupSettingsHelper.getSummary();
assertThat(backupSummary).isEqualTo(mContext.getString(R.string.backup_summary_state_off));
}
@Test
public void testGetSummary_TwoProfiles_returnsNull() throws Exception {
mUserManager.addUserProfile(new UserHandle(0));
mUserManager.addUserProfile(new UserHandle(10));
when(mBackupManager.isBackupEnabled()).thenReturn(true);
String backupSummary = mBackupSettingsHelper.getSummary();
assertThat(backupSummary).isNull();
}
@Test

View File

@@ -35,8 +35,6 @@ import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.android.settings.search.SearchIndexableRaw;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -54,14 +52,12 @@ import org.robolectric.annotation.Implements;
import org.robolectric.annotation.Resetter;
import org.robolectric.shadows.ShadowPackageManager;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {BackupSettingsActivityTest.ShadowBackupSettingsHelper.class,
BackupSettingsActivityTest.ShadowUserHandle.class})
public class BackupSettingsActivityTest {
private ActivityController<BackupSettingsActivity> mActivityController;
private BackupSettingsActivity mActivity;
@Config(shadows = {UserBackupSettingsActivityTest.ShadowBackupSettingsHelper.class,
UserBackupSettingsActivityTest.ShadowUserHandle.class})
public class UserBackupSettingsActivityTest {
private ActivityController<UserBackupSettingsActivity> mActivityController;
private UserBackupSettingsActivity mActivity;
private Application mApplication;
private ShadowPackageManager mPackageManager;
private static boolean mIsBackupProvidedByOEM;
@@ -81,7 +77,7 @@ public class BackupSettingsActivityTest {
MockitoAnnotations.initMocks(this);
mApplication = RuntimeEnvironment.application;
mActivityController = Robolectric.buildActivity(BackupSettingsActivity.class);
mActivityController = Robolectric.buildActivity(UserBackupSettingsActivity.class);
mActivity = mActivityController.get();
mPackageManager = Shadows.shadowOf(mApplication.getPackageManager());
when(mIntent.getComponent()).thenReturn(mComponent);
@@ -130,9 +126,9 @@ public class BackupSettingsActivityTest {
@Test
public void getNonIndexableKeys_SystemUser() {
assertThat(BackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getRawDataToIndex(
assertThat(UserBackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getRawDataToIndex(
mApplication, true)).isNotEmpty();
assertThat(BackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
assertThat(UserBackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
mApplication)).isEmpty();
}
@@ -140,16 +136,10 @@ public class BackupSettingsActivityTest {
public void getNonIndexableKeys_NonSystemUser() {
ShadowUserHandle.setUid(1); // Non-SYSTEM user.
final List<SearchIndexableRaw> indexableRaws =
BackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getRawDataToIndex(
mApplication, true);
final List<String> nonIndexableKeys =
BackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
mApplication);
assertThat(indexableRaws).isNotNull();
assertThat(indexableRaws).isNotEmpty();
assertThat(nonIndexableKeys).isNotEmpty();
assertThat(UserBackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getRawDataToIndex(
mApplication, true)).isNotEmpty();
assertThat(UserBackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
mApplication)).isEmpty();
}
@Implements(BackupSettingsHelper.class)

View File

@@ -37,12 +37,9 @@ import java.util.List;
@RunWith(AndroidJUnit4.class)
@SmallTest
public class BackupIntentTest {
private static final String INTENT_PRIVACY_SETTINGS = "android.settings.PRIVACY_SETTINGS";
private static final String INTENT_BACKUP_SETTINGS =
"android.settings.BACKUP_AND_RESET_SETTINGS";
private static final String BACKUP_SETTINGS_ACTIVITY =
"com.android.settings.backup.BackupSettingsActivity";
"com.android.settings.backup.UserBackupSettingsActivity";
private Context mContext;
@@ -52,17 +49,6 @@ public class BackupIntentTest {
mContext = instrumentation.getTargetContext();
}
@Test
public void testBackupSettingsIntentResolvesToOnlyOneActivity(){
PackageManager pm = mContext.getPackageManager();
Intent intent = new Intent(INTENT_BACKUP_SETTINGS);
List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
assertThat(activities).isNotNull();
assertThat(activities.size()).isEqualTo(1);
assertThat(activities.get(0).activityInfo.getComponentName().getClassName()).
isEqualTo(BACKUP_SETTINGS_ACTIVITY);
}
@Test
public void testPrivacySettingsIntentResolvesToOnlyOneActivity(){
PackageManager pm = mContext.getPackageManager();
@@ -73,5 +59,4 @@ public class BackupIntentTest {
assertThat(activities.get(0).activityInfo.getComponentName().getClassName()).
isEqualTo(BACKUP_SETTINGS_ACTIVITY);
}
}