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

Commit 84522e05 authored by Daniel Nishi's avatar Daniel Nishi
Browse files

Unify automatic storage management enabled checks.

By putting it in SettingsLib, it should be accessible to every app that
needs it.

This also adds in the ability for the framework to define default
behavior. The default behavior for the storage manager enabled status
may differ based upon the device.

Bug: 70893105
Test: SettingsLib Robotests
Change-Id: I9082a77f90289b8fa208c628dd3606000e19495b
parent fbe9d430
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -3316,4 +3316,5 @@
    <string-array name="config_wearActivityModeRadios">
        <item>"wifi"</item>
    </string-array>

</resources>
+1 −0
Original line number Diff line number Diff line
@@ -3262,4 +3262,5 @@

  <java-symbol type="string" name="zen_upgrade_notification_title" />
  <java-symbol type="string" name="zen_upgrade_notification_content" />

</resources>
+29 −8
Original line number Diff line number Diff line
package com.android.settingslib;

import android.annotation.ColorInt;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
@@ -17,10 +18,13 @@ import android.graphics.drawable.Drawable;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.os.BatteryManager;
import android.os.SystemProperties;
import android.os.UserHandle;
import android.os.UserManager;
import android.print.PrintManager;
import android.provider.Settings;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.UserIcons;
import com.android.settingslib.drawable.UserIconDrawable;
import com.android.settingslib.wrapper.LocationManagerWrapper;
@@ -30,6 +34,9 @@ public class Utils {

    private static final String CURRENT_MODE_KEY = "CURRENT_MODE";
    private static final String NEW_MODE_KEY = "NEW_MODE";
    @VisibleForTesting
    static final String STORAGE_MANAGER_SHOW_OPT_IN_PROPERTY =
            "ro.storage_manager.show_opt_in";

    private static Signature[] sSystemSignature;
    private static String sPermissionControllerPackageName;
@@ -312,7 +319,6 @@ public class Utils {
     * Returns the Wifi icon resource for a given RSSI level.
     *
     * @param level The number of bars to show (0-4)
     *
     * @throws IllegalArgumentException if an invalid RSSI level is given.
     */
    public static int getWifiIconResource(int level) {
@@ -342,4 +348,19 @@ public class Utils {
        return !context.getSystemService(ConnectivityManager.class)
                .isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
    }

    /** Returns if the automatic storage management feature is turned on or not. **/
    public static boolean isStorageManagerEnabled(Context context) {
        boolean isDefaultOn;
        try {
            // Turn off by default if the opt-in was shown.
            isDefaultOn = !SystemProperties.getBoolean(STORAGE_MANAGER_SHOW_OPT_IN_PROPERTY, true);
        } catch (Resources.NotFoundException e) {
            isDefaultOn = false;
        }
        return Settings.Secure.getInt(context.getContentResolver(),
                Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED,
                isDefaultOn ? 1 : 0)
                != 0;
    }
}
+11 −1
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@
package com.android.settingslib;

import static android.Manifest.permission.WRITE_SECURE_SETTINGS;

import static com.android.settingslib.Utils.STORAGE_MANAGER_SHOW_OPT_IN_PROPERTY;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Matchers.eq;
@@ -30,6 +33,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.location.LocationManager;
import android.os.SystemProperties;
import android.os.UserHandle;
import android.provider.Settings;
import android.provider.Settings.Secure;
@@ -136,7 +140,7 @@ public class UtilsTest {
    }

    @Test
    public void testStorageManagerDaysToRetainUsesResources() {
    public void testGetDefaultStorageManagerDaysToRetain_storageManagerDaysToRetainUsesResources() {
        Resources resources = mock(Resources.class);
        when(resources.getInteger(
                        eq(
@@ -149,6 +153,12 @@ public class UtilsTest {
        assertThat(Utils.getDefaultStorageManagerDaysToRetain(resources)).isEqualTo(60);
    }

    @Test
    public void testIsStorageManagerEnabled_UsesSystemProperties() {
        SystemProperties.set(STORAGE_MANAGER_SHOW_OPT_IN_PROPERTY, "false");
        assertThat(Utils.isStorageManagerEnabled(mContext)).isTrue();
    }

    private static ArgumentMatcher<Intent> actionMatches(String expected) {
        return intent -> TextUtils.equals(expected, intent.getAction());
    }