am fd445d3: Merge change 1095 into donut

Merge commit 'fd445d3510cddc7a67cf7720935626684a2f3011'

* commit 'fd445d3510cddc7a67cf7720935626684a2f3011':
  gps: Move GPS scheduling from libgps to GpsLocationProvider.
diff --git a/location/java/com/android/internal/location/GpsLocationProvider.java b/location/java/com/android/internal/location/GpsLocationProvider.java
index 5877dd1..97b6a62 100644
--- a/location/java/com/android/internal/location/GpsLocationProvider.java
+++ b/location/java/com/android/internal/location/GpsLocationProvider.java
@@ -16,6 +16,8 @@
 
 package com.android.internal.location;
 
+import android.app.AlarmManager;
+import android.app.PendingIntent;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -59,6 +61,9 @@
 public class GpsLocationProvider extends ILocationProvider.Stub {
 
     private static final String TAG = "GpsLocationProvider";
+
+    private static final boolean DEBUG = true;
+    private static final boolean VERBOSE = false;
     
     /**
      * Broadcast intent action indicating that the GPS has either been
@@ -151,6 +156,9 @@
     
     // turn off GPS fix icon if we haven't received a fix in 10 seconds
     private static final long RECENT_FIX_TIMEOUT = 10 * 1000;
+    
+    // number of fixes to receive before disabling GPS
+    private static final int MIN_FIX_COUNT = 10;
 
     // true if we are enabled
     private boolean mEnabled;
@@ -164,6 +172,9 @@
     // requested frequency of fixes, in seconds
     private int mFixInterval = 1;
 
+    // number of fixes we have received since we started navigating
+    private int mFixCount;
+
     private int mPositionMode = GPS_POSITION_MODE_STANDALONE;
 
     // true if we started navigation
@@ -196,6 +207,11 @@
     private int mSuplDataConnectionState;
     private final ConnectivityManager mConnMgr;
 
+    // Alarms
+    private final static String ALARM_WAKEUP = "com.android.internal.location.ALARM_WAKEUP";
+    private final AlarmManager mAlarmManager;
+    private final PendingIntent mWakeupIntent;
+
     private final IBatteryStats mBatteryStats;
     private final SparseIntArray mClientUids = new SparseIntArray();
 
@@ -257,11 +273,14 @@
         return mGpsStatusProvider;
     }
 
-    private class TelephonyBroadcastReceiver extends BroadcastReceiver {
+    private final BroadcastReceiver mBroadcastReciever = new BroadcastReceiver() {
         @Override public void onReceive(Context context, Intent intent) {
             String action = intent.getAction();
 
-            if (action.equals(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED)) {
+            if (action.equals(ALARM_WAKEUP)) {
+                if (DEBUG) Log.d(TAG, "ALARM_WAKEUP");
+                startNavigating();
+            } else if (action.equals(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED)) {
                 String state = intent.getStringExtra(Phone.STATE_KEY);
                 String apnName = intent.getStringExtra(Phone.DATA_APN_KEY);
                 String reason = intent.getStringExtra(Phone.STATE_CHANGE_REASON_KEY);
@@ -278,7 +297,7 @@
                 }
             }
         }
-    }
+    };
 
     public static boolean isSupported() {
         return native_is_supported();
@@ -288,10 +307,13 @@
         mContext = context;
         mLocationManager = locationManager;
 
-        TelephonyBroadcastReceiver receiver = new TelephonyBroadcastReceiver();
+        mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
+        mWakeupIntent = PendingIntent.getBroadcast(mContext, 0, new Intent(ALARM_WAKEUP), 0);
+
         IntentFilter intentFilter = new IntentFilter();
+        intentFilter.addAction(ALARM_WAKEUP);
         intentFilter.addAction(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
-        context.registerReceiver(receiver, intentFilter);
+        context.registerReceiver(mBroadcastReciever, intentFilter);
 
         mConnMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
 
@@ -512,11 +534,11 @@
 
     public void enableLocationTracking(boolean enable) {
         if (enable) {
-            mFixRequestTime = System.currentTimeMillis();
             mTTFF = 0;
             mLastFixTime = 0;
             startNavigating();
         } else {
+            mAlarmManager.cancel(mWakeupIntent);
             stopNavigating();
         }
     }
@@ -622,7 +644,7 @@
 
     public void startNavigating() {
         if (!mStarted) {
-            if (Config.LOGV) Log.v(TAG, "startNavigating");
+            if (DEBUG) Log.d(TAG, "startNavigating");
             mStarted = true;
             if (!native_start(mPositionMode, false, mFixInterval)) {
                 mStarted = false;
@@ -631,11 +653,13 @@
 
             // reset SV count to zero
             updateStatus(LocationProvider.TEMPORARILY_UNAVAILABLE, 0);
+            mFixCount = 0;
+            mFixRequestTime = System.currentTimeMillis();
         }
     }
 
     public void stopNavigating() {
-        if (Config.LOGV) Log.v(TAG, "stopNavigating");
+        if (DEBUG) Log.d(TAG, "stopNavigating");
         if (mStarted) {
             mStarted = false;
             native_stop();
@@ -653,7 +677,7 @@
      */
     private void reportLocation(int flags, double latitude, double longitude, double altitude,
             float speed, float bearing, float accuracy, long timestamp) {
-        if (Config.LOGV) Log.v(TAG, "reportLocation lat: " + latitude + " long: " + longitude +
+        if (VERBOSE) Log.v(TAG, "reportLocation lat: " + latitude + " long: " + longitude +
                 " timestamp: " + timestamp);
 
         mLastFixTime = System.currentTimeMillis();
@@ -721,13 +745,23 @@
             mContext.sendBroadcast(intent);
             updateStatus(LocationProvider.AVAILABLE, mSvCount);
         }
+
+        if (mFixCount++ >= MIN_FIX_COUNT && mFixInterval > 1) {
+            if (DEBUG) Log.d(TAG, "exceeded MIN_FIX_COUNT");
+            stopNavigating();
+            mFixCount = 0;
+            mAlarmManager.cancel(mWakeupIntent);
+            long now = SystemClock.elapsedRealtime();
+            mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
+                    SystemClock.elapsedRealtime() + mFixInterval * 1000, mWakeupIntent);
+        }
    }
 
     /**
      * called from native code to update our status
      */
     private void reportStatus(int status) {
-        if (Config.LOGV) Log.v(TAG, "reportStatus status: " + status);
+        if (VERBOSE) Log.v(TAG, "reportStatus status: " + status);
 
         boolean wasNavigating = mNavigating;
         mNavigating = (status == GPS_STATUS_SESSION_BEGIN);
@@ -797,12 +831,12 @@
             }
         }
 
-        if (Config.LOGD) {
-            if (Config.LOGV) Log.v(TAG, "SV count: " + svCount +
+        if (VERBOSE) {
+            Log.v(TAG, "SV count: " + svCount +
                     " ephemerisMask: " + Integer.toHexString(mSvMasks[EPHEMERIS_MASK]) +
                     " almanacMask: " + Integer.toHexString(mSvMasks[ALMANAC_MASK]));
             for (int i = 0; i < svCount; i++) {
-                if (Config.LOGV) Log.v(TAG, "sv: " + mSvs[i] +
+                Log.v(TAG, "sv: " + mSvs[i] +
                         " snr: " + (float)mSnrs[i]/10 +
                         " elev: " + mSvElevations[i] +
                         " azimuth: " + mSvAzimuths[i] +