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