blob: f864d041c24b728187773384f9e86bce724219ef [file] [log] [blame]
Daniel Sandlere40451a2011-02-03 14:51:35 -05001/*
2 * Copyright (C) 2008 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.systemui.statusbar.policy;
18
19import java.util.ArrayList;
20
21import android.app.Notification;
22import android.app.NotificationManager;
Romain Guye5e764a2011-02-03 23:55:59 -080023import android.app.PendingIntent;
Daniel Sandlere40451a2011-02-03 14:51:35 -050024import android.content.BroadcastReceiver;
25import android.content.Context;
26import android.content.Intent;
27import android.content.IntentFilter;
28import android.location.LocationManager;
Dianne Hackborn41203752012-08-31 14:05:51 -070029import android.os.UserHandle;
Romain Guye5e764a2011-02-03 23:55:59 -080030import android.provider.Settings;
Daniel Sandlere40451a2011-02-03 14:51:35 -050031import android.util.Slog;
32import android.view.View;
33import android.widget.ImageView;
34
35// private NM API
36import android.app.INotificationManager;
37import com.android.internal.statusbar.StatusBarNotification;
38
39import com.android.systemui.R;
Winson Chungd63c59782012-09-05 17:34:41 -070040import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback;
Daniel Sandlere40451a2011-02-03 14:51:35 -050041
42public class LocationController extends BroadcastReceiver {
43 private static final String TAG = "StatusBar.LocationController";
44
45 private static final int GPS_NOTIFICATION_ID = 374203-122084;
46
47 private Context mContext;
48
49 private INotificationManager mNotificationService;
50
Winson Chungd63c59782012-09-05 17:34:41 -070051 private ArrayList<LocationGpsStateChangeCallback> mChangeCallbacks =
52 new ArrayList<LocationGpsStateChangeCallback>();
53
54 public interface LocationGpsStateChangeCallback {
55 public void onLocationGpsStateChanged(boolean inUse, String description);
56 }
57
Daniel Sandlere40451a2011-02-03 14:51:35 -050058 public LocationController(Context context) {
59 mContext = context;
60
61 IntentFilter filter = new IntentFilter();
62 filter.addAction(LocationManager.GPS_ENABLED_CHANGE_ACTION);
63 filter.addAction(LocationManager.GPS_FIX_CHANGE_ACTION);
64 context.registerReceiver(this, filter);
65
66 NotificationManager nm = (NotificationManager)context.getSystemService(
67 Context.NOTIFICATION_SERVICE);
68 mNotificationService = nm.getService();
69 }
70
Winson Chungd63c59782012-09-05 17:34:41 -070071 public void addStateChangedCallback(LocationGpsStateChangeCallback cb) {
72 mChangeCallbacks.add(cb);
73 }
74
Daniel Sandlere40451a2011-02-03 14:51:35 -050075 @Override
76 public void onReceive(Context context, Intent intent) {
77 final String action = intent.getAction();
78 final boolean enabled = intent.getBooleanExtra(LocationManager.EXTRA_GPS_ENABLED, false);
79
80 boolean visible;
81 int iconId, textResId;
82
83 if (action.equals(LocationManager.GPS_FIX_CHANGE_ACTION) && enabled) {
84 // GPS is getting fixes
85 iconId = com.android.internal.R.drawable.stat_sys_gps_on;
86 textResId = R.string.gps_notification_found_text;
87 visible = true;
88 } else if (action.equals(LocationManager.GPS_ENABLED_CHANGE_ACTION) && !enabled) {
89 // GPS is off
90 visible = false;
91 iconId = textResId = 0;
92 } else {
93 // GPS is on, but not receiving fixes
94 iconId = R.drawable.stat_sys_gps_acquiring_anim;
95 textResId = R.string.gps_notification_searching_text;
96 visible = true;
97 }
98
99 try {
Dianne Hackborn41203752012-08-31 14:05:51 -0700100 // XXX WHAT TO DO ABOUT MULTI-USER?
Daniel Sandlere40451a2011-02-03 14:51:35 -0500101 if (visible) {
Romain Guye5e764a2011-02-03 23:55:59 -0800102 Intent gpsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
103 gpsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungd63c59782012-09-05 17:34:41 -0700104
Dianne Hackborn41203752012-08-31 14:05:51 -0700105 PendingIntent pendingIntent = PendingIntent.getActivityAsUser(context, 0,
106 gpsIntent, 0, null, UserHandle.CURRENT);
Winson Chungd63c59782012-09-05 17:34:41 -0700107 String text = mContext.getText(textResId).toString();
Romain Guye5e764a2011-02-03 23:55:59 -0800108
Daniel Sandlere40451a2011-02-03 14:51:35 -0500109 Notification n = new Notification.Builder(mContext)
110 .setSmallIcon(iconId)
Winson Chungd63c59782012-09-05 17:34:41 -0700111 .setContentTitle(text)
Daniel Sandlere40451a2011-02-03 14:51:35 -0500112 .setOngoing(true)
Romain Guye5e764a2011-02-03 23:55:59 -0800113 .setContentIntent(pendingIntent)
Daniel Sandlere40451a2011-02-03 14:51:35 -0500114 .getNotification();
115
116 // Notification.Builder will helpfully fill these out for you no matter what you do
117 n.tickerView = null;
118 n.tickerText = null;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500119
120 n.priority = Notification.PRIORITY_HIGH;
Daniel Sandlere40451a2011-02-03 14:51:35 -0500121
122 int[] idOut = new int[1];
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500123 mNotificationService.enqueueNotificationWithTag(
Daniel Sandlere40451a2011-02-03 14:51:35 -0500124 mContext.getPackageName(),
125 null,
126 GPS_NOTIFICATION_ID,
Daniel Sandlere40451a2011-02-03 14:51:35 -0500127 n,
Dianne Hackborn41203752012-08-31 14:05:51 -0700128 idOut,
129 UserHandle.USER_CURRENT);
Winson Chunge641b6a2012-09-10 17:30:27 -0700130
131 for (LocationGpsStateChangeCallback cb : mChangeCallbacks) {
132 cb.onLocationGpsStateChanged(true, text);
133 }
Daniel Sandlere40451a2011-02-03 14:51:35 -0500134 } else {
Dianne Hackborn41203752012-08-31 14:05:51 -0700135 mNotificationService.cancelNotificationWithTag(
136 mContext.getPackageName(), null,
137 GPS_NOTIFICATION_ID, UserHandle.USER_CURRENT);
Winson Chungd63c59782012-09-05 17:34:41 -0700138
139 for (LocationGpsStateChangeCallback cb : mChangeCallbacks) {
140 cb.onLocationGpsStateChanged(false, null);
141 }
Daniel Sandlere40451a2011-02-03 14:51:35 -0500142 }
143 } catch (android.os.RemoteException ex) {
144 // well, it was worth a shot
145 }
146 }
147}
148