Home | History | Annotate | Download | only in policy
      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;
     23 import android.app.PendingIntent;
     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;
     29 import android.os.UserHandle;
     30 import android.provider.Settings;
     31 
     32 // private NM API
     33 import android.app.INotificationManager;
     34 
     35 import com.android.systemui.R;
     36 
     37 public 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 
     46     private ArrayList<LocationGpsStateChangeCallback> mChangeCallbacks =
     47             new ArrayList<LocationGpsStateChangeCallback>();
     48 
     49     public interface LocationGpsStateChangeCallback {
     50         public void onLocationGpsStateChanged(boolean inUse, String description);
     51     }
     52 
     53     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 
     66     public void addStateChangedCallback(LocationGpsStateChangeCallback cb) {
     67         mChangeCallbacks.add(cb);
     68     }
     69 
     70     @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) {
     96                 Intent gpsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
     97                 gpsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     98 
     99                 PendingIntent pendingIntent = PendingIntent.getActivityAsUser(context, 0,
    100                         gpsIntent, 0, null, UserHandle.CURRENT);
    101                 String text = mContext.getText(textResId).toString();
    102 
    103                 Notification n = new Notification.Builder(mContext)
    104                     .setSmallIcon(iconId)
    105                     .setContentTitle(text)
    106                     .setOngoing(true)
    107                     .setContentIntent(pendingIntent)
    108                     .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;
    113 
    114                 n.priority = Notification.PRIORITY_HIGH;
    115 
    116                 int[] idOut = new int[1];
    117                 mNotificationService.enqueueNotificationWithTag(
    118                         mContext.getPackageName(), mContext.getBasePackageName(),
    119                         null,
    120                         GPS_NOTIFICATION_ID,
    121                         n,
    122                         idOut,
    123                         UserHandle.USER_ALL);
    124 
    125                 for (LocationGpsStateChangeCallback cb : mChangeCallbacks) {
    126                     cb.onLocationGpsStateChanged(true, text);
    127                 }
    128             } else {
    129                 mNotificationService.cancelNotificationWithTag(
    130                         mContext.getPackageName(), null,
    131                         GPS_NOTIFICATION_ID, UserHandle.USER_ALL);
    132 
    133                 for (LocationGpsStateChangeCallback cb : mChangeCallbacks) {
    134                     cb.onLocationGpsStateChanged(false, null);
    135                 }
    136             }
    137         } catch (android.os.RemoteException ex) {
    138             // well, it was worth a shot
    139         }
    140     }
    141 }
    142 
    143