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

Commit d0d07ddc authored by Fabian Kozynski's avatar Fabian Kozynski
Browse files

Use PackageManager.FEATURE_CONTROLS to decide availability

Using this features, some of the Preference in Power Menu Settings can
change their availability and their summary.

Test: Robotest Settings
Fixes: 157244528
Change-Id: I704438dda181aa6347c3f168ac5ef6bd16148993
parent 5c7f6d1b
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -12056,6 +12056,9 @@
    <!-- Power menu setting privacy show controls [CHAR LIMIT=NONE] -->
    <string name="power_menu_privacy_show_controls">Show controls when locked</string>
    <!-- Power menu setting privacy show cards [CHAR LIMIT=NONE] -->
    <string name="power_menu_privacy_show_cards">Show cards when locked</string>
    <!-- Power menu setting privacy hide all [CHAR LIMIT=NONE] -->
    <string name="power_menu_privacy_hide">Hide cards and controls when locked</string>
+4 −1
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.settings.gestures;

import android.content.Context;
import android.content.pm.PackageManager;
import android.provider.Settings;
import android.text.TextUtils;

@@ -37,7 +38,9 @@ public class DeviceControlsPreferenceController extends GesturePreferenceControl

    @Override
    public int getAvailabilityStatus() {
        return AVAILABLE;
        boolean available = mContext.getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_CONTROLS);
        return available ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
    }

    @Override
+19 −9
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.settings.gestures;

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

import com.android.settings.R;
@@ -37,15 +38,15 @@ public class PowerMenuPreferenceController extends BasePreferenceController {

    @Override
    public CharSequence getSummary() {
        boolean controlsEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
        boolean controlsVisible = isControlsAvailable()
                && Settings.Secure.getInt(mContext.getContentResolver(),
                        CONTROLS_ENABLED_SETTING, 1) == 1;
        boolean cardsEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
        boolean cardsVisible = isCardsAvailable()
                && Settings.Secure.getInt(mContext.getContentResolver(),
                        CARDS_ENABLED_SETTING, 0) == 1;
        boolean cardsVisible = cardsEnabled && Settings.Secure.getInt(mContext.getContentResolver(),
                CARDS_AVAILABLE_SETTING, 0) == 1;
        if (controlsEnabled && cardsVisible) {
        if (controlsVisible && cardsVisible) {
            return mContext.getText(R.string.power_menu_cards_passes_device_controls);
        } else if (controlsEnabled) {
        } else if (controlsVisible) {
            return mContext.getText(R.string.power_menu_device_controls);
        } else if (cardsVisible) {
            return mContext.getText(R.string.power_menu_cards_passes);
@@ -56,6 +57,15 @@ public class PowerMenuPreferenceController extends BasePreferenceController {

    @Override
    public int getAvailabilityStatus() {
        return AVAILABLE;
        return isCardsAvailable() || isControlsAvailable() ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
    }

    private boolean isControlsAvailable() {
        return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CONTROLS);
    }

    private boolean isCardsAvailable() {
        return Settings.Secure.getInt(mContext.getContentResolver(),
                CARDS_AVAILABLE_SETTING, 0) == 1;
    }
}
+15 −3
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.settings.gestures;

import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.UserHandle;
import android.provider.Settings;

@@ -57,13 +58,20 @@ public class PowerMenuPrivacyPreferenceController extends TogglePreferenceContro
    public CharSequence getSummary() {
        boolean cardsAvailable = Settings.Secure.getInt(mContext.getContentResolver(),
                CARDS_AVAILABLE_KEY, 0) != 0;
        boolean controlsAvailable = isControlsAvailable();
        final int res;
        if (!isSecure()) {
            res = R.string.power_menu_privacy_not_secure;
        } else if (cardsAvailable) {
        } else if (cardsAvailable && controlsAvailable) {
            res = R.string.power_menu_privacy_show;
        } else {
        } else if (!cardsAvailable && controlsAvailable) {
            res = R.string.power_menu_privacy_show_controls;
        } else if (cardsAvailable) {
            res = R.string.power_menu_privacy_show_cards;
        } else {
            // In this case, neither cards nor controls are available. This preference should not
            // be accessible as the power menu setting is not accessible
            return "";
        }
        return mContext.getText(res);
    }
@@ -87,7 +95,7 @@ public class PowerMenuPrivacyPreferenceController extends TogglePreferenceContro
        boolean cardsAvailable = Settings.Secure.getInt(resolver, CARDS_AVAILABLE_KEY, 0) != 0;
        boolean cardsEnabled = Settings.Secure.getInt(resolver, CARDS_ENABLED_KEY, 0) != 0;
        boolean controlsEnabled = Settings.Secure.getInt(resolver, CONTROLS_ENABLED_KEY, 1) != 0;
        return (cardsAvailable && cardsEnabled) || controlsEnabled;
        return (cardsAvailable && cardsEnabled) || (isControlsAvailable() && controlsEnabled);
    }

    private boolean isSecure() {
@@ -97,4 +105,8 @@ public class PowerMenuPrivacyPreferenceController extends TogglePreferenceContro
        int userId = UserHandle.myUserId();
        return utils.isSecure(userId);
    }

    private boolean isControlsAvailable() {
        return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CONTROLS);
    }
}
+16 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.settings.gestures;
import static com.google.common.truth.Truth.assertThat;

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

import com.android.settings.core.BasePreferenceController;
@@ -28,12 +29,15 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.shadows.ShadowPackageManager;

@RunWith(RobolectricTestRunner.class)
public class DeviceControlsPreferenceControllerTest {

    private Context mContext;
    private DeviceControlsPreferenceController mController;
    private ShadowPackageManager mShadowPackageManager;

    private static final String KEY_GESTURE_PANEL = "gesture_device_controls";
    private static final String ENABLED_SETTING =
@@ -42,6 +46,7 @@ public class DeviceControlsPreferenceControllerTest {
    @Before
    public void setUp() {
        mContext = RuntimeEnvironment.application;
        mShadowPackageManager = Shadows.shadowOf(mContext.getPackageManager());
        mController = new DeviceControlsPreferenceController(mContext, KEY_GESTURE_PANEL);
    }

@@ -60,11 +65,21 @@ public class DeviceControlsPreferenceControllerTest {
    }

    @Test
    public void getAvailabilityStatus_panelAvailable() {
    public void getAvailabilityStatus_hasSystemFeature_panelAvailable() {
        mShadowPackageManager.setSystemFeature(PackageManager.FEATURE_CONTROLS, true);

        assertThat(mController.getAvailabilityStatus())
                .isEqualTo(BasePreferenceController.AVAILABLE);
    }

    @Test
    public void getAvailabilityStatus_hasntSystemFeature_panelUnsupported() {
        mShadowPackageManager.setSystemFeature(PackageManager.FEATURE_CONTROLS, false);

        assertThat(mController.getAvailabilityStatus())
                .isEqualTo(BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
    }

    @Test
    public void isSliceable_correctKey() {
        final DeviceControlsPreferenceController controller =
Loading