1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17 package com.android.packageinstaller.permission.ui;
18 
19 import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
20 
21 import static com.android.packageinstaller.Constants.INVALID_SESSION_ID;
22 
23 import android.content.Intent;
24 import android.os.Bundle;
25 import android.os.UserHandle;
26 import android.util.Log;
27 import android.view.MenuItem;
28 
29 import androidx.fragment.app.Fragment;
30 import androidx.fragment.app.FragmentActivity;
31 
32 import com.android.packageinstaller.Constants;
33 import com.android.packageinstaller.DeviceUtils;
34 import com.android.packageinstaller.permission.ui.auto.AutoAllAppPermissionsFragment;
35 import com.android.packageinstaller.permission.ui.auto.AutoAppPermissionsFragment;
36 import com.android.packageinstaller.permission.ui.auto.AutoManageStandardPermissionsFragment;
37 import com.android.packageinstaller.permission.ui.auto.AutoPermissionAppsFragment;
38 import com.android.packageinstaller.permission.ui.handheld.ManageStandardPermissionsFragment;
39 import com.android.packageinstaller.permission.ui.wear.AppPermissionsFragmentWear;
40 import com.android.permissioncontroller.R;
41 
42 import java.util.Random;
43 
44 public final class ManagePermissionsActivity extends FragmentActivity {
45     private static final String LOG_TAG = ManagePermissionsActivity.class.getSimpleName();
46 
47     public static final String EXTRA_ALL_PERMISSIONS =
48             "com.android.packageinstaller.extra.ALL_PERMISSIONS";
49 
50     @Override
onCreate(Bundle savedInstanceState)51     public void onCreate(Bundle savedInstanceState) {
52         if (DeviceUtils.isAuto(this)) {
53             // Automotive relies on a different theme. Apply before calling super so that
54             // fragments are restored properly on configuration changes.
55             setTheme(R.style.CarSettings);
56         }
57         super.onCreate(savedInstanceState);
58 
59         // If there is a previous instance, re-use its Fragment instead of making a new one.
60         if (savedInstanceState != null) {
61             return;
62         }
63 
64         android.app.Fragment fragment = null;
65         Fragment androidXFragment = null;
66         String action = getIntent().getAction();
67 
68         getWindow().addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
69 
70         long sessionId = getIntent().getLongExtra(Constants.EXTRA_SESSION_ID, INVALID_SESSION_ID);
71         while (sessionId == INVALID_SESSION_ID) {
72             sessionId = new Random().nextLong();
73         }
74 
75         String permissionName;
76         switch (action) {
77             case Intent.ACTION_MANAGE_PERMISSIONS:
78                 if (DeviceUtils.isAuto(this)) {
79                     androidXFragment = AutoManageStandardPermissionsFragment.newInstance();
80                 } else if (DeviceUtils.isTelevision(this)) {
81                     fragment =
82                             com.android.packageinstaller.permission.ui.television
83                                     .ManagePermissionsFragment.newInstance();
84                 } else {
85                     androidXFragment = ManageStandardPermissionsFragment.newInstance(sessionId);
86                 }
87                 break;
88 
89             case Intent.ACTION_REVIEW_PERMISSION_USAGE:
90                 finish();
91                 return;
92 
93             case Intent.ACTION_MANAGE_APP_PERMISSIONS: {
94                 String packageName = getIntent().getStringExtra(Intent.EXTRA_PACKAGE_NAME);
95                 if (packageName == null) {
96                     Log.i(LOG_TAG, "Missing mandatory argument EXTRA_PACKAGE_NAME");
97                     finish();
98                     return;
99                 }
100 
101                 final boolean allPermissions = getIntent().getBooleanExtra(
102                         EXTRA_ALL_PERMISSIONS, false);
103 
104                 UserHandle userHandle = getIntent().getParcelableExtra(Intent.EXTRA_USER);
105                 if (userHandle == null) {
106                     userHandle = UserHandle.of(UserHandle.myUserId());
107                 }
108 
109                 if (DeviceUtils.isAuto(this)) {
110                     if (allPermissions) {
111                         androidXFragment = AutoAllAppPermissionsFragment.newInstance(packageName,
112                                 userHandle);
113                     } else {
114                         androidXFragment = AutoAppPermissionsFragment.newInstance(packageName,
115                                 userHandle);
116                     }
117                 } else if (DeviceUtils.isWear(this)) {
118                     androidXFragment = AppPermissionsFragmentWear.newInstance(packageName);
119                 } else if (DeviceUtils.isTelevision(this)) {
120                     fragment = com.android.packageinstaller.permission.ui.television
121                             .AppPermissionsFragment.newInstance(packageName);
122                 } else {
123                     if (allPermissions) {
124                         androidXFragment = com.android.packageinstaller.permission.ui.handheld
125                                 .AllAppPermissionsFragment.newInstance(packageName, userHandle);
126                     } else {
127                         androidXFragment = com.android.packageinstaller.permission.ui.handheld
128                                 .AppPermissionsFragment.newInstance(
129                                         packageName, userHandle, sessionId);
130                     }
131                 }
132             } break;
133 
134             case Intent.ACTION_MANAGE_PERMISSION_APPS: {
135                 permissionName = getIntent().getStringExtra(Intent.EXTRA_PERMISSION_NAME);
136 
137                 if (permissionName == null) {
138                     Log.i(LOG_TAG, "Missing mandatory argument EXTRA_PERMISSION_NAME");
139                     finish();
140                     return;
141                 }
142                 if (DeviceUtils.isAuto(this)) {
143                     androidXFragment = AutoPermissionAppsFragment.newInstance(permissionName);
144                 } else if (DeviceUtils.isTelevision(this)) {
145                     fragment = com.android.packageinstaller.permission.ui.television
146                             .PermissionAppsFragment.newInstance(permissionName);
147                 } else {
148                     androidXFragment = com.android.packageinstaller.permission.ui.handheld
149                             .PermissionAppsFragment.newInstance(permissionName, sessionId);
150                 }
151             } break;
152 
153             default: {
154                 Log.w(LOG_TAG, "Unrecognized action " + action);
155                 finish();
156                 return;
157             }
158         }
159 
160         if (fragment != null) {
161             getFragmentManager().beginTransaction().replace(android.R.id.content, fragment)
162                     .commit();
163         } else {
164             getSupportFragmentManager().beginTransaction().replace(android.R.id.content,
165                     androidXFragment).commit();
166         }
167     }
168 
169     @Override
onOptionsItemSelected(MenuItem item)170     public boolean onOptionsItemSelected(MenuItem item) {
171         // in automotive mode, there's no system wide back button, so need to add that
172         if (DeviceUtils.isAuto(this)) {
173             switch (item.getItemId()) {
174                 case android.R.id.home:
175                     onBackPressed();
176                     return true;
177                 default:
178                     return super.onOptionsItemSelected(item);
179             }
180         }
181         return super.onOptionsItemSelected(item);
182     }
183 }
184