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