1 /*
2  * Copyright (C) 2016 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.tv.util;
18 
19 import android.content.Context;
20 import android.location.Address;
21 import android.location.Geocoder;
22 import android.location.Location;
23 import android.location.LocationListener;
24 import android.location.LocationManager;
25 import android.os.Bundle;
26 import android.util.Log;
27 
28 
29 import java.io.IOException;
30 import java.util.List;
31 import java.util.Locale;
32 
33 /**
34  * A utility class to get the current location.
35  */
36 public class LocationUtils {
37     private static final String TAG = "LocationUtils";
38     private static final boolean DEBUG = false;
39 
40     private static Context sApplicationContext;
41     private static Address sAddress;
42     private static IOException sError;
43 
44     /**
45      * Checks the current location.
46      */
getCurrentAddress(Context context)47     public static synchronized Address getCurrentAddress(Context context) throws IOException,
48             SecurityException {
49         if (sAddress != null) {
50             return sAddress;
51         }
52         if (sError != null) {
53             throw sError;
54         }
55         if (sApplicationContext == null) {
56             sApplicationContext = context.getApplicationContext();
57         }
58         LocationUtilsHelper.startLocationUpdates();
59         return null;
60     }
61 
updateAddress(Location location)62     private static void updateAddress(Location location) {
63         if (DEBUG) Log.d(TAG, "Updating address with " + location);
64         if (location == null) {
65             return;
66         }
67         Geocoder geocoder = new Geocoder(sApplicationContext, Locale.getDefault());
68         try {
69             List<Address> addresses = geocoder.getFromLocation(
70                     location.getLatitude(), location.getLongitude(), 1);
71             if (addresses != null) {
72                 sAddress = addresses.get(0);
73                 if (DEBUG) Log.d(TAG, "Got " + sAddress);
74             } else {
75                 if (DEBUG) Log.d(TAG, "No address returned");
76             }
77             sError = null;
78         } catch (IOException e) {
79             Log.w(TAG, "Error in updating address", e);
80             sError = e;
81         }
82     }
83 
LocationUtils()84     private LocationUtils() { }
85 
86     private static class LocationUtilsHelper {
87         private static final LocationListener LOCATION_LISTENER = new LocationListener() {
88             @Override
89             public void onLocationChanged(Location location) {
90                 updateAddress(location);
91             }
92 
93             @Override
94             public void onStatusChanged(String provider, int status, Bundle extras) { }
95 
96             @Override
97             public void onProviderEnabled(String provider) { }
98 
99             @Override
100             public void onProviderDisabled(String provider) { }
101         };
102 
103         private static LocationManager sLocationManager;
104 
startLocationUpdates()105         public static void startLocationUpdates() {
106             if (sLocationManager == null) {
107                 sLocationManager = (LocationManager) sApplicationContext.getSystemService(
108                         Context.LOCATION_SERVICE);
109                 try {
110                     sLocationManager.requestLocationUpdates(
111                             LocationManager.NETWORK_PROVIDER, 1000, 10, LOCATION_LISTENER, null);
112                 } catch (SecurityException e) {
113                     // Enables requesting the location updates again.
114                     sLocationManager = null;
115                     throw e;
116                 }
117             }
118         }
119     }
120 }
121