blob: 0944b403471642d0eb573b44c2a5a8cb5f4ea252 [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 {
100 if (visible) {
Romain Guye5e764a2011-02-03 23:55:59 -0800101 Intent gpsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
102 gpsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungd63c59782012-09-05 17:34:41 -0700103
Dianne Hackborn41203752012-08-31 14:05:51 -0700104 PendingIntent pendingIntent = PendingIntent.getActivityAsUser(context, 0,
105 gpsIntent, 0, null, UserHandle.CURRENT);
Winson Chungd63c59782012-09-05 17:34:41 -0700106 String text = mContext.getText(textResId).toString();
Romain Guye5e764a2011-02-03 23:55:59 -0800107
Daniel Sandlere40451a2011-02-03 14:51:35 -0500108 Notification n = new Notification.Builder(mContext)
109 .setSmallIcon(iconId)
Winson Chungd63c59782012-09-05 17:34:41 -0700110 .setContentTitle(text)
Daniel Sandlere40451a2011-02-03 14:51:35 -0500111 .setOngoing(true)
Romain Guye5e764a2011-02-03 23:55:59 -0800112 .setContentIntent(pendingIntent)
Daniel Sandlere40451a2011-02-03 14:51:35 -0500113 .getNotification();
114
115 // Notification.Builder will helpfully fill these out for you no matter what you do
116 n.tickerView = null;
117 n.tickerText = null;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500118
119 n.priority = Notification.PRIORITY_HIGH;
Daniel Sandlere40451a2011-02-03 14:51:35 -0500120
121 int[] idOut = new int[1];
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500122 mNotificationService.enqueueNotificationWithTag(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800123 mContext.getPackageName(), mContext.getBasePackageName(),
Daniel Sandlere40451a2011-02-03 14:51:35 -0500124 null,
125 GPS_NOTIFICATION_ID,
Daniel Sandlere40451a2011-02-03 14:51:35 -0500126 n,
Dianne Hackborn41203752012-08-31 14:05:51 -0700127 idOut,
Victoria Lease38389b62012-09-30 11:44:22 -0700128 UserHandle.USER_ALL);
Winson Chunge641b6a2012-09-10 17:30:27 -0700129
130 for (LocationGpsStateChangeCallback cb : mChangeCallbacks) {
131 cb.onLocationGpsStateChanged(true, text);
132 }
Daniel Sandlere40451a2011-02-03 14:51:35 -0500133 } else {
Dianne Hackborn41203752012-08-31 14:05:51 -0700134 mNotificationService.cancelNotificationWithTag(
135 mContext.getPackageName(), null,
Victoria Lease38389b62012-09-30 11:44:22 -0700136 GPS_NOTIFICATION_ID, UserHandle.USER_ALL);
Winson Chungd63c59782012-09-05 17:34:41 -0700137
138 for (LocationGpsStateChangeCallback cb : mChangeCallbacks) {
139 cb.onLocationGpsStateChanged(false, null);
140 }
Daniel Sandlere40451a2011-02-03 14:51:35 -0500141 }
142 } catch (android.os.RemoteException ex) {
143 // well, it was worth a shot
144 }
145 }
146}
147