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