1 /* 2 * Copyright (C) 2014 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.documentclient; 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.SynchronousQueue; 25 import java.util.concurrent.TimeUnit; 26 27 public class MyActivity extends Activity { 28 private final SynchronousQueue<Result> mResult = new SynchronousQueue<>(); 29 30 public static class Result { 31 public final int requestCode; 32 public final int resultCode; 33 public final Intent data; 34 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 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 52 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 53 try { 54 mResult.offer(new Result(requestCode, resultCode, data), 5, TimeUnit.SECONDS); 55 } catch (InterruptedException e) { 56 throw new RuntimeException(e); 57 } 58 } 59 60 public Result getResult() { 61 final Result result; 62 try { 63 result = mResult.poll(30, TimeUnit.SECONDS); 64 } catch (InterruptedException e) { 65 throw new RuntimeException(e); 66 } 67 if (result == null) { 68 throw new IllegalStateException("Activity didn't receive a Result in 30 seconds"); 69 } 70 return result; 71 } 72 } 73