Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1c690751 authored by Mike LeBeau's avatar Mike LeBeau
Browse files

Trigger the LocationManager whenever the SearchDialog is shown

(and stop when the SearchDialog is stopped). This way we get a
network-based location quickly so that by the time any location-
based suggestion provider wants to do suggestions, it's likely
to have a good fresh location.
parent 1480eb27
Loading
Loading
Loading
Loading
+68 −0
Original line number Diff line number Diff line
@@ -33,6 +33,11 @@ import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
@@ -145,6 +150,15 @@ public class SearchDialog extends Dialog implements OnItemClickListener, OnItemS
    private final WeakHashMap<String, Drawable> mOutsideDrawablesCache =
            new WeakHashMap<String, Drawable>();
    
    // Objects we keep around for requesting location updates when the dialog is started
    // (and canceling them when the dialog is stopped). We don't actually make use of the
    // updates ourselves here, so the LocationListener is just a dummy which doesn't do
    // anything. We only do this here so that other suggest providers which wish to provide
    // location-based suggestions are more likely to get a good fresh location.
    private LocationManager mLocationManager;
    private LocationProvider mLocationProvider;
    private LocationListener mDummyLocationListener;
    
    /**
     * Constructor - fires it up and makes it look like the search UI.
     * 
@@ -221,6 +235,37 @@ public class SearchDialog extends Dialog implements OnItemClickListener, OnItemS
                RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
        
        mVoiceAppSearchIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        
        mLocationManager =
                (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
        
        if (mLocationManager != null) {
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
    
            String providerName = mLocationManager.getBestProvider(criteria, true);
    
            if (providerName != null) {
                mLocationProvider = mLocationManager.getProvider(providerName);
            }
            
            // Just a dumb listener that doesn't do anything - requesting location updates here
            // is only intended to give location-based suggestion providers the best chance
            // of getting a good fresh location.
            mDummyLocationListener = new LocationListener() {
                public void onLocationChanged(Location location) {                    
                }

                public void onProviderDisabled(String provider) {
                }

                public void onProviderEnabled(String provider) {
                }

                public void onStatusChanged(String provider, int status, Bundle extras) {
                }
            };
        }
    }

    /**
@@ -363,6 +408,8 @@ public class SearchDialog extends Dialog implements OnItemClickListener, OnItemS
        // receive broadcasts
        getContext().registerReceiver(mBroadcastReceiver, mCloseDialogsFilter);
        getContext().registerReceiver(mBroadcastReceiver, mPackageFilter);
        
        startLocationUpdates();
    }

    /**
@@ -375,6 +422,8 @@ public class SearchDialog extends Dialog implements OnItemClickListener, OnItemS
    public void onStop() {
        super.onStop();
        
        stopLocationUpdates();
        
        // TODO: Removing the listeners means that they never get called, since 
        // Dialog.dismissDialog() calls onStop() before sendDismissMessage().
        setOnCancelListener(null);
@@ -398,6 +447,25 @@ public class SearchDialog extends Dialog implements OnItemClickListener, OnItemS
        mPreviousComponents = null;
    }
    
    /**
     * Asks the LocationManager for location updates so that it goes and gets a fresh location
     * if needed.
     */
    private void startLocationUpdates() {
        if (mLocationManager != null && mLocationProvider != null) {
            mLocationManager.requestLocationUpdates(mLocationProvider.getName(),
                    0, 0, mDummyLocationListener, getContext().getMainLooper());
        }

    }
    
    /**
     * Makes sure to stop listening for location updates to save battery.
     */
    private void stopLocationUpdates() {
        mLocationManager.removeUpdates(mDummyLocationListener);
    }
    
    /**
     * Sets the search dialog to the 'working' state, which shows a working spinner in the
     * right hand size of the text field.