summaryrefslogtreecommitdiffstats
path: root/src/com/android/packageinstaller/permission/ui/ReviewPermissionsActivity.java
blob: f31d618e083f44825fcc17911910b4467e185861 (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * Copyright (C) 2015 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.permission.ui;

import android.annotation.Nullable;
import android.app.Activity;

import android.app.DialogFragment;
import android.app.Fragment;
import android.content.Intent;
import android.content.IntentSender;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.RemoteCallback;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
import android.preference.PreferenceGroup;
import android.preference.PreferenceScreen;
import android.preference.SwitchPreference;
import android.preference.TwoStatePreference;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.packageinstaller.R;
import com.android.packageinstaller.permission.model.AppPermissionGroup;
import com.android.packageinstaller.permission.model.AppPermissions;
import com.android.packageinstaller.util.Utils;
import com.android.packageinstaller.permission.ui.ConfirmActionDialogFragment.OnActionConfirmedListener;

import java.util.List;

public final class ReviewPermissionsActivity extends Activity
        implements OnActionConfirmedListener {

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

        PackageInfo packageInfo = getTargetPackageInfo();
        if (packageInfo == null) {
            finish();
            return;
        }

        setContentView(R.layout.review_permissions);
        if (getFragmentManager().findFragmentById(R.id.preferences_frame) == null) {
            getFragmentManager().beginTransaction().add(R.id.preferences_frame,
                    ReviewPermissionsFragment.newInstance(packageInfo)).commit();
        }
    }

    @Override
    public void onActionConfirmed(String action) {
        Fragment fragment = getFragmentManager().findFragmentById(R.id.preferences_frame);
        if (fragment instanceof OnActionConfirmedListener) {
            ((OnActionConfirmedListener) fragment).onActionConfirmed(action);
        }
    }

    private PackageInfo getTargetPackageInfo() {
        String packageName = getIntent().getStringExtra(Intent.EXTRA_PACKAGE_NAME);
        if (TextUtils.isEmpty(packageName)) {
            return null;
        }
        try {
            return getPackageManager().getPackageInfo(packageName,
                    PackageManager.GET_PERMISSIONS);
        } catch (PackageManager.NameNotFoundException e) {
            return null;
        }
    }

    public static final class ReviewPermissionsFragment extends PreferenceFragment
            implements View.OnClickListener, Preference.OnPreferenceChangeListener,
            ConfirmActionDialogFragment.OnActionConfirmedListener {
        public static final String EXTRA_PACKAGE_INFO =
                "com.android.packageinstaller.permission.ui.extra.PACKAGE_INFO";

        private AppPermissions mAppPermissions;

        private Button mContinueButton;
        private Button mCancelButton;

        private PreferenceCategory mNewPermissionsCategory;

        private boolean mHasConfirmedRevoke;

        public static ReviewPermissionsFragment newInstance(PackageInfo packageInfo) {
            Bundle arguments = new Bundle();
            arguments.putParcelable(ReviewPermissionsFragment.EXTRA_PACKAGE_INFO, packageInfo);
            ReviewPermissionsFragment instance = new ReviewPermissionsFragment();
            instance.setArguments(arguments);
            instance.setRetainInstance(true);
            return instance;
        }

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

            Activity activity = getActivity();
            if (activity == null) {
                return;
            }

            PackageInfo packageInfo = getArguments().getParcelable(EXTRA_PACKAGE_INFO);
            if (packageInfo == null) {
                activity.finish();
                return;
            }

            mAppPermissions = new AppPermissions(activity, packageInfo, null, false,
                    new Runnable() {
                        @Override
                        public void run() {
                            getActivity().finish();
                        }
                    });

            if (mAppPermissions.getPermissionGroups().isEmpty()) {
                activity.finish();
                return;
            }

            boolean reviewRequired= false;
            for (AppPermissionGroup group : mAppPermissions.getPermissionGroups()) {
                if (group.isReviewRequired()) {
                    reviewRequired = true;
                    break;
                }
            }

            if (!reviewRequired) {
                activity.finish();
            }
        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            bindUi();
        }

        @Override
        public void onResume() {
            super.onResume();
            loadPreferences();
        }

        @Override
        public void onClick(View view) {
            Activity activity = getActivity();
            if (activity == null) {
                return;
            }
            if (view == mContinueButton) {
                confirmPermissionsReview();
                executeCallback(true);
            } else if (view == mCancelButton) {
                executeCallback(false);
                activity.setResult(Activity.RESULT_CANCELED);
            }
            activity.finish();
        }

        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            if (mHasConfirmedRevoke) {
                return true;
            }
            if (preference instanceof SwitchPreference) {
                SwitchPreference switchPreference = (SwitchPreference) preference;
                if (switchPreference.isChecked()) {
                    showWarnRevokeDialog(switchPreference.getKey());
                } else {
                    return true;
                }
            }
            return false;
        }

        @Override
        public void onActionConfirmed(String action) {
            Preference preference = getPreferenceManager().findPreference(action);
            if (preference instanceof SwitchPreference) {
                SwitchPreference switchPreference = (SwitchPreference) preference;
                switchPreference.setChecked(false);
                mHasConfirmedRevoke = true;
            }
        }

        private void showWarnRevokeDialog(final String groupName) {
            DialogFragment fragment = ConfirmActionDialogFragment.newInstance(
                    getString(R.string.old_sdk_deny_warning), groupName);
            fragment.show(getFragmentManager(), fragment.getClass().getName());
        }

        private void confirmPermissionsReview() {
            PreferenceGroup preferenceGroup = mNewPermissionsCategory != null
                ? mNewPermissionsCategory : getPreferenceScreen();

            final int preferenceCount = preferenceGroup.getPreferenceCount();
            for (int i = 0; i < preferenceCount; i++) {
                Preference preference = preferenceGroup.getPreference(i);
                if (preference instanceof TwoStatePreference) {
                    TwoStatePreference twoStatePreference = (TwoStatePreference) preference;
                    String groupName = preference.getKey();
                    AppPermissionGroup group = mAppPermissions.getPermissionGroup(groupName);
                    if (twoStatePreference.isChecked()) {
                        group.grantRuntimePermissions(false);
                    } else {
                        group.revokeRuntimePermissions(false);
                    }
                    group.resetReviewRequired();
                }
            }
        }

        private void bindUi() {
            Activity activity = getActivity();

            // Set icon
            Drawable icon = mAppPermissions.getPackageInfo().applicationInfo.loadIcon(
                    activity.getPackageManager());
            ImageView iconView = (ImageView) activity.findViewById(R.id.app_icon);
            iconView.setImageDrawable(icon);

            // Set message
            String appLabel = mAppPermissions.getAppLabel().toString();
            final int labelTemplateResId = isPackageUpdated()
                    ?  R.string.permission_review_title_template_update
                    :  R.string.permission_review_title_template_install;
            SpannableString message = new SpannableString(getString(labelTemplateResId, appLabel));
            // Set the permission message as the title so it can be announced.
            activity.setTitle(message);

            // Color the app name.
            final int appLabelStart = message.toString().indexOf(appLabel, 0);
            final int appLabelLength = appLabel.length();

            TypedValue typedValue = new TypedValue();
            activity.getTheme().resolveAttribute(android.R.attr.colorAccent, typedValue, true);
            final int color = activity.getColor(typedValue.resourceId);

            message.setSpan(new ForegroundColorSpan(color), appLabelStart,
                    appLabelStart + appLabelLength, 0);
            TextView permissionsMessageView = (TextView) activity.findViewById(
                    R.id.permissions_message);
            permissionsMessageView.setText(message);


            mContinueButton = (Button) getActivity().findViewById(R.id.continue_button);
            mContinueButton.setOnClickListener(this);

            mCancelButton = (Button) getActivity().findViewById(R.id.cancel_button);
            mCancelButton.setOnClickListener(this);
        }

        private void loadPreferences() {
            PreferenceScreen screen = getPreferenceScreen();
            if (screen == null) {
                screen = getPreferenceManager().createPreferenceScreen(getActivity());
                setPreferenceScreen(screen);
            } else {
                screen.removeAll();
            }

            PreferenceGroup currentPermissionsCategory = null;
            PreferenceGroup oldNewPermissionsCategory = mNewPermissionsCategory;
            mNewPermissionsCategory = null;

            final boolean isPackageUpdated = isPackageUpdated();

            for (AppPermissionGroup group : mAppPermissions.getPermissionGroups()) {
                if (!Utils.shouldShowPermission(group,
                        mAppPermissions.getPackageInfo().packageName)) {
                    continue;
                }

                // TODO: Sort permissions - platform first then third-party ones

                final SwitchPreference preference;
                Preference cachedPreference = oldNewPermissionsCategory != null
                        ? oldNewPermissionsCategory.findPreference(group.getName()) : null;
                if (cachedPreference instanceof SwitchPreference) {
                    preference = (SwitchPreference) cachedPreference;
                } else {
                    preference = new SwitchPreference(getActivity());

                    // We update permission grants based on the final preference states
                    if (group.isReviewRequired()) {
                        // If review is required use granted as default
                        preference.setChecked(true);
                    } else {
                        // If review not required use the current grant state as default
                        preference.setChecked(group.areRuntimePermissionsGranted());
                    }

                    preference.setKey(group.getName());
                    Drawable icon = Utils.loadDrawable(getActivity().getPackageManager(),
                            group.getIconPkg(), group.getIconResId());
                    preference.setIcon(Utils.applyTint(getContext(), icon,
                            android.R.attr.colorControlNormal));
                    preference.setTitle(group.getLabel());
                    preference.setSummary(group.getDescription());
                    preference.setPersistent(false);

                    preference.setOnPreferenceChangeListener(this);
                }

                // Mutable state
                if (group.isPolicyFixed()) {
                    preference.setEnabled(false);
                    preference.setSummary(getString(
                            R.string.permission_summary_enforced_by_policy));
                } else {
                    preference.setEnabled(true);
                }

                if (group.isReviewRequired()) {
                    if (!isPackageUpdated) {
                        screen.addPreference(preference);
                    } else {
                        if (mNewPermissionsCategory == null) {
                            mNewPermissionsCategory = new PreferenceCategory(getActivity());
                            mNewPermissionsCategory.setTitle(R.string.new_permissions_category);
                            mNewPermissionsCategory.setOrder(1);
                            screen.addPreference(mNewPermissionsCategory);
                        }
                        mNewPermissionsCategory.addPreference(preference);
                    }
                } else {
                    if (currentPermissionsCategory == null) {
                        currentPermissionsCategory = new PreferenceCategory(getActivity());
                        currentPermissionsCategory.setTitle(R.string.current_permissions_category);
                        currentPermissionsCategory.setOrder(2);
                        screen.addPreference(currentPermissionsCategory);
                    }
                    currentPermissionsCategory.addPreference(preference);
                }
            }
        }

        private boolean isPackageUpdated() {
            List<AppPermissionGroup> groups = mAppPermissions.getPermissionGroups();
            final int groupCount = groups.size();
            for (int i = 0; i < groupCount; i++) {
                AppPermissionGroup group = groups.get(i);
                if (!group.isReviewRequired()) {
                    return true;
                }
            }
            return false;
        }

        private void executeCallback(boolean success) {
            Activity activity = getActivity();
            if (success) {
                IntentSender intent = activity.getIntent().getParcelableExtra(Intent.EXTRA_INTENT);
                if (intent != null) {
                    try {
                        int flagMask = 0;
                        int flagValues = 0;
                        if (activity.getIntent().getBooleanExtra(
                                Intent.EXTRA_RESULT_NEEDED, false)) {
                            flagMask = Intent.FLAG_ACTIVITY_FORWARD_RESULT;
                            flagValues = Intent.FLAG_ACTIVITY_FORWARD_RESULT;
                        }
                        activity.startIntentSenderForResult(intent, -1, null,
                                flagMask, flagValues, 0);
                    } catch (IntentSender.SendIntentException e) {
                        /* ignore */
                    }
                    return;
                }
            }
            RemoteCallback callback = activity.getIntent().getParcelableExtra(
                    Intent.EXTRA_REMOTE_CALLBACK);
            if (callback != null) {
                Bundle result = new Bundle();
                result.putBoolean(Intent.EXTRA_RETURN_RESULT, success);
                callback.sendResult(result);
            }
        }
    }
}