Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.statusbar.policy; |
| 18 | |
| 19 | import java.util.ArrayList; |
| 20 | |
| 21 | import android.app.Notification; |
| 22 | import android.app.NotificationManager; |
Romain Guy | e5e764a | 2011-02-03 23:55:59 -0800 | [diff] [blame] | 23 | import android.app.PendingIntent; |
Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 24 | import android.content.BroadcastReceiver; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.IntentFilter; |
| 28 | import android.location.LocationManager; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 29 | import android.os.UserHandle; |
Romain Guy | e5e764a | 2011-02-03 23:55:59 -0800 | [diff] [blame] | 30 | import android.provider.Settings; |
Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 31 | import android.util.Slog; |
| 32 | import android.view.View; |
| 33 | import android.widget.ImageView; |
| 34 | |
| 35 | // private NM API |
| 36 | import android.app.INotificationManager; |
| 37 | import com.android.internal.statusbar.StatusBarNotification; |
| 38 | |
| 39 | import com.android.systemui.R; |
Winson Chung | d63c5978 | 2012-09-05 17:34:41 -0700 | [diff] [blame] | 40 | import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback; |
Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 41 | |
| 42 | public 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 Chung | d63c5978 | 2012-09-05 17:34:41 -0700 | [diff] [blame] | 51 | private ArrayList<LocationGpsStateChangeCallback> mChangeCallbacks = |
| 52 | new ArrayList<LocationGpsStateChangeCallback>(); |
| 53 | |
| 54 | public interface LocationGpsStateChangeCallback { |
| 55 | public void onLocationGpsStateChanged(boolean inUse, String description); |
| 56 | } |
| 57 | |
Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 58 | 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 Chung | d63c5978 | 2012-09-05 17:34:41 -0700 | [diff] [blame] | 71 | public void addStateChangedCallback(LocationGpsStateChangeCallback cb) { |
| 72 | mChangeCallbacks.add(cb); |
| 73 | } |
| 74 | |
Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 75 | @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 Guy | e5e764a | 2011-02-03 23:55:59 -0800 | [diff] [blame] | 101 | Intent gpsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); |
| 102 | gpsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Winson Chung | d63c5978 | 2012-09-05 17:34:41 -0700 | [diff] [blame] | 103 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 104 | PendingIntent pendingIntent = PendingIntent.getActivityAsUser(context, 0, |
| 105 | gpsIntent, 0, null, UserHandle.CURRENT); |
Winson Chung | d63c5978 | 2012-09-05 17:34:41 -0700 | [diff] [blame] | 106 | String text = mContext.getText(textResId).toString(); |
Romain Guy | e5e764a | 2011-02-03 23:55:59 -0800 | [diff] [blame] | 107 | |
Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 108 | Notification n = new Notification.Builder(mContext) |
| 109 | .setSmallIcon(iconId) |
Winson Chung | d63c5978 | 2012-09-05 17:34:41 -0700 | [diff] [blame] | 110 | .setContentTitle(text) |
Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 111 | .setOngoing(true) |
Romain Guy | e5e764a | 2011-02-03 23:55:59 -0800 | [diff] [blame] | 112 | .setContentIntent(pendingIntent) |
Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 113 | .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 Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 118 | |
| 119 | n.priority = Notification.PRIORITY_HIGH; |
Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 120 | |
| 121 | int[] idOut = new int[1]; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 122 | mNotificationService.enqueueNotificationWithTag( |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame^] | 123 | mContext.getPackageName(), mContext.getBasePackageName(), |
Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 124 | null, |
| 125 | GPS_NOTIFICATION_ID, |
Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 126 | n, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 127 | idOut, |
Victoria Lease | 38389b6 | 2012-09-30 11:44:22 -0700 | [diff] [blame] | 128 | UserHandle.USER_ALL); |
Winson Chung | e641b6a | 2012-09-10 17:30:27 -0700 | [diff] [blame] | 129 | |
| 130 | for (LocationGpsStateChangeCallback cb : mChangeCallbacks) { |
| 131 | cb.onLocationGpsStateChanged(true, text); |
| 132 | } |
Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 133 | } else { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 134 | mNotificationService.cancelNotificationWithTag( |
| 135 | mContext.getPackageName(), null, |
Victoria Lease | 38389b6 | 2012-09-30 11:44:22 -0700 | [diff] [blame] | 136 | GPS_NOTIFICATION_ID, UserHandle.USER_ALL); |
Winson Chung | d63c5978 | 2012-09-05 17:34:41 -0700 | [diff] [blame] | 137 | |
| 138 | for (LocationGpsStateChangeCallback cb : mChangeCallbacks) { |
| 139 | cb.onLocationGpsStateChanged(false, null); |
| 140 | } |
Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 141 | } |
| 142 | } catch (android.os.RemoteException ex) { |
| 143 | // well, it was worth a shot |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |