• 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.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                 Context.RECEIVER_EXPORTED_UNAUDITED);
59         Intent intent = new Intent(action);
60         PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent, FLAG_MUTABLE);
61         return pending.getIntentSender();
62     }
63 
64     /**
65      * Returns and remove the most early Intent received by this LocalIntentSender.
66      */
getResult()67     public Intent getResult() throws InterruptedException {
68         Intent intent = mResults.take();
69         Log.i(TAG, "Taking intent " + prettyPrint(intent));
70         return intent;
71     }
72 
prettyPrint(Intent intent)73     private static String prettyPrint(Intent intent) {
74         int sessionId = intent.getIntExtra(PackageInstaller.EXTRA_SESSION_ID, -1);
75         int status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS,
76                 PackageInstaller.STATUS_FAILURE);
77         String message = intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE);
78         return String.format("%s: {\n"
79                 + "sessionId = %d\n"
80                 + "status = %d\n"
81                 + "message = %s\n"
82                 + "}", intent, sessionId, status, message);
83     }
84 }
85