• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.incallui.calllocation.impl;
18 
19 import android.location.Location;
20 import android.net.TrafficStats;
21 import android.os.AsyncTask;
22 import com.android.dialer.common.LogUtil;
23 import com.android.incallui.calllocation.impl.LocationPresenter.LocationUi;
24 import java.lang.ref.WeakReference;
25 import org.json.JSONArray;
26 import org.json.JSONException;
27 import org.json.JSONObject;
28 
29 class ReverseGeocodeTask extends AsyncTask<Location, Void, String> {
30 
31   // Below are the JSON keys for the reverse geocode response.
32   // Source: https://developers.google.com/maps/documentation/geocoding/#ReverseGeocoding
33   private static final String JSON_KEY_RESULTS = "results";
34   private static final String JSON_KEY_ADDRESS = "formatted_address";
35   private static final String JSON_KEY_ADDRESS_COMPONENTS = "address_components";
36   private static final String JSON_KEY_PREMISE = "premise";
37   private static final String JSON_KEY_TYPES = "types";
38   private static final String JSON_KEY_LONG_NAME = "long_name";
39   private static final String JSON_KEY_SHORT_NAME = "short_name";
40 
41   private WeakReference<LocationUi> mUiReference;
42 
ReverseGeocodeTask(WeakReference<LocationUi> uiReference)43   public ReverseGeocodeTask(WeakReference<LocationUi> uiReference) {
44     mUiReference = uiReference;
45   }
46 
47   @Override
doInBackground(Location... locations)48   protected String doInBackground(Location... locations) {
49     LocationUi ui = mUiReference.get();
50     if (ui == null) {
51       return null;
52     }
53     if (locations == null || locations.length == 0) {
54       LogUtil.e("ReverseGeocodeTask.onLocationChanged", "No location provided");
55       return null;
56     }
57 
58     try {
59       String address = null;
60       String url = LocationUrlBuilder.getReverseGeocodeUrl(locations[0]);
61 
62       TrafficStats.setThreadStatsTag(TrafficStatsTags.REVERSE_GEOCODE_TAG);
63       String jsonResponse = HttpFetcher.getRequestAsString(ui.getContext(), url);
64 
65       // Parse the JSON response for the formatted address of the first result.
66       JSONObject responseObject = new JSONObject(jsonResponse);
67       if (responseObject != null) {
68         JSONArray results = responseObject.optJSONArray(JSON_KEY_RESULTS);
69         if (results != null && results.length() > 0) {
70           JSONObject topResult = results.optJSONObject(0);
71           if (topResult != null) {
72             address = topResult.getString(JSON_KEY_ADDRESS);
73 
74             // Strip off the Premise component from the address, if present.
75             JSONArray components = topResult.optJSONArray(JSON_KEY_ADDRESS_COMPONENTS);
76             if (components != null) {
77               boolean stripped = false;
78               for (int i = 0; !stripped && i < components.length(); i++) {
79                 JSONObject component = components.optJSONObject(i);
80                 JSONArray types = component.optJSONArray(JSON_KEY_TYPES);
81                 if (types != null) {
82                   for (int j = 0; !stripped && j < types.length(); j++) {
83                     if (JSON_KEY_PREMISE.equals(types.getString(j))) {
84                       String premise = null;
85                       if (component.has(JSON_KEY_SHORT_NAME)
86                           && address.startsWith(component.getString(JSON_KEY_SHORT_NAME))) {
87                         premise = component.getString(JSON_KEY_SHORT_NAME);
88                       } else if (component.has(JSON_KEY_LONG_NAME)
89                           && address.startsWith(component.getString(JSON_KEY_LONG_NAME))) {
90                         premise = component.getString(JSON_KEY_SHORT_NAME);
91                       }
92                       if (premise != null) {
93                         int index = address.indexOf(',', premise.length());
94                         if (index > 0 && index < address.length()) {
95                           address = address.substring(index + 1).trim();
96                         }
97                         stripped = true;
98                         break;
99                       }
100                     }
101                   }
102                 }
103               }
104             }
105 
106             // Strip off the country, if its USA.  Note: unfortunately the country in the formatted
107             // address field doesn't match the country in the address component fields (USA != US)
108             // so we can't easily strip off the country for all cases, thus this hack.
109             if (address.endsWith(", USA")) {
110               address = address.substring(0, address.length() - 5);
111             }
112           }
113         }
114       }
115 
116       return address;
117     } catch (AuthException ex) {
118       LogUtil.e("ReverseGeocodeTask.onLocationChanged", "AuthException", ex);
119       return null;
120     } catch (JSONException ex) {
121       LogUtil.e("ReverseGeocodeTask.onLocationChanged", "JSONException", ex);
122       return null;
123     } catch (Exception ex) {
124       LogUtil.e("ReverseGeocodeTask.onLocationChanged", "Exception!!!", ex);
125       return null;
126     } finally {
127       TrafficStats.clearThreadStatsTag();
128     }
129   }
130 
131   @Override
onPostExecute(String address)132   protected void onPostExecute(String address) {
133     LocationUi ui = mUiReference.get();
134     if (ui == null) {
135       return;
136     }
137 
138     try {
139       ui.setAddress(address);
140     } catch (Exception ex) {
141       LogUtil.e("ReverseGeocodeTask.onPostExecute", "Exception!!!", ex);
142     }
143   }
144 }
145