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