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

Commit 74b1a46c authored by Tsung-Mao Fang's avatar Tsung-Mao Fang
Browse files

Move Automatic System Updates from Developer Options to System

- Move the preference to the system xml file.
- DisableAutomaticUpdatesPreferenceController should not
inherit from the developer preference controller now since it's not used
in developer page anymore. Instead, just use toggle preference
controller for changing the state.
- Rewrite the test cases based on new controller.

Test: The ui works well and search can navigate to correct path.
Fix: 196001125
Change-Id: Iaea3e11d9cdbfa073ebd41bc3c84356d564c7e71
parent 1741d5d1
Loading
Loading
Loading
Loading
+0 −5
Original line number Diff line number Diff line
@@ -109,11 +109,6 @@
            android:title="@string/color_temperature"
            android:summary="@string/color_temperature_desc" />

        <SwitchPreference
            android:key="ota_disable_automatic_update"
            android:title="@string/ota_disable_automatic_update"
            android:summary="@string/ota_disable_automatic_update_summary" />

        <Preference android:key="dsu_loader"
                    android:title="@string/dsu_loader_title" />

+8 −0
Original line number Diff line number Diff line
@@ -65,6 +65,14 @@
        <intent android:action="android.settings.SYSTEM_UPDATE_SETTINGS"/>
    </Preference>

    <SwitchPreference
        android:key="ota_disable_automatic_update"
        android:title="@string/ota_disable_automatic_update"
        android:summary="@string/ota_disable_automatic_update_summary"
        android:icon="@drawable/ic_system_update"
        android:order="-57"
        settings:controller="com.android.settings.system.DisableAutomaticUpdatesPreferenceController"/>

    <Preference
        android:key="reset_dashboard"
        android:title="@string/reset_dashboard_title"
+0 −1
Original line number Diff line number Diff line
@@ -481,7 +481,6 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
        controllers.add(new PictureColorModePreferenceController(context, lifecycle));
        controllers.add(new WebViewAppPreferenceController(context));
        controllers.add(new CoolColorTemperaturePreferenceController(context));
        controllers.add(new DisableAutomaticUpdatesPreferenceController(context));
        controllers.add(new SelectDSUPreferenceController(context));
        controllers.add(new AdbPreferenceController(context, fragment));
        controllers.add(new ClearAdbKeysPreferenceController(context, fragment));
+59 −0
Original line number Diff line number Diff line
@@ -14,63 +14,46 @@
 * limitations under the License.
 */

package com.android.settings.development;
package com.android.settings.system;

import android.content.Context;
import android.provider.Settings;

import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.SwitchPreference;

import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.development.DeveloperOptionsPreferenceController;
import com.android.settings.core.TogglePreferenceController;

