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

Commit 95264455 authored by Annie Meng's avatar Annie Meng
Browse files

Fix accessibility_display_magnification_scale restore

accessibility_display_magnification_scale has a default value set, so
the current check to see if it was already configured during restore
always falsely returned true. Now, we compare it to the default value
and only say it's configured if different from the default.

Bug: 72737403
Test: 1) Manual d2d restore:
a) Set non-default value on source -> correctly restores on target
b) Default value on source -> correctly has default value on target
c) Set non-default value in SUW during restore -> correctly does not
override with value from source
2) Unit: atest SettingsHelperRestoreTest

Change-Id: Ie0670aac7b4ce806ac7b8baef4eb15a7c40d5919
parent 444ef94f
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -3,6 +3,8 @@ include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res

LOCAL_SRC_FILES := $(call all-java-files-under, src) \
    src/com/android/providers/settings/EventLogTags.logtags

+8 −6
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package com.android.providers.settings;

import android.os.Process;
import com.android.internal.R;
import com.android.internal.app.LocalePicker;
import com.android.internal.annotations.VisibleForTesting;

@@ -30,13 +29,11 @@ import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.hardware.display.DisplayManager;
import android.icu.util.ULocale;
import android.location.LocationManager;
import android.media.AudioManager;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.IPowerManager;
import android.os.LocaleList;
import android.os.RemoteException;
import android.os.ServiceManager;
@@ -46,16 +43,16 @@ import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.ArraySet;
import android.util.Slog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

public class SettingsHelper {
    private static final String TAG = "SettingsHelper";
    private static final String SILENT_RINGTONE = "_silent";
    private static final float FLOAT_TOLERANCE = 0.01f;

    private Context mContext;
    private AudioManager mAudioManager;
    private TelephonyManager mTelephonyManager;
@@ -259,9 +256,14 @@ public class SettingsHelper {
            case Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES:
            case Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES:
            case Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER:
            case Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_SCALE:
                return !TextUtils.isEmpty(Settings.Secure.getString(
                        mContext.getContentResolver(), name));
            case Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_SCALE:
                float defaultScale = mContext.getResources().getFraction(
                        R.fraction.def_accessibility_display_magnification_scale, 1, 1);
                float currentScale = Settings.Secure.getFloat(
                        mContext.getContentResolver(), name, defaultScale);
                return Math.abs(currentScale - defaultScale) >= FLOAT_TOLERANCE;
            case Settings.System.FONT_SCALE:
                return Settings.System.getFloat(mContext.getContentResolver(), name, 1.0f) != 1.0f;
            default:
+4 −0
Original line number Diff line number Diff line
@@ -14,6 +14,10 @@ LOCAL_STATIC_JAVA_LIBRARIES := android-support-test

LOCAL_JAVA_LIBRARIES := android.test.base

LOCAL_RESOURCE_DIR := frameworks/base/packages/SettingsProvider/res

LOCAL_AAPT_FLAGS += --auto-add-overlay --extra-packages com.android.providers.settings

LOCAL_PACKAGE_NAME := SettingsProviderTest
LOCAL_PRIVATE_PLATFORM_APIS := true

+121 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.providers.settings;

import static junit.framework.Assert.assertEquals;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Tests for {@link SettingsHelper#restoreValue(Context, ContentResolver, ContentValues, Uri,
 * String, String, int)}. Specifically verifies that we restore critical accessibility settings only
 * if the user has not already configured these in SUW.
 */
@RunWith(AndroidJUnit4.class)
public class SettingsHelperRestoreTest {
    private static final float FLOAT_TOLERANCE = 0.01f;

    private Context mContext;
    private ContentResolver mContentResolver;
    private SettingsHelper mSettingsHelper;

    @Before
    public void setUp() {
        mContext = InstrumentationRegistry.getContext();
        mContentResolver = mContext.getContentResolver();
        mSettingsHelper = new SettingsHelper(mContext);
    }

    /** Tests for {@link Settings.Secure#ACCESSIBILITY_DISPLAY_MAGNIFICATION_SCALE}. */
    @Test
    public void
            testRestoreAccessibilityDisplayMagnificationScale_alreadyConfigured_doesNotRestoreValue()
                    throws Exception {
        float defaultSettingValue = setDefaultAccessibilityDisplayMagnificationScale();
        String settingName = Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_SCALE;
        float restoreSettingValue = defaultSettingValue + 0.5f;

        // Simulate already configuring setting via SUW.
        float configuredSettingValue = defaultSettingValue + 1.0f;
        Settings.Secure.putFloat(mContentResolver, settingName, configuredSettingValue);

        mSettingsHelper.restoreValue(
                mContext,
                mContentResolver,
                new ContentValues(2),
                Settings.Secure.getUriFor(settingName),
                settingName,
                String.valueOf(restoreSettingValue),
                Build.VERSION.SDK_INT);

        assertEquals(
                configuredSettingValue,
                Settings.Secure.getFloat(mContentResolver, settingName),
                FLOAT_TOLERANCE);
    }

    @Test
    public void
            testRestoreAccessibilityDisplayMagnificationScale_notAlreadyConfigured_restoresValue()
                    throws Exception {
        float defaultSettingValue = setDefaultAccessibilityDisplayMagnificationScale();
        String settingName = Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_SCALE;
        float restoreSettingValue = defaultSettingValue + 0.5f;

        mSettingsHelper.restoreValue(
                mContext,
                mContentResolver,
                new ContentValues(2),
                Settings.Secure.getUriFor(settingName),
                settingName,
                String.valueOf(restoreSettingValue),
                Build.VERSION.SDK_INT);

        assertEquals(
                restoreSettingValue,
                Settings.Secure.getFloat(mContentResolver, settingName),
                FLOAT_TOLERANCE);
    }

    /**
     * Simulate {@link Settings.Secure#ACCESSIBILITY_DISPLAY_MAGNIFICATION_SCALE} value at boot by
     * loading the default.
     *
     * @return the default value.
     */
    private float setDefaultAccessibilityDisplayMagnificationScale() {
        float defaultSettingValue =
                mContext.getResources()
                        .getFraction(
                                R.fraction.def_accessibility_display_magnification_scale, 1, 1);
        Settings.Secure.putFloat(
                mContentResolver,
                Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_SCALE,
                defaultSettingValue);
        return defaultSettingValue;
    }
}