Utils to operate tooltips widget for QS panel
Bug: 210356011 Test: make RunSettingsRoboTests ROBOTEST_FILTER=AccessibilityQuickSettingUtilsTest Change-Id: Ia743be8350e6a84cbe44827fb2d6f44f1f04e426
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
/** Provides utility methods to accessibility quick settings only. */
|
||||
final class AccessibilityQuickSettingUtils {
|
||||
|
||||
private static final String ACCESSIBILITY_PERF = "accessibility_prefs";
|
||||
private static final String KEY_TILE_SERVICE_SHOWN = "tile_service_shown";
|
||||
private static final char COMPONENT_NAME_SEPARATOR = ':';
|
||||
private static final TextUtils.SimpleStringSplitter sStringColonSplitter =
|
||||
new TextUtils.SimpleStringSplitter(COMPONENT_NAME_SEPARATOR);
|
||||
|
||||
/**
|
||||
* Opts in component name into {@link AccessibilityQuickSettingUtils#KEY_TILE_SERVICE_SHOWN}
|
||||
* colon-separated string in {@link SharedPreferences}.
|
||||
*
|
||||
* @param context The current context.
|
||||
* @param componentName The component name that need to be opted in SharedPreferences.
|
||||
*/
|
||||
public static void optInValueToSharedPreferences(Context context,
|
||||
@NonNull ComponentName componentName) {
|
||||
final String targetString = getFromSharedPreferences(context);
|
||||
if (hasValueInSharedPreferences(targetString, componentName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
final StringJoiner joiner = new StringJoiner(String.valueOf(COMPONENT_NAME_SEPARATOR));
|
||||
if (!TextUtils.isEmpty(targetString)) {
|
||||
joiner.add(targetString);
|
||||
}
|
||||
joiner.add(componentName.flattenToString());
|
||||
|
||||
SharedPreferences.Editor editor = getSharedPreferences(context).edit();
|
||||
editor.putString(KEY_TILE_SERVICE_SHOWN, joiner.toString()).apply();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if component name existed in {@link
|
||||
* AccessibilityQuickSettingUtils#KEY_TILE_SERVICE_SHOWN} string in {@link SharedPreferences}.
|
||||
*
|
||||
* @param context The current context.
|
||||
* @param componentName The component name that need to be checked existed in SharedPreferences.
|
||||
* @return {@code true} if componentName existed in SharedPreferences.
|
||||
*/
|
||||
public static boolean hasValueInSharedPreferences(Context context,
|
||||
@NonNull ComponentName componentName) {
|
||||
final String targetString = getFromSharedPreferences(context);
|
||||
return hasValueInSharedPreferences(targetString, componentName);
|
||||
}
|
||||
|
||||
private static boolean hasValueInSharedPreferences(String targetString,
|
||||
@NonNull ComponentName componentName) {
|
||||
if (TextUtils.isEmpty(targetString)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
sStringColonSplitter.setString(targetString);
|
||||
|
||||
while (sStringColonSplitter.hasNext()) {
|
||||
final String name = sStringColonSplitter.next();
|
||||
if (TextUtils.equals(componentName.flattenToString(), name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static String getFromSharedPreferences(Context context) {
|
||||
return getSharedPreferences(context).getString(KEY_TILE_SERVICE_SHOWN, "");
|
||||
}
|
||||
|
||||
private static SharedPreferences getSharedPreferences(Context context) {
|
||||
return context.getSharedPreferences(ACCESSIBILITY_PERF, Context.MODE_PRIVATE);
|
||||
}
|
||||
|
||||
private AccessibilityQuickSettingUtils(){}
|
||||
}
|
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 android.content.Context;
|
||||
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
|
||||
/** Tests for {@link AccessibilityQuickSettingUtils}. */
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public final class AccessibilityQuickSettingUtilsTest {
|
||||
private static final String DUMMY_PACKAGE_NAME = "com.mock.example";
|
||||
private static final String DUMMY_CLASS_NAME = DUMMY_PACKAGE_NAME + ".mock_a11y_service";
|
||||
private static final String DUMMY_CLASS_NAME2 = DUMMY_PACKAGE_NAME + ".mock_a11y_service2";
|
||||
private static final ComponentName DUMMY_COMPONENT_NAME = new ComponentName(DUMMY_PACKAGE_NAME,
|
||||
DUMMY_CLASS_NAME);
|
||||
private static final ComponentName DUMMY_COMPONENT_NAME2 = new ComponentName(DUMMY_PACKAGE_NAME,
|
||||
DUMMY_CLASS_NAME2);
|
||||
private final Context mContext = ApplicationProvider.getApplicationContext();
|
||||
|
||||
@Test
|
||||
public void optInValueToSharedPreferences_optInValue_haveMatchString() {
|
||||
AccessibilityQuickSettingUtils.optInValueToSharedPreferences(mContext,
|
||||
DUMMY_COMPONENT_NAME);
|
||||
|
||||
assertThat(AccessibilityQuickSettingUtils.hasValueInSharedPreferences(mContext,
|
||||
DUMMY_COMPONENT_NAME)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optInValueToSharedPreferences_optInTwoValues_haveMatchString() {
|
||||
AccessibilityQuickSettingUtils.optInValueToSharedPreferences(mContext,
|
||||
DUMMY_COMPONENT_NAME);
|
||||
AccessibilityQuickSettingUtils.optInValueToSharedPreferences(mContext,
|
||||
DUMMY_COMPONENT_NAME2);
|
||||
|
||||
assertThat(AccessibilityQuickSettingUtils.hasValueInSharedPreferences(mContext,
|
||||
DUMMY_COMPONENT_NAME)).isTrue();
|
||||
assertThat(AccessibilityQuickSettingUtils.hasValueInSharedPreferences(mContext,
|
||||
DUMMY_COMPONENT_NAME2)).isTrue();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user