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

Commit b914ec91 authored by Oli Lan's avatar Oli Lan Committed by Android (Google) Code Review
Browse files

Merge "Use DeviceConfig property for default value of clipboard access...

Merge "Use DeviceConfig property for default value of clipboard access notification setting." into sc-dev
parents 1e3a18a3 413f0685
Loading
Loading
Loading
Loading
+49 −4
Original line number Original line Diff line number Diff line
@@ -16,33 +16,47 @@


package com.android.settings.privacy;
package com.android.settings.privacy;


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


import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

import com.android.settings.core.TogglePreferenceController;
import com.android.settings.core.TogglePreferenceController;


/**
/**
 * Controller for preference to toggle whether clipboard access notifications should be shown.
 * Controller for preference to toggle whether clipboard access notifications should be shown.
 */
 */
public class ShowClipAccessNotificationPreferenceController extends TogglePreferenceController {
public class ShowClipAccessNotificationPreferenceController
        extends TogglePreferenceController implements LifecycleObserver {


    private static final String KEY_SHOW_CLIP_ACCESS_NOTIFICATION = "show_clip_access_notification";
    private static final String KEY_SHOW_CLIP_ACCESS_NOTIFICATION = "show_clip_access_notification";


    private final DeviceConfig.OnPropertiesChangedListener mDeviceConfigListener =
            properties -> updateConfig();
    private boolean mDefault;
    private Preference mPreference;

    public ShowClipAccessNotificationPreferenceController(Context context) {
    public ShowClipAccessNotificationPreferenceController(Context context) {
        super(context, KEY_SHOW_CLIP_ACCESS_NOTIFICATION);
        super(context, KEY_SHOW_CLIP_ACCESS_NOTIFICATION);
        updateConfig();
    }
    }


    @Override
    @Override
    public boolean isChecked() {
    public boolean isChecked() {
        // TODO(b/182349993) Retrieve default value from DeviceConfig.
        return Settings.Secure.getInt(mContext.getContentResolver(),
        return Settings.Secure.getInt(mContext.getContentResolver(),
                Settings.Secure.CLIPBOARD_SHOW_ACCESS_NOTIFICATIONS, 1) != 0;
                Settings.Secure.CLIPBOARD_SHOW_ACCESS_NOTIFICATIONS, (mDefault ? 1 : 0)) != 0;
    }
    }


    @Override
    @Override
    public boolean setChecked(boolean isChecked) {
    public boolean setChecked(boolean isChecked) {
        Settings.Secure.putInt(mContext.getContentResolver(),
        Settings.Secure.putInt(mContext.getContentResolver(),
                Settings.Secure.CLIPBOARD_SHOW_ACCESS_NOTIFICATIONS, isChecked ? 1 : 0);
                Settings.Secure.CLIPBOARD_SHOW_ACCESS_NOTIFICATIONS, (isChecked ? 1 : 0));
        return true;
        return true;
    }
    }


@@ -51,4 +65,35 @@ public class ShowClipAccessNotificationPreferenceController extends TogglePrefer
        return AVAILABLE;
        return AVAILABLE;
    }
    }


    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);

        mPreference = screen.findPreference(getPreferenceKey());
    }

    /**
     * Registers a DeviceConfig listener on start.
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onStart() {
        DeviceConfig.addOnPropertiesChangedListener(DeviceConfig.NAMESPACE_CLIPBOARD,
                mContext.getMainExecutor(), mDeviceConfigListener);
    }

    /**
     * Removes the DeviceConfig listener on stop.
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onStop() {
        DeviceConfig.removeOnPropertiesChangedListener(mDeviceConfigListener);
    }

    private void updateConfig() {
        mDefault = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_CLIPBOARD,
                ClipboardManager.DEVICE_CONFIG_SHOW_ACCESS_NOTIFICATIONS,
                ClipboardManager.DEVICE_CONFIG_DEFAULT_SHOW_ACCESS_NOTIFICATIONS);
        updateState(mPreference);
    }

}
}
+42 −2
Original line number Original line Diff line number Diff line
@@ -18,8 +18,10 @@ package com.android.settings.privacy;


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


import android.content.ClipboardManager;
import android.content.ContentResolver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Context;
import android.provider.DeviceConfig;
import android.provider.Settings;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.provider.Settings.SettingNotFoundException;


@@ -27,14 +29,18 @@ import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.core.app.ApplicationProvider;


import com.android.settings.testutils.shadow.ShadowDeviceConfig;

import org.junit.Before;
import org.junit.Before;
import org.junit.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;


@RunWith(RobolectricTestRunner.class)
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowDeviceConfig.class})
public class ShowClipAccessNotificationPreferenceControllerTest {
public class ShowClipAccessNotificationPreferenceControllerTest {


    @Mock
    @Mock
@@ -54,19 +60,47 @@ public class ShowClipAccessNotificationPreferenceControllerTest {
    }
    }


    @Test
    @Test
    public void isChecked_settingIsOff_shouldReturnFalse() throws Exception {
    public void isChecked_settingIsOff_shouldReturnFalse() {
        setProperty(0);
        setProperty(0);


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


    @Test
    @Test
    public void isChecked_settingIsOn_shouldReturnTrue() throws Exception {
    public void isChecked_settingIsOn_shouldReturnTrue() {
        setProperty(1);
        setProperty(1);


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


    @Test
    public void isChecked_settingIsUnset_deviceConfigProvidesDefaultOfTrue() {
        clearProperty();

        DeviceConfig.setProperty(DeviceConfig.NAMESPACE_CLIPBOARD,
                ClipboardManager.DEVICE_CONFIG_SHOW_ACCESS_NOTIFICATIONS,
                "true", false);

        ShowClipAccessNotificationPreferenceController controller =
                new ShowClipAccessNotificationPreferenceController(mContext);

        assertThat(controller.isChecked()).isTrue();
    }

    @Test
    public void isChecked_settingIsUnset_deviceConfigProvidesDefaultOfFalse() {
        clearProperty();

        DeviceConfig.setProperty(DeviceConfig.NAMESPACE_CLIPBOARD,
                ClipboardManager.DEVICE_CONFIG_SHOW_ACCESS_NOTIFICATIONS,
                "false", false);

        ShowClipAccessNotificationPreferenceController controller =
                new ShowClipAccessNotificationPreferenceController(mContext);

        assertThat(controller.isChecked()).isFalse();
    }

    @Test
    @Test
    public void onPreferenceChange_turnOn_shouldChangeSettingTo1() throws Exception {
    public void onPreferenceChange_turnOn_shouldChangeSettingTo1() throws Exception {
        setProperty(0);
        setProperty(0);
@@ -93,6 +127,12 @@ public class ShowClipAccessNotificationPreferenceControllerTest {
                contentResolver, Settings.Secure.CLIPBOARD_SHOW_ACCESS_NOTIFICATIONS, newValue);
                contentResolver, Settings.Secure.CLIPBOARD_SHOW_ACCESS_NOTIFICATIONS, newValue);
    }
    }


    private void clearProperty() {
        final ContentResolver contentResolver = mContext.getContentResolver();
        Settings.Secure.putString(
                contentResolver, Settings.Secure.CLIPBOARD_SHOW_ACCESS_NOTIFICATIONS, null);
    }

    private void assertProperty(int expectedValue) throws SettingNotFoundException {
    private void assertProperty(int expectedValue) throws SettingNotFoundException {
        final ContentResolver contentResolver = mContext.getContentResolver();
        final ContentResolver contentResolver = mContext.getContentResolver();
        assertThat(Settings.Secure.getInt(
        assertThat(Settings.Secure.getInt(