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.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.telecom.Log;
24 
25 import com.android.internal.annotations.VisibleForTesting;
26 import com.android.internal.util.IndentingPrintWriter;
27 
28 import java.util.Collections;
29 import java.util.Set;
30 import java.util.concurrent.ConcurrentHashMap;
31 
32 /** Listens for and caches car dock state. */
33 @VisibleForTesting
34 public class DockManager {
35     @VisibleForTesting
36     public interface Listener {
onDockChanged(boolean isDocked)37         void onDockChanged(boolean isDocked);
38     }
39 
40     /** Receiver for car dock plugged and unplugged events. */
41     private class DockBroadcastReceiver extends BroadcastReceiver {
42         @Override
onReceive(Context context, Intent intent)43         public void onReceive(Context context, Intent intent) {
44             Log.startSession("DM.oR");
45             try {
46                 if (Intent.ACTION_DOCK_EVENT.equals(intent.getAction())) {
47                     int dockState = intent.getIntExtra(
48                             Intent.EXTRA_DOCK_STATE, Intent.EXTRA_DOCK_STATE_UNDOCKED);
49                     onDockChanged(dockState);
50                 }
51             } finally {
52                 Log.endSession();
53             }
54         }
55     }
56 
57     private final DockBroadcastReceiver mReceiver;
58 
59     /**
60      * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
61      * load factor before resizing, 1 means we only expect a single thread to
62      * access the map so make only a single shard
63      */
64     private final Set<Listener> mListeners = Collections.newSetFromMap(
65             new ConcurrentHashMap<Listener, Boolean>(8, 0.9f, 1));
66 
67     private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
68 
DockManager(Context context)69     DockManager(Context context) {
70         mReceiver = new DockBroadcastReceiver();
71 
72         // Register for misc other intent broadcasts.
73         IntentFilter intentFilter = new IntentFilter(Intent.ACTION_DOCK_EVENT);
74         context.registerReceiver(mReceiver, intentFilter);
75     }
76 
77     @VisibleForTesting
addListener(Listener listener)78     public void addListener(Listener listener) {
79         mListeners.add(listener);
80     }
81 
removeListener(Listener listener)82     void removeListener(Listener listener) {
83         if (listener != null) {
84             mListeners.remove(listener);
85         }
86     }
87 
isDocked()88     boolean isDocked() {
89         switch (mDockState) {
90             case Intent.EXTRA_DOCK_STATE_DESK:
91             case Intent.EXTRA_DOCK_STATE_HE_DESK:
92             case Intent.EXTRA_DOCK_STATE_LE_DESK:
93             case Intent.EXTRA_DOCK_STATE_CAR:
94                 return true;
95             default:
96                 return false;
97         }
98     }
99 
onDockChanged(int dockState)100     private void onDockChanged(int dockState) {
101         if (mDockState != dockState) {
102             Log.v(this, "onDockChanged: is docked?%b", dockState == Intent.EXTRA_DOCK_STATE_CAR);
103             mDockState = dockState;
104             for (Listener listener : mListeners) {
105                 listener.onDockChanged(isDocked());
106             }
107         }
108     }
109 
110     /**
111      * Dumps the state of the {@link DockManager}.
112      *
113      * @param pw The {@code IndentingPrintWriter} to write the state to.
114      */
dump(IndentingPrintWriter pw)115     public void dump(IndentingPrintWriter pw) {
116         pw.println("mIsDocked: " + isDocked());
117     }
118 }
119