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

Commit fbd4bf7b authored by Peter_Liang's avatar Peter_Liang
Browse files

Redesign for AccessibilityShortcutChooserActivity (3/n).

1. Migrate the functions related to permision dialog into AccessibilityTargetHelper file.
2. Remove redundant codes.

Bug: 147655054
Test: manual test
Change-Id: Ie052bd7c4159552a78193464602e3d1d9471554b
parent 7d9af05e
Loading
Loading
Loading
Loading
+0 −41
Original line number Diff line number Diff line
@@ -78,21 +78,6 @@ public final class ShortcutConstants {
        int LAUNCH_ACTIVITY = 3;
    }

    /**
     * Annotation for different shortcut target.
     */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef({
            TargetType.ACCESSIBILITY_SERVICE,
            TargetType.ACCESSIBILITY_ACTIVITY,
            TargetType.WHITE_LISTING,
    })
    public @interface TargetType {
        int ACCESSIBILITY_SERVICE = 0;
        int ACCESSIBILITY_ACTIVITY = 1;
        int WHITE_LISTING = 2;
    }

    /**
     * Annotation for different shortcut menu mode.
     *
@@ -108,30 +93,4 @@ public final class ShortcutConstants {
        int LAUNCH = 0;
        int EDIT = 1;
    }

    /**
     * Annotation for align the element index of white listing feature
     * {@code WHITE_LISTING_FEATURES}.
     *
     * {@code COMPONENT_ID} is to get the service component name.
     * {@code LABEL_ID} is to get the service label text.
     * {@code ICON_ID} is to get the service icon.
     * {@code FRAGMENT_TYPE} is to get the service fragment type.
     * {@code SETTINGS_KEY} is to get the service settings key.
     */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef({
            WhiteListingFeatureElementIndex.COMPONENT_ID,
            WhiteListingFeatureElementIndex.LABEL_ID,
            WhiteListingFeatureElementIndex.ICON_ID,
            WhiteListingFeatureElementIndex.FRAGMENT_TYPE,
            WhiteListingFeatureElementIndex.SETTINGS_KEY,
    })
    public @interface WhiteListingFeatureElementIndex {
        int COMPONENT_ID = 0;
        int LABEL_ID = 1;
        int ICON_ID = 2;
        int FRAGMENT_TYPE = 3;
        int SETTINGS_KEY = 4;
    }
}
+1 −60
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import static android.view.accessibility.AccessibilityManager.ACCESSIBILITY_SHOR
import static android.view.accessibility.AccessibilityManager.ShortcutType;

import static com.android.internal.accessibility.common.ShortcutConstants.ShortcutMenuMode;
import static com.android.internal.accessibility.dialog.AccessibilityTargetHelper.createEnableDialogContentView;
import static com.android.internal.accessibility.dialog.AccessibilityTargetHelper.getInstalledTargets;
import static com.android.internal.accessibility.dialog.AccessibilityTargetHelper.getTargets;
import static com.android.internal.util.Preconditions.checkArgument;
@@ -27,26 +28,18 @@ import static com.android.internal.util.Preconditions.checkArgument;
import android.annotation.Nullable;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.storage.StorageManager;
import android.text.BidiFormatter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.accessibility.AccessibilityManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.internal.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * Activity used to display various targets related to accessibility service, accessibility
@@ -169,56 +162,4 @@ public class AccessibilityShortcutChooserActivity extends Activity {
        mMenuDialog.getListView().setOnItemClickListener(
                isEditMenuMode ? this::onTargetChecked : this::onTargetSelected);
    }

    private static View createEnableDialogContentView(Context context,
            AccessibilityServiceTarget target, View.OnClickListener allowListener,
            View.OnClickListener denyListener) {
        final LayoutInflater inflater = (LayoutInflater) context.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);

        final View content = inflater.inflate(
                R.layout.accessibility_enable_service_encryption_warning, /* root= */ null);

        final TextView encryptionWarningView = (TextView) content.findViewById(
                R.id.accessibility_encryption_warning);
        if (StorageManager.isNonDefaultBlockEncrypted()) {
            final String text = context.getString(
                    R.string.accessibility_enable_service_encryption_warning,
                    getServiceName(context, target.getLabel()));
            encryptionWarningView.setText(text);
            encryptionWarningView.setVisibility(View.VISIBLE);
        } else {
            encryptionWarningView.setVisibility(View.GONE);
        }

        final ImageView dialogIcon = content.findViewById(
                R.id.accessibility_permissionDialog_icon);
        dialogIcon.setImageDrawable(target.getIcon());

        final TextView dialogTitle = content.findViewById(
                R.id.accessibility_permissionDialog_title);
        dialogTitle.setText(context.getString(R.string.accessibility_enable_service_title,
                getServiceName(context, target.getLabel())));

        final Button allowButton = content.findViewById(
                R.id.accessibility_permission_enable_allow_button);
        final Button denyButton = content.findViewById(
                R.id.accessibility_permission_enable_deny_button);
        allowButton.setOnClickListener((view) -> {
            target.onCheckedChanged(/* isChecked= */ true);
            allowListener.onClick(view);
        });
        denyButton.setOnClickListener((view) -> {
            target.onCheckedChanged(/* isChecked= */ false);
            denyListener.onClick(view);
        });

        return content;
    }

    // Gets the service name and bidi wrap it to protect from bidi side effects.
    private static CharSequence getServiceName(Context context, CharSequence label) {
        final Locale locale = context.getResources().getConfiguration().getLocales().get(0);
        return BidiFormatter.getInstance(locale).unicodeWrap(label);
    }
}
+60 −0
Original line number Diff line number Diff line
@@ -31,9 +31,16 @@ import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.os.Build;
import android.os.storage.StorageManager;
import android.provider.Settings;
import android.text.BidiFormatter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.accessibility.AccessibilityManager;
import android.view.accessibility.AccessibilityManager.ShortcutType;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.internal.R;
import com.android.internal.accessibility.common.ShortcutConstants.AccessibilityFragmentType;
@@ -41,6 +48,7 @@ import com.android.internal.accessibility.common.ShortcutConstants.Accessibility
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

/**
 * Collection of utilities for accessibility target.
@@ -198,4 +206,56 @@ final class AccessibilityTargetHelper {
                throw new IllegalStateException("Unexpected fragment type");
        }
    }

    static View createEnableDialogContentView(Context context,
            AccessibilityServiceTarget target, View.OnClickListener allowListener,
            View.OnClickListener denyListener) {
        final LayoutInflater inflater = (LayoutInflater) context.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);

        final View content = inflater.inflate(
                R.layout.accessibility_enable_service_encryption_warning, /* root= */ null);

        final TextView encryptionWarningView = (TextView) content.findViewById(
                R.id.accessibility_encryption_warning);
        if (StorageManager.isNonDefaultBlockEncrypted()) {
            final String text = context.getString(
                    R.string.accessibility_enable_service_encryption_warning,
                    getServiceName(context, target.getLabel()));
            encryptionWarningView.setText(text);
            encryptionWarningView.setVisibility(View.VISIBLE);
        } else {
            encryptionWarningView.setVisibility(View.GONE);
        }

        final ImageView dialogIcon = content.findViewById(
                R.id.accessibility_permissionDialog_icon);
        dialogIcon.setImageDrawable(target.getIcon());

        final TextView dialogTitle = content.findViewById(
                R.id.accessibility_permissionDialog_title);
        dialogTitle.setText(context.getString(R.string.accessibility_enable_service_title,
                getServiceName(context, target.getLabel())));

        final Button allowButton = content.findViewById(
                R.id.accessibility_permission_enable_allow_button);
        final Button denyButton = content.findViewById(
                R.id.accessibility_permission_enable_deny_button);
        allowButton.setOnClickListener((view) -> {
            target.onCheckedChanged(/* isChecked= */ true);
            allowListener.onClick(view);
        });
        denyButton.setOnClickListener((view) -> {
            target.onCheckedChanged(/* isChecked= */ false);
            denyListener.onClick(view);
        });

        return content;
    }

    // Gets the service name and bidi wrap it to protect from bidi side effects.
    private static CharSequence getServiceName(Context context, CharSequence label) {
        final Locale locale = context.getResources().getConfiguration().getLocales().get(0);
        return BidiFormatter.getInstance(locale).unicodeWrap(label);
    }
}
+0 −23
Original line number Diff line number Diff line
@@ -98,29 +98,6 @@ public final class ShortcutUtils {
        Settings.Secure.putString(context.getContentResolver(), targetsKey, joiner.toString());
    }

    /**
     * Returns if component id existed in one of {@link UserShortcutType} string from Settings.
     *
     * @param context The current context.
     * @param shortcutTypes A combination of {@link UserShortcutType}.
     * @param componentId The component id that need to be checked existed in Settings.
     * @return {@code true} if component id existed in Settings.
     */
    public static boolean hasValuesInSettings(Context context, @UserShortcutType int shortcutTypes,
            @NonNull String componentId) {
        boolean exist = false;
        if ((shortcutTypes & UserShortcutType.SOFTWARE) == UserShortcutType.SOFTWARE) {
            exist = isComponentIdExistingInSettings(context, UserShortcutType.SOFTWARE,
                    componentId);
        }
        if (((shortcutTypes & UserShortcutType.HARDWARE) == UserShortcutType.HARDWARE)) {
            exist |= isComponentIdExistingInSettings(context, UserShortcutType.HARDWARE,
                    componentId);
        }
        return exist;
    }


    /**
     * Returns if component id existed in Settings.
     *