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