Home | History | Annotate | Download | only in usepermission
      1 /*
      2  * Copyright (C) 2018 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.cts.usepermission;
     18 
     19 import android.app.IntentService;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.pm.PackageManager;
     23 import android.os.Bundle;
     24 import android.os.ResultReceiver;
     25 
     26 /**
     27  * A service that can check if a permission is currently granted
     28  */
     29 public class PermissionCheckerService extends IntentService {
     30     private final String REVIEW_PERMISSION_PKG = "com.android.cts.reviewpermissionhelper";
     31 
     32     public PermissionCheckerService() {
     33         super(PermissionCheckerService.class.getSimpleName());
     34     }
     35 
     36     @Override
     37     protected void onHandleIntent(Intent intent) {
     38         // Load bundle with context of client package so ResultReceiver class can be resolved
     39         Context context;
     40         try {
     41             context = createPackageContext(REVIEW_PERMISSION_PKG,
     42                     CONTEXT_INCLUDE_CODE | CONTEXT_IGNORE_SECURITY);
     43         } catch (PackageManager.NameNotFoundException e) {
     44             throw new IllegalStateException("Cannot find client package " + REVIEW_PERMISSION_PKG);
     45         }
     46         ClassLoader cl = context.getClassLoader();
     47         Bundle bundle = intent.getExtras();
     48         bundle.setClassLoader(cl);
     49 
     50         ResultReceiver result = bundle.getParcelable(getPackageName() + ".RESULT");
     51         String permission = bundle.getString(getPackageName() + ".PERMISSION");
     52 
     53         result.send(checkSelfPermission(permission), null);
     54     }
     55 }
     56