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.qc;
18 
19 import android.app.ActivityOptions;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.util.Log;
23 
24 import com.android.car.ui.utils.CarUxRestrictionsUtil;
25 import com.android.systemui.car.systembar.element.CarSystemBarElementController;
26 import com.android.systemui.car.systembar.element.CarSystemBarElementStateController;
27 import com.android.systemui.car.systembar.element.CarSystemBarElementStatusBarDisableController;
28 import com.android.systemui.settings.UserTracker;
29 
30 import dagger.assisted.Assisted;
31 import dagger.assisted.AssistedFactory;
32 import dagger.assisted.AssistedInject;
33 
34 /** Default controller for QCFooterButton views */
35 public class QCFooterButtonController extends CarSystemBarElementController<QCFooterButton> {
36     private static final String TAG = QCFooterButtonController.class.getSimpleName();
37 
38     private final Context mContext;
39     private final UserTracker mUserTracker;
40     private final CarUxRestrictionsUtil.OnUxRestrictionsChangedListener mListener =
41             carUxRestrictions -> mView.setEnabled(
42                     !carUxRestrictions.isRequiresDistractionOptimization());
43 
44     @AssistedInject
QCFooterButtonController(@ssisted QCFooterButton view, CarSystemBarElementStatusBarDisableController disableController, CarSystemBarElementStateController stateController, Context context, UserTracker userTracker)45     protected QCFooterButtonController(@Assisted QCFooterButton view,
46             CarSystemBarElementStatusBarDisableController disableController,
47             CarSystemBarElementStateController stateController, Context context,
48             UserTracker userTracker) {
49         super(view, disableController, stateController);
50         mContext = context;
51         mUserTracker = userTracker;
52     }
53 
54     @AssistedFactory
55     public interface Factory extends
56             CarSystemBarElementController.Factory<QCFooterButton, QCFooterButtonController> {}
57 
58     @Override
onInit()59     protected void onInit() {
60         Intent intent = mView.getOnClickIntent();
61         if (intent == null) return;
62         mView.setOnClickListener(v -> {
63             mContext.sendBroadcastAsUser(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS),
64                     mUserTracker.getUserHandle());
65             try {
66                 ActivityOptions options = ActivityOptions.makeBasic();
67                 options.setLaunchDisplayId(mContext.getDisplayId());
68                 mContext.startActivityAsUser(intent, options.toBundle(),
69                         mUserTracker.getUserHandle());
70             } catch (Exception e) {
71                 Log.e(TAG, "Failed to launch intent", e);
72             }
73         });
74     }
75 
76     @Override
onViewAttached()77     protected void onViewAttached() {
78         super.onViewAttached();
79         if (mView.isDisableWhileDriving()) {
80             CarUxRestrictionsUtil.getInstance(mContext).register(mListener);
81         }
82     }
83 
84     @Override
onViewDetached()85     protected void onViewDetached() {
86         super.onViewDetached();
87         if (mView.isDisableWhileDriving()) {
88             CarUxRestrictionsUtil.getInstance(mContext).unregister(mListener);
89         }
90     }
91 }
92