LocationManager.java revision c343fa06491ee5677b735920032d92e055c16366
1/* 2 * Copyright (C) 2011 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.camera; 18 19import android.content.Context; 20import android.location.Location; 21import android.location.LocationProvider; 22import android.os.Bundle; 23import android.util.Log; 24 25/** 26 * A class that handles everything about location. 27 */ 28public class LocationManager { 29 private static final String TAG = "LocationManager"; 30 31 private Context mContext; 32 private Listener mListener; 33 private android.location.LocationManager mLocationManager; 34 private boolean mRecordLocation; 35 36 LocationListener [] mLocationListeners = new LocationListener[] { 37 new LocationListener(android.location.LocationManager.GPS_PROVIDER), 38 new LocationListener(android.location.LocationManager.NETWORK_PROVIDER) 39 }; 40 41 public interface Listener { 42 public void showGpsOnScreenIndicator(boolean hasSignal); 43 public void hideGpsOnScreenIndicator(); 44 } 45 46 public LocationManager(Context context, Listener listener) { 47 mContext = context; 48 mListener = listener; 49 } 50 51 public void startReceivingLocationUpdates() { 52 mRecordLocation = true; 53 if (mLocationManager == null) { 54 mLocationManager = (android.location.LocationManager) 55 mContext.getSystemService(Context.LOCATION_SERVICE); 56 } 57 if (mLocationManager != null) { 58 try { 59 mLocationManager.requestLocationUpdates( 60 android.location.LocationManager.NETWORK_PROVIDER, 61 1000, 62 0F, 63 mLocationListeners[1]); 64 } catch (SecurityException ex) { 65 Log.i(TAG, "fail to request location update, ignore", ex); 66 } catch (IllegalArgumentException ex) { 67 Log.d(TAG, "provider does not exist " + ex.getMessage()); 68 } 69 try { 70 mLocationManager.requestLocationUpdates( 71 android.location.LocationManager.GPS_PROVIDER, 72 1000, 73 0F, 74 mLocationListeners[0]); 75 mListener.showGpsOnScreenIndicator(false); 76 } catch (SecurityException ex) { 77 Log.i(TAG, "fail to request location update, ignore", ex); 78 } catch (IllegalArgumentException ex) { 79 Log.d(TAG, "provider does not exist " + ex.getMessage()); 80 } 81 Log.d(TAG, "startReceivingLocationUpdates"); 82 } 83 } 84 85 public void stopReceivingLocationUpdates() { 86 mRecordLocation = false; 87 if (mLocationManager != null) { 88 for (int i = 0; i < mLocationListeners.length; i++) { 89 try { 90 mLocationManager.removeUpdates(mLocationListeners[i]); 91 } catch (Exception ex) { 92 Log.i(TAG, "fail to remove location listners, ignore", ex); 93 } 94 } 95 Log.d(TAG, "stopReceivingLocationUpdates"); 96 } 97 mListener.hideGpsOnScreenIndicator(); 98 } 99 100 public Location getCurrentLocation() { 101 // go in best to worst order 102 for (int i = 0; i < mLocationListeners.length; i++) { 103 Location l = mLocationListeners[i].current(); 104 if (l != null) return l; 105 } 106 Log.d(TAG, "No location received yet."); 107 return null; 108 } 109 110 private class LocationListener 111 implements android.location.LocationListener { 112 Location mLastLocation; 113 boolean mValid = false; 114 String mProvider; 115 116 public LocationListener(String provider) { 117 mProvider = provider; 118 mLastLocation = new Location(mProvider); 119 } 120 121 @Override 122 public void onLocationChanged(Location newLocation) { 123 if (newLocation.getLatitude() == 0.0 124 && newLocation.getLongitude() == 0.0) { 125 // Hack to filter out 0.0,0.0 locations 126 return; 127 } 128 // If GPS is available before start camera, we won't get status 129 // update so update GPS indicator when we receive data. 130 if (mRecordLocation && 131 android.location.LocationManager.GPS_PROVIDER.equals(mProvider)) { 132 mListener.showGpsOnScreenIndicator(true); 133 } 134 if (!mValid) { 135 Log.d(TAG, "Got first location."); 136 } 137 mLastLocation.set(newLocation); 138 mValid = true; 139 } 140 141 @Override 142 public void onProviderEnabled(String provider) { 143 } 144 145 @Override 146 public void onProviderDisabled(String provider) { 147 mValid = false; 148 } 149 150 @Override 151 public void onStatusChanged( 152 String provider, int status, Bundle extras) { 153 switch(status) { 154 case LocationProvider.OUT_OF_SERVICE: 155 case LocationProvider.TEMPORARILY_UNAVAILABLE: { 156 mValid = false; 157 if (mRecordLocation && 158 android.location.LocationManager.GPS_PROVIDER.equals(provider)) { 159 mListener.showGpsOnScreenIndicator(false); 160 } 161 break; 162 } 163 } 164 } 165 166 public Location current() { 167 return mValid ? mLastLocation : null; 168 } 169 } 170} 171