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.usepermission;
18 
19 import android.app.Activity;
20 import android.os.Bundle;
21 import android.view.WindowManager;
22 
23 import java.util.concurrent.CountDownLatch;
24 import java.util.concurrent.SynchronousQueue;
25 import java.util.concurrent.TimeUnit;
26 
27 public class BasePermissionActivity extends Activity {
28     private static final long OPERATION_TIMEOUT_MILLIS = 5000;
29 
30     private final SynchronousQueue<Result> mResult = new SynchronousQueue<>();
31     private final CountDownLatch mOnCreateSync = new CountDownLatch(1);
32 
33     public static class Result {
34         public final int requestCode;
35         public final String[] permissions;
36         public final int[] grantResults;
37 
Result(int requestCode, String[] permissions, int[] grantResults)38         public Result(int requestCode, String[] permissions, int[] grantResults) {
39             this.requestCode = requestCode;
40             this.permissions = permissions;
41             this.grantResults = grantResults;
42         }
43     }
44 
45     @Override
onCreate(Bundle savedInstanceState)46     protected void onCreate(Bundle savedInstanceState) {
47         super.onCreate(savedInstanceState);
48 
49         getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
50                 | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
51                 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
52 
53         mOnCreateSync.countDown();
54     }
55 
56     @Override
onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)57     public void onRequestPermissionsResult(int requestCode, String[] permissions,
58             int[] grantResults) {
59         try {
60             mResult.offer(new Result(requestCode, permissions, grantResults), 5, TimeUnit.SECONDS);
61         } catch (InterruptedException e) {
62             throw new RuntimeException(e);
63         }
64     }
65 
waitForOnCreate()66     public void waitForOnCreate() {
67         try {
68             mOnCreateSync.await(OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
69         } catch (InterruptedException e) {
70             throw new RuntimeException(e);
71         }
72     }
73 
getResult()74     public Result getResult() {
75         try {
76             return mResult.poll(OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
77         } catch (InterruptedException e) {
78             throw new RuntimeException(e);
79         }
80     }
81 }
82