Add data class PreferredShortcut to replace inner class UserShortcutType
* Add basic function equals(), hashcode() into data class * Change flatternToString() to toString() * Change constructor to fromString(flatternToString) Bug: 158540780 Test: atest PreferredShortcutTest Change-Id: I0ee46dd940d22ff9f168b95fe75d9cff2f0fddfb
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
@@ -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();
|
||||
}
|
||||
|
||||
|
@@ -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();
|
||||
}
|
||||
|
||||
|
@@ -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()));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user