Home | History | Annotate | Download | only in service
      1 /*
      2  * Copyright (C) 2016 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.service;
     18 
     19 import android.content.pm.PackageInfo;
     20 import android.content.pm.PackageManager;
     21 import android.content.pm.permission.RuntimePermissionPresentationInfo;
     22 import android.permissionpresenterservice.RuntimePermissionPresenterService;
     23 import android.util.Log;
     24 
     25 import com.android.packageinstaller.permission.model.AppPermissionGroup;
     26 import com.android.packageinstaller.permission.model.AppPermissions;
     27 import com.android.packageinstaller.permission.utils.Utils;
     28 
     29 import java.util.ArrayList;
     30 import java.util.List;
     31 
     32 /**
     33  * Service that provides presentation information for runtime permissions.
     34  */
     35 public final class RuntimePermissionPresenterServiceImpl extends RuntimePermissionPresenterService {
     36     private static final String LOG_TAG = "PermissionPresenter";
     37 
     38     @Override
     39     public List<RuntimePermissionPresentationInfo> onGetAppPermissions(String packageName) {
     40         final PackageInfo packageInfo;
     41         try {
     42             packageInfo = getPackageManager().getPackageInfo(packageName,
     43                     PackageManager.GET_PERMISSIONS);
     44         } catch (PackageManager.NameNotFoundException e) {
     45             Log.e(LOG_TAG, "Error getting package:" + packageName, e);
     46             return null;
     47         }
     48 
     49         List<RuntimePermissionPresentationInfo> permissions = new ArrayList<>();
     50 
     51         AppPermissions appPermissions = new AppPermissions(this, packageInfo, null, false, null);
     52         for (AppPermissionGroup group : appPermissions.getPermissionGroups()) {
     53             if (Utils.shouldShowPermission(group, packageName)) {
     54                 final boolean granted = group.areRuntimePermissionsGranted();
     55                 final boolean standard = Utils.OS_PKG.equals(group.getDeclaringPackage());
     56                 RuntimePermissionPresentationInfo permission =
     57                         new RuntimePermissionPresentationInfo(group.getLabel(),
     58                                 granted, standard);
     59                 permissions.add(permission);
     60             }
     61         }
     62 
     63         return permissions;
     64     }
     65 
     66     @Override
     67     public void onRevokeRuntimePermission(String packageName, String permissionName) {
     68         try {
     69             final PackageInfo packageInfo = getPackageManager().getPackageInfo(packageName,
     70                     PackageManager.GET_PERMISSIONS);
     71             final AppPermissions appPermissions = new AppPermissions(this, packageInfo, null, false,
     72                     null);
     73 
     74             final AppPermissionGroup appPermissionGroup = appPermissions.getGroupForPermission(
     75                     permissionName);
     76 
     77             if (appPermissionGroup != null) {
     78                 appPermissionGroup.revokeRuntimePermissions(false);
     79             }
     80         } catch (PackageManager.NameNotFoundException e) {
     81             Log.e(LOG_TAG, "Error getting package:" + packageName, e);
     82         }
     83     }
     84 }
     85