public class DisableAutomaticUpdatesPreferenceController extends
        DeveloperOptionsPreferenceController implements
        Preference.OnPreferenceChangeListener, PreferenceControllerMixin {

    private static final String OTA_DISABLE_AUTOMATIC_UPDATE_KEY = "ota_disable_automatic_update";
/** A controller maintains the state of Automatic System Update feature. */
public class DisableAutomaticUpdatesPreferenceController extends TogglePreferenceController {

    // We use the "disabled status" in code, but show the opposite text
    // "Automatic system updates" on screen. So a value 0 indicates the
    // automatic update is enabled.
    @VisibleForTesting
    final static int DISABLE_UPDATES_SETTING = 1;
    static final int DISABLE_UPDATES_SETTING = 1;
    @VisibleForTesting
    final static int ENABLE_UPDATES_SETTING = 0;
    static final int ENABLE_UPDATES_SETTING = 0;

    public DisableAutomaticUpdatesPreferenceController(Context context) {
        super(context);
    public DisableAutomaticUpdatesPreferenceController(Context context, String key) {
        super(context, key);
    }

    @Override
    public String getPreferenceKey() {
        return OTA_DISABLE_AUTOMATIC_UPDATE_KEY;
    public int getAvailabilityStatus() {
        return AVAILABLE;
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        final boolean updatesEnabled = (Boolean) newValue;
        Settings.Global.putInt(mContext.getContentResolver(),
                Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE,
                updatesEnabled ? ENABLE_UPDATES_SETTING : DISABLE_UPDATES_SETTING);
        return true;
    public boolean isChecked() {
        return Settings.Global.getInt(mContext.getContentResolver(),
                Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE, ENABLE_UPDATES_SETTING /* default */)
                == ENABLE_UPDATES_SETTING;
    }

    @Override
    public void updateState(Preference preference) {
        final int updatesEnabled = Settings.Global.getInt(mContext.getContentResolver(),
                Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE, 0 /* default */);

        ((SwitchPreference) mPreference).setChecked(updatesEnabled != DISABLE_UPDATES_SETTING);
    }

    @Override
    protected void onDeveloperOptionsSwitchDisabled() {
        super.onDeveloperOptionsSwitchDisabled();
        Settings.Global.putInt(mContext.getContentResolver(),
                Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE, DISABLE_UPDATES_SETTING);
        ((SwitchPreference) mPreference).setChecked(false);
    public boolean setChecked(boolean isChecked) {
        return Settings.Global.putInt(mContext.getContentResolver(),
                Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE,
                isChecked ? ENABLE_UPDATES_SETTING : DISABLE_UPDATES_SETTING);
    }
}
+102 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 * Copyright (C) 2021 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.
@@ -14,19 +14,18 @@
 * limitations under the License.
 */

package com.android.settings.development;
package com.android.settings.system;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.provider.Settings;

import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;

import com.android.settings.core.BasePreferenceController;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -50,59 +49,54 @@ public class DisableAutomaticUpdatesPreferenceControllerTest {
    public void setup() {
        MockitoAnnotations.initMocks(this);
        mContext = RuntimeEnvironment.application;
        mController = new DisableAutomaticUpdatesPreferenceController(mContext);
        when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
                mPreference);
        mController.displayPreference(mPreferenceScreen);
        mController = new DisableAutomaticUpdatesPreferenceController(mContext, "test");
    }

    @Test
    public void onPreferenceChanged_turnOnAutomaticUpdates() {
        mController.onPreferenceChange(null, true);

        final int mode = Settings.Global.getInt(mContext.getContentResolver(),
                Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE, -1);

        assertThat(mode).isEqualTo(
                DisableAutomaticUpdatesPreferenceController.ENABLE_UPDATES_SETTING);
    public void getAvailabilityStatus_shouldReturnAVAILABLE() {
        assertThat(mController.getAvailabilityStatus()).isEqualTo(
                BasePreferenceController.AVAILABLE);
    }

    @Test
    public void onPreferenceChanged_turnOffAutomaticUpdates() {
        mController.onPreferenceChange(null, false);

        final int mode = Settings.Global.getInt(mContext.getContentResolver(),
                Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE, -1);
    public void isChecked_valueIsZeroInProvider_shouldReturnTrue() {
        Settings.Global.putInt(mContext.getContentResolver(),
                Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE,
                DisableAutomaticUpdatesPreferenceController.ENABLE_UPDATES_SETTING);

        assertThat(mode).isEqualTo(
                DisableAutomaticUpdatesPreferenceController.DISABLE_UPDATES_SETTING);
        assertThat(mController.isChecked()).isTrue();
    }

    @Test
    public void updateState_preferenceShouldBeChecked() {
        Settings.Global
                .putInt(mContext.getContentResolver(), Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE,
                        DisableAutomaticUpdatesPreferenceController.ENABLE_UPDATES_SETTING);
        mController.updateState(mPreference);
    public void isChecked_valueIsOneInProvider_shouldReturnFalse() {
        Settings.Global.putInt(mContext.getContentResolver(),
                Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE,
                DisableAutomaticUpdatesPreferenceController.DISABLE_UPDATES_SETTING);

        verify(mPreference).setChecked(true);
        assertThat(mController.isChecked()).isFalse();
    }

    @Test
    public void updateState_preferenceShouldNotBeChecked() {
        Settings.Global
                .putInt(mContext.getContentResolver(), Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE,
                        DisableAutomaticUpdatesPreferenceController.DISABLE_UPDATES_SETTING);
        mController.updateState(mPreference);
    public void setChecked_true_providerValueIsZero() {
        mController.setChecked(true);

        int value = Settings.Global.getInt(mContext.getContentResolver(),
                Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE,
                DisableAutomaticUpdatesPreferenceController.ENABLE_UPDATES_SETTING /* default */);

        verify(mPreference).setChecked(false);
        assertThat(value).isEqualTo(
                DisableAutomaticUpdatesPreferenceController.ENABLE_UPDATES_SETTING);
    }

    @Test
    public void onDeveloperOptionsDisabled_shouldDisablePreference() {
        mController.onDeveloperOptionsDisabled();
    public void setChecked_false_providerValueIsOne() {
        mController.setChecked(false);

        int value = Settings.Global.getInt(mContext.getContentResolver(),
                Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE,
                DisableAutomaticUpdatesPreferenceController.ENABLE_UPDATES_SETTING /* default */);

        verify(mPreference).setEnabled(false);
        verify(mPreference).setChecked(false);
        assertThat(value).isEqualTo(
                DisableAutomaticUpdatesPreferenceController.DISABLE_UPDATES_SETTING);
    }
}
Loading