blob: 46b2efc5942f82d1986a3078b82fe8d7f01e4ea6 [file] [log] [blame]
Santos Cordona04bdca2015-03-04 16:57:55 -08001/*
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
17package com.android.server.telecom;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
Brad Ebingera3eccfe2016-10-05 15:45:22 -070023import android.telecom.Log;
Santos Cordona04bdca2015-03-04 16:57:55 -080024
Hall Liuf62630a2015-10-27 14:53:39 -070025import com.android.internal.annotations.VisibleForTesting;
Santos Cordona04bdca2015-03-04 16:57:55 -080026import com.android.internal.util.IndentingPrintWriter;
27
28import java.util.Collections;
29import java.util.Set;
30import java.util.concurrent.ConcurrentHashMap;
31
32/** Listens for and caches car dock state. */
Hall Liuf62630a2015-10-27 14:53:39 -070033@VisibleForTesting
34public class DockManager {
35 @VisibleForTesting
36 public interface Listener {
Santos Cordona04bdca2015-03-04 16:57:55 -080037 void onDockChanged(boolean isDocked);
38 }
39
40 /** Receiver for car dock plugged and unplugged events. */
41 private class DockBroadcastReceiver extends BroadcastReceiver {
42 @Override
43 public void onReceive(Context context, Intent intent) {
Brad Ebinger58f36582016-01-21 14:20:07 -080044 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();
Santos Cordona04bdca2015-03-04 16:57:55 -080053 }
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
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
Hall Liuf62630a2015-10-27 14:53:39 -070077 @VisibleForTesting
78 public void addListener(Listener listener) {
Santos Cordona04bdca2015-03-04 16:57:55 -080079 mListeners.add(listener);
80 }
81
82 void removeListener(Listener listener) {
83 if (listener != null) {
84 mListeners.remove(listener);
85 }
86 }
87
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
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 */
115 public void dump(IndentingPrintWriter pw) {
116 pw.println("mIsDocked: " + isDocked());
117 }
118}