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

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

Merge changes from topics "role-dialer-dialog", "role-sms-dialog" into qt-dev

* changes:
  Replace ChangeDefaultDialerDialog with RequestRoleActivity.
  Replace SmsDefaultDialog with RequestRoleActivity.
parents a267cbec 00b3239f
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -179,11 +179,14 @@
                <action android:name="android.app.role.action.REQUEST_ROLE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <!-- TODO: STOPSHIP: give this a priority greater than 1 to override Settings. -->
            <intent-filter android:priority="0">
            <intent-filter android:priority="2">
                <action android:name="android.provider.Telephony.ACTION_CHANGE_DEFAULT" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter android:priority="1001">
                <action android:name="android.telecom.action.CHANGE_DEFAULT_DIALER" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity android:name="com.android.packageinstaller.role.ui.DefaultAppListActivity"
+89 −27
Original line number Diff line number Diff line
@@ -20,9 +20,10 @@ import android.app.role.RoleManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Process;
import android.provider.Telephony;
import android.telecom.TelecomManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.WindowManager;
@@ -32,6 +33,7 @@ import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.android.packageinstaller.PermissionControllerStatsLog;
import com.android.packageinstaller.permission.utils.CollectionUtils;
import com.android.packageinstaller.role.model.Role;
import com.android.packageinstaller.role.model.Roles;
import com.android.packageinstaller.role.model.UserDeniedManager;
@@ -60,7 +62,19 @@ public class RequestRoleActivity extends FragmentActivity {
        mRoleName = getIntent().getStringExtra(Intent.EXTRA_ROLE_NAME);
        mPackageName = getCallingPackage();

        ensureSmsDefaultDialogCompatibility();
        if (!handleChangeDefaultDialerDialogCompatibility()) {
            reportRequestResult(
                    PermissionControllerStatsLog.ROLE_REQUEST_RESULT_REPORTED__RESULT__IGNORED);
            finish();
            return;
        }

        if (!handleSmsDefaultDialogCompatibility()) {
            reportRequestResult(
                    PermissionControllerStatsLog.ROLE_REQUEST_RESULT_REPORTED__RESULT__IGNORED);
            finish();
            return;
        }

        if (TextUtils.isEmpty(mRoleName)) {
            Log.w(LOG_TAG, "Role name cannot be null or empty: " + mRoleName);
@@ -167,47 +181,95 @@ public class RequestRoleActivity extends FragmentActivity {
    }

    /**
     * @see com.android.settings.SmsDefaultDialog
     * Handle compatibility with the old
     * {@link com.android.server.telecom.components.ChangeDefaultDialerDialog}.
     *
     * @return whether we should continue requesting the role. The activity should be finished if
     *         {@code false} is returned.
     */
    private void ensureSmsDefaultDialogCompatibility() {
    private boolean handleChangeDefaultDialerDialogCompatibility() {
        Intent intent = getIntent();
        if (!Objects.equals(intent.getAction(), Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT)) {
            return;
        }
        if (intent.hasExtra(Intent.EXTRA_ROLE_NAME)) {
            // Don't allow calling legacy interface with a role name.
            return;
        if (!Objects.equals(intent.getAction(), TelecomManager.ACTION_CHANGE_DEFAULT_DIALER)) {
            return true;
        }

        Log.w(LOG_TAG, "Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT is deprecated; please use"
        Log.w(LOG_TAG, "TelecomManager.ACTION_CHANGE_DEFAULT_DIALER is deprecated; please use"
                + " RoleManager.createRequestRoleIntent() and Activity.startActivityForResult()"
                + " instead");
        if (intent.hasExtra(Intent.EXTRA_PACKAGE_NAME)) {
            Log.w(LOG_TAG, "Intent.EXTRA_PACKAGE_NAME is deprecated, and will be ignored in most"
                    + " cases. For SMS backup, please use the new backup role instead.");
        }

        mRoleName = null;
        mRoleName = RoleManager.ROLE_DIALER;
        mPackageName = null;

        String packageName = getCallingPackage();
        if (packageName == null) {
            return;
        String callingPackageName = getCallingPackage();
        String extraPackageName = intent.getStringExtra(
                TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME);
        if (Objects.equals(extraPackageName, callingPackageName)) {
            // Requesting for itself is okay.
            mPackageName = extraPackageName;
            return true;
        }
        ApplicationInfo applicationInfo = PackageUtils.getApplicationInfo(packageName, this);
        if (applicationInfo == null || applicationInfo.targetSdkVersion >= Build.VERSION_CODES.Q) {
            return;

        RoleManager roleManager = getSystemService(RoleManager.class);
        String holderPackageName = CollectionUtils.firstOrNull(roleManager.getRoleHolders(
                RoleManager.ROLE_DIALER));
        if (Objects.equals(callingPackageName, holderPackageName)) {
            // Giving away its own role is okay.
            mPackageName = extraPackageName;
            return true;
        }

        // If we reach here it's not okay.
        return false;
    }

    /**
     * Handle compatibility with the old {@link com.android.settings.SmsDefaultDialog}.
     *
     * @return whether we should continue requesting the role. The activity should be finished if
     *         {@code false} is returned.
     */
    private boolean handleSmsDefaultDialogCompatibility() {
        Intent intent = getIntent();
        if (!Objects.equals(intent.getAction(), Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT)) {
            return true;
        }

        Log.w(LOG_TAG, "Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT is deprecated; please use"
                + " RoleManager.createRequestRoleIntent() and Activity.startActivityForResult()"
                + " instead");

        mRoleName = RoleManager.ROLE_SMS;
        mPackageName = packageName;
        mPackageName = null;

        RoleManager roleManager = getSystemService(RoleManager.class);
        if (roleManager.getRoleHolders(RoleManager.ROLE_SMS).contains(mPackageName)) {
            if (intent.hasExtra(Intent.EXTRA_PACKAGE_NAME)) {
                mPackageName = intent.getStringExtra(Intent.EXTRA_PACKAGE_NAME);
        String callingPackageName = getCallingPackage();
        String extraPackageName = intent.getStringExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME);
        if (extraPackageName == null) {
            // Launch the settings activity to show the list.
            // TODO: Return RESULT_OK if any changes were made?
            Intent defaultAppActivityIntent = DefaultAppActivity.createIntent(
                    RoleManager.ROLE_SMS, Process.myUserHandle(), this)
                    .addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
            startActivity(defaultAppActivityIntent);
            return false;
        }

        if (Objects.equals(extraPackageName, callingPackageName)) {
            // Requesting for itself is okay.
            mPackageName = extraPackageName;
            return true;
        }

        RoleManager roleManager = getSystemService(RoleManager.class);
        String holderPackageName = CollectionUtils.firstOrNull(roleManager.getRoleHolders(
                RoleManager.ROLE_SMS));
        if (Objects.equals(callingPackageName, holderPackageName)) {
            // Giving away its own role is okay.
            mPackageName = extraPackageName;
            return true;
        }

        // If we reach here it's not okay.
        return false;
    }

    private void reportRequestResult(int result) {