• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.rollback.lib;
18 
19 import android.app.PendingIntent;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentSender;
24 
25 import androidx.test.InstrumentationRegistry;
26 
27 import java.util.concurrent.BlockingQueue;
28 import java.util.concurrent.LinkedBlockingQueue;
29 
30 /**
31  * Helper for making IntentSenders whose results are sent back to the test
32  * app.
33  */
34 public class LocalIntentSender extends BroadcastReceiver {
35 
36     private static final BlockingQueue<Intent> sIntentSenderResults = new LinkedBlockingQueue<>();
37 
38     @Override
onReceive(Context context, Intent intent)39     public void onReceive(Context context, Intent intent) {
40         sIntentSenderResults.add(intent);
41     }
42 
43     /**
44      * Get a LocalIntentSender.
45      */
getIntentSender()46     static IntentSender getIntentSender() {
47         Context context = InstrumentationRegistry.getContext();
48         Intent intent = new Intent(context, LocalIntentSender.class);
49         PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent, 0);
50         return pending.getIntentSender();
51     }
52 
53     /**
54      * Returns the most recent Intent sent by a LocalIntentSender.
55      */
getIntentSenderResult()56     static Intent getIntentSenderResult() throws InterruptedException {
57         return sIntentSenderResults.take();
58     }
59 }
60