DockEventReceiver.java revision 845e740fc63657438b9085376c8e7d60d8334a72
1/*
2 * Copyright (C) 2009 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.settings.bluetooth;
18
19import android.app.Service;
20import android.bluetooth.BluetoothAdapter;
21import android.bluetooth.BluetoothDevice;
22import android.content.BroadcastReceiver;
23import android.content.Context;
24import android.content.Intent;
25import android.os.PowerManager;
26import android.util.Log;
27
28public class DockEventReceiver extends BroadcastReceiver {
29
30    private static final boolean DEBUG = DockService.DEBUG;
31
32    private static final String TAG = "DockEventReceiver";
33
34    public static final String ACTION_DOCK_SHOW_UI =
35        "com.android.settings.bluetooth.action.DOCK_SHOW_UI";
36
37    private static final int EXTRA_INVALID = -1234;
38
39    private static final Object mStartingServiceSync = new Object();
40
41    private static final long WAKELOCK_TIMEOUT = 5000;
42
43    private static PowerManager.WakeLock mStartingService;
44
45    @Override
46    public void onReceive(Context context, Intent intent) {
47        if (intent == null)
48            return;
49
50        int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, EXTRA_INVALID);
51        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
52
53        if (DEBUG) {
54            Log.d(TAG, "Action: " + intent.getAction() + " State:" + state + " Device: "
55                    + (device == null ? "null" : device.getName()));
56        }
57
58        if (Intent.ACTION_DOCK_EVENT.equals(intent.getAction())
59                || ACTION_DOCK_SHOW_UI.endsWith(intent.getAction())) {
60            if (device == null) {
61                if (DEBUG) Log.d(TAG, "Device is missing");
62                return;
63            }
64
65            switch (state) {
66                case Intent.EXTRA_DOCK_STATE_UNDOCKED:
67                case Intent.EXTRA_DOCK_STATE_CAR:
68                case Intent.EXTRA_DOCK_STATE_DESK:
69                    Intent i = new Intent(intent);
70                    i.setClass(context, DockService.class);
71                    beginStartingService(context, i);
72                    break;
73                default:
74                    if (DEBUG) Log.e(TAG, "Unknown state");
75                    break;
76            }
77        } else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
78            int btState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
79            if (btState == BluetoothAdapter.STATE_ON) {
80                Intent i = new Intent(intent);
81                i.setClass(context, DockService.class);
82                beginStartingService(context, i);
83            }
84        }
85    }
86
87    /**
88     * Start the service to process the current event notifications, acquiring
89     * the wake lock before returning to ensure that the service will run.
90     */
91    public static void beginStartingService(Context context, Intent intent) {
92        synchronized (mStartingServiceSync) {
93            if (mStartingService == null) {
94                PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
95                mStartingService = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
96                        "StartingDockService");
97            }
98
99            mStartingService.acquire(WAKELOCK_TIMEOUT);
100
101            if (context.startService(intent) == null) {
102                Log.e(TAG, "Can't start DockService");
103            }
104        }
105    }
106
107    /**
108     * Called back by the service when it has finished processing notifications,
109     * releasing the wake lock if the service is now stopping.
110     */
111    public static void finishStartingService(Service service, int startId) {
112        synchronized (mStartingServiceSync) {
113            if (mStartingService != null) {
114                if (DEBUG) Log.d(TAG, "stopSelf id = "+ startId);
115                service.stopSelfResult(startId);
116            }
117        }
118    }
119}
120