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.google.android.car.kitchensink.drivemode;
18 
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.content.om.OverlayInfo;
22 import android.content.om.OverlayManager;
23 import android.os.UserHandle;
24 import android.util.Log;
25 import android.widget.Toast;
26 
27 /**
28  * A class that provides methods to enable and disable the DriveMode Switch on SystemUI.
29  * The Drive Mode switch is a button in the Quick Control Area that allows the user to switch
30  * between pre-defined driving modes (e.g. Comfort, Eco and Sport).
31  */
32 public final class DriveModeSwitchController {
33 
34     private static final String DRIVE_MODE_ON_RRO_PACKAGE = "com.android.systemui.drivemode.on.rro";
35     private static final String TAG = DriveModeSwitchController.class.getSimpleName();
36 
37     @Nullable
38     private final OverlayManager mOverlayManager;
39     private final Context mContext;
40 
DriveModeSwitchController(Context context)41     public DriveModeSwitchController(Context context) {
42         mContext = context;
43         mOverlayManager = context.getSystemService(OverlayManager.class);
44         if (overlayNotSupported()) {
45             showToast("OverlayManager not available");
46         }
47     }
48 
49     /**
50      * Returns the current state of the DriveMode Switch
51      */
isDriveModeActive()52     boolean isDriveModeActive() {
53         if (overlayNotSupported()) {
54             return false;
55         }
56 
57         OverlayInfo overlayInfo = mOverlayManager.getOverlayInfo(DRIVE_MODE_ON_RRO_PACKAGE,
58                 UserHandle.SYSTEM);
59         return overlayInfo != null && overlayInfo.isEnabled();
60     }
61 
62     /**
63      * Enables or disables the DriveMode Switch
64      */
setDriveMode(boolean newState)65     public void setDriveMode(boolean newState) {
66         if (overlayNotSupported()) {
67             return;
68         }
69 
70         mOverlayManager.setEnabled(DRIVE_MODE_ON_RRO_PACKAGE, newState, UserHandle.SYSTEM);
71         showToast("New state: %s. Please reboot to apply changes.", newState);
72     }
73 
overlayNotSupported()74     private boolean overlayNotSupported() {
75         return mOverlayManager == null;
76     }
77 
showToast(String msg, Object... args)78     private void showToast(String msg, Object... args) {
79         Toast.makeText(mContext, String.format(msg, args), Toast.LENGTH_LONG).show();
80         Log.i(TAG, String.format(msg, args));
81     }
82 }
83