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 com.google.android.car.kitchensink; 18 19 import static android.car.Car.APP_FOCUS_SERVICE; 20 import static android.car.Car.CAR_OCCUPANT_ZONE_SERVICE; 21 import static android.car.Car.CAR_WAIT_TIMEOUT_WAIT_FOREVER; 22 import static android.car.CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION; 23 import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; 24 25 import android.app.Activity; 26 import android.car.Car; 27 import android.car.CarAppFocusManager; 28 import android.car.CarOccupantZoneManager; 29 import android.car.CarOccupantZoneManager.OccupantZoneInfo; 30 import android.content.ComponentName; 31 import android.content.Intent; 32 import android.os.Bundle; 33 import android.os.Handler; 34 import android.os.Looper; 35 import android.os.UserHandle; 36 import android.util.Log; 37 import android.view.Display; 38 39 import java.util.List; 40 41 public final class OccupantZoneStartActivity extends Activity { 42 43 private static final String TAG = "CAR.OCCUPANT.KS.START"; 44 private static final String AUDIO_ACTIVITY = 45 "com.google.android.car.kitchensink/.AudioAutoStartActivity"; 46 47 private Handler mHandler; 48 private Car mCar; 49 private CarAppFocusManager mAppFocusManager; 50 private CarOccupantZoneManager mOccupantZoneManager; 51 52 @Override onCreate(Bundle savedInstanceState)53 protected void onCreate(Bundle savedInstanceState) { 54 super.onCreate(savedInstanceState); 55 connectCar(); 56 setContentView(R.layout.empty_activity); 57 mHandler.post(() -> startActivity()); 58 } 59 startActivity()60 private void startActivity() { 61 Intent intent = getAppIntent(); 62 63 Display display = getDisplay(); 64 65 if (display == null) { 66 Log.e(TAG, "Could not start activity, null display."); 67 return; 68 } 69 70 OccupantZoneInfo zoneInfo = getOccupantZoneForDisplay(display); 71 if (zoneInfo == null) { 72 Log.e(TAG, "Could not start activity, no occupant zone for display"); 73 return; 74 } 75 int userId = mOccupantZoneManager.getUserForOccupant(zoneInfo); 76 if (userId == UserHandle.USER_NULL) { 77 Log.e(TAG, "Could not start activity, no userId for occupant zone"); 78 return; 79 } 80 Log.i(TAG, "Start activty for user " + userId); 81 startActivityAsUser(intent, UserHandle.of(userId)); 82 } 83 getOccupantZoneForDisplay(Display display)84 private OccupantZoneInfo getOccupantZoneForDisplay(Display display) { 85 List<OccupantZoneInfo> occupantZoneInfos = mOccupantZoneManager.getAllOccupantZones(); 86 for (int index = 0; index < occupantZoneInfos.size(); index++) { 87 OccupantZoneInfo occupantZoneInfo = occupantZoneInfos.get(index); 88 List<Display> displays = mOccupantZoneManager.getAllDisplaysForOccupant( 89 occupantZoneInfo); 90 for (int displayIndex = 0; displayIndex < displays.size(); displayIndex++) { 91 if (displays.get(displayIndex).getDisplayId() == display.getDisplayId()) { 92 return occupantZoneInfo; 93 } 94 } 95 } 96 return null; 97 } 98 getAppIntent()99 private Intent getAppIntent() { 100 return new Intent() 101 .setComponent(ComponentName.unflattenFromString(AUDIO_ACTIVITY)) 102 .addFlags(FLAG_ACTIVITY_NEW_TASK); 103 } 104 connectCar()105 private void connectCar() { 106 mHandler = new Handler(Looper.getMainLooper(), null, true); 107 mCar = Car.createCar(this, null, 108 CAR_WAIT_TIMEOUT_WAIT_FOREVER, (car, ready) -> { 109 if (!ready) { 110 return; 111 } 112 mAppFocusManager = 113 (CarAppFocusManager) car.getCarManager(APP_FOCUS_SERVICE); 114 mAppFocusManager.addFocusListener( 115 (CarAppFocusManager.OnAppFocusChangedListener) (appType, active) -> { 116 117 }, 118 APP_FOCUS_TYPE_NAVIGATION); 119 120 mOccupantZoneManager = (CarOccupantZoneManager) 121 car.getCarManager(CAR_OCCUPANT_ZONE_SERVICE); 122 }); 123 } 124 } 125