OSDN Git Service

am a20da808: am 14638566: b/2296110 Car Dock - Enable BT if not already enabled.
[android-x86/packages-apps-Settings.git] / src / com / android / settings / bluetooth / DockEventReceiver.java
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
17 package com.android.settings.bluetooth;
18
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;
26
27 public class DockEventReceiver extends BroadcastReceiver {
28
29     private static final boolean DEBUG = true;
30
31     private static final String TAG = "DockEventReceiver";
32
33     public static final String ACTION_DOCK_SHOW_UI =
34         "com.android.settings.bluetooth.action.DOCK_SHOW_UI";
35
36     private static final int EXTRA_INVALID = -1234;
37
38     static final Object mStartingServiceSync = new Object();
39
40     static PowerManager.WakeLock mStartingService;
41
42     @Override
43     public void onReceive(Context context, Intent intent) {
44         if (intent == null)
45             return;
46
47         int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, EXTRA_INVALID);
48         BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
49
50         if (DEBUG) {
51             Log.d(TAG, "Action: " + intent.getAction() + " State:" + state + " Device: "
52                     + (device == null ? "null" : device.getName()));
53         }
54
55         if (Intent.ACTION_DOCK_EVENT.equals(intent.getAction())
56                 || ACTION_DOCK_SHOW_UI.endsWith(intent.getAction())) {
57             if (device == null) {
58                 if (DEBUG) Log.e(TAG, "Device is missing");
59                 return;
60             }
61
62             switch (state) {
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);
69                     break;
70                 default:
71                     if (DEBUG) Log.e(TAG, "Unknown state");
72                     break;
73             }
74         }
75     }
76
77     /**
78      * Start the service to process the current event notifications, acquiring
79      * the wake lock before returning to ensure that the service will run.
80      */
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);
88             }
89
90             mStartingService.acquire();
91
92             if (context.startService(intent) == null) {
93                 Log.e(TAG, "Can't start DockService");
94                 mStartingService.release();
95             }
96         }
97     }
98
99     /**
100      * Called back by the service when it has finished processing notifications,
101      * releasing the wake lock if the service is now stopping.
102      */
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();
109                 }
110             }
111         }
112     }
113 }