DockObserver.java revision 786546eaccfc7a284b90b077f505cf42206c6f5f
1/*
2 * Copyright (C) 2008 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;
18
19import static android.provider.Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK;
20
21import com.android.server.power.PowerManagerService;
22
23import android.bluetooth.BluetoothAdapter;
24import android.bluetooth.BluetoothDevice;
25import android.content.ContentResolver;
26import android.content.Context;
27import android.content.Intent;
28import android.media.AudioManager;
29import android.media.Ringtone;
30import android.media.RingtoneManager;
31import android.net.Uri;
32import android.os.Handler;
33import android.os.Message;
34import android.os.RemoteException;
35import android.os.ServiceManager;
36import android.os.SystemClock;
37import android.os.UEventObserver;
38import android.provider.Settings;
39import android.service.dreams.IDreamManager;
40import android.util.Log;
41import android.util.Slog;
42
43import java.io.FileNotFoundException;
44import java.io.FileReader;
45
46/**
47 * <p>DockObserver monitors for a docking station.
48 */
49class DockObserver extends UEventObserver {
50    private static final String TAG = DockObserver.class.getSimpleName();
51    private static final boolean LOG = false;
52
53    private static final String DOCK_UEVENT_MATCH = "DEVPATH=/devices/virtual/switch/dock";
54    private static final String DOCK_STATE_PATH = "/sys/class/switch/dock/state";
55
56    private static final int DEFAULT_DOCK = 1;
57
58    private static final int MSG_DOCK_STATE = 0;
59
60    private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
61    private int mPreviousDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
62
63    private boolean mSystemReady;
64
65    private final Context mContext;
66
67    private PowerManagerService mPowerManager;
68
69    public DockObserver(Context context, PowerManagerService pm) {
70        mContext = context;
71        mPowerManager = pm;
72        init();  // set initial status
73
74        startObserving(DOCK_UEVENT_MATCH);
75    }
76
77    @Override
78    public void onUEvent(UEventObserver.UEvent event) {
79        if (Log.isLoggable(TAG, Log.VERBOSE)) {
80            Slog.v(TAG, "Dock UEVENT: " + event.toString());
81        }
82
83        synchronized (this) {
84            try {
85                int newState = Integer.parseInt(event.get("SWITCH_STATE"));
86                if (newState != mDockState) {
87                    mPreviousDockState = mDockState;
88                    mDockState = newState;
89                    if (mSystemReady) {
90                        // Don't force screen on when undocking from the desk dock.
91                        // The change in power state will do this anyway.
92                        // FIXME - we should be configurable.
93                        if ((mPreviousDockState != Intent.EXTRA_DOCK_STATE_DESK
94                                && mPreviousDockState != Intent.EXTRA_DOCK_STATE_LE_DESK
95                                && mPreviousDockState != Intent.EXTRA_DOCK_STATE_HE_DESK) ||
96                                mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
97                            mPowerManager.userActivityWithForce(SystemClock.uptimeMillis(),
98                                    false, true);
99                        }
100                        update();
101                    }
102                }
103            } catch (NumberFormatException e) {
104                Slog.e(TAG, "Could not parse switch state from event " + event);
105            }
106        }
107    }
108
109    private final void init() {
110        char[] buffer = new char[1024];
111
112        try {
113            FileReader file = new FileReader(DOCK_STATE_PATH);
114            int len = file.read(buffer, 0, 1024);
115            file.close();
116            mPreviousDockState = mDockState = Integer.valueOf((new String(buffer, 0, len)).trim());
117        } catch (FileNotFoundException e) {
118            Slog.w(TAG, "This kernel does not have dock station support");
119        } catch (Exception e) {
120            Slog.e(TAG, "" , e);
121        }
122    }
123
124    void systemReady() {
125        synchronized (this) {
126            // don't bother broadcasting undocked here
127            if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
128                update();
129            }
130            mSystemReady = true;
131        }
132    }
133
134    private final void update() {
135        mHandler.sendEmptyMessage(MSG_DOCK_STATE);
136    }
137
138    private static boolean isScreenSaverActivatedOnDock(Context context) {
139        return 0 != Settings.Secure.getInt(
140                    context.getContentResolver(), SCREENSAVER_ACTIVATE_ON_DOCK, DEFAULT_DOCK);
141    }
142
143    private final Handler mHandler = new Handler() {
144        @Override
145        public void handleMessage(Message msg) {
146            switch (msg.what) {
147                case MSG_DOCK_STATE:
148                    synchronized (this) {
149                        Slog.i(TAG, "Dock state changed: " + mDockState);
150
151                        final ContentResolver cr = mContext.getContentResolver();
152
153                        if (Settings.Secure.getInt(cr,
154                                Settings.Secure.DEVICE_PROVISIONED, 0) == 0) {
155                            Slog.i(TAG, "Device not provisioned, skipping dock broadcast");
156                            return;
157                        }
158                        // Pack up the values and broadcast them to everyone
159                        Intent intent = new Intent(Intent.ACTION_DOCK_EVENT);
160                        intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
161                        intent.putExtra(Intent.EXTRA_DOCK_STATE, mDockState);
162
163                        // Check if this is Bluetooth Dock
164                        // TODO(BT): Get Dock address.
165                        String address = null;
166                        if (address != null)
167                            intent.putExtra(BluetoothDevice.EXTRA_DEVICE,
168                                    BluetoothAdapter.getDefaultAdapter().getRemoteDevice(address));
169
170                        // User feedback to confirm dock connection. Particularly
171                        // useful for flaky contact pins...
172                        if (Settings.System.getInt(cr,
173                                Settings.System.DOCK_SOUNDS_ENABLED, 1) == 1)
174                        {
175                            String whichSound = null;
176                            if (mDockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) {
177                                if ((mPreviousDockState == Intent.EXTRA_DOCK_STATE_DESK) ||
178                                    (mPreviousDockState == Intent.EXTRA_DOCK_STATE_LE_DESK) ||
179                                    (mPreviousDockState == Intent.EXTRA_DOCK_STATE_HE_DESK)) {
180                                    whichSound = Settings.System.DESK_UNDOCK_SOUND;
181                                } else if (mPreviousDockState == Intent.EXTRA_DOCK_STATE_CAR) {
182                                    whichSound = Settings.System.CAR_UNDOCK_SOUND;
183                                }
184                            } else {
185                                if ((mDockState == Intent.EXTRA_DOCK_STATE_DESK) ||
186                                    (mDockState == Intent.EXTRA_DOCK_STATE_LE_DESK) ||
187                                    (mDockState == Intent.EXTRA_DOCK_STATE_HE_DESK)) {
188                                    whichSound = Settings.System.DESK_DOCK_SOUND;
189                                } else if (mDockState == Intent.EXTRA_DOCK_STATE_CAR) {
190                                    whichSound = Settings.System.CAR_DOCK_SOUND;
191                                }
192                            }
193
194                            if (whichSound != null) {
195                                final String soundPath = Settings.System.getString(cr, whichSound);
196                                if (soundPath != null) {
197                                    final Uri soundUri = Uri.parse("file://" + soundPath);
198                                    if (soundUri != null) {
199                                        final Ringtone sfx = RingtoneManager.getRingtone(mContext, soundUri);
200                                        if (sfx != null) {
201                                            sfx.setStreamType(AudioManager.STREAM_SYSTEM);
202                                            sfx.play();
203                                        }
204                                    }
205                                }
206                            }
207                        }
208
209                        IDreamManager mgr = IDreamManager.Stub.asInterface(ServiceManager.getService("dreams"));
210                        if (mgr != null) {
211                            // dreams feature enabled
212                            boolean undocked = mDockState == Intent.EXTRA_DOCK_STATE_UNDOCKED;
213                            if (undocked) {
214                                try {
215                                    if (mgr.isDreaming()) {
216                                        mgr.awaken();
217                                    }
218                                } catch (RemoteException e) {
219                                    Slog.w(TAG, "Unable to awaken!", e);
220                                }
221                            } else {
222                                if (isScreenSaverActivatedOnDock(mContext)) {
223                                    try {
224                                        mgr.dream();
225                                    } catch (RemoteException e) {
226                                        Slog.w(TAG, "Unable to dream!", e);
227                                    }
228                                }
229                            }
230                        } else {
231                            // dreams feature not enabled, send legacy intent
232                            mContext.sendStickyBroadcast(intent);
233                        }
234                    }
235                    break;
236            }
237        }
238    };
239}
240