summaryrefslogtreecommitdiffstats
path: root/src/com/android/packageinstaller/permission/ui/wear/AppPermissionsFragmentWear.java
blob: 6eac0c67d36a6589cfa050e55eb0efd55165d0be (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
/*
* 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.wear;

import android.Manifest;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.UserHandle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.preference.SwitchPreference;
import android.support.wearable.view.WearableDialogHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.packageinstaller.R;
import com.android.packageinstaller.permission.model.AppPermissionGroup;
import com.android.packageinstaller.permission.model.AppPermissions;
import com.android.packageinstaller.permission.utils.LocationUtils;
import com.android.packageinstaller.permission.utils.SafetyNetLogger;
import com.android.packageinstaller.permission.utils.Utils;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;

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

public final class AppPermissionsFragmentWear extends PreferenceFragment {
    private static final String LOG_TAG = "AppPermFragWear";

    private static final String KEY_NO_PERMISSIONS = "no_permissions";

    public static AppPermissionsFragmentWear newInstance(String packageName) {
        return setPackageName(new AppPermissionsFragmentWear(), packageName);
    }

    private static <T extends Fragment> T setPackageName(T fragment, String packageName) {
        Bundle arguments = new Bundle();
        arguments.putString(Intent.EXTRA_PACKAGE_NAME, packageName);
        fragment.setArguments(arguments);
        return fragment;
    }

    private List<AppPermissionGroup> mToggledGroups;
    private AppPermissions mAppPermissions;

    private boolean mHasConfirmedRevoke;

    /**
     * Provides click behavior for disabled preferences.
     * We can't use {@link PreferenceFragment#onPreferenceTreeClick}, as the base
     * {@link SwitchPreference} doesn't delegate to that method if the preference is disabled.
     */
    private static class PermissionSwitchPreference extends SwitchPreference {

        private final Activity mActivity;

        public PermissionSwitchPreference(Activity activity) {
            super(activity);
            this.mActivity = activity;
        }

        @Override
        public void performClick(PreferenceScreen preferenceScreen) {
            super.performClick(preferenceScreen);
            if (!isEnabled()) {
                // If setting the permission is disabled, it must have been locked
                // by the device or profile owner. So get that info and pass it to
                // the support details dialog.
                EnforcedAdmin deviceOrProfileOwner = RestrictedLockUtils.getProfileOrDeviceOwner(
                    mActivity, UserHandle.myUserId());
                RestrictedLockUtils.sendShowAdminSupportDetailsIntent(
                    mActivity, deviceOrProfileOwner);
            }
        }
    }

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

        String packageName = getArguments().getString(Intent.EXTRA_PACKAGE_NAME);
        Activity activity = getActivity();
        PackageManager pm = activity.getPackageManager();
        PackageInfo packageInfo;

        try {
            packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
        } catch (PackageManager.NameNotFoundException e) {
            Log.i(LOG_TAG, "No package:" + activity.getCallingPackage(), e);
            packageInfo = null;
        }

        if (packageInfo == null) {
            Toast.makeText(activity, R.string.app_not_found_dlg_title, Toast.LENGTH_LONG).show();
            activity.finish();
            return;
        }

        mAppPermissions = new AppPermissions(
                activity, packageInfo, null, true, () -> getActivity().finish());

        addPreferencesFromResource(R.xml.watch_permissions);
        initializePermissionGroupList();
    }

    @Override
    public void onResume() {
        super.onResume();
        mAppPermissions.refresh();

        // Also refresh the UI
        for (final AppPermissionGroup group : mAppPermissions.getPermissionGroups()) {
            Preference pref = findPreference(group.getName());
            if (pref instanceof SwitchPreference) {
                ((SwitchPreference) pref).setChecked(group.areRuntimePermissionsGranted());
            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        logAndClearToggledGroups();
    }

    private void initializePermissionGroupList() {
        final String packageName = mAppPermissions.getPackageInfo().packageName;
        List<AppPermissionGroup> groups = mAppPermissions.getPermissionGroups();
        List<SwitchPreference> nonSystemGroups = new ArrayList<>();

        if (!groups.isEmpty()) {
            getPreferenceScreen().removePreference(findPreference(KEY_NO_PERMISSIONS));
        }

        for (final AppPermissionGroup group : groups) {
            if (!Utils.shouldShowPermission(group, packageName)) {
                continue;
            }

            boolean isPlatform = group.getDeclaringPackage().equals(Utils.OS_PKG);

            final SwitchPreference pref = new PermissionSwitchPreference(getActivity());
            pref.setKey(group.getName());
            pref.setTitle(group.getLabel());
            pref.setChecked(group.areRuntimePermissionsGranted());

            if (group.isPolicyFixed()) {
                pref.setEnabled(false);
            } else {
                pref.setOnPreferenceChangeListener((p, newVal) -> {
                    if (LocationUtils.isLocationGroupAndProvider(
                            group.getName(), group.getApp().packageName)) {
                        LocationUtils.showLocationDialog(
                                getContext(), mAppPermissions.getAppLabel());
                        return false;
                    }

                    if ((Boolean) newVal) {
                        setPermission(group, pref, true);
                    } else {
                        final boolean grantedByDefault = group.hasGrantedByDefaultPermission();
                        if (grantedByDefault
                                || (!group.hasRuntimePermission() && !mHasConfirmedRevoke)) {
                            new WearableDialogHelper.DialogBuilder(getContext())
                                    .setNegativeIcon(R.drawable.confirm_button)
                                    .setPositiveIcon(R.drawable.cancel_button)
                                    .setNegativeButton(R.string.grant_dialog_button_deny_anyway,
                                            (dialog, which) -> {
                                                setPermission(group, pref, false);
                                                if (!group.hasGrantedByDefaultPermission()) {
                                                    mHasConfirmedRevoke = true;
                                                }
                                            })
                                    .setPositiveButton(R.string.cancel, (dialog, which) -> {})
                                    .setMessage(grantedByDefault ?
                                            R.string.system_warning : R.string.old_sdk_deny_warning)
                                    .show();
                            return false;
                        } else {
                            setPermission(group, pref, false);
                        }
                    }

                    return true;
                });
            }

            // The UI shows System settings first, then non-system settings
            if (isPlatform) {
                getPreferenceScreen().addPreference(pref);
            } else {
                nonSystemGroups.add(pref);
            }
        }

        // Now add the non-system settings to the end of the list
        for (SwitchPreference nonSystemGroup : nonSystemGroups) {
            getPreferenceScreen().addPreference(nonSystemGroup);
        }
    }

    private void setPermission(AppPermissionGroup group, SwitchPreference pref, boolean grant) {
        if (grant) {
            group.grantRuntimePermissions(false);
        } else {
            group.revokeRuntimePermissions(false);
        }
        addToggledGroup(group);
        pref.setChecked(grant);
    }

    private void addToggledGroup(AppPermissionGroup group) {
        if (mToggledGroups == null) {
            mToggledGroups = new ArrayList<>();
        }
        // Double toggle is back to initial state.
        if (mToggledGroups.contains(group)) {
            mToggledGroups.remove(group);
        } else {
            mToggledGroups.add(group);
        }
    }

    private void logAndClearToggledGroups() {
        if (mToggledGroups != null) {
            String packageName = mAppPermissions.getPackageInfo().packageName;
            SafetyNetLogger.logPermissionsToggled(packageName, mToggledGroups);
            mToggledGroups = null;
        }
    }
}