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

Commit 69a6c0d5 authored by Matt Garnes's avatar Matt Garnes
Browse files

Add support for permanently enabled carrier apps

Read a list of packages from the resources String array called
config_enabledPreinstalledCarrierApps and add these unconditionally to
the list of carrier apps. These packages are carrier apps that will be
enabled, regardless of the SIM card inserted.

Part of CYNGNOS-1650.

Change-Id: Ia8ddb6b022b63ebe8eb555d7c1ea0db4a58821a7
(cherry picked from commit ee0fc82a)
parent 54f645f2
Loading
Loading
Loading
Loading
+43 −11
Original line number Original line Diff line number Diff line
@@ -27,6 +27,7 @@ import android.util.Slog;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.annotations.VisibleForTesting;


import java.util.ArrayList;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.List;


/**
/**
@@ -52,6 +53,10 @@ public final class CarrierAppUtils {
     * in the default state (e.g. not explicitly DISABLED/DISABLED_BY_USER/ENABLED), or we enable if
     * in the default state (e.g. not explicitly DISABLED/DISABLED_BY_USER/ENABLED), or we enable if
     * the app is carrier privileged and in either the default state or DISABLED_UNTIL_USED.
     * the app is carrier privileged and in either the default state or DISABLED_UNTIL_USED.
     *
     *
     * In addition, a list of permanently enabled carrier apps is packaged with the system and read
     * here. All permanently enabled carrier apps are treated the same as the SIM enabled carrier
     * apps here.
     *
     * When enabling a carrier app we also grant it default permissions.
     * When enabling a carrier app we also grant it default permissions.
     *
     *
     * This method is idempotent and is safe to be called at any time; it should be called once at
     * This method is idempotent and is safe to be called at any time; it should be called once at
@@ -65,17 +70,19 @@ public final class CarrierAppUtils {
        }
        }
        String[] systemCarrierAppsDisabledUntilUsed = Resources.getSystem().getStringArray(
        String[] systemCarrierAppsDisabledUntilUsed = Resources.getSystem().getStringArray(
                com.android.internal.R.array.config_disabledUntilUsedPreinstalledCarrierApps);
                com.android.internal.R.array.config_disabledUntilUsedPreinstalledCarrierApps);
        String[] systemCarrierAppsEnabled = Resources.getSystem().getStringArray(
                com.android.internal.R.array.config_enabledPreinstalledCarrierApps);
        disableCarrierAppsUntilPrivileged(callingPackage, packageManager, telephonyManager, userId,
        disableCarrierAppsUntilPrivileged(callingPackage, packageManager, telephonyManager, userId,
                systemCarrierAppsDisabledUntilUsed);
                systemCarrierAppsDisabledUntilUsed, systemCarrierAppsEnabled);
    }
    }


    // Must be public b/c framework unit tests can't access package-private methods.
    // Must be public b/c framework unit tests can't access package-private methods.
    @VisibleForTesting
    @VisibleForTesting
    public static void disableCarrierAppsUntilPrivileged(String callingPackage,
    public static void disableCarrierAppsUntilPrivileged(String callingPackage,
            IPackageManager packageManager, TelephonyManager telephonyManager, int userId,
            IPackageManager packageManager, TelephonyManager telephonyManager, int userId,
            String[] systemCarrierAppsDisabledUntilUsed) {
            String[] systemCarrierAppsDisabledUntilUsed, String[] systemCarrierAppsEnabled) {
        List<ApplicationInfo> candidates = getDefaultCarrierAppCandidatesHelper(packageManager,
        List<ApplicationInfo> candidates = getDefaultCarrierAppCandidatesHelper(packageManager,
                userId, systemCarrierAppsDisabledUntilUsed);
                userId, systemCarrierAppsDisabledUntilUsed, systemCarrierAppsEnabled);
        if (candidates == null || candidates.isEmpty()) {
        if (candidates == null || candidates.isEmpty()) {
            return;
            return;
        }
        }
@@ -118,6 +125,11 @@ public final class CarrierAppUtils {
                }
                }
            }
            }


            // Add all permanently enabled packages to the enabled list
            for (String enabledPackageName : systemCarrierAppsEnabled) {
                enabledCarrierPackages.add(enabledPackageName);
            }

            if (!enabledCarrierPackages.isEmpty()) {
            if (!enabledCarrierPackages.isEmpty()) {
                // Since we enabled at least one app, ensure we grant default permissions to those
                // Since we enabled at least one app, ensure we grant default permissions to those
                // apps.
                // apps.
@@ -135,7 +147,8 @@ public final class CarrierAppUtils {
     *
     *
     * This is the subset of apps returned by
     * This is the subset of apps returned by
     * {@link #getDefaultCarrierAppCandidates(IPackageManager, int)} which currently have carrier
     * {@link #getDefaultCarrierAppCandidates(IPackageManager, int)} which currently have carrier
     * privileges per the SIM(s) inserted in the device.
     * privileges per the SIM(s) inserted in the device. All permanent system enabled carrier
     * apps are always included in the returned list.
     */
     */
    public static List<ApplicationInfo> getDefaultCarrierApps(IPackageManager packageManager,
    public static List<ApplicationInfo> getDefaultCarrierApps(IPackageManager packageManager,
            TelephonyManager telephonyManager, int userId) {
            TelephonyManager telephonyManager, int userId) {
@@ -145,6 +158,13 @@ public final class CarrierAppUtils {
            return null;
            return null;
        }
        }


        String[] systemCarrierAppsEnabled = Resources.getSystem().getStringArray(
                com.android.internal.R.array.config_enabledPreinstalledCarrierApps);
        HashSet<String> enabledCarrierAppsSet = new HashSet<>(systemCarrierAppsEnabled.length);
        for (String packageName : systemCarrierAppsEnabled) {
            enabledCarrierAppsSet.add(packageName);
        }

        // Filter out apps without carrier privileges.
        // Filter out apps without carrier privileges.
        // Iterate from the end to avoid creating an Iterator object and because we will be removing
        // Iterate from the end to avoid creating an Iterator object and because we will be removing
        // elements from the list as we pass through it.
        // elements from the list as we pass through it.
@@ -154,7 +174,7 @@ public final class CarrierAppUtils {
            boolean hasPrivileges =
            boolean hasPrivileges =
                    telephonyManager.checkCarrierPrivilegesForPackageAnyPhone(packageName) ==
                    telephonyManager.checkCarrierPrivilegesForPackageAnyPhone(packageName) ==
                            TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
                            TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
            if (!hasPrivileges) {
            if (!hasPrivileges && !enabledCarrierAppsSet.contains(packageName)) {
                candidates.remove(i);
                candidates.remove(i);
            }
            }
        }
        }
@@ -168,7 +188,8 @@ public final class CarrierAppUtils {
     * These are the apps subject to the hiding/showing logic in
     * These are the apps subject to the hiding/showing logic in
     * {@link CarrierAppUtils#disableCarrierAppsUntilPrivileged(String, IPackageManager,
     * {@link CarrierAppUtils#disableCarrierAppsUntilPrivileged(String, IPackageManager,
     * TelephonyManager, int)}, as well as the apps which should have default permissions granted,
     * TelephonyManager, int)}, as well as the apps which should have default permissions granted,
     * when a matching SIM is inserted.
     * when a matching SIM is inserted. Additionally, all permanently enabled system carrier
     * apps are included in the returned list.
     *
     *
     * Whether or not the app is actually considered a default app depends on whether the app has
     * Whether or not the app is actually considered a default app depends on whether the app has
     * carrier privileges as determined by the SIMs in the device.
     * carrier privileges as determined by the SIMs in the device.
@@ -177,20 +198,26 @@ public final class CarrierAppUtils {
            IPackageManager packageManager, int userId) {
            IPackageManager packageManager, int userId) {
        String[] systemCarrierAppsDisabledUntilUsed = Resources.getSystem().getStringArray(
        String[] systemCarrierAppsDisabledUntilUsed = Resources.getSystem().getStringArray(
                com.android.internal.R.array.config_disabledUntilUsedPreinstalledCarrierApps);
                com.android.internal.R.array.config_disabledUntilUsedPreinstalledCarrierApps);
        String[] systemCarrierAppsEnabled = Resources.getSystem().getStringArray(
                com.android.internal.R.array.config_enabledPreinstalledCarrierApps);
        return getDefaultCarrierAppCandidatesHelper(packageManager, userId,
        return getDefaultCarrierAppCandidatesHelper(packageManager, userId,
                systemCarrierAppsDisabledUntilUsed);
                systemCarrierAppsDisabledUntilUsed, systemCarrierAppsEnabled);
    }
    }


    private static List<ApplicationInfo> getDefaultCarrierAppCandidatesHelper(
    private static List<ApplicationInfo> getDefaultCarrierAppCandidatesHelper(
            IPackageManager packageManager, int userId,
            IPackageManager packageManager, int userId,
            String[] systemCarrierAppsDisabledUntilUsed) {
            String[] systemCarrierAppsDisabledUntilUsed,
        if (systemCarrierAppsDisabledUntilUsed == null
            String[] systemCarrierAppsEnabled) {
                || systemCarrierAppsDisabledUntilUsed.length == 0) {
        if ((systemCarrierAppsDisabledUntilUsed == null
                || systemCarrierAppsDisabledUntilUsed.length == 0)
            && (systemCarrierAppsEnabled == null
                || systemCarrierAppsEnabled.length == 0)) {
            return null;
            return null;
        }
        }
        List<ApplicationInfo> apps = null;
        List<ApplicationInfo> apps = null;
        try {
        try {
            apps = new ArrayList<>(systemCarrierAppsDisabledUntilUsed.length);
            apps = new ArrayList<>(systemCarrierAppsDisabledUntilUsed.length
                    + systemCarrierAppsEnabled.length);
            for (String packageName : systemCarrierAppsDisabledUntilUsed) {
            for (String packageName : systemCarrierAppsDisabledUntilUsed) {
                ApplicationInfo ai = packageManager.getApplicationInfo(packageName,
                ApplicationInfo ai = packageManager.getApplicationInfo(packageName,
                        PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS, userId);
                        PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS, userId);
@@ -203,6 +230,11 @@ public final class CarrierAppUtils {
                }
                }
                apps.add(ai);
                apps.add(ai);
            }
            }

            for (String carrierApp : systemCarrierAppsEnabled) {
                ApplicationInfo ai = packageManager.getApplicationInfo(carrierApp, 0, userId);
                apps.add(ai);
            }
        } catch (RemoteException e) {
        } catch (RemoteException e) {
            Slog.w(TAG, "Could not reach PackageManager", e);
            Slog.w(TAG, "Could not reach PackageManager", e);
        }
        }