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.provider.Settings;
     30 import android.util.Slog;
     31 import android.view.View;
     32 import android.widget.ImageView;
     33 
     34 // private NM API
     35 import android.app.INotificationManager;
     36 import com.android.internal.statusbar.StatusBarNotification;
     37 
     38 import com.android.systemui.R;
     39 
     40 public class LocationController extends BroadcastReceiver {
     41     private static final String TAG = "StatusBar.LocationController";
     42 
     43     private static final int GPS_NOTIFICATION_ID = 374203-122084;
     44 
     45     private Context mContext;
     46 
     47     private INotificationManager mNotificationService;
     48 
     49     public LocationController(Context context) {
     50         mContext = context;
     51 
     52         IntentFilter filter = new IntentFilter();
     53         filter.addAction(LocationManager.GPS_ENABLED_CHANGE_ACTION);
     54         filter.addAction(LocationManager.GPS_FIX_CHANGE_ACTION);
     55         context.registerReceiver(this, filter);
     56 
     57         NotificationManager nm = (NotificationManager)context.getSystemService(
     58                 Context.NOTIFICATION_SERVICE);
     59         mNotificationService = nm.getService();
     60     }
     61 
     62     @Override
     63     public void onReceive(Context context, Intent intent) {
     64         final String action = intent.getAction();
     65         final boolean enabled = intent.getBooleanExtra(LocationManager.EXTRA_GPS_ENABLED, false);
     66 
     67         boolean visible;
     68         int iconId, textResId;
     69 
     70         if (action.equals(LocationManager.GPS_FIX_CHANGE_ACTION) && enabled) {
     71             // GPS is getting fixes
     72             iconId = com.android.internal.R.drawable.stat_sys_gps_on;
     73             textResId = R.string.gps_notification_found_text;
     74             visible = true;
     75         } else if (action.equals(LocationManager.GPS_ENABLED_CHANGE_ACTION) && !enabled) {
     76             // GPS is off
     77             visible = false;
     78             iconId = textResId = 0;
     79         } else {
     80             // GPS is on, but not receiving fixes
     81             iconId = R.drawable.stat_sys_gps_acquiring_anim;
     82             textResId = R.string.gps_notification_searching_text;
     83             visible = true;
     84         }
     85 
     86         try {
     87             if (visible) {
     88                 Intent gpsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
     89                 gpsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     90                 PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, gpsIntent, 0);
     91 
     92                 Notification n = new Notification.Builder(mContext)
     93                     .setSmallIcon(iconId)
     94                     .setContentTitle(mContext.getText(textResId))
     95                     .setOngoing(true)
     96                     .setContentIntent(pendingIntent)
     97                     .getNotification();
     98 
     99                 // Notification.Builder will helpfully fill these out for you no matter what you do
    100                 n.tickerView = null;
    101                 n.tickerText = null;
    102 
    103                 n.priority = Notification.PRIORITY_HIGH;
    104 
    105                 int[] idOut = new int[1];
    106                 mNotificationService.enqueueNotificationWithTag(
    107                         mContext.getPackageName(),
    108                         null,
    109                         GPS_NOTIFICATION_ID,
    110                         n,
    111                         idOut);
    112             } else {
    113                 mNotificationService.cancelNotification(
    114                         mContext.getPackageName(),
    115                         GPS_NOTIFICATION_ID);
    116             }
    117         } catch (android.os.RemoteException ex) {
    118             // well, it was worth a shot
    119         }
    120     }
    121 }
    122 
    123