DockEventReceiver.java revision fb5b54d77aee1b2d6a0c0ffe7d47c73a204dee28
1df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan/* 2df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan * Copyright (C) 2009 The Android Open Source Project 3df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan * 4df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan * Licensed under the Apache License, Version 2.0 (the "License"); 5df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan * you may not use this file except in compliance with the License. 6df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan * You may obtain a copy of the License at 7df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan * 8df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan * http://www.apache.org/licenses/LICENSE-2.0 9df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan * 10df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan * Unless required by applicable law or agreed to in writing, software 11df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan * distributed under the License is distributed on an "AS IS" BASIS, 12df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan * See the License for the specific language governing permissions and 14df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan * limitations under the License. 15df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan */ 16df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan 17df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chanpackage com.android.settings.bluetooth; 18df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan 19df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chanimport android.app.Service; 20fb5b54d77aee1b2d6a0c0ffe7d47c73a204dee28Michael Chanimport android.bluetooth.BluetoothAdapter; 21df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chanimport android.bluetooth.BluetoothDevice; 22df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chanimport android.content.BroadcastReceiver; 23df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chanimport android.content.Context; 24df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chanimport android.content.Intent; 25df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chanimport android.os.PowerManager; 26df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chanimport android.util.Log; 27df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan 28df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chanpublic class DockEventReceiver extends BroadcastReceiver { 29df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan 306fd5bc935ec3de1d74bd4bbbfc467b8cd4101902Michael Chan private static final boolean DEBUG = false; 31df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan 32df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan private static final String TAG = "DockEventReceiver"; 33df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan 34df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan public static final String ACTION_DOCK_SHOW_UI = 35df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan "com.android.settings.bluetooth.action.DOCK_SHOW_UI"; 36df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan 37df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan private static final int EXTRA_INVALID = -1234; 38df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan 39fb5b54d77aee1b2d6a0c0ffe7d47c73a204dee28Michael Chan private static final Object mStartingServiceSync = new Object(); 40df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan 41fb5b54d77aee1b2d6a0c0ffe7d47c73a204dee28Michael Chan private static final long WAKELOCK_TIMEOUT = 5000; 42fb5b54d77aee1b2d6a0c0ffe7d47c73a204dee28Michael Chan 43fb5b54d77aee1b2d6a0c0ffe7d47c73a204dee28Michael Chan private static PowerManager.WakeLock mStartingService; 44df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan 45df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan @Override 46df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan public void onReceive(Context context, Intent intent) { 47df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan if (intent == null) 48df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan return; 49df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan 50df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, EXTRA_INVALID); 51df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 52df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan 53df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan if (DEBUG) { 54df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan Log.d(TAG, "Action: " + intent.getAction() + " State:" + state + " Device: " 55df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan + (device == null ? "null" : device.getName())); 56df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan } 57df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan 58df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan if (Intent.ACTION_DOCK_EVENT.equals(intent.getAction()) 59df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan || ACTION_DOCK_SHOW_UI.endsWith(intent.getAction())) { 60df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan if (device == null) { 61503c236db541b3d7c8278fa63afe4d1b792ac9a9Michael Chan if (DEBUG) Log.d(TAG, "Device is missing"); 62df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan return; 63df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan } 64df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan 65df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan switch (state) { 66df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan case Intent.EXTRA_DOCK_STATE_UNDOCKED: 67df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan case Intent.EXTRA_DOCK_STATE_CAR: 68df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan case Intent.EXTRA_DOCK_STATE_DESK: 69df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan Intent i = new Intent(intent); 70df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan i.setClass(context, DockService.class); 71df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan beginStartingService(context, i); 72df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan break; 73df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan default: 74df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan if (DEBUG) Log.e(TAG, "Unknown state"); 75df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan break; 76df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan } 77df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan } 78df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan } 79df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan 80df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan /** 81df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan * Start the service to process the current event notifications, acquiring 82df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan * the wake lock before returning to ensure that the service will run. 83df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan */ 84df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan public static void beginStartingService(Context context, Intent intent) { 85df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan synchronized (mStartingServiceSync) { 86df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan if (mStartingService == null) { 87df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 88df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan mStartingService = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, 89df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan "StartingDockService"); 90df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan } 91df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan 92fb5b54d77aee1b2d6a0c0ffe7d47c73a204dee28Michael Chan mStartingService.acquire(WAKELOCK_TIMEOUT); 93df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan 94df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan if (context.startService(intent) == null) { 95df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan Log.e(TAG, "Can't start DockService"); 96df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan } 97df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan } 98df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan } 99df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan 100df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan /** 101df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan * Called back by the service when it has finished processing notifications, 102df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan * releasing the wake lock if the service is now stopping. 103df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan */ 104df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan public static void finishStartingService(Service service, int startId) { 105df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan synchronized (mStartingServiceSync) { 106df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan if (mStartingService != null) { 107146385663bb55d9f55ad179d3c35ccf5cac13fc8Michael Chan if (DEBUG) Log.d(TAG, "stopSelf id = "+ startId); 108fb5b54d77aee1b2d6a0c0ffe7d47c73a204dee28Michael Chan service.stopSelfResult(startId); 109df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan } 110df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan } 111df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan } 112df9504ef58e8dafdd80ca9cd780510f9444943e2Michael Chan} 113