diff --git a/src/com/android/settings/accessibility/PreferredShortcut.java b/src/com/android/settings/accessibility/PreferredShortcut.java new file mode 100644 index 00000000000..1654992c8b3 --- /dev/null +++ b/src/com/android/settings/accessibility/PreferredShortcut.java @@ -0,0 +1,96 @@ +/* + * 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.ComponentName; +import android.text.TextUtils; + +import com.android.settings.accessibility.AccessibilityUtil.UserShortcutType; + +import com.google.common.base.Objects; + +/** + * A data class for containing {@link ComponentName#flattenToString()} and + * {@link UserShortcutType}. Represents the preferred shortcuts of the service or activity. + */ +public class PreferredShortcut { + + private static final char COMPONENT_NAME_SEPARATOR = ':'; + private static final TextUtils.SimpleStringSplitter sStringColonSplitter = + new TextUtils.SimpleStringSplitter(COMPONENT_NAME_SEPARATOR); + + /** + * Creates a {@link PreferredShortcut} from a encoded string described in {@link #toString()}. + * + * @param preferredShortcutString A string conform to the format described in {@link + * #toString()} + * @return A {@link PreferredShortcut} with the specified value + * @throws IllegalArgumentException If preferredShortcutString does not conform to the format + * described in {@link #toString()} + */ + public static PreferredShortcut fromString(String preferredShortcutString) { + sStringColonSplitter.setString(preferredShortcutString); + if (sStringColonSplitter.hasNext()) { + final String componentName = sStringColonSplitter.next(); + final int type = Integer.parseInt(sStringColonSplitter.next()); + return new PreferredShortcut(componentName, type); + } + + throw new IllegalArgumentException( + "Invalid PreferredShortcut string: " + preferredShortcutString); + } + + /** The format of {@link ComponentName#flattenToString()} */ + private String mComponentName; + /** The format of {@link UserShortcutType} */ + private int mType; + + public PreferredShortcut(String componentName, int type) { + mComponentName = componentName; + mType = type; + } + + public String getComponentName() { + return mComponentName; + } + + public int getType() { + return mType; + } + + @Override + public String toString() { + return mComponentName + COMPONENT_NAME_SEPARATOR + mType; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + PreferredShortcut that = (PreferredShortcut) o; + return mType == that.mType && Objects.equal(mComponentName, that.mComponentName); + } + + @Override + public int hashCode() { + return Objects.hashCode(mComponentName, mType); + } +} diff --git a/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java b/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java index 88f58be066c..0f16136b6af 100644 --- a/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java +++ b/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java @@ -66,7 +66,6 @@ import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Set; -import java.util.StringJoiner; import java.util.stream.Collectors; /** @@ -481,51 +480,6 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference } } - static final class AccessibilityUserShortcutType { - private static final char COMPONENT_NAME_SEPARATOR = ':'; - private static final TextUtils.SimpleStringSplitter sStringColonSplitter = - new TextUtils.SimpleStringSplitter(COMPONENT_NAME_SEPARATOR); - - private String mComponentName; - private int mType; - - AccessibilityUserShortcutType(String componentName, int type) { - this.mComponentName = componentName; - this.mType = type; - } - - AccessibilityUserShortcutType(String flattenedString) { - sStringColonSplitter.setString(flattenedString); - if (sStringColonSplitter.hasNext()) { - this.mComponentName = sStringColonSplitter.next(); - this.mType = Integer.parseInt(sStringColonSplitter.next()); - } - } - - String getComponentName() { - return mComponentName; - } - - void setComponentName(String componentName) { - this.mComponentName = componentName; - } - - int getType() { - return mType; - } - - void setType(int type) { - this.mType = type; - } - - String flattenToString() { - final StringJoiner joiner = new StringJoiner(String.valueOf(COMPONENT_NAME_SEPARATOR)); - joiner.add(mComponentName); - joiner.add(String.valueOf(mType)); - return joiner.toString(); - } - } - private void setDialogTextAreaClickListener(View dialogView, CheckBox checkBox) { final View dialogTextArea = dialogView.findViewById(R.id.container); dialogTextArea.setOnClickListener(v -> { @@ -592,9 +546,8 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference .collect(Collectors.toSet()); info.removeAll(filtered); } - final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType( - componentName, type); - info.add(shortcut.flattenToString()); + final PreferredShortcut shortcut = new PreferredShortcut(componentName, type); + info.add(shortcut.toString()); SharedPreferenceUtils.setUserShortcutType(context, info); } @@ -651,7 +604,7 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference } final String str = (String) filtered.toArray()[0]; - final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType(str); + final PreferredShortcut shortcut = PreferredShortcut.fromString(str); return shortcut.getType(); } diff --git a/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java b/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java index 108c1c0405e..a5a20e102bd 100644 --- a/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java +++ b/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java @@ -226,9 +226,9 @@ public class ToggleScreenMagnificationPreferenceFragment extends Collectors.toSet()); info.removeAll(filtered); } - final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType( - MAGNIFICATION_CONTROLLER_NAME, type); - info.add(shortcut.flattenToString()); + final PreferredShortcut shortcut = new PreferredShortcut(MAGNIFICATION_CONTROLLER_NAME, + type); + info.add(shortcut.toString()); SharedPreferenceUtils.setUserShortcutType(context, info); } @@ -284,7 +284,7 @@ public class ToggleScreenMagnificationPreferenceFragment extends } final String str = (String) filtered.toArray()[0]; - final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType(str); + final PreferredShortcut shortcut = PreferredShortcut.fromString(str); return shortcut.getType(); } diff --git a/src/com/android/settings/accessibility/VolumeShortcutToggleAccessibilityServicePreferenceFragment.java b/src/com/android/settings/accessibility/VolumeShortcutToggleAccessibilityServicePreferenceFragment.java index 31f97a8f600..0f04b3bd752 100644 --- a/src/com/android/settings/accessibility/VolumeShortcutToggleAccessibilityServicePreferenceFragment.java +++ b/src/com/android/settings/accessibility/VolumeShortcutToggleAccessibilityServicePreferenceFragment.java @@ -67,10 +67,10 @@ public class VolumeShortcutToggleAccessibilityServicePreferenceFragment extends } private void setAllowedPreferredShortcutType(int type) { - final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType( - mComponentName.flattenToString(), type); + final String componentNameString = mComponentName.flattenToString(); + final PreferredShortcut shortcut = new PreferredShortcut(componentNameString, type); SharedPreferenceUtils.setUserShortcutType(getPrefContext(), - ImmutableSet.of(shortcut.flattenToString())); + ImmutableSet.of(shortcut.toString())); } } diff --git a/tests/robotests/src/com/android/settings/accessibility/PreferredShortcutTest.java b/tests/robotests/src/com/android/settings/accessibility/PreferredShortcutTest.java new file mode 100644 index 00000000000..e7d09963ed4 --- /dev/null +++ b/tests/robotests/src/com/android/settings/accessibility/PreferredShortcutTest.java @@ -0,0 +1,63 @@ +/* + * 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.ComponentName; + +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +/** Tests for {@link PreferredShortcut} */ +@RunWith(AndroidJUnit4.class) +public class PreferredShortcutTest { + + private static final String STUB_COMPONENT_NAME = new ComponentName("com.example", + "com.example.testActivity").flattenToString(); + private static final int STUB_TYPE = 3; + + @Test + public void fromString_matchMemberObject() { + final String preferredShortcutString = STUB_COMPONENT_NAME + ":" + STUB_TYPE; + + final PreferredShortcut shortcut = PreferredShortcut.fromString(preferredShortcutString); + + assertThat(shortcut.getComponentName()).isEqualTo(STUB_COMPONENT_NAME); + assertThat(shortcut.getType()).isEqualTo(STUB_TYPE); + } + + @Test + public void toString_matchString() { + final PreferredShortcut shortcut = new PreferredShortcut(STUB_COMPONENT_NAME, STUB_TYPE); + + final String preferredShortcutString = shortcut.toString(); + + assertThat(preferredShortcutString).isEqualTo(STUB_COMPONENT_NAME + ":" + STUB_TYPE); + } + + @Test + public void assertSameObject() { + final String preferredShortcutString = STUB_COMPONENT_NAME + ":" + STUB_TYPE; + final PreferredShortcut targetShortcut = PreferredShortcut.fromString( + preferredShortcutString); + + assertThat(targetShortcut).isEqualTo(new PreferredShortcut(STUB_COMPONENT_NAME, STUB_TYPE)); + } +} diff --git a/tests/robotests/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragmentTest.java b/tests/robotests/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragmentTest.java index f008b03a752..f7c94381981 100644 --- a/tests/robotests/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragmentTest.java +++ b/tests/robotests/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragmentTest.java @@ -43,7 +43,6 @@ import androidx.test.core.app.ApplicationProvider; import com.android.settings.R; import com.android.settings.accessibility.AccessibilityUtil.UserShortcutType; -import com.android.settings.accessibility.ToggleFeaturePreferenceFragment.AccessibilityUserShortcutType; import com.android.settings.testutils.shadow.ShadowFragment; import org.junit.Before; @@ -58,18 +57,11 @@ import org.robolectric.shadows.androidx.fragment.FragmentController; import java.util.Collections; import java.util.HashSet; import java.util.Set; -import java.util.stream.Collectors; /** Tests for {@link ToggleFeaturePreferenceFragment} */ @RunWith(RobolectricTestRunner.class) public class ToggleFeaturePreferenceFragmentTest { - private static final String TEST_SERVICE_KEY_1 = "abc:111"; - private static final String TEST_SERVICE_KEY_2 = "mno:222"; - private static final String TEST_SERVICE_KEY_3 = "xyz:333"; - private static final String TEST_SERVICE_NAME_1 = "abc"; - private static final int TEST_SERVICE_VALUE_1 = 111; - private static final String PLACEHOLDER_PACKAGE_NAME = "com.placeholder.example"; private static final String PLACEHOLDER_CLASS_NAME = PLACEHOLDER_PACKAGE_NAME + ".placeholder"; private static final ComponentName PLACEHOLDER_COMPONENT_NAME = new ComponentName( @@ -97,59 +89,6 @@ public class ToggleFeaturePreferenceFragmentTest { doReturn(null).when(mFragment).getPreferenceScreen(); } - @Test - public void a11yUserShortcutType_setConcatString_shouldReturnTargetValue() { - final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType( - TEST_SERVICE_KEY_1); - - assertThat(shortcut.getComponentName()).isEqualTo(TEST_SERVICE_NAME_1); - assertThat(shortcut.getType()).isEqualTo(TEST_SERVICE_VALUE_1); - } - - @Test - public void a11yUserShortcutType_shouldUpdateConcatString() { - final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType( - TEST_SERVICE_KEY_2); - - shortcut.setComponentName(TEST_SERVICE_NAME_1); - shortcut.setType(TEST_SERVICE_VALUE_1); - - assertThat(shortcut.flattenToString()).isEqualTo(TEST_SERVICE_KEY_1); - } - - @Test - public void stringSet_convertA11yPreferredShortcut_shouldRemoveTarget() { - Set mySet = new HashSet<>(); - mySet.add(TEST_SERVICE_KEY_1); - mySet.add(TEST_SERVICE_KEY_2); - mySet.add(TEST_SERVICE_KEY_3); - - final Set filtered = mySet.stream() - .filter(str -> str.contains(TEST_SERVICE_NAME_1)) - .collect(Collectors.toSet()); - mySet.removeAll(filtered); - - assertThat(mySet).doesNotContain(TEST_SERVICE_KEY_1); - assertThat(mySet).hasSize(/* expectedSize= */2); - } - - @Test - public void stringSet_convertA11yUserShortcutType_shouldReturnPreferredShortcut() { - Set mySet = new HashSet<>(); - mySet.add(TEST_SERVICE_KEY_1); - mySet.add(TEST_SERVICE_KEY_2); - mySet.add(TEST_SERVICE_KEY_3); - - final Set filtered = mySet.stream() - .filter(str -> str.contains(TEST_SERVICE_NAME_1)) - .collect(Collectors.toSet()); - - final String str = (String) filtered.toArray()[0]; - final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType(str); - final int type = shortcut.getType(); - assertThat(type).isEqualTo(TEST_SERVICE_VALUE_1); - } - @Test public void createFragment_shouldOnlyAddPreferencesOnce() { FragmentController.setupFragment(mFragment, FragmentActivity.class, @@ -184,7 +123,7 @@ public class ToggleFeaturePreferenceFragmentTest { @Test public void updateShortcutPreferenceData_hasValueInSharedPreference_assignToVariable() { mFragment.mComponentName = PLACEHOLDER_COMPONENT_NAME; - final AccessibilityUserShortcutType hardwareShortcut = new AccessibilityUserShortcutType( + final PreferredShortcut hardwareShortcut = new PreferredShortcut( PLACEHOLDER_COMPONENT_NAME.flattenToString(), UserShortcutType.HARDWARE); putUserShortcutTypeIntoSharedPreference(mContext, hardwareShortcut); @@ -219,8 +158,8 @@ public class ToggleFeaturePreferenceFragmentTest { } private void putUserShortcutTypeIntoSharedPreference(Context context, - AccessibilityUserShortcutType shortcut) { - Set value = new HashSet<>(Collections.singletonList(shortcut.flattenToString())); + PreferredShortcut shortcut) { + Set value = new HashSet<>(Collections.singletonList(shortcut.toString())); SharedPreferenceUtils.setUserShortcutType(context, value); } diff --git a/tests/robotests/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragmentTest.java b/tests/robotests/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragmentTest.java index 8bc3009d2fb..324ee594fe2 100644 --- a/tests/robotests/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragmentTest.java +++ b/tests/robotests/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragmentTest.java @@ -44,7 +44,6 @@ import androidx.preference.PreferenceManager; import androidx.test.core.app.ApplicationProvider; import com.android.settings.R; -import com.android.settings.accessibility.ToggleFeaturePreferenceFragment.AccessibilityUserShortcutType; import com.android.settings.testutils.shadow.ShadowFragment; import org.junit.Before; @@ -183,7 +182,7 @@ public class ToggleScreenMagnificationPreferenceFragmentTest { @Test public void updateShortcutPreferenceData_hasValueInSharedPreference_assignToVariable() { - final AccessibilityUserShortcutType tripleTapShortcut = new AccessibilityUserShortcutType( + final PreferredShortcut tripleTapShortcut = new PreferredShortcut( MAGNIFICATION_CONTROLLER_NAME, UserShortcutType.TRIPLETAP); putUserShortcutTypeIntoSharedPreference(mContext, tripleTapShortcut); @@ -217,8 +216,8 @@ public class ToggleScreenMagnificationPreferenceFragmentTest { } private void putUserShortcutTypeIntoSharedPreference(Context context, - AccessibilityUserShortcutType shortcut) { - Set value = new HashSet<>(Collections.singletonList(shortcut.flattenToString())); + PreferredShortcut shortcut) { + Set value = new HashSet<>(Collections.singletonList(shortcut.toString())); SharedPreferenceUtils.setUserShortcutType(context, value); }