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 android.providerui.cts;
18 
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.view.WindowManager;
23 
24 import java.util.concurrent.LinkedBlockingQueue;
25 import java.util.concurrent.TimeUnit;
26 
27 public class GetResultActivity extends Activity {
28     private static LinkedBlockingQueue<Result> sResult;
29 
30     public static class Result {
31         public final int requestCode;
32         public final int resultCode;
33         public final Intent data;
34 
Result(int requestCode, int resultCode, Intent data)35         public Result(int requestCode, int resultCode, Intent data) {
36             this.requestCode = requestCode;
37             this.resultCode = resultCode;
38             this.data = data;
39         }
40     }
41 
42     @Override
onCreate(Bundle savedInstanceState)43     protected void onCreate(Bundle savedInstanceState) {
44         super.onCreate(savedInstanceState);
45 
46         getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
47                 | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
48                 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
49     }
50 
51     @Override
onActivityResult(int requestCode, int resultCode, Intent data)52     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
53         try {
54             sResult.offer(new Result(requestCode, resultCode, data), 5, TimeUnit.SECONDS);
55         } catch (InterruptedException e) {
56             throw new RuntimeException(e);
57         }
58     }
59 
clearResult()60     public void clearResult() {
61         sResult = new LinkedBlockingQueue<>();
62     }
63 
getResult()64     public Result getResult() {
65         final Result result;
66         try {
67             result = sResult.poll(30, TimeUnit.SECONDS);
68         } catch (InterruptedException e) {
69             throw new RuntimeException(e);
70         }
71         if (result == null) {
72             throw new IllegalStateException("Activity didn't receive a Result in 30 seconds");
73         }
74         return result;
75     }
76 
getResult(long timeout, TimeUnit unit)77     public Result getResult(long timeout, TimeUnit unit) {
78         try {
79             return sResult.poll(timeout, unit);
80         } catch (InterruptedException e) {
81             throw new RuntimeException(e);
82         }
83     }
84 }
85