1 /* 2 * Copyright (C) 2024 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.android.systemui.car.systembar; 18 19 import android.app.ActivityOptions; 20 import android.car.app.CarActivityManager; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.os.Build; 24 import android.text.TextUtils; 25 import android.util.Log; 26 import android.view.View; 27 import android.widget.Toast; 28 29 import com.android.car.ui.utils.CarUxRestrictionsUtil; 30 import com.android.systemui.R; 31 import com.android.systemui.car.CarDeviceProvisionedController; 32 import com.android.systemui.car.CarServiceProvider; 33 import com.android.systemui.car.statusicon.StatusIconPanelViewController; 34 import com.android.systemui.car.systembar.element.CarSystemBarElementController; 35 import com.android.systemui.car.systembar.element.CarSystemBarElementStateController; 36 import com.android.systemui.car.systembar.element.CarSystemBarElementStatusBarDisableController; 37 import com.android.systemui.car.users.CarSystemUIUserUtil; 38 import com.android.systemui.settings.UserTracker; 39 40 import dagger.assisted.Assisted; 41 import dagger.assisted.AssistedFactory; 42 import dagger.assisted.AssistedInject; 43 44 import java.net.URISyntaxException; 45 46 import javax.inject.Provider; 47 48 public class UserNamePanelButtonViewController extends CarSystemBarPanelButtonViewController { 49 private static final String TAG = UserNamePanelButtonViewController.class.getName(); 50 private final Context mContext; 51 private final UserTracker mUserTracker; 52 private final CarServiceProvider mCarServiceProvider; 53 private final CarDeviceProvisionedController mCarDeviceProvisionedController; 54 private final boolean mIsMUMDSystemUI; 55 private CarActivityManager mCarActivityManager; 56 57 private final CarServiceProvider.CarServiceOnConnectedListener mCarServiceOnConnectedListener = 58 car -> { 59 mCarActivityManager = car.getCarManager(CarActivityManager.class); 60 }; 61 62 @AssistedInject UserNamePanelButtonViewController(@ssisted CarSystemBarPanelButtonView view, CarSystemBarElementStatusBarDisableController disableController, CarSystemBarElementStateController stateController, Provider<StatusIconPanelViewController.Builder> statusIconPanelBuilder, Context context, UserTracker userTracker, CarServiceProvider carServiceProvider, CarDeviceProvisionedController deviceProvisionedController)63 protected UserNamePanelButtonViewController(@Assisted CarSystemBarPanelButtonView view, 64 CarSystemBarElementStatusBarDisableController disableController, 65 CarSystemBarElementStateController stateController, 66 Provider<StatusIconPanelViewController.Builder> statusIconPanelBuilder, 67 Context context, UserTracker userTracker, CarServiceProvider carServiceProvider, 68 CarDeviceProvisionedController deviceProvisionedController) { 69 super(view, disableController, stateController, statusIconPanelBuilder); 70 mContext = context; 71 mUserTracker = userTracker; 72 mCarServiceProvider = carServiceProvider; 73 mCarDeviceProvisionedController = deviceProvisionedController; 74 mIsMUMDSystemUI = CarSystemUIUserUtil.isMUMDSystemUI(); 75 } 76 77 @AssistedFactory 78 public interface Factory extends 79 CarSystemBarElementController.Factory<CarSystemBarPanelButtonView, 80 UserNamePanelButtonViewController> { 81 } 82 83 @Override onInit()84 protected void onInit() { 85 if (mIsMUMDSystemUI) { 86 // TODO(b/269490856): consider removal of UserPicker carve-outs 87 mView.setOnClickListener(getMUMDUserPickerClickListener()); 88 } else { 89 super.onInit(); 90 } 91 if (!Build.IS_ENG && !Build.IS_USERDEBUG) { 92 return; 93 } 94 String longIntentString = mContext.getString(R.string.user_profile_long_press_intent); 95 if (!TextUtils.isEmpty(longIntentString)) { 96 Intent intent; 97 try { 98 intent = Intent.parseUri(longIntentString, Intent.URI_INTENT_SCHEME); 99 } catch (URISyntaxException e) { 100 return; 101 } 102 Intent finalIntent = intent; 103 mView.setOnLongClickListener(v -> { 104 Intent broadcast = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); 105 mContext.sendBroadcastAsUser(broadcast, mUserTracker.getUserHandle()); 106 try { 107 ActivityOptions options = ActivityOptions.makeBasic(); 108 options.setLaunchDisplayId(mContext.getDisplayId()); 109 mContext.startActivityAsUser(finalIntent, options.toBundle(), 110 mUserTracker.getUserHandle()); 111 } catch (Exception e) { 112 Log.e(TAG, "Failed to launch intent", e); 113 } 114 return true; 115 }); 116 } 117 } 118 119 @Override onViewAttached()120 protected void onViewAttached() { 121 super.onViewAttached(); 122 if (mIsMUMDSystemUI) { 123 mCarServiceProvider.addListener(mCarServiceOnConnectedListener); 124 } 125 } 126 127 @Override onViewDetached()128 protected void onViewDetached() { 129 super.onViewDetached(); 130 if (mIsMUMDSystemUI) { 131 mCarServiceProvider.removeListener(mCarServiceOnConnectedListener); 132 mCarActivityManager = null; 133 } 134 } 135 136 @Override shouldRestoreState()137 protected boolean shouldRestoreState() { 138 // TODO(b/269490856): consider removal of UserPicker carve-outs 139 return !CarSystemUIUserUtil.isMUMDSystemUI(); 140 } 141 getMUMDUserPickerClickListener()142 private View.OnClickListener getMUMDUserPickerClickListener() { 143 boolean disabledWhileDriving = 144 mView.getDisabledWhileDriving() != null ? mView.getDisabledWhileDriving() 145 : false; 146 boolean disabledWhileUnprovisioned = mView.getDisabledWhileUnprovisioned() != null 147 ? mView.getDisabledWhileUnprovisioned() : false; 148 CarUxRestrictionsUtil carUxRestrictionsUtil; 149 if (disabledWhileDriving) { 150 carUxRestrictionsUtil = CarUxRestrictionsUtil.getInstance(mContext); 151 } else { 152 carUxRestrictionsUtil = null; 153 } 154 return v -> { 155 if (disabledWhileUnprovisioned && !isDeviceSetupForUser()) { 156 return; 157 } 158 if (disabledWhileDriving && carUxRestrictionsUtil.getCurrentRestrictions() 159 .isRequiresDistractionOptimization()) { 160 Toast.makeText(mContext, R.string.car_ui_restricted_while_driving, 161 Toast.LENGTH_LONG).show(); 162 return; 163 } 164 if (mCarActivityManager != null) { 165 mCarActivityManager.startUserPickerOnDisplay(mContext.getDisplayId()); 166 } 167 }; 168 } 169 170 private boolean isDeviceSetupForUser() { 171 return mCarDeviceProvisionedController.isCurrentUserSetup() 172 && !mCarDeviceProvisionedController.isCurrentUserSetupInProgress(); 173 } 174 } 175