1 /*
2  * Copyright (C) 2022 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.content.Context;
20 
21 import androidx.annotation.NonNull;
22 
23 import com.android.car.qc.QCItem;
24 import com.android.car.qc.QCList;
25 import com.android.car.qc.QCRow;
26 import com.android.car.qc.provider.BaseLocalQCProvider;
27 import com.android.systemui.R;
28 import com.android.systemui.car.drivemode.DriveModeManager;
29 
30 import javax.inject.Inject;
31 
32 /**
33  * Local provider for the DriveMode panel.
34  */
35 public class DriveModeQcPanel extends BaseLocalQCProvider implements DriveModeManager.Callback {
36 
37     private final DriveModeManager mDriveModeManager;
38 
39     @Inject
DriveModeQcPanel(Context context, DriveModeManager driveModeManager)40     public DriveModeQcPanel(Context context, DriveModeManager driveModeManager) {
41         super(context);
42         mDriveModeManager = driveModeManager;
43         mDriveModeManager.addCallback(this);
44     }
45 
46     @Override
getQCItem()47     public QCItem getQCItem() {
48         QCList.Builder listBuilder = new QCList.Builder();
49 
50         for (String driveMode : mDriveModeManager.getAvailableDriveModes()) {
51             QCRow row = new QCRow.Builder()
52                     .setTitle(driveMode)
53                     .setSubtitle(getSubtitle(driveMode))
54                     .build();
55             row.setActionHandler((item, context, intent) -> {
56                 mDriveModeManager.setDriveMode(driveMode);
57             });
58             listBuilder.addRow(row);
59         }
60 
61         return listBuilder.build();
62     }
63 
getSubtitle(String driveMode)64     private String getSubtitle(String driveMode) {
65         return mDriveModeManager.getDriveMode().equals(driveMode)
66                 ? mContext.getResources().getString(R.string.qc_drive_mode_active_subtitle)
67                 : null;
68     }
69 
70     @Override
onDestroy()71     public void onDestroy() {
72         super.onDestroy();
73 
74         if (mDriveModeManager != null) {
75             mDriveModeManager.removeCallback(this);
76         }
77     }
78 
79     @Override
onDriveModeChanged(String newDriveMode)80     public void onDriveModeChanged(String newDriveMode) {
81         notifyChange();
82     }
83 }
84