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