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 java.util.ArrayList;
20
21import android.app.Notification;
22import android.app.NotificationManager;
23import android.app.PendingIntent;
24import android.content.BroadcastReceiver;
25import android.content.Context;
26import android.content.Intent;
27import android.content.IntentFilter;
28import android.location.LocationManager;
29import android.os.UserHandle;
30import android.provider.Settings;
31import 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;
40import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback;
41
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
51    private ArrayList<LocationGpsStateChangeCallback> mChangeCallbacks =
52            new ArrayList<LocationGpsStateChangeCallback>();
53
54    public interface LocationGpsStateChangeCallback {
55        public void onLocationGpsStateChanged(boolean inUse, String description);
56    }
57
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
71    public void addStateChangedCallback(LocationGpsStateChangeCallback cb) {
72        mChangeCallbacks.add(cb);
73    }
74
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) {
101                Intent gpsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
102                gpsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
103
104                PendingIntent pendingIntent = PendingIntent.getActivityAsUser(context, 0,
105                        gpsIntent, 0, null, UserHandle.CURRENT);
106                String text = mContext.getText(textResId).toString();
107
108                Notification n = new Notification.Builder(mContext)
109                    .setSmallIcon(iconId)
110                    .setContentTitle(text)
111                    .setOngoing(true)
112                    .setContentIntent(pendingIntent)
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;
118
119                n.priority = Notification.PRIORITY_HIGH;
120
121                int[] idOut = new int[1];
122                mNotificationService.enqueueNotificationWithTag(
123                        mContext.getPackageName(),
124                        null,
125                        GPS_NOTIFICATION_ID,
126                        n,
127                        idOut,
128                        UserHandle.USER_ALL);
129
130                for (LocationGpsStateChangeCallback cb : mChangeCallbacks) {
131                    cb.onLocationGpsStateChanged(true, text);
132                }
133            } else {
134                mNotificationService.cancelNotificationWithTag(
135                        mContext.getPackageName(), null,
136                        GPS_NOTIFICATION_ID, UserHandle.USER_ALL);
137
138                for (LocationGpsStateChangeCallback cb : mChangeCallbacks) {
139                    cb.onLocationGpsStateChanged(false, null);
140                }
141            }
142        } catch (android.os.RemoteException ex) {
143            // well, it was worth a shot
144        }
145    }
146}
147
148