summaryrefslogtreecommitdiffstats
path: root/src/com/android/packageinstaller/role/ui/RequestRoleActivity.java
blob: c57dfef2d7c46a4bfa86cc9115f22024d8698e56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * Copyright (C) 2018 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.ui;

import android.app.role.RoleManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
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;

import androidx.annotation.NonNull;
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;
import com.android.packageinstaller.role.utils.PackageUtils;

import java.util.List;
import java.util.Objects;

/**
 * {@code Activity} for a role request.
 */
public class RequestRoleActivity extends FragmentActivity {

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

    private String mRoleName;
    private String mPackageName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().addSystemFlags(
                WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);

        mRoleName = getIntent().getStringExtra(Intent.EXTRA_ROLE_NAME);
        mPackageName = getCallingPackage();

        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);
            reportRequestResult(
                    PermissionControllerStatsLog.ROLE_REQUEST_RESULT_REPORTED__RESULT__IGNORED);
            finish();
            return;
        }
        if (TextUtils.isEmpty(mPackageName)) {
            Log.w(LOG_TAG, "Package name cannot be null or empty: " + mPackageName);
            reportRequestResult(
                    PermissionControllerStatsLog.ROLE_REQUEST_RESULT_REPORTED__RESULT__IGNORED);
            finish();
            return;
        }

        // Perform checks here so that we have a chance to finish without being visible to user.
        Role role = Roles.get(this).get(mRoleName);
        if (role == null) {
            Log.w(LOG_TAG, "Unknown role: " + mRoleName);
            reportRequestResult(
                    PermissionControllerStatsLog.ROLE_REQUEST_RESULT_REPORTED__RESULT__IGNORED);
            finish();
            return;
        }

        if (!role.isAvailable(this)) {
            Log.e(LOG_TAG, "Role is unavailable: " + mRoleName);
            reportRequestResult(
                    PermissionControllerStatsLog.ROLE_REQUEST_RESULT_REPORTED__RESULT__IGNORED);
            finish();
            return;
        }

        if (!role.isVisible(this)) {
            Log.e(LOG_TAG, "Role is invisible: " + mRoleName);
            reportRequestResult(
                    PermissionControllerStatsLog.ROLE_REQUEST_RESULT_REPORTED__RESULT__IGNORED);
            finish();
            return;
        }

        if (!role.isRequestable()) {
            Log.e(LOG_TAG, "Role is not requestable: " + mRoleName);
            reportRequestResult(
                    PermissionControllerStatsLog.ROLE_REQUEST_RESULT_REPORTED__RESULT__IGNORED);
            finish();
            return;
        }

        if (!role.isExclusive()) {
            Log.e(LOG_TAG, "Role is not exclusive: " + mRoleName);
            reportRequestResult(
                    PermissionControllerStatsLog.ROLE_REQUEST_RESULT_REPORTED__RESULT__IGNORED);
            finish();
            return;
        }

        if (PackageUtils.getApplicationInfo(mPackageName, this) == null) {
            Log.w(LOG_TAG, "Unknown application: " + mPackageName);
            reportRequestResult(
                    PermissionControllerStatsLog.ROLE_REQUEST_RESULT_REPORTED__RESULT__IGNORED);
            finish();
            return;
        }

        RoleManager roleManager = getSystemService(RoleManager.class);
        List<String> currentPackageNames = roleManager.getRoleHolders(mRoleName);
        if (currentPackageNames.contains(mPackageName)) {
            Log.i(LOG_TAG, "Application is already a role holder, role: " + mRoleName
                    + ", package: " + mPackageName);
            reportRequestResult(PermissionControllerStatsLog
                    .ROLE_REQUEST_RESULT_REPORTED__RESULT__IGNORED_ALREADY_GRANTED);
            setResult(RESULT_OK);
            finish();
            return;
        }

        if (!role.isPackageQualified(mPackageName, this)) {
            Log.w(LOG_TAG, "Application doesn't qualify for role, role: " + mRoleName
                    + ", package: " + mPackageName);
            reportRequestResult(PermissionControllerStatsLog
                    .ROLE_REQUEST_RESULT_REPORTED__RESULT__IGNORED_NOT_QUALIFIED);
            finish();
            return;
        }

        if (UserDeniedManager.getInstance(this).isDeniedAlways(mRoleName, mPackageName)) {
            Log.w(LOG_TAG, "Application is denied always for role, role: " + mRoleName
                    + ", package: " + mPackageName);
            reportRequestResult(PermissionControllerStatsLog
                    .ROLE_REQUEST_RESULT_REPORTED__RESULT__IGNORED_USER_ALWAYS_DENIED);
            finish();
            return;
        }

        if (savedInstanceState == null) {
            RequestRoleFragment fragment = RequestRoleFragment.newInstance(mRoleName, mPackageName);
            getSupportFragmentManager().beginTransaction()
                    .add(fragment, null)
                    .commit();
        }
    }

    /**
     * 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 boolean handleChangeDefaultDialerDialogCompatibility() {
        Intent intent = getIntent();
        if (!Objects.equals(intent.getAction(), TelecomManager.ACTION_CHANGE_DEFAULT_DIALER)) {
            return true;
        }

        Log.w(LOG_TAG, "TelecomManager.ACTION_CHANGE_DEFAULT_DIALER is deprecated; please use"
                + " RoleManager.createRequestRoleIntent() and Activity.startActivityForResult()"
                + " instead");

        mRoleName = RoleManager.ROLE_DIALER;
        mPackageName = null;

        // Intent.EXTRA_CALLING_PACKAGE is set in PermissionPolicyService.Internal
        // .isActionRemovedForCallingPackage() and can be trusted.
        String callingPackageName = intent.getStringExtra(Intent.EXTRA_CALLING_PACKAGE);
        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;
        }

        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 = null;

        // Intent.EXTRA_CALLING_PACKAGE is set in PermissionPolicyService.Internal
        // .isActionRemovedForCallingPackage() and can be trusted.
        String callingPackageName = intent.getStringExtra(Intent.EXTRA_CALLING_PACKAGE);
        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) {
        RequestRoleFragment.reportRequestResult(getApplicationUid(mPackageName, this), mPackageName,
                mRoleName, -1, -1, null, -1, null, result);
    }

    private static int getApplicationUid(@Nullable String packageName, @NonNull Context context) {
        if (packageName == null) {
            return -1;
        }
        ApplicationInfo applicationInfo = PackageUtils.getApplicationInfo(packageName, context);
        if (applicationInfo == null) {
            return -1;
        }
        return applicationInfo.uid;
    }
}