1 /* 2 * Copyright (C) 2023 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 com.example.android.vdmdemo.demos; 18 19 import android.app.ActivityOptions; 20 import android.app.AlertDialog; 21 import android.content.Intent; 22 import android.hardware.display.DisplayManager; 23 import android.os.Bundle; 24 import android.view.Display; 25 import android.view.View; 26 27 import androidx.appcompat.app.AppCompatActivity; 28 29 /** Demo activity for showcasing Virtual Devices with home experience. */ 30 public final class HomeDemoActivity extends AppCompatActivity { 31 32 private DisplayManager mDisplayManager; 33 34 @Override onCreate(Bundle savedInstanceState)35 protected void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 setContentView(R.layout.home_demo_activity); 38 39 mDisplayManager = getSystemService(DisplayManager.class); 40 } 41 42 /** Handle home intent request. */ onSendHomeIntent(View view)43 public void onSendHomeIntent(View view) { 44 sendIntentToDisplay(Intent.CATEGORY_HOME); 45 } 46 47 /** Handle secondary home intent request. */ onSendSecondaryHomeIntent(View view)48 public void onSendSecondaryHomeIntent(View view) { 49 sendIntentToDisplay(Intent.CATEGORY_SECONDARY_HOME); 50 } 51 52 /** Handle a request to move the task to back. */ onMoveTaskToBack(View view)53 public void onMoveTaskToBack(View view) { 54 moveTaskToBack(true); 55 } 56 57 /** Handle calculator intent request. */ onCalculatorNewTask(View view)58 public void onCalculatorNewTask(View view) { 59 sendIntentToDisplay(Intent.CATEGORY_APP_CALCULATOR); 60 } 61 sendIntentToDisplay(String category)62 private void sendIntentToDisplay(String category) { 63 Display[] displays = mDisplayManager.getDisplays(); 64 65 String[] displayNames = new String[displays.length + 1]; 66 displayNames[0] = "No display"; 67 for (int i = 0; i < displays.length; ++i) { 68 displayNames[i + 1] = displays[i].getName(); 69 } 70 71 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(HomeDemoActivity.this); 72 alertDialogBuilder.setTitle("Choose display"); 73 alertDialogBuilder.setItems( 74 displayNames, 75 (dialog, which) -> { 76 Intent intent = new Intent(Intent.ACTION_MAIN); 77 intent.addCategory(category); 78 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 79 ActivityOptions options = ActivityOptions.makeBasic(); 80 if (which > 0) { 81 options.setLaunchDisplayId(displays[which - 1].getDisplayId()); 82 } 83 startActivity(intent, options.toBundle()); 84 }); 85 alertDialogBuilder.show(); 86 } 87 } 88