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.deskclock;
19 import android.app.Notification;
20 import android.app.NotificationManager;
21 import android.app.PendingIntent;
22 import android.content.BroadcastReceiver;
23 import android.content.ComponentName;
24 import android.content.Context;
25 import android.content.Intent;
28 * BroadcastReceiver which receives {@link Intent#ACTION_DOCK_EVENT} events.
29 * Launches the CarDockActivity if the device is placed into a car dock.
31 * TODO: This is the wrong way to launch, as this would cause contention
32 * between multiple activities trying to launch if others did the same. Instead
33 * register for a regular intent which should fire when placed into a car dock.
35 public class DockEventReceiver extends BroadcastReceiver {
37 public void onReceive(Context context, Intent intent) {
38 Intent clockIntent = new Intent(Intent.ACTION_MAIN);
39 clockIntent.setComponent(
40 new ComponentName(context, AlarmClock.class));
41 clockIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
43 String action = intent.getAction();
44 if (Intent.ACTION_DOCK_EVENT.equals(action)) {
45 // Code to control a sticky notification for the dock.
47 NotificationManager notificationManager = (NotificationManager)
48 context.getSystemService(Context.NOTIFICATION_SERVICE);
50 int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, -1);
51 if (dockState == Intent.EXTRA_DOCK_STATE_DESK) {
52 Notification n = new Notification();
53 n.icon = R.drawable.notification;
54 n.defaults = Notification.DEFAULT_LIGHTS;
55 n.flags = Notification.FLAG_ONGOING_EVENT;
56 n.tickerText = context.getString(R.string.notification_title);
60 context.getString(R.string.notification_title),
61 context.getString(R.string.notification_text),
62 PendingIntent.getActivity(context, 0, clockIntent, 0));
63 notificationManager.notify(0, n);
64 } else if (dockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) {
65 notificationManager.cancelAll();
68 } else if (android.provider.Telephony.Intents.SECRET_CODE_ACTION.equals(action)) {
69 // The user dialed *#*#DESK#*#*
70 context.startActivity(clockIntent);