1 /* 2 * Copyright (C) 2018 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.tests.rollback; 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 * Make IntentSender that sends intent locally. 32 */ 33 public class LocalIntentSender extends BroadcastReceiver { 34 35 private static final String TAG = "RollbackTest"; 36 37 private static final BlockingQueue<Intent> sIntentSenderResults = new LinkedBlockingQueue<>(); 38 39 @Override onReceive(Context context, Intent intent)40 public void onReceive(Context context, Intent intent) { 41 sIntentSenderResults.add(intent); 42 } 43 44 /** 45 * Get a LocalIntentSender. 46 */ getIntentSender()47 static IntentSender getIntentSender() { 48 Context context = InstrumentationRegistry.getContext(); 49 Intent intent = new Intent(context, LocalIntentSender.class); 50 PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent, 0); 51 return pending.getIntentSender(); 52 } 53 54 /** 55 * Returns the most recent Intent sent by a LocalIntentSender. 56 */ getIntentSenderResult()57 static Intent getIntentSenderResult() throws InterruptedException { 58 return sIntentSenderResults.take(); 59 } 60 } 61