1 /*
2  * Copyright (C) 2015 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.server.telecom;
18 
19 import android.app.UiModeManager;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.content.res.Configuration;
25 import android.telecom.Log;
26 
27 import java.util.Set;
28 import java.util.concurrent.CopyOnWriteArraySet;
29 
30 /**
31  * Provides various system states to the rest of the telecom codebase. So far, that's only car-mode.
32  */
33 public class SystemStateProvider {
34 
35     public static interface SystemStateListener {
onCarModeChanged(boolean isCarMode)36         public void onCarModeChanged(boolean isCarMode);
37     }
38 
39     private final Context mContext;
40     private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
41         @Override
42         public void onReceive(Context context, Intent intent) {
43             Log.startSession("SSP.oR");
44             try {
45                 String action = intent.getAction();
46                 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
47                     onEnterCarMode();
48                 } else if (UiModeManager.ACTION_EXIT_CAR_MODE.equals(action)) {
49                     onExitCarMode();
50                 } else {
51                     Log.w(this, "Unexpected intent received: %s", intent.getAction());
52                 }
53             } finally {
54                 Log.endSession();
55             }
56         }
57     };
58 
59     private Set<SystemStateListener> mListeners = new CopyOnWriteArraySet<>();
60     private boolean mIsCarMode;
61 
SystemStateProvider(Context context)62     public SystemStateProvider(Context context) {
63         mContext = context;
64 
65         IntentFilter intentFilter = new IntentFilter(UiModeManager.ACTION_ENTER_CAR_MODE);
66         intentFilter.addAction(UiModeManager.ACTION_EXIT_CAR_MODE);
67         mContext.registerReceiver(mBroadcastReceiver, intentFilter);
68         Log.i(this, "Registering car mode receiver: %s", intentFilter);
69 
70         mIsCarMode = getSystemCarMode();
71     }
72 
addListener(SystemStateListener listener)73     public void addListener(SystemStateListener listener) {
74         if (listener != null) {
75             mListeners.add(listener);
76         }
77     }
78 
removeListener(SystemStateListener listener)79     public boolean removeListener(SystemStateListener listener) {
80         return mListeners.remove(listener);
81     }
82 
isCarMode()83     public boolean isCarMode() {
84         return mIsCarMode;
85     }
86 
onEnterCarMode()87     private void onEnterCarMode() {
88         if (!mIsCarMode) {
89             Log.i(this, "Entering carmode");
90             mIsCarMode = true;
91             notifyCarMode();
92         }
93     }
94 
onExitCarMode()95     private void onExitCarMode() {
96         if (mIsCarMode) {
97             Log.i(this, "Exiting carmode");
98             mIsCarMode = false;
99             notifyCarMode();
100         }
101     }
102 
notifyCarMode()103     private void notifyCarMode() {
104         for (SystemStateListener listener : mListeners) {
105             listener.onCarModeChanged(mIsCarMode);
106         }
107     }
108 
109     /**
110      * Checks the system for the current car mode.
111      *
112      * @return True if in car mode, false otherwise.
113      */
getSystemCarMode()114     private boolean getSystemCarMode() {
115         UiModeManager uiModeManager =
116                 (UiModeManager) mContext.getSystemService(Context.UI_MODE_SERVICE);
117 
118         if (uiModeManager != null) {
119             return uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_CAR;
120         }
121 
122         return false;
123     }
124 }
125