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 com.android.providers.media; 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 finish(); 60 } 61 clearResult()62 public void clearResult() { 63 sResult = new LinkedBlockingQueue<>(); 64 } 65 getResult()66 public Result getResult() { 67 final Result result; 68 try { 69 result = sResult.poll(30, TimeUnit.SECONDS); 70 } catch (InterruptedException e) { 71 throw new RuntimeException(e); 72 } 73 if (result == null) { 74 throw new IllegalStateException("Activity didn't receive a Result in 30 seconds"); 75 } 76 return result; 77 } 78 getResult(long timeout, TimeUnit unit)79 public Result getResult(long timeout, TimeUnit unit) { 80 try { 81 return sResult.poll(timeout, unit); 82 } catch (InterruptedException e) { 83 throw new RuntimeException(e); 84 } 85 } 86 } 87