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