1 /* 2 * Copyright (C) 2021 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 androidx.car.app.activity; 18 19 20 import static com.android.car.pm.ActivityBlockingActivityTest.DoActivity.DIALOG_TITLE; 21 22 import android.app.Activity; 23 import android.app.AlertDialog; 24 import android.content.BroadcastReceiver; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.content.IntentFilter; 28 import android.os.Bundle; 29 30 import androidx.annotation.Nullable; 31 32 /** 33 * An activity to represent a template activity in tests. 34 */ 35 public class CarAppActivity extends Activity { 36 public static final String ACTION_SHOW_DIALOG = "SHOW_DIALOG"; 37 public static final String ACTION_START_SECOND_INSTANCE = "START_SECOND_INSTANCE"; 38 public static final String SECOND_INSTANCE_TITLE = "Second Instance"; 39 private static final String BUNDLE_KEY_IS_SECOND_INSTANCE = "is_second_instance"; 40 41 private final ShowDialogReceiver mShowDialogReceiver = new ShowDialogReceiver(); 42 private final StartSecondInstanceReceiver 43 mStartSecondInstanceReceiver = new StartSecondInstanceReceiver(); 44 45 private class ShowDialogReceiver extends BroadcastReceiver { 46 @Override onReceive(Context context, Intent intent)47 public void onReceive(Context context, Intent intent) { 48 showDialog(); 49 } 50 } 51 52 private class StartSecondInstanceReceiver extends BroadcastReceiver { 53 @Override onReceive(Context context, Intent intent)54 public void onReceive(Context context, Intent intent) { 55 startSecondInstance(); 56 } 57 } 58 59 @Override onCreate(@ullable Bundle savedInstanceState)60 protected void onCreate(@Nullable Bundle savedInstanceState) { 61 super.onCreate(savedInstanceState); 62 if (getIntent().getBooleanExtra(BUNDLE_KEY_IS_SECOND_INSTANCE, false)) { 63 getActionBar().setTitle(SECOND_INSTANCE_TITLE); 64 } 65 this.registerReceiver(mShowDialogReceiver, new IntentFilter(ACTION_SHOW_DIALOG)); 66 this.registerReceiver(mStartSecondInstanceReceiver, 67 new IntentFilter(ACTION_START_SECOND_INSTANCE)); 68 } 69 70 @Override onDestroy()71 protected void onDestroy() { 72 super.onDestroy(); 73 this.unregisterReceiver(mShowDialogReceiver); 74 this.unregisterReceiver(mStartSecondInstanceReceiver); 75 } 76 startSecondInstance()77 private void startSecondInstance() { 78 Intent intent = new Intent(CarAppActivity.this, CarAppActivity.class); 79 intent.putExtra(BUNDLE_KEY_IS_SECOND_INSTANCE, true); 80 startActivity(intent); 81 } 82 showDialog()83 private void showDialog() { 84 AlertDialog dialog = new AlertDialog.Builder(this) 85 .setTitle(DIALOG_TITLE) 86 .setMessage("Message") 87 .create(); 88 dialog.show(); 89 } 90 } 91