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.install.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 import android.content.pm.PackageInstaller;
25 import android.util.Log;
26 
27 import androidx.test.InstrumentationRegistry;
28 
29 import java.util.concurrent.BlockingQueue;
30 import java.util.concurrent.LinkedBlockingQueue;
31 
32 /**
33  * Helper for making IntentSenders whose results are sent back to the test
34  * app.
35  */
36 public class LocalIntentSender extends BroadcastReceiver {
37     private static final String TAG = "cts.install.lib";
38 
39     private static final BlockingQueue<Intent> sIntentSenderResults = new LinkedBlockingQueue<>();
40 
41     @Override
onReceive(Context context, Intent intent)42     public void onReceive(Context context, Intent intent) {
43         Log.i(TAG, "Received intent " + prettyPrint(intent));
44         sIntentSenderResults.add(intent);
45     }
46 
47     /**
48      * Get a LocalIntentSender.
49      */
getIntentSender()50     public static IntentSender getIntentSender() {
51         Context context = InstrumentationRegistry.getContext();
52         Intent intent = new Intent(context, LocalIntentSender.class);
53         PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent, 0);
54         return pending.getIntentSender();
55     }
56 
57     /**
58      * Returns the most recent Intent sent by a LocalIntentSender.
59      */
getIntentSenderResult()60     public static Intent getIntentSenderResult() throws InterruptedException {
61         Intent intent = sIntentSenderResults.take();
62         Log.i(TAG, "Taking intent " + prettyPrint(intent));
63         return intent;
64     }
65 
prettyPrint(Intent intent)66     private static String prettyPrint(Intent intent) {
67         int sessionId = intent.getIntExtra(PackageInstaller.EXTRA_SESSION_ID, -1);
68         int status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS,
69                 PackageInstaller.STATUS_FAILURE);
70         String message = intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE);
71         return String.format("%s: {\n"
72                 + "sessionId = %d\n"
73                 + "status = %d\n"
74                 + "message = %s\n"
75                 + "}", intent, sessionId, status, message);
76     }
77 }
78