2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.bluetooth;
19 import android.app.Service;
20 import android.bluetooth.BluetoothDevice;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.os.PowerManager;
25 import android.util.Log;
27 public class DockEventReceiver extends BroadcastReceiver {
29 private static final boolean DEBUG = true;
31 private static final String TAG = "DockEventReceiver";
33 public static final String ACTION_DOCK_SHOW_UI =
34 "com.android.settings.bluetooth.action.DOCK_SHOW_UI";
36 private static final int EXTRA_INVALID = -1234;
38 static final Object mStartingServiceSync = new Object();
40 static PowerManager.WakeLock mStartingService;
43 public void onReceive(Context context, Intent intent) {
47 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, EXTRA_INVALID);
48 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
51 Log.d(TAG, "Action: " + intent.getAction() + " State:" + state + " Device: "
52 + (device == null ? "null" : device.getName()));
55 if (Intent.ACTION_DOCK_EVENT.equals(intent.getAction())
56 || ACTION_DOCK_SHOW_UI.endsWith(intent.getAction())) {
58 if (DEBUG) Log.e(TAG, "Device is missing");
63 case Intent.EXTRA_DOCK_STATE_UNDOCKED:
64 case Intent.EXTRA_DOCK_STATE_CAR:
65 case Intent.EXTRA_DOCK_STATE_DESK:
66 Intent i = new Intent(intent);
67 i.setClass(context, DockService.class);
68 beginStartingService(context, i);
71 if (DEBUG) Log.e(TAG, "Unknown state");
78 * Start the service to process the current event notifications, acquiring
79 * the wake lock before returning to ensure that the service will run.
81 public static void beginStartingService(Context context, Intent intent) {
82 synchronized (mStartingServiceSync) {
83 if (mStartingService == null) {
84 PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
85 mStartingService = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
86 "StartingDockService");
87 mStartingService.setReferenceCounted(false);
90 mStartingService.acquire();
92 if (context.startService(intent) == null) {
93 Log.e(TAG, "Can't start DockService");
94 mStartingService.release();
100 * Called back by the service when it has finished processing notifications,
101 * releasing the wake lock if the service is now stopping.
103 public static void finishStartingService(Service service, int startId) {
104 synchronized (mStartingServiceSync) {
105 if (mStartingService != null) {
106 if (DEBUG) Log.d(TAG, "stopSelf id = "+ startId);
107 if (service.stopSelfResult(startId)) {
108 mStartingService.release();