Extract magnification capabilities from MagnificationSettingsFragment into a new class.
* To increase single responsibility of MagnificationSettingsFragment
Bug: 168567356
Test: atest MagnificationCapabilitiesTest
Change-Id: Ic5e8a556858ac2b6ab9929b1bbdc373ce320c270
diff --git a/src/com/android/settings/accessibility/MagnificationCapabilities.java b/src/com/android/settings/accessibility/MagnificationCapabilities.java
new file mode 100644
index 0000000..079b06c
--- /dev/null
+++ b/src/com/android/settings/accessibility/MagnificationCapabilities.java
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2020 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.settings.accessibility;
+
+import android.content.ContentResolver;
+import android.content.Context;
+import android.provider.Settings;
+
+import androidx.annotation.IntDef;
+
+import com.android.settings.R;
+
+import com.google.common.primitives.Ints;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+/** Class to provide magnification capabilities. */
+public final class MagnificationCapabilities {
+
+ private static final String KEY_CAPABILITY =
+ Settings.Secure.ACCESSIBILITY_MAGNIFICATION_CAPABILITY;
+
+ /**
+ * Annotation for supported magnification mode.
+ *
+ * @see Settings.Secure#ACCESSIBILITY_MAGNIFICATION_CAPABILITY
+ */
+ @Retention(RetentionPolicy.SOURCE)
+ @IntDef({
+ MagnificationMode.FULLSCREEN,
+ MagnificationMode.WINDOW,
+ MagnificationMode.ALL,
+ })
+
+ public @interface MagnificationMode {
+ int FULLSCREEN = Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_FULLSCREEN;
+ int WINDOW = Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_WINDOW;
+ int ALL = Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_ALL;
+ }
+
+ /**
+ * Gets the summary for the given {@code capabilities}.
+ *
+ * @param context A {@link Context}.
+ * @param capabilities Magnification capabilities {@link MagnificationMode}
+ * @return The summary text represents the given capabilities
+ */
+ public static String getSummary(Context context, @MagnificationMode int capabilities) {
+ final String[] summaries = context.getResources().getStringArray(
+ R.array.magnification_mode_summaries);
+ final int[] values = context.getResources().getIntArray(
+ R.array.magnification_mode_values);
+
+ final int idx = Ints.indexOf(values, capabilities);
+ return summaries[idx == /* no index exist */ -1 ? 0 : idx];
+ }
+
+ /**
+ * Sets the magnification capabilities {@link MagnificationMode} to settings key. This
+ * overwrites any existing capabilities.
+ *
+ * @param context A {@link Context}.
+ * @param capabilities Magnification capabilities {@link MagnificationMode}
+ */
+ public static void setCapabilities(Context context, @MagnificationMode int capabilities) {
+ final ContentResolver contentResolver = context.getContentResolver();
+
+ Settings.Secure.putIntForUser(contentResolver, KEY_CAPABILITY, capabilities,
+ contentResolver.getUserId());
+ }
+
+ /**
+ * Returns the magnification capabilities {@link MagnificationMode} from setting's key. May be
+ * default value {@link MagnificationMode#FULLSCREEN} if not set.
+ *
+ * @param context A {@link Context}.
+ * @return The magnification capabilities {@link MagnificationMode}
+ */
+ @MagnificationMode
+ public static int getCapabilities(Context context) {
+ final ContentResolver contentResolver = context.getContentResolver();
+
+ return Settings.Secure.getIntForUser(contentResolver, KEY_CAPABILITY,
+ MagnificationMode.FULLSCREEN, contentResolver.getUserId());
+ }
+
+ private MagnificationCapabilities() {}
+}
diff --git a/src/com/android/settings/accessibility/MagnificationModePreferenceController.java b/src/com/android/settings/accessibility/MagnificationModePreferenceController.java
index b45ad88..5dc93b1 100644
--- a/src/com/android/settings/accessibility/MagnificationModePreferenceController.java
+++ b/src/com/android/settings/accessibility/MagnificationModePreferenceController.java
@@ -35,7 +35,7 @@
@Override
public CharSequence getSummary() {
- return MagnificationSettingsFragment.getMagnificationCapabilitiesSummary(
- mContext);
+ final int capabilities = MagnificationCapabilities.getCapabilities(mContext);
+ return MagnificationCapabilities.getSummary(mContext, capabilities);
}
}
diff --git a/src/com/android/settings/accessibility/MagnificationSettingsFragment.java b/src/com/android/settings/accessibility/MagnificationSettingsFragment.java
index 6b91fab..6ea6366 100644
--- a/src/com/android/settings/accessibility/MagnificationSettingsFragment.java
+++ b/src/com/android/settings/accessibility/MagnificationSettingsFragment.java
@@ -22,7 +22,6 @@
import android.app.Dialog;
import android.app.settings.SettingsEnums;
-import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.provider.Settings;
@@ -38,8 +37,6 @@
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.SearchIndexable;
-import com.google.common.primitives.Ints;
-
import java.util.StringJoiner;
/** Settings page for magnification. */
@@ -48,8 +45,6 @@
private static final String TAG = "MagnificationSettingsFragment";
private static final String PREF_KEY_MODE = "magnification_mode";
- private static final String KEY_CAPABILITY =
- Settings.Secure.ACCESSIBILITY_MAGNIFICATION_CAPABILITY;
private static final int DIALOG_MAGNIFICATION_CAPABILITY = 1;
private static final int DIALOG_MAGNIFICATION_SWITCH_SHORTCUT = 2;
private static final String EXTRA_CAPABILITY = "capability";
@@ -61,29 +56,6 @@
private CheckBox mMagnifyWindowCheckBox;
private Dialog mDialog;
- static String getMagnificationCapabilitiesSummary(Context context) {
- final String[] magnificationModeSummaries = context.getResources().getStringArray(
- R.array.magnification_mode_summaries);
- final int[] magnificationModeValues = context.getResources().getIntArray(
- R.array.magnification_mode_values);
- final int capabilities = MagnificationSettingsFragment.getMagnificationCapabilities(
- context);
-
- final int idx = Ints.indexOf(magnificationModeValues, capabilities);
- return magnificationModeSummaries[idx == -1 ? 0 : idx];
- }
-
- private static int getMagnificationCapabilities(Context context) {
- return getSecureIntValue(context, KEY_CAPABILITY,
- Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_FULLSCREEN);
- }
-
- private static int getSecureIntValue(Context context, String key, int defaultValue) {
- return Settings.Secure.getIntForUser(
- context.getContentResolver(),
- key, defaultValue, context.getContentResolver().getUserId());
- }
-
@Override
public int getMetricsCategory() {
return SettingsEnums.ACCESSIBILITY_MAGNIFICATION_SETTINGS;
@@ -102,7 +74,7 @@
mCapabilities = savedInstanceState.getInt(EXTRA_CAPABILITY, NONE);
}
if (mCapabilities == NONE) {
- mCapabilities = getMagnificationCapabilities(getPrefContext());
+ mCapabilities = MagnificationCapabilities.getCapabilities(getPrefContext());
}
}
@@ -128,7 +100,7 @@
super.onCreate(icicle);
mModePreference = findPreference(PREF_KEY_MODE);
mModePreference.setOnPreferenceClickListener(preference -> {
- mCapabilities = getMagnificationCapabilities(getPrefContext());
+ mCapabilities = MagnificationCapabilities.getCapabilities(getPrefContext());
showDialog(DIALOG_MAGNIFICATION_CAPABILITY);
return true;
});
@@ -164,7 +136,7 @@
private void callOnAlertDialogCheckboxClicked(DialogInterface dialog, int which) {
updateCapabilities(true);
mModePreference.setSummary(
- getMagnificationCapabilitiesSummary(getPrefContext()));
+ MagnificationCapabilities.getSummary(getPrefContext(), mCapabilities));
}
private void onSwitchShortcutDialogPositiveButtonClicked(View view) {
@@ -285,7 +257,7 @@
? Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_WINDOW : 0;
mCapabilities = capabilities;
if (saveToDB) {
- setMagnificationCapabilities(capabilities);
+ MagnificationCapabilities.setCapabilities(getPrefContext(), mCapabilities);
}
}
@@ -294,15 +266,6 @@
Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED, OFF) == ON;
}
- private void setSecureIntValue(String key, int value) {
- Settings.Secure.putIntForUser(getPrefContext().getContentResolver(),
- key, value, getPrefContext().getContentResolver().getUserId());
- }
-
- private void setMagnificationCapabilities(int capabilities) {
- setSecureIntValue(KEY_CAPABILITY, capabilities);
- }
-
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.accessibility_magnification_service_settings);
}
diff --git a/tests/robotests/src/com/android/settings/accessibility/MagnificationCapabilitiesTest.java b/tests/robotests/src/com/android/settings/accessibility/MagnificationCapabilitiesTest.java
new file mode 100644
index 0000000..ac7f073
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/accessibility/MagnificationCapabilitiesTest.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2020 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.settings.accessibility;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.content.Context;
+
+import androidx.test.core.app.ApplicationProvider;
+
+import com.android.settings.R;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+
+/** Tests for {@link MagnificationCapabilities} */
+@RunWith(RobolectricTestRunner.class)
+public final class MagnificationCapabilitiesTest {
+
+ private final Context mContext = ApplicationProvider.getApplicationContext();
+
+ @Test
+ public void getCapabilities_windowMode_expectedCapabilities() {
+ MagnificationCapabilities.setCapabilities(mContext,
+ MagnificationCapabilities.MagnificationMode.WINDOW);
+
+ final int windowCapabilities = MagnificationCapabilities.getCapabilities(mContext);
+ assertThat(windowCapabilities).isEqualTo(
+ MagnificationCapabilities.MagnificationMode.WINDOW);
+
+ }
+
+ @Test
+ public void getSummary_fullScreenMode_expectedSummary() {
+ final int fullScreenCapabilities = MagnificationCapabilities.MagnificationMode.FULLSCREEN;
+
+ final String actualString = MagnificationCapabilities.getSummary(mContext,
+ fullScreenCapabilities);
+
+ final String expectedString = mContext.getString(
+ R.string.accessibility_magnification_area_settings_full_screen_summary);
+ assertThat(actualString).isEqualTo(expectedString);
+ }
+}