blob: 0bda5bfae0ebf0530e868e8eba646a38ea41d30a [file] [log] [blame]
Michael Kolb8872c232013-01-29 10:33:22 -08001/*
Kevin Gabayanffbc43c2013-12-09 11:41:50 -08002 * Copyright (C) 2013 The Android Open Source Project
Michael Kolb8872c232013-01-29 10:33:22 -08003 *
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
Kevin Gabayanffbc43c2013-12-09 11:41:50 -080017package com.android.camera.app;
Michael Kolb8872c232013-01-29 10:33:22 -080018
19import android.content.Context;
20import android.location.Location;
Michael Kolb8872c232013-01-29 10:33:22 -080021import android.os.Bundle;
Angus Kong2bca2102014-03-11 16:27:30 -070022
23import com.android.camera.debug.Log;
Michael Kolb8872c232013-01-29 10:33:22 -080024
25/**
Kevin Gabayanffbc43c2013-12-09 11:41:50 -080026 * A class that handles legacy (network, gps) location providers, in the event
27 * the fused location provider from Google Play Services is unavailable.
Michael Kolb8872c232013-01-29 10:33:22 -080028 */
Kevin Gabayanffbc43c2013-12-09 11:41:50 -080029public class LegacyLocationProvider implements LocationProvider {
Angus Kong2bca2102014-03-11 16:27:30 -070030 private static final Log.Tag TAG = new Log.Tag("LcyLocProvider");
Michael Kolb8872c232013-01-29 10:33:22 -080031
32 private Context mContext;
Michael Kolb8872c232013-01-29 10:33:22 -080033 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
Kevin Gabayanffbc43c2013-12-09 11:41:50 -080041 public LegacyLocationProvider(Context context) {
Michael Kolb8872c232013-01-29 10:33:22 -080042 mContext = context;
Michael Kolb8872c232013-01-29 10:33:22 -080043 }
44
Kevin Gabayanffbc43c2013-12-09 11:41:50 -080045 @Override
Michael Kolb8872c232013-01-29 10:33:22 -080046 public Location getCurrentLocation() {
47 if (!mRecordLocation) return null;
48
49 // go in best to worst order
50 for (int i = 0; i < mLocationListeners.length; i++) {
51 Location l = mLocationListeners[i].current();
52 if (l != null) return l;
53 }
54 Log.d(TAG, "No location received yet.");
55 return null;
56 }
57
58 public void recordLocation(boolean recordLocation) {
59 if (mRecordLocation != recordLocation) {
60 mRecordLocation = recordLocation;
61 if (recordLocation) {
62 startReceivingLocationUpdates();
63 } else {
64 stopReceivingLocationUpdates();
65 }
66 }
67 }
68
Kevin Gabayanffbc43c2013-12-09 11:41:50 -080069 @Override
70 public void disconnect() {
71 Log.d(TAG, "disconnect");
72 // The onPause() call to stopReceivingLocationUpdates is sufficient to unregister the
73 // Network/GPS listener.
74 }
75
Michael Kolb8872c232013-01-29 10:33:22 -080076 private void startReceivingLocationUpdates() {
77 if (mLocationManager == null) {
78 mLocationManager = (android.location.LocationManager)
79 mContext.getSystemService(Context.LOCATION_SERVICE);
80 }
81 if (mLocationManager != null) {
82 try {
83 mLocationManager.requestLocationUpdates(
84 android.location.LocationManager.NETWORK_PROVIDER,
85 1000,
86 0F,
87 mLocationListeners[1]);
88 } catch (SecurityException ex) {
89 Log.i(TAG, "fail to request location update, ignore", ex);
90 } catch (IllegalArgumentException ex) {
91 Log.d(TAG, "provider does not exist " + ex.getMessage());
92 }
93 try {
94 mLocationManager.requestLocationUpdates(
95 android.location.LocationManager.GPS_PROVIDER,
96 1000,
97 0F,
98 mLocationListeners[0]);
Michael Kolb8872c232013-01-29 10:33:22 -080099 } catch (SecurityException ex) {
100 Log.i(TAG, "fail to request location update, ignore", ex);
101 } catch (IllegalArgumentException ex) {
102 Log.d(TAG, "provider does not exist " + ex.getMessage());
103 }
104 Log.d(TAG, "startReceivingLocationUpdates");
105 }
106 }
107
108 private void stopReceivingLocationUpdates() {
109 if (mLocationManager != null) {
110 for (int i = 0; i < mLocationListeners.length; i++) {
111 try {
112 mLocationManager.removeUpdates(mLocationListeners[i]);
113 } catch (Exception ex) {
114 Log.i(TAG, "fail to remove location listners, ignore", ex);
115 }
116 }
117 Log.d(TAG, "stopReceivingLocationUpdates");
118 }
Michael Kolb8872c232013-01-29 10:33:22 -0800119 }
120
121 private class LocationListener
122 implements android.location.LocationListener {
123 Location mLastLocation;
124 boolean mValid = false;
125 String mProvider;
126
127 public LocationListener(String provider) {
128 mProvider = provider;
129 mLastLocation = new Location(mProvider);
130 }
131
132 @Override
133 public void onLocationChanged(Location newLocation) {
134 if (newLocation.getLatitude() == 0.0
135 && newLocation.getLongitude() == 0.0) {
136 // Hack to filter out 0.0,0.0 locations
137 return;
138 }
Michael Kolb8872c232013-01-29 10:33:22 -0800139 if (!mValid) {
140 Log.d(TAG, "Got first location.");
141 }
142 mLastLocation.set(newLocation);
143 mValid = true;
144 }
145
146 @Override
147 public void onProviderEnabled(String provider) {
148 }
149
150 @Override
151 public void onProviderDisabled(String provider) {
152 mValid = false;
153 }
154
155 @Override
156 public void onStatusChanged(
157 String provider, int status, Bundle extras) {
158 switch(status) {
Kevin Gabayanffbc43c2013-12-09 11:41:50 -0800159 case android.location.LocationProvider.OUT_OF_SERVICE:
160 case android.location.LocationProvider.TEMPORARILY_UNAVAILABLE: {
Michael Kolb8872c232013-01-29 10:33:22 -0800161 mValid = false;
Michael Kolb8872c232013-01-29 10:33:22 -0800162 break;
163 }
164 }
165 }
166
167 public Location current() {
168 return mValid ? mLastLocation : null;
169 }
170 }
Angus Kong2bca2102014-03-11 16:27:30 -0700171}