• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.suspensionchecker;
18 
19 import android.app.Activity;
20 import android.content.BroadcastReceiver;
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.os.Bundle;
26 
27 import androidx.localbroadcastmanager.content.LocalBroadcastManager;
28 import androidx.test.platform.app.InstrumentationRegistry;
29 import androidx.test.uiautomator.UiDevice;
30 
31 import java.util.concurrent.CountDownLatch;
32 import java.util.concurrent.TimeUnit;
33 
34 /**
35  * Activity that sends a local broadcast when launched, so that the launch can be verified.
36  */
37 public class LaunchCheckingActivity extends Activity {
38     /** Broadcast action that is used to signal successful activity launch. */
39     private static final String LAUNCHED_BROADCAST_ACTION =
40             "com.android.cts.suspensionchecker.LAUNCHED";
41 
checkLaunch(Context context)42     public static boolean checkLaunch(Context context) throws InterruptedException {
43         final CountDownLatch activityStartedLatch = new CountDownLatch(1);
44 
45         LocalBroadcastManager.getInstance(context).registerReceiver(new BroadcastReceiver() {
46             @Override
47             public void onReceive(Context context, Intent intent) {
48                 activityStartedLatch.countDown();
49             }
50         }, new IntentFilter(LAUNCHED_BROADCAST_ACTION));
51 
52         for (int i = 0; i < 3; i++) {
53             final Intent intent = new Intent()
54                     .setComponent(new ComponentName(context, LaunchCheckingActivity.class))
55                     // Required when launching not from activity context.
56                     .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
57             context.startActivity(intent);
58 
59             if (activityStartedLatch.await(1, TimeUnit.SECONDS)) {
60                 return true;
61             }
62 
63             // Press back in case there is a transparency dialog that is blocking the launch.
64             UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).pressBack();
65         }
66 
67         return false;
68     }
69 
70     @Override
onCreate(Bundle savedInstanceState)71     protected void onCreate(Bundle savedInstanceState) {
72         super.onCreate(savedInstanceState);
73 
74         LocalBroadcastManager.getInstance(this)
75                 .sendBroadcast(new Intent(LAUNCHED_BROADCAST_ACTION));
76 
77         finish();
78     }
79 }
80