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 
17 package android.autofillservice.cts.activities;
18 
19 import android.app.Activity;
20 import android.autofillservice.cts.R;
21 import android.content.Context;
22 import android.os.Bundle;
23 import android.util.Log;
24 
25 import androidx.annotation.NonNull;
26 import androidx.annotation.Nullable;
27 
28 import java.io.File;
29 import java.io.IOException;
30 
31 /**
32  * Simple activity showing R.layout.login_activity. Started outside of the test process.
33  */
34 public class OutOfProcessLoginActivity extends Activity {
35     private static final String TAG = "OutOfProcessLoginActivity";
36 
37     private static OutOfProcessLoginActivity sInstance;
38 
39     @Override
onCreate(@ullable Bundle savedInstanceState)40     protected void onCreate(@Nullable Bundle savedInstanceState) {
41         Log.i(TAG, "onCreate(" + savedInstanceState + ")");
42         super.onCreate(savedInstanceState);
43 
44         setContentView(R.layout.login_activity);
45 
46         findViewById(R.id.login).setOnClickListener((v) -> finish());
47 
48         sInstance = this;
49     }
50 
51     @Override
onStart()52     protected void onStart() {
53         Log.i(TAG, "onStart()");
54         super.onStart();
55         try {
56             if (!getStartedMarker(this).createNewFile()) {
57                 Log.e(TAG, "cannot write started file");
58             }
59         } catch (IOException e) {
60             Log.e(TAG, "cannot write started file: " + e);
61         }
62     }
63 
64     @Override
onStop()65     protected void onStop() {
66         Log.i(TAG, "onStop()");
67         super.onStop();
68 
69         try {
70             if (!getStoppedMarker(this).createNewFile()) {
71                 Log.e(TAG, "could not write stopped marker");
72             } else {
73                 Log.v(TAG, "wrote stopped marker");
74             }
75         } catch (IOException e) {
76             Log.e(TAG, "could write stopped marker: " + e);
77         }
78     }
79 
80     @Override
onDestroy()81     protected void onDestroy() {
82         Log.i(TAG, "onDestroy()");
83         try {
84             if (!getDestroyedMarker(this).createNewFile()) {
85                 Log.e(TAG, "could not write destroyed marker");
86             } else {
87                 Log.v(TAG, "wrote destroyed marker");
88             }
89         } catch (IOException e) {
90             Log.e(TAG, "could write destroyed marker: " + e);
91         }
92         super.onDestroy();
93         sInstance = null;
94     }
95 
96     /**
97      * Get the file that signals that the activity has entered {@link Activity#onStop()}.
98      *
99      * @param context Context of the app
100      * @return The marker file that is written onStop()
101      */
getStoppedMarker(@onNull Context context)102     @NonNull public static File getStoppedMarker(@NonNull Context context) {
103         return new File(context.getFilesDir(), "stopped");
104     }
105 
106     /**
107      * Get the file that signals that the activity has entered {@link Activity#onStart()}.
108      *
109      * @param context Context of the app
110      * @return The marker file that is written onStart()
111      */
getStartedMarker(@onNull Context context)112     @NonNull public static File getStartedMarker(@NonNull Context context) {
113         return new File(context.getFilesDir(), "started");
114     }
115 
116    /**
117      * Get the file that signals that the activity has entered {@link Activity#onDestroy()}.
118      *
119      * @param context Context of the app
120      * @return The marker file that is written onDestroy()
121      */
getDestroyedMarker(@onNull Context context)122     @NonNull public static File getDestroyedMarker(@NonNull Context context) {
123         return new File(context.getFilesDir(), "destroyed");
124     }
125 
finishIt()126     public static void finishIt() {
127         Log.v(TAG, "Finishing " + sInstance);
128         if (sInstance != null) {
129             sInstance.finish();
130         }
131     }
132 
hasInstance()133     public static boolean hasInstance() {
134         return sInstance != null;
135     }
136 }
137