Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d1cc1365 authored by Anton Philippov's avatar Anton Philippov Committed by android-build-merger
Browse files

Merge "Add summary for Backup menu in Settings." into oc-dev

am: 646d9968

Change-Id: I34cd8a9ab8482f287ea36051242df501e7efb0b0
parents 7038d268 646d9968
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -2933,8 +2933,6 @@
            <intent-filter android:priority="60">
                <action android:name="com.android.settings.action.SETTINGS" />
            </intent-filter>
            <meta-data android:name="com.android.settings.category"
                       android:value="com.android.settings.category.ia.system" />
            <meta-data android:name="com.android.settings.summary"
                       android:resource="@string/summary_empty"/>
            <meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"
+10 −0
Original line number Diff line number Diff line
@@ -18,6 +18,16 @@
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="@string/header_category_system">

    <!-- 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">
        <intent android:action="android.settings.BACKUP_AND_RESET_SETTINGS" />
    </Preference>

    <!-- System updates -->
    <Preference
        android:key="system_update_settings"
+64 −0
Original line number Diff line number Diff line
/*
 * 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.Build;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.UserManager;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.util.Log;

import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.core.PreferenceController;

public class BackupSettingsActivityPreferenceController extends PreferenceController {
    private static final String KEY_BACKUP_SETTINGS = "backup_settings";
    private static final String TAG = "BackupSettingActivityPC" ;

    private final UserManager mUm;
    private final BackupManager mBackupManager;

    public BackupSettingsActivityPreferenceController(Context context) {
        super(context);
        mUm = (UserManager) context.getSystemService(Context.USER_SERVICE);
        mBackupManager = new BackupManager(context);
    }

    @Override
    public boolean isAvailable() {
        return mUm.isAdminUser();
    }

    @Override
    public String getPreferenceKey() {
        return KEY_BACKUP_SETTINGS;
    }

    @Override
    public void updateState(Preference preference) {
        final boolean backupEnabled = mBackupManager.isBackupEnabled();

        preference.setSummary(backupEnabled
                ? R.string.accessibility_feature_state_on
                : R.string.accessibility_feature_state_off);
    }
}
+2 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.provider.SearchIndexableResource;

import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.backup.BackupSettingsActivityPreferenceController;
import com.android.settings.core.PreferenceController;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.deviceinfo.AdditionalSystemUpdatePreferenceController;
@@ -60,6 +61,7 @@ public class SystemDashboardFragment extends DashboardFragment {
        final List<PreferenceController> controllers = new ArrayList<>();
        controllers.add(new SystemUpdatePreferenceController(context, UserManager.get(context)));
        controllers.add(new AdditionalSystemUpdatePreferenceController(context));
        controllers.add(new BackupSettingsActivityPreferenceController(context));
        return controllers;
    }

+122 −0
Original line number Diff line number Diff line
/*
 * 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.any;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.accounts.AccountManager;
import android.app.backup.BackupManager;
import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.os.UserManager;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;

import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
        shadows = {BackupSettingsActivityPreferenceControllerTest.ShadowBackupManager.class})
public class BackupSettingsActivityPreferenceControllerTest {
    private static final String KEY_BACKUP_SETTINGS = "backup_settings";

    private Context mContext;
    @Mock
    private UserManager mUserManager;

    @Mock
    private PreferenceScreen mScreen;
    @Mock
    private Preference mBackupPreference;

    private BackupSettingsActivityPreferenceController mController;

    private static boolean mBackupEnabled;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        mContext = spy(RuntimeEnvironment.application.getApplicationContext());
        when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);

        mController = new BackupSettingsActivityPreferenceController(mContext);
    }

    @Test
    public void updateState_backupOn() throws RemoteException {
        mBackupEnabled = true;

        mController.updateState(mBackupPreference);

        verify(mBackupPreference).setSummary(R.string.accessibility_feature_state_on);
    }

    @Test
    public void updateState_backupOff() throws RemoteException {
        mBackupEnabled = false;

        mController.updateState(mBackupPreference);

        verify(mBackupPreference).setSummary(R.string.accessibility_feature_state_off);
    }

    @Test
    public void isAvailable_systemUser() {
        when(mUserManager.isAdminUser()).thenReturn(true);

        assertThat(mController.isAvailable()).isTrue();
    }

    @Test
    public void isAvailable_nonSystemUser() {
        when(mUserManager.isAdminUser()).thenReturn(false);

        assertThat(mController.isAvailable()).isFalse();
    }

    @Test
    public void getPreferenceKey() {
        assertThat(mController.getPreferenceKey()).isEqualTo(KEY_BACKUP_SETTINGS);
    }

    @Implements(BackupManager.class)
    public static class ShadowBackupManager {

        @Implementation
        public boolean isBackupEnabled() {
            return mBackupEnabled;
        }
    }
}