blob: 68d048d4f355d8303e9f51a2f92a7ceb6a54f4f5 [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 -050031
32// private NM API
33import android.app.INotificationManager;
Daniel Sandlere40451a2011-02-03 14:51:35 -050034
35import com.android.systemui.R;
36
37public class LocationController extends BroadcastReceiver {
38 private static final String TAG = "StatusBar.LocationController";
39
40 private static final int GPS_NOTIFICATION_ID = 374203-122084;
41
42 private Context mContext;
43
44 private INotificationManager mNotificationService;
45
Winson Chungd63c59782012-09-05 17:34:41 -070046 private ArrayList<LocationGpsStateChangeCallback> mChangeCallbacks =
47 new ArrayList<LocationGpsStateChangeCallback>();
48
49 public interface LocationGpsStateChangeCallback {
50 public void onLocationGpsStateChanged(boolean inUse, String description);
51 }
52
Daniel Sandlere40451a2011-02-03 14:51:35 -050053 public LocationController(Context context) {
54 mContext = context;
55
56 IntentFilter filter = new IntentFilter();
57 filter.addAction(LocationManager.GPS_ENABLED_CHANGE_ACTION);
58 filter.addAction(LocationManager.GPS_FIX_CHANGE_ACTION);
59 context.registerReceiver(this, filter);
60
61 NotificationManager nm = (NotificationManager)context.getSystemService(
62 Context.NOTIFICATION_SERVICE);
63 mNotificationService = nm.getService();
64 }
65
Winson Chungd63c59782012-09-05 17:34:41 -070066 public void addStateChangedCallback(LocationGpsStateChangeCallback cb) {
67 mChangeCallbacks.add(cb);
68 }
69
Daniel Sandlere40451a2011-02-03 14:51:35 -050070 @Override
71 public void onReceive(Context context, Intent intent) {
72 final String action = intent.getAction();
73 final boolean enabled = intent.getBooleanExtra(LocationManager.EXTRA_GPS_ENABLED, false);
74
75 boolean visible;
76 int iconId, textResId;
77
78 if (action.equals(LocationManager.GPS_FIX_CHANGE_ACTION) && enabled) {
79 // GPS is getting fixes
80 iconId = com.android.internal.R.drawable.stat_sys_gps_on;
81 textResId = R.string.gps_notification_found_text;
82 visible = true;
83 } else if (action.equals(LocationManager.GPS_ENABLED_CHANGE_ACTION) && !enabled) {
84 // GPS is off
85 visible = false;
86 iconId = textResId = 0;
87 } else {
88 // GPS is on, but not receiving fixes
89 iconId = R.drawable.stat_sys_gps_acquiring_anim;
90 textResId = R.string.gps_notification_searching_text;
91 visible = true;
92 }
93
94 try {
95 if (visible) {
Romain Guye5e764a2011-02-03 23:55:59 -080096 Intent gpsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
97 gpsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungd63c59782012-09-05 17:34:41 -070098
Dianne Hackborn41203752012-08-31 14:05:51 -070099 PendingIntent pendingIntent = PendingIntent.getActivityAsUser(context, 0,
100 gpsIntent, 0, null, UserHandle.CURRENT);
Winson Chungd63c59782012-09-05 17:34:41 -0700101 String text = mContext.getText(textResId).toString();
Romain Guye5e764a2011-02-03 23:55:59 -0800102
Daniel Sandlere40451a2011-02-03 14:51:35 -0500103 Notification n = new Notification.Builder(mContext)
104 .setSmallIcon(iconId)
Winson Chungd63c59782012-09-05 17:34:41 -0700105 .setContentTitle(text)
Daniel Sandlere40451a2011-02-03 14:51:35 -0500106 .setOngoing(true)
Romain Guye5e764a2011-02-03 23:55:59 -0800107 .setContentIntent(pendingIntent)
Daniel Sandlere40451a2011-02-03 14:51:35 -0500108 .getNotification();
109
110 // Notification.Builder will helpfully fill these out for you no matter what you do
111 n.tickerView = null;
112 n.tickerText = null;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500113
114 n.priority = Notification.PRIORITY_HIGH;
Daniel Sandlere40451a2011-02-03 14:51:35 -0500115
116 int[] idOut = new int[1];
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500117 mNotificationService.enqueueNotificationWithTag(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800118 mContext.getPackageName(), mContext.getBasePackageName(),
Daniel Sandlere40451a2011-02-03 14:51:35 -0500119 null,
120 GPS_NOTIFICATION_ID,
Daniel Sandlere40451a2011-02-03 14:51:35 -0500121 n,
Dianne Hackborn41203752012-08-31 14:05:51 -0700122 idOut,
Victoria Lease38389b62012-09-30 11:44:22 -0700123 UserHandle.USER_ALL);
Winson Chunge641b6a2012-09-10 17:30:27 -0700124
125 for (LocationGpsStateChangeCallback cb : mChangeCallbacks) {
126 cb.onLocationGpsStateChanged(true, text);
127 }
Daniel Sandlere40451a2011-02-03 14:51:35 -0500128 } else {
Dianne Hackborn41203752012-08-31 14:05:51 -0700129 mNotificationService.cancelNotificationWithTag(
130 mContext.getPackageName(), null,
Victoria Lease38389b62012-09-30 11:44:22 -0700131 GPS_NOTIFICATION_ID, UserHandle.USER_ALL);
Winson Chungd63c59782012-09-05 17:34:41 -0700132
133 for (LocationGpsStateChangeCallback cb : mChangeCallbacks) {
134 cb.onLocationGpsStateChanged(false, null);
135 }
Daniel Sandlere40451a2011-02-03 14:51:35 -0500136 }
137 } catch (android.os.RemoteException ex) {
138 // well, it was worth a shot
139 }
140 }
141}
142