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

Commit 9c9549bd authored by Andrew Sapperstein's avatar Andrew Sapperstein
Browse files

Refactoring DoubleTwistPreferenceController.

Made the availability and setting logic static so that
it can be invoked without creating a controller.

Bug: 62022517
Test: robotests
Change-Id: I3a51c61849e6ba8b8aa850ca22d666a9f84d252f
parent 2f525fbd
Loading
Loading
Loading
Loading
+13 −8
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ public class DoubleTwistPreferenceController extends GesturePreferenceController
                || prefs.getBoolean(DoubleTwistGestureSettings.PREF_KEY_SUGGESTION_COMPLETE, false);
    }

    private static boolean isGestureAvailable(Context context) {
    public static boolean isGestureAvailable(Context context) {
        final Resources resources = context.getResources();
        final String name = resources.getString(R.string.gesture_double_twist_sensor_name);
        final String vendor = resources.getString(R.string.gesture_double_twist_sensor_vendor);
@@ -86,25 +86,30 @@ public class DoubleTwistPreferenceController extends GesturePreferenceController
    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        final int enabled = (boolean) newValue ? ON : OFF;
        Settings.Secure.putInt(mContext.getContentResolver(),
        setDoubleTwistPreference(mContext, mUserManager, enabled);
        return true;
    }

    public static void setDoubleTwistPreference(Context context, UserManager userManager,
            int enabled) {
        Settings.Secure.putInt(context.getContentResolver(),
                Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, enabled);
        final int managedProfileUserId = getManagedProfileUserId();
        final int managedProfileUserId = getManagedProfileId(userManager);
        if (managedProfileUserId != UserHandle.USER_NULL) {
            Settings.Secure.putIntForUser(mContext.getContentResolver(),
            Settings.Secure.putIntForUser(context.getContentResolver(),
                Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, enabled, managedProfileUserId);
        }
        return true;
    }

    @Override
    protected boolean isSwitchPrefEnabled() {
        final int doubleTwistEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
                Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 1);
                Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, ON);
        return doubleTwistEnabled != 0;
    }

    @VisibleForTesting
    int getManagedProfileUserId() {
        return Utils.getManagedProfileId(mUserManager, UserHandle.myUserId());
    public static int getManagedProfileId(UserManager userManager) {
        return Utils.getManagedProfileId(userManager, UserHandle.myUserId());
    }
}
+6 −3
Original line number Diff line number Diff line
@@ -41,11 +41,12 @@ import java.util.List;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

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

@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class DoubleTwistPreferenceControllerTest {
@@ -102,14 +103,16 @@ public class DoubleTwistPreferenceControllerTest {
    }

    @Test
    @Config(shadows = {ShadowSecureSettings.class})
    @Config(shadows = {
            ShadowDoubleTwistPreferenceController.class,
            ShadowSecureSettings.class})
    public void onPreferenceChange_hasWorkProfile_shouldUpdateSettingForWorkProfileUser() {
        final int managedId = 2;
        ShadowSecureSettings.putIntForUser(
            null, Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 0, managedId);
        DoubleTwistPreferenceController controller =
            spy(new DoubleTwistPreferenceController(mContext, null, KEY_DOUBLE_TWIST));
        doReturn(managedId).when(controller).getManagedProfileUserId();
        ShadowDoubleTwistPreferenceController.setManagedProfileId(managedId);

        // enable the gesture
        controller.onPreferenceChange(null, true);
+28 −0
Original line number Diff line number Diff line
package com.android.settings.testutils.shadow;

import android.content.Context;
import android.os.UserHandle;
import android.os.UserManager;

import com.android.settings.gestures.DoubleTwistPreferenceController;

import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

@Implements(DoubleTwistPreferenceController.class)
public class ShadowDoubleTwistPreferenceController {
    private static int sManagedProfileId = UserHandle.USER_NULL;
    @Implementation
    public static boolean isGestureAvailable(Context context) {
        return true;
    }

    @Implementation
    public static int getManagedProfileId(UserManager userManager) {
        return sManagedProfileId;
    }

    public static void setManagedProfileId(int managedProfileId) {
        sManagedProfileId = managedProfileId;
    }
}