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

Commit a3cce241 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Migrate default browser to use role."

parents 16f373b0 a22cd83c
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -843,4 +843,14 @@ public abstract class PackageManagerInternal {
     * {@code token} can be completed.
     */
    public abstract void finishPackageInstall(int token, boolean didLaunch);

    /**
     * Remove the default browser stored in the legacy package settings.
     *
     * @param userId the user id
     *
     * @return the package name of the default browser, or {@code null} if none
     */
    @Nullable
    public abstract String removeLegacyDefaultBrowserPackageName(int userId);
}
+0 −8
Original line number Diff line number Diff line
@@ -1933,14 +1933,6 @@
         the platform will search for an SMS app and use that (if there is one)-->
    <string name="default_sms_application" translatable="false">com.android.messaging</string>

    <!-- Default web browser.  This is the package name of the application that will
         be the default browser when the device first boots.  Afterwards the user
         can select whatever browser app they wish to use as the default.

         If this string is empty or the specified package does not exist, then
         the behavior will be as though no app was named as an explicit default. -->
    <string name="default_browser" translatable="false"></string>

    <!-- Default role holders. This will be an array of roles and package names of their default
         holders, with each item in the format of "ROLE_NAME: PACKAGE_NAME_1, PACKAGE_NAME_2". -->
    <string-array name="config_defaultRoleHolders" translatable="false">
+0 −1
Original line number Diff line number Diff line
@@ -1023,7 +1023,6 @@
  <java-symbol type="string" name="sipAddressTypeOther" />
  <java-symbol type="string" name="sipAddressTypeWork" />
  <java-symbol type="string" name="default_sms_application" />
  <java-symbol type="string" name="default_browser" />
  <java-symbol type="string" name="sms_control_message" />
  <java-symbol type="string" name="sms_control_title" />
  <java-symbol type="string" name="sms_control_no" />
+40 −77
Original line number Diff line number Diff line
@@ -131,6 +131,8 @@ import android.app.ResourcesManager;
import android.app.admin.IDevicePolicyManager;
import android.app.admin.SecurityLog;
import android.app.backup.IBackupManager;
import android.app.role.RoleManager;
import android.app.role.RoleManagerCallback;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
@@ -288,6 +290,7 @@ import com.android.internal.os.SomeArgs;
import com.android.internal.os.Zygote;
import com.android.internal.telephony.CarrierAppUtils;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.CollectionUtils;
import com.android.internal.util.ConcurrentUtils;
import com.android.internal.util.DumpUtils;
import com.android.internal.util.FastXmlSerializer;
@@ -369,8 +372,10 @@ import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
@@ -1927,7 +1932,7 @@ public class PackageManagerService extends IPackageManager.Stub
                            final PackageSetting pkgSetting = mSettings.mPackages.get(packageName);
                            if (pkgSetting.getInstallReason(userId)
                                    != PackageManager.INSTALL_REASON_DEVICE_RESTORE) {
                                mSettings.setDefaultBrowserPackageNameLPw(null, userId);
                                setDefaultBrowserPackageName(null, userId);
                            }
                        }
@@ -2941,7 +2946,6 @@ public class PackageManagerService extends IPackageManager.Stub
            if (!onlyCore && (mPromoteSystemApps || mFirstBoot)) {
                for (UserInfo user : sUserManager.getUsers(true)) {
                    mSettings.applyDefaultPreferredAppsLPw(user.id);
                    applyFactoryDefaultBrowserLPw(user.id);
                    primeDomainVerificationsLPw(user.id);
                }
            }
@@ -3014,8 +3018,6 @@ public class PackageManagerService extends IPackageManager.Stub
                ver.fingerprint = Build.FINGERPRINT;
            }
            checkDefaultBrowser();
            // clear only after permissions and other defaults have been updated
            mExistingSystemPackages.clear();
            mPromoteSystemApps = false;
@@ -3670,58 +3672,6 @@ public class PackageManagerService extends IPackageManager.Stub
        scheduleWriteSettingsLocked();
    }
    @GuardedBy("mPackages")
    private void applyFactoryDefaultBrowserLPw(int userId) {
        // The default browser app's package name is stored in a string resource,
        // with a product-specific overlay used for vendor customization.
        String browserPkg = mContext.getResources().getString(
                com.android.internal.R.string.default_browser);
        if (!TextUtils.isEmpty(browserPkg)) {
            // non-empty string => required to be a known package
            PackageSetting ps = mSettings.mPackages.get(browserPkg);
            if (ps == null) {
                Slog.e(TAG, "Product default browser app does not exist: " + browserPkg);
                browserPkg = null;
            } else {
                mSettings.setDefaultBrowserPackageNameLPw(browserPkg, userId);
            }
        }
        // Nothing valid explicitly set? Make the factory-installed browser the explicit
        // default.  If there's more than one, just leave everything alone.
        if (browserPkg == null) {
            calculateDefaultBrowserLPw(userId);
        }
    }
    @GuardedBy("mPackages")
    private void calculateDefaultBrowserLPw(int userId) {
        List<String> allBrowsers = resolveAllBrowserApps(userId);
        final String browserPkg = (allBrowsers.size() == 1) ? allBrowsers.get(0) : null;
        mSettings.setDefaultBrowserPackageNameLPw(browserPkg, userId);
    }
    private List<String> resolveAllBrowserApps(int userId) {
        // Resolve the canonical browser intent and check that the handleAllWebDataURI boolean is set
        List<ResolveInfo> list = queryIntentActivitiesInternal(sBrowserIntent, null,
                PackageManager.MATCH_ALL, userId);
        final int count = list.size();
        List<String> result = new ArrayList<>(count);
        for (int i=0; i<count; i++) {
            ResolveInfo info = list.get(i);
            if (info.activityInfo == null
                    || !info.handleAllWebDataURI
                    || (info.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0
                    || result.contains(info.activityInfo.packageName)) {
                continue;
            }
            result.add(info.activityInfo.packageName);
        }
        return result;
    }
    private boolean packageIsBrowser(String packageName, int userId) {
        List<ResolveInfo> list = queryIntentActivitiesInternal(sBrowserIntent, null,
                PackageManager.MATCH_ALL, userId);
@@ -3735,20 +3685,6 @@ public class PackageManagerService extends IPackageManager.Stub
        return false;
    }
    private void checkDefaultBrowser() {
        final int myUserId = UserHandle.myUserId();
        final String packageName = getDefaultBrowserPackageName(myUserId);
        if (packageName != null) {
            PackageInfo info = getPackageInfo(packageName, 0, myUserId);
            if (info == null) {
                Slog.w(TAG, "Default browser no longer installed: " + packageName);
                synchronized (mPackages) {
                    applyFactoryDefaultBrowserLPw(myUserId);    // leaves ambiguous when > 1
                }
            }
        }
    }
    @Override
    public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
            throws RemoteException {
@@ -13646,14 +13582,33 @@ public class PackageManagerService extends IPackageManager.Stub
                    android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, null);
        }
        if (userId == UserHandle.USER_ALL) {
            return false;
        }
        RoleManager roleManager = mContext.getSystemService(RoleManager.class);
        UserHandle user = UserHandle.of(userId);
        RoleManagerCallback.Future future = new RoleManagerCallback.Future();
        if (packageName != null) {
            Binder.withCleanCallingIdentity(() -> roleManager.addRoleHolderAsUser(
                    RoleManager.ROLE_BROWSER, packageName, user, AsyncTask.THREAD_POOL_EXECUTOR,
                    future));
        } else {
            Binder.withCleanCallingIdentity(() -> roleManager.clearRoleHoldersAsUser(
                    RoleManager.ROLE_BROWSER, user, AsyncTask.THREAD_POOL_EXECUTOR, future));
        }
        try {
            future.get(5, TimeUnit.SECONDS);
        } catch (InterruptedException | ExecutionException | TimeoutException e) {
            Slog.e(TAG, "Exception while setting default browser package name: " + packageName, e);
            return false;
        }
        synchronized (mPackages) {
            boolean result = mSettings.setDefaultBrowserPackageNameLPw(packageName, userId);
            if (packageName != null) {
                mDefaultPermissionPolicy.grantDefaultPermissionsToDefaultBrowser(
                        packageName, userId);
            }
            return result;
        }
        return true;
    }
    @Override
@@ -13665,9 +13620,10 @@ public class PackageManagerService extends IPackageManager.Stub
        if (getInstantAppPackageName(Binder.getCallingUid()) != null) {
            return null;
        }
        synchronized (mPackages) {
            return mSettings.getDefaultBrowserPackageNameLPw(userId);
        }
        RoleManager roleManager = mContext.getSystemService(RoleManager.class);
        List<String> packageNames = Binder.withCleanCallingIdentity(() ->
                roleManager.getRoleHoldersAsUser(RoleManager.ROLE_BROWSER, UserHandle.of(userId)));
        return CollectionUtils.firstOrNull(packageNames);
    }
    /**
@@ -19419,7 +19375,7 @@ public class PackageManagerService extends IPackageManager.Stub
                // significant refactoring to keep all default apps in the package
                // manager (cleaner but more work) or have the services provide
                // callbacks to the package manager to request a default app reset.
                applyFactoryDefaultBrowserLPw(userId);
                setDefaultBrowserPackageName(null, userId);
                clearIntentFilterVerificationsLPw(userId);
                primeDomainVerificationsLPw(userId);
                resetUserChangesToRuntimePermissionsAndFlagsLPw(userId);
@@ -22761,7 +22717,6 @@ public class PackageManagerService extends IPackageManager.Stub
        synchronized (mPackages) {
            scheduleWritePackageRestrictionsLocked(userId);
            scheduleWritePackageListLocked(userId);
            applyFactoryDefaultBrowserLPw(userId);
            primeDomainVerificationsLPw(userId);
        }
    }
@@ -23959,6 +23914,14 @@ public class PackageManagerService extends IPackageManager.Stub
        public void finishPackageInstall(int token, boolean didLaunch) {
            PackageManagerService.this.finishPackageInstall(token, didLaunch);
        }
        @Nullable
        @Override
        public String removeLegacyDefaultBrowserPackageName(int userId) {
            synchronized (mPackages) {
                return mSettings.removeDefaultBrowserPackageNameLPw(userId);
            }
        }
    }
    @GuardedBy("mPackages")
+2 −15
Original line number Diff line number Diff line
@@ -1327,21 +1327,8 @@ public final class Settings {
        return result;
    }

    boolean setDefaultBrowserPackageNameLPw(String packageName, int userId) {
        if (userId == UserHandle.USER_ALL) {
            return false;
        }
        if (packageName != null) {
            mDefaultBrowserApp.put(userId, packageName);
        } else {
            mDefaultBrowserApp.remove(userId);
        }
        writePackageRestrictionsLPr(userId);
        return true;
    }

    String getDefaultBrowserPackageNameLPw(int userId) {
        return (userId == UserHandle.USER_ALL) ? null : mDefaultBrowserApp.get(userId);
    String removeDefaultBrowserPackageNameLPw(int userId) {
        return (userId == UserHandle.USER_ALL) ? null : mDefaultBrowserApp.removeReturnOld(userId);
    }

    boolean setDefaultDialerPackageNameLPw(String packageName, int userId) {
Loading