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.graphics.drawable.Drawable; 20 import android.location.Location; 21 import android.net.TrafficStats; 22 import android.os.AsyncTask; 23 import com.android.dialer.common.LogUtil; 24 import com.android.dialer.constants.TrafficStatsTags; 25 import com.android.incallui.calllocation.impl.LocationPresenter.LocationUi; 26 import java.io.InputStream; 27 import java.lang.ref.WeakReference; 28 import java.net.URL; 29 30 class DownloadMapImageTask extends AsyncTask<Location, Void, Drawable> { 31 32 private static final String STATIC_MAP_SRC_NAME = "src"; 33 34 private final WeakReference<LocationUi> mUiReference; 35 36 public DownloadMapImageTask(WeakReference<LocationUi> uiReference) { 37 mUiReference = uiReference; 38 } 39 40 @Override 41 protected Drawable doInBackground(Location... locations) { 42 LocationUi ui = mUiReference.get(); 43 if (ui == null) { 44 return null; 45 } 46 if (locations == null || locations.length == 0) { 47 LogUtil.e("DownloadMapImageTask.doInBackground", "No location provided"); 48 return null; 49 } 50 51 try { 52 URL mapUrl = new URL(LocationUrlBuilder.getStaticMapUrl(ui.getContext(), locations[0])); 53 InputStream content = (InputStream) mapUrl.getContent(); 54 55 TrafficStats.setThreadStatsTag(TrafficStatsTags.DOWNLOAD_LOCATION_MAP_TAG); 56 return Drawable.createFromStream(content, STATIC_MAP_SRC_NAME); 57 } catch (Exception ex) { 58 LogUtil.e("DownloadMapImageTask.doInBackground", "Exception!!!", ex); 59 return null; 60 } finally { 61 TrafficStats.clearThreadStatsTag(); 62 } 63 } 64 65 @Override 66 protected void onPostExecute(Drawable mapImage) { 67 LocationUi ui = mUiReference.get(); 68 if (ui == null) { 69 return; 70 } 71 72 try { 73 ui.setMap(mapImage); 74 } catch (Exception ex) { 75 LogUtil.e("DownloadMapImageTask.onPostExecute", "Exception!!!", ex); 76 } 77 } 78 } 79