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