• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.cts.permissionapp;
18 
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.util.Log;
23 
24 import java.lang.Override;
25 
26 /**
27  * A simple activity that requests permissions and returns the result.
28  */
29 public class PermissionActivity extends Activity {
30     private static final String TAG = "PermissionActivity";
31 
32     private static final String ACTION_CHECK_HAS_PERMISSION
33             = "com.android.cts.permission.action.CHECK_HAS_PERMISSION";
34     private static final String ACTION_REQUEST_PERMISSION
35             = "com.android.cts.permission.action.REQUEST_PERMISSION";
36     private static final String ACTION_PERMISSION_RESULT
37             = "com.android.cts.permission.action.PERMISSION_RESULT";
38     private static final String EXTRA_PERMISSION
39             = "com.android.cts.permission.extra.PERMISSION";
40     private static final String EXTRA_GRANT_STATE
41             = "com.android.cts.permission.extra.GRANT_STATE";
42     private static final int PERMISSION_ERROR = -2;
43     private static final int PERMISSIONS_REQUEST_CODE = 100;
44 
45     private String mPermission;
46 
47     @Override
onCreate(Bundle savedInstanceState)48     public void onCreate(Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50 
51         final Intent received = getIntent();
52         Log.d(TAG, "Started with " + received);
53 
54         final String action = received.getAction();
55         mPermission = received.getStringExtra(EXTRA_PERMISSION);
56         if (ACTION_REQUEST_PERMISSION.equals(action)) {
57             Log.d(TAG, "Requesting permission " + mPermission);
58             requestPermissions(new String[] {mPermission}, PERMISSIONS_REQUEST_CODE);
59         } else if (ACTION_CHECK_HAS_PERMISSION.equals(action)) {
60             Log.d(TAG, "Checking permission " + mPermission);
61             sendResultBroadcast(checkSelfPermission(mPermission));
62         } else {
63             Log.w(TAG, "Unknown intent received: " + received);
64             finish();
65         }
66     }
67 
68     @Override
onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)69     public void onRequestPermissionsResult(int requestCode, String[] permissions,
70             int[] grantResults) {
71         if (requestCode != PERMISSIONS_REQUEST_CODE ||
72                 permissions.length != 1 ||
73                 !permissions[0].equals(mPermission)) {
74             Log.d(TAG, "Received wrong permissions result");
75             sendResultBroadcast(PERMISSION_ERROR);
76         } else {
77             Log.d(TAG, "Received valid permission result: " + grantResults[0]);
78             sendResultBroadcast(grantResults[0]);
79         }
80     }
81 
sendResultBroadcast(int result)82     private void sendResultBroadcast(int result) {
83         Log.d(TAG, "Sending result broadcast: " + result);
84         Intent broadcast = new Intent(ACTION_PERMISSION_RESULT);
85         broadcast.putExtra(EXTRA_GRANT_STATE, result);
86         sendBroadcast(broadcast);
87         finish();
88     }
89 }
90