1 /*
2  * Copyright (C) 2016 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 package android.content.pm.cts.shortcutmanager;
17 
18 import android.app.Activity;
19 import android.content.Intent;
20 import android.os.Bundle;
21 import android.os.Handler;
22 import android.util.Log;
23 
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
27 import java.util.Objects;
28 import java.util.concurrent.atomic.AtomicInteger;
29 
30 /**
31  * Activity that's started from shortcuts during CTS.  This one closes itself when it's shown,
32  * and record all the incoming intents, which we'll examine during the tests.
33  */
34 public class ShortcutLaunchedActivity extends Activity {
35     private static final String TAG = "ShortcutLA";
36 
37     private static final AtomicInteger sNextInstanceId = new AtomicInteger();
38 
39     private final int mInstanceId = sNextInstanceId.getAndIncrement();
40 
41     private static final Object sLock = new Object();
42 
43     // @GuardedBy("sLock")
44     private static final ArrayList<Intent> sReceivedIntents = new ArrayList<>();
45 
46     // @GuardedBy("sLock")
47     private static final ArrayList<String> sExpectedVisibleOrder = new ArrayList<>();
48 
49     private Handler mHandler = new Handler();
50 
51     private Intent mIntentToAdd;
52 
log(String action)53     private void log(String action) {
54         Log.i(TAG, String.format("Activity #%d %s: intent=%s",
55                 mInstanceId, action, getIntent()));
56     }
57 
ShortcutLaunchedActivity()58     public ShortcutLaunchedActivity() {
59         log("ctor");
60     }
61 
62     @Override
onCreate(Bundle savedInstanceState)63     protected void onCreate(Bundle savedInstanceState) {
64         super.onCreate(savedInstanceState);
65 
66         mIntentToAdd = getIntent();
67 
68         log("onCreate");
69     }
70 
71     @Override
onResume()72     protected void onResume() {
73         super.onResume();
74 
75         log("onResume");
76 
77         synchronized (sLock) {
78             if (!Objects.equals(getIntent().getAction(), sExpectedVisibleOrder.get(0))) {
79                 log("Not my turn yet.");
80                 return;
81             }
82             sExpectedVisibleOrder.remove(0);
83 
84             // Make sure we only add it once, ever.
85             if (mIntentToAdd != null) {
86                 sReceivedIntents.add(new Intent(getIntent()));
87                 mIntentToAdd = null;
88             }
89         }
90         finish();
91     }
92 
93     @Override
onPause()94     protected void onPause() {
95         log("onPause");
96 
97         super.onPause();
98     }
99 
100     @Override
onDestroy()101     protected void onDestroy() {
102         log("onDestroy");
103 
104         super.onDestroy();
105     }
106 
setExpectedOrder(String[] actions)107     public static void setExpectedOrder(String[] actions) {
108         synchronized (sLock) {
109             sReceivedIntents.clear();
110 
111             sExpectedVisibleOrder.clear();
112             sExpectedVisibleOrder.addAll(Arrays.asList(actions));
113         }
114     }
115 
getIntents()116     public static List<Intent> getIntents() {
117         synchronized (sLock) {
118             return new ArrayList(sReceivedIntents);
119         }
120     }
121 }
122