blob: c5563daeee8a7703ab73ba4ed3e3de94b2a587c4 [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
John Spurlockde84f0e2013-06-12 12:41:00 -040019import android.app.INotificationManager;
Daniel Sandlere40451a2011-02-03 14:51:35 -050020import android.app.Notification;
21import android.app.NotificationManager;
Romain Guye5e764a2011-02-03 23:55:59 -080022import android.app.PendingIntent;
Daniel Sandlere40451a2011-02-03 14:51:35 -050023import android.content.BroadcastReceiver;
24import android.content.Context;
25import android.content.Intent;
26import android.content.IntentFilter;
27import android.location.LocationManager;
Dianne Hackborn41203752012-08-31 14:05:51 -070028import android.os.UserHandle;
Romain Guye5e764a2011-02-03 23:55:59 -080029import android.provider.Settings;
Daniel Sandlere40451a2011-02-03 14:51:35 -050030
Daniel Sandlere40451a2011-02-03 14:51:35 -050031import com.android.systemui.R;
32
John Spurlockde84f0e2013-06-12 12:41:00 -040033import java.util.ArrayList;
34
Daniel Sandlere40451a2011-02-03 14:51:35 -050035public class LocationController extends BroadcastReceiver {
36 private static final String TAG = "StatusBar.LocationController";
37
38 private static final int GPS_NOTIFICATION_ID = 374203-122084;
39
40 private Context mContext;
41
42 private INotificationManager mNotificationService;
43
Winson Chungd63c59782012-09-05 17:34:41 -070044 private ArrayList<LocationGpsStateChangeCallback> mChangeCallbacks =
45 new ArrayList<LocationGpsStateChangeCallback>();
46
47 public interface LocationGpsStateChangeCallback {
48 public void onLocationGpsStateChanged(boolean inUse, String description);
49 }
50
Daniel Sandlere40451a2011-02-03 14:51:35 -050051 public LocationController(Context context) {
52 mContext = context;
53
54 IntentFilter filter = new IntentFilter();
55 filter.addAction(LocationManager.GPS_ENABLED_CHANGE_ACTION);
56 filter.addAction(LocationManager.GPS_FIX_CHANGE_ACTION);
57 context.registerReceiver(this, filter);
58
59 NotificationManager nm = (NotificationManager)context.getSystemService(
60 Context.NOTIFICATION_SERVICE);
61 mNotificationService = nm.getService();
62 }
63
Winson Chungd63c59782012-09-05 17:34:41 -070064 public void addStateChangedCallback(LocationGpsStateChangeCallback cb) {
65 mChangeCallbacks.add(cb);
66 }
67
Daniel Sandlere40451a2011-02-03 14:51:35 -050068 @Override
69 public void onReceive(Context context, Intent intent) {
70 final String action = intent.getAction();
71 final boolean enabled = intent.getBooleanExtra(LocationManager.EXTRA_GPS_ENABLED, false);
72
73 boolean visible;
74 int iconId, textResId;
75
76 if (action.equals(LocationManager.GPS_FIX_CHANGE_ACTION) && enabled) {
77 // GPS is getting fixes
78 iconId = com.android.internal.R.drawable.stat_sys_gps_on;
79 textResId = R.string.gps_notification_found_text;
80 visible = true;
81 } else if (action.equals(LocationManager.GPS_ENABLED_CHANGE_ACTION) && !enabled) {
82 // GPS is off
83 visible = false;
84 iconId = textResId = 0;
85 } else {
86 // GPS is on, but not receiving fixes
87 iconId = R.drawable.stat_sys_gps_acquiring_anim;
88 textResId = R.string.gps_notification_searching_text;
89 visible = true;
90 }
John Spurlock209bede2013-07-17 12:23:27 -040091
Daniel Sandlere40451a2011-02-03 14:51:35 -050092 try {
93 if (visible) {
Romain Guye5e764a2011-02-03 23:55:59 -080094 Intent gpsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
95 gpsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungd63c59782012-09-05 17:34:41 -070096
Dianne Hackborn41203752012-08-31 14:05:51 -070097 PendingIntent pendingIntent = PendingIntent.getActivityAsUser(context, 0,
98 gpsIntent, 0, null, UserHandle.CURRENT);
Winson Chungd63c59782012-09-05 17:34:41 -070099 String text = mContext.getText(textResId).toString();
Romain Guye5e764a2011-02-03 23:55:59 -0800100
Daniel Sandlere40451a2011-02-03 14:51:35 -0500101 Notification n = new Notification.Builder(mContext)
102 .setSmallIcon(iconId)
Winson Chungd63c59782012-09-05 17:34:41 -0700103 .setContentTitle(text)
Daniel Sandlere40451a2011-02-03 14:51:35 -0500104 .setOngoing(true)
Romain Guye5e764a2011-02-03 23:55:59 -0800105 .setContentIntent(pendingIntent)
Daniel Sandlere40451a2011-02-03 14:51:35 -0500106 .getNotification();
107
108 // Notification.Builder will helpfully fill these out for you no matter what you do
109 n.tickerView = null;
110 n.tickerText = null;
John Spurlock209bede2013-07-17 12:23:27 -0400111
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500112 n.priority = Notification.PRIORITY_HIGH;
Daniel Sandlere40451a2011-02-03 14:51:35 -0500113
114 int[] idOut = new int[1];
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500115 mNotificationService.enqueueNotificationWithTag(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800116 mContext.getPackageName(), mContext.getBasePackageName(),
John Spurlock209bede2013-07-17 12:23:27 -0400117 null,
118 GPS_NOTIFICATION_ID,
Daniel Sandlere40451a2011-02-03 14:51:35 -0500119 n,
Dianne Hackborn41203752012-08-31 14:05:51 -0700120 idOut,
Victoria Lease38389b62012-09-30 11:44:22 -0700121 UserHandle.USER_ALL);
Winson Chunge641b6a2012-09-10 17:30:27 -0700122
123 for (LocationGpsStateChangeCallback cb : mChangeCallbacks) {
124 cb.onLocationGpsStateChanged(true, text);
125 }
Daniel Sandlere40451a2011-02-03 14:51:35 -0500126 } else {
Dianne Hackborn41203752012-08-31 14:05:51 -0700127 mNotificationService.cancelNotificationWithTag(
128 mContext.getPackageName(), null,
Victoria Lease38389b62012-09-30 11:44:22 -0700129 GPS_NOTIFICATION_ID, UserHandle.USER_ALL);
Winson Chungd63c59782012-09-05 17:34:41 -0700130
131 for (LocationGpsStateChangeCallback cb : mChangeCallbacks) {
132 cb.onLocationGpsStateChanged(false, null);
133 }
Daniel Sandlere40451a2011-02-03 14:51:35 -0500134 }
135 } catch (android.os.RemoteException ex) {
136 // well, it was worth a shot
137 }
138 }
139}
140