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