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

Commit 105908d0 authored by Fan Zhang's avatar Fan Zhang
Browse files

Fix StringIndexOutOfBoundsException in AccessibilityUtils

The string splitter is a sigleton but the access is not thread-safe. So
now it's changed to be a local instance to avoid threading problems.

Also removed a few unused test classes as clean up.

Change-Id: Ia2b3f3058fcbb0b28afff4e3d1b29184072bf45e
Fixes: 110255579
Test: robotests
parent bb2c6c9f
Loading
Loading
Loading
Loading
+7 −11
Original line number Diff line number Diff line
@@ -40,9 +40,6 @@ import java.util.Set;
public class AccessibilityUtils {
    public static final char ENABLED_ACCESSIBILITY_SERVICES_SEPARATOR = ':';

    final static TextUtils.SimpleStringSplitter sStringColonSplitter =
            new TextUtils.SimpleStringSplitter(ENABLED_ACCESSIBILITY_SERVICES_SEPARATOR);

    /**
     * @return the set of enabled accessibility services. If there are no services,
     * it returns the unmodifiable {@link Collections#emptySet()}.
@@ -72,16 +69,16 @@ public class AccessibilityUtils {
        final String enabledServicesSetting = Settings.Secure.getStringForUser(
                context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
                userId);
        if (enabledServicesSetting == null) {
        if (TextUtils.isEmpty(enabledServicesSetting)) {
            return Collections.emptySet();
        }

        final Set<ComponentName> enabledServices = new HashSet<>();
        final TextUtils.SimpleStringSplitter colonSplitter = sStringColonSplitter;
        final TextUtils.StringSplitter colonSplitter =
                new TextUtils.SimpleStringSplitter(ENABLED_ACCESSIBILITY_SERVICES_SEPARATOR);
        colonSplitter.setString(enabledServicesSetting);

        while (colonSplitter.hasNext()) {
            final String componentNameString = colonSplitter.next();
        for (String componentNameString : colonSplitter) {
            final ComponentName enabledService = ComponentName.unflattenFromString(
                    componentNameString);
            if (enabledService != null) {
@@ -169,7 +166,6 @@ public class AccessibilityUtils {
     *
     * @param context A valid context
     * @param userId  The user whose settings should be checked
     *
     * @return The component name, flattened to a string, of the target service.
     */
    public static String getShortcutTargetServiceComponentNameString(
+0 −26
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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 android.bluetooth;

/**
 * A placeholder class to prevent ClassNotFound exceptions caused by lack of visibility.
 */
public class BluetoothCodecConfig {
    public boolean isMandatoryCodec() { return true; }
    public String getCodecName() { return null; }
    public int getCodecType() { return -1; }
}
+0 −25
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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 android.bluetooth;

/**
 * A placeholder class to prevent ClassNotFound exceptions caused by lack of visibility.
 */
public class BluetoothCodecStatus {
    public BluetoothCodecConfig getCodecConfig() { return null; }
    public BluetoothCodecConfig[] getCodecsSelectableCapabilities() { return null; }
}
+82 −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.settingslib.accessibility;

import static com.google.common.truth.Truth.assertThat;

import android.content.ComponentName;
import android.content.Context;
import android.os.UserHandle;
import android.provider.Settings;

import com.android.settingslib.SettingsLibRobolectricTestRunner;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;

@RunWith(SettingsLibRobolectricTestRunner.class)
public class AccessibilityUtilsTest {

    private Context mContext;

    @Before
    public void setUp() {
        mContext = RuntimeEnvironment.application;
    }

    @Test
    public void getEnabledServicesFromSettings_noService_emptyResult() {
        assertThat(AccessibilityUtils.getEnabledServicesFromSettings(mContext)).isEmpty();
    }

    @Test
    public void getEnabledServicesFromSettings_badFormat_emptyResult() {
        Settings.Secure.putStringForUser(
                mContext.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
                ":",
                UserHandle.myUserId());

        assertThat(AccessibilityUtils.getEnabledServicesFromSettings(mContext)).isEmpty();
    }

    @Test
    public void getEnabledServicesFromSettings_1Service_1result() {
        final ComponentName cn = new ComponentName("pkg", "serv");
        Settings.Secure.putStringForUser(
                mContext.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
                cn.flattenToString() + ":",
                UserHandle.myUserId());

        assertThat(AccessibilityUtils.getEnabledServicesFromSettings(mContext))
                .containsExactly(cn);
    }

    @Test
    public void getEnabledServicesFromSettings_2Services_2results() {
        final ComponentName cn1 = new ComponentName("pkg", "serv");
        final ComponentName cn2 = new ComponentName("pkg", "serv2");
        Settings.Secure.putStringForUser(
                mContext.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
                cn1.flattenToString() + ":" + cn2.flattenToString(),
                UserHandle.myUserId());

        assertThat(AccessibilityUtils.getEnabledServicesFromSettings(mContext))
                .containsExactly(cn1, cn2);
    }
}