LocationController.java revision de84f0e77ea2bf713d15c290264059a413c2486a
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
17package com.android.systemui.statusbar.policy;
18
19import android.app.INotificationManager;
20import android.app.Notification;
21import android.app.NotificationManager;
22import android.app.PendingIntent;
23import android.content.BroadcastReceiver;
24import android.content.Context;
25import android.content.Intent;
26import android.content.IntentFilter;
27import android.location.LocationManager;
28import android.os.UserHandle;
29import android.provider.Settings;
30
31import com.android.systemui.R;
32
33import java.util.ArrayList;
34
35public 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
44    private ArrayList<LocationGpsStateChangeCallback> mChangeCallbacks =
45            new ArrayList<LocationGpsStateChangeCallback>();
46
47    public interface LocationGpsStateChangeCallback {
48        public void onLocationGpsStateChanged(boolean inUse, String description);
49    }
50
51    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
64    public void addStateChangedCallback(LocationGpsStateChangeCallback cb) {
65        mChangeCallbacks.add(cb);
66    }
67
68    @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        }
91
92        try {
93            if (visible) {
94                Intent gpsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
95                gpsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
96
97                PendingIntent pendingIntent = PendingIntent.getActivityAsUser(context, 0,
98                        gpsIntent, 0, null, UserHandle.CURRENT);
99                String text = mContext.getText(textResId).toString();
100
101                Notification n = new Notification.Builder(mContext)
102                    .setSmallIcon(iconId)
103                    .setContentTitle(text)
104                    .setOngoing(true)
105                    .setContentIntent(pendingIntent)
106                    .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;
111
112                n.priority = Notification.PRIORITY_HIGH;
113
114                int[] idOut = new int[1];
115                mNotificationService.enqueueNotificationWithTag(
116                        mContext.getPackageName(), mContext.getBasePackageName(),
117                        null,
118                        GPS_NOTIFICATION_ID,
119                        n,
120                        idOut,
121                        UserHandle.USER_ALL);
122
123                for (LocationGpsStateChangeCallback cb : mChangeCallbacks) {
124                    cb.onLocationGpsStateChanged(true, text);
125                }
126            } else {
127                mNotificationService.cancelNotificationWithTag(
128                        mContext.getPackageName(), null,
129                        GPS_NOTIFICATION_ID, UserHandle.USER_ALL);
130
131                for (LocationGpsStateChangeCallback cb : mChangeCallbacks) {
132                    cb.onLocationGpsStateChanged(false, null);
133                }
134            }
135        } catch (android.os.RemoteException ex) {
136            // well, it was worth a shot
137        }
138    }
139}
140
141