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