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