1 /*
2  * Copyright (C) 2017 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.autofillservice.cts;
17 
18 import static com.google.common.truth.Truth.assertWithMessage;
19 
20 import android.app.PendingIntent;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentSender;
24 import android.os.Bundle;
25 import android.support.test.uiautomator.UiObject2;
26 import android.text.TextUtils;
27 import android.util.Log;
28 import android.widget.TextView;
29 
30 import androidx.annotation.Nullable;
31 
32 /**
33  * Activity that displays a "Welcome USER" message after login.
34  */
35 public class WelcomeActivity extends AbstractAutoFillActivity {
36 
37     private static WelcomeActivity sInstance;
38 
39     private static final String TAG = "WelcomeActivity";
40 
41     static final String EXTRA_MESSAGE = "message";
42     static final String ID_WELCOME = "welcome";
43 
44     private static int sPendingIntentId;
45     private static PendingIntent sPendingIntent;
46 
47     private TextView mWelcome;
48 
WelcomeActivity()49     public WelcomeActivity() {
50         sInstance = this;
51     }
52 
53     @Override
onCreate(Bundle savedInstanceState)54     protected void onCreate(Bundle savedInstanceState) {
55         super.onCreate(savedInstanceState);
56 
57         setContentView(R.layout.welcome_activity);
58 
59         mWelcome = (TextView) findViewById(R.id.welcome);
60 
61         final Intent intent = getIntent();
62         final String message = intent.getStringExtra(EXTRA_MESSAGE);
63 
64         if (!TextUtils.isEmpty(message)) {
65             mWelcome.setText(message);
66         }
67 
68         Log.d(TAG, "Message: " + message);
69     }
70 
71     @Override
onDestroy()72     protected void onDestroy() {
73         super.onDestroy();
74 
75         Log.v(TAG, "Setting sInstance to null onDestroy()");
76         sInstance = null;
77     }
78 
79     @Override
finish()80     public void finish() {
81         super.finish();
82         Log.d(TAG, "So long and thanks for all the finish!");
83 
84         if (sPendingIntent != null) {
85             Log.v(TAG, " canceling pending intent on finish(): " + sPendingIntent);
86             sPendingIntent.cancel();
87         }
88     }
89 
finishIt()90     static void finishIt() {
91         if (sInstance != null) {
92             sInstance.finish();
93         }
94     }
95 
96     // TODO: reuse in other places
assertShowingDefaultMessage(UiBot uiBot)97     static void assertShowingDefaultMessage(UiBot uiBot) throws Exception {
98         assertShowing(uiBot, null);
99     }
100 
101     // TODO: reuse in other places
assertShowing(UiBot uiBot, @Nullable String expectedMessage)102     static void assertShowing(UiBot uiBot, @Nullable String expectedMessage) throws Exception {
103         final UiObject2 activity = uiBot.assertShownByRelativeId(ID_WELCOME);
104         if (expectedMessage == null) {
105             expectedMessage = "Welcome to the jungle!";
106         }
107         assertWithMessage("wrong text on '%s'", activity).that(activity.getText())
108                 .isEqualTo(expectedMessage);
109     }
110 
createSender(Context context, String message)111     public static IntentSender createSender(Context context, String message) {
112         if (sPendingIntent != null) {
113             throw new IllegalArgumentException("Already have pending intent (id="
114                     + sPendingIntentId + "): " + sPendingIntent);
115         }
116         ++sPendingIntentId;
117         Log.v(TAG, "createSender: id=" + sPendingIntentId + " message=" + message);
118         final Intent intent = new Intent(context, WelcomeActivity.class)
119                 .putExtra(EXTRA_MESSAGE, message)
120                 .setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
121         sPendingIntent = PendingIntent.getActivity(context, sPendingIntentId, intent, 0);
122         return sPendingIntent.getIntentSender();
123     }
124 }
125