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

Commit 4e9907da authored by Hai Zhang's avatar Hai Zhang Committed by Android (Google) Code Review
Browse files

Merge "Use separate config entries for default role holders."

parents 042cd43a ffa1ad0a
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package com.android.packageinstaller.role.model;


import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT;
import static org.xmlpull.v1.XmlPullParser.START_TAG;

@@ -66,7 +65,8 @@ public class AssistantRoleBehavior implements RoleBehavior {
    @Nullable
    @Override
    public String getFallbackHolder(@NonNull Role role, @NonNull Context context) {
        return ExclusiveDefaultHolderMixin.getDefaultHolder(role, context);
        return ExclusiveDefaultHolderMixin.getDefaultHolder(role, "config_defaultAssistant",
                context);
    }

    @Nullable
+2 −1
Original line number Diff line number Diff line
@@ -49,7 +49,8 @@ public class BrowserRoleBehavior implements RoleBehavior {
    @NonNull
    @Override
    public List<String> getDefaultHolders(@NonNull Role role, @NonNull Context context) {
        return ExclusiveDefaultHolderMixin.getDefaultHolders(role, context);
        return ExclusiveDefaultHolderMixin.getDefaultHolders(role, "config_defaultBrowser",
                context);
    }

    @Nullable
+0 −98
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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.packageinstaller.role.model;

import android.content.Context;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.Log;

import androidx.annotation.NonNull;

import java.util.Arrays;
import java.util.List;

/**
 * Provides access to all the default role holders.
 */
public class DefaultRoleHolders {

    private static final String LOG_TAG = DefaultRoleHolders.class.getSimpleName();

    @NonNull
    private static final Object sLock = new Object();

    private static ArrayMap<String, List<String>> sDefaultRoleHolders;

    /**
     * Get the default role holders.
     *
     * @param context the {@code Context} used to read the system resource
     *
     * @return a map from role name to a list of package names of the default holders
     */
    @NonNull
    public static ArrayMap<String, List<String>> get(@NonNull Context context) {
        synchronized (sLock) {
            if (sDefaultRoleHolders == null) {
                sDefaultRoleHolders = load(context);
            }
            return sDefaultRoleHolders;
        }
    }

    @NonNull
    private static ArrayMap<String, List<String>> load(@NonNull Context context) {
        ArrayMap<String, List<String>> defaultRoleHolders = new ArrayMap<>();
        String[] items = context.getResources().getStringArray(
                android.R.array.config_defaultRoleHolders);
        int itemsLength = items.length;
        for (int i = 0; i < itemsLength; i++) {
            String item = items[i];

            item = item.trim();
            String[] roleNameAndPackageNames = item.split("\\s*:\\s*", 2);
            if (roleNameAndPackageNames.length != 2) {
                Log.e(LOG_TAG, "Invalid item: " + item);
                continue;
            }
            String roleName = roleNameAndPackageNames[0];
            if (roleName.isEmpty()) {
                Log.e(LOG_TAG, "Empty role name: " + item);
                continue;
            }
            if (defaultRoleHolders.containsKey(roleName)) {
                Log.e(LOG_TAG, "Duplicate role name: " + roleName);
                continue;
            }
            String packageNamesString = roleNameAndPackageNames[1];
            if (packageNamesString.isEmpty()) {
                Log.e(LOG_TAG, "Empty package names: " + item);
                continue;
            }
            List<String> packageNames = Arrays.asList(packageNamesString.split("\\s*,\\s*"));
            ArraySet<String> uniquePackageNames = new ArraySet<>(packageNames);
            if (packageNames.size() != uniquePackageNames.size()) {
                Log.e(LOG_TAG, "Duplicate package names: " + packageNamesString);
                packageNames.clear();
                packageNames.addAll(uniquePackageNames);
            }
            defaultRoleHolders.put(roleName, packageNames);
        }
        return defaultRoleHolders;
    }
}
+21 −9
Original line number Diff line number Diff line
@@ -17,6 +17,9 @@
package com.android.packageinstaller.role.model;

import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@@ -27,33 +30,42 @@ import java.util.List;

/**
 * Mixin for {@link RoleBehavior#getDefaultHolders(Role, Context)} that returns a single default
 * role holder from {@link DefaultRoleHolders}.
 * role holder from the corresponding string resource.
 */
public class ExclusiveDefaultHolderMixin {

    private static final String LOG_TAG = ExclusiveDefaultHolderMixin.class.getSimpleName();

    private ExclusiveDefaultHolderMixin() {}

    /**
     * @see Role#getDefaultHolders(Context)
     */
    @NonNull
    public static List<String> getDefaultHolders(@NonNull Role role, @NonNull Context context) {
        return CollectionUtils.singletonOrEmpty(getDefaultHolder(role, context));
    public static List<String> getDefaultHolders(@NonNull Role role, @NonNull String resourceName,
            @NonNull Context context) {
        return CollectionUtils.singletonOrEmpty(getDefaultHolder(role, resourceName, context));
    }

    /**
     * @see Role#getDefaultHolders(Context)
     */
    @Nullable
    public static String getDefaultHolder(@NonNull Role role, @NonNull Context context) {
        String defaultPackageName = CollectionUtils.firstOrNull(DefaultRoleHolders.get(context).get(
                role.getName()));
        if (defaultPackageName == null) {
    public static String getDefaultHolder(@NonNull Role role, @NonNull String resourceName,
            @NonNull Context context) {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier(resourceName, "string", "android");
        if (resourceId == 0) {
            Log.w(LOG_TAG, "Cannot find resource for default holder: " + resourceName);
            return null;
        }
        String packageName = resources.getString(resourceId);
        if (TextUtils.isEmpty(packageName)) {
            return null;
        }
        if (!role.isPackageQualified(defaultPackageName, context)) {
        if (!role.isPackageQualified(packageName, context)) {
            return null;
        }
        return defaultPackageName;
        return packageName;
    }
}
+2 −1
Original line number Diff line number Diff line
@@ -60,7 +60,8 @@ public class SmsRoleBehavior implements RoleBehavior {
    @Nullable
    @Override
    public String getFallbackHolder(@NonNull Role role, @NonNull Context context) {
        String defaultPackageName = ExclusiveDefaultHolderMixin.getDefaultHolder(role, context);
        String defaultPackageName = ExclusiveDefaultHolderMixin.getDefaultHolder(role,
                "config_defaultSms", context);
        if (defaultPackageName != null) {
            return defaultPackageName;
        }