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.content.Context; 20 import android.location.Location; 21 import android.os.AsyncTask; 22 import com.android.dialer.common.LogUtil; 23 import com.android.dialer.logging.DialerImpression; 24 import com.android.dialer.logging.Logger; 25 import com.android.incallui.baseui.Presenter; 26 import com.android.incallui.baseui.Ui; 27 import com.google.android.gms.location.LocationListener; 28 import java.lang.ref.WeakReference; 29 import java.util.Objects; 30 31 /** 32 * Presenter for the {@code LocationFragment}. 33 * 34 * <p>Performs lookup for the address and map image to show. 35 */ 36 public class LocationPresenter extends Presenter<LocationPresenter.LocationUi> 37 implements LocationListener { 38 39 private Location lastLocation; 40 private AsyncTask reverseGeocodeTask; 41 LocationPresenter()42 LocationPresenter() {} 43 44 @Override onUiReady(LocationUi ui)45 public void onUiReady(LocationUi ui) { 46 LogUtil.i("LocationPresenter.onUiReady", ""); 47 super.onUiReady(ui); 48 updateLocation(lastLocation, true); 49 } 50 51 @Override onUiUnready(LocationUi ui)52 public void onUiUnready(LocationUi ui) { 53 LogUtil.i("LocationPresenter.onUiUnready", ""); 54 super.onUiUnready(ui); 55 56 if (reverseGeocodeTask != null) { 57 reverseGeocodeTask.cancel(true); 58 } 59 } 60 61 @Override onLocationChanged(Location location)62 public void onLocationChanged(Location location) { 63 LogUtil.i("LocationPresenter.onLocationChanged", ""); 64 updateLocation(location, false); 65 } 66 updateLocation(Location location, boolean forceUpdate)67 private void updateLocation(Location location, boolean forceUpdate) { 68 LogUtil.i("LocationPresenter.updateLocation", "location: " + location); 69 if (forceUpdate || !Objects.equals(lastLocation, location)) { 70 lastLocation = location; 71 int status = LocationHelper.checkLocation(location); 72 LocationUi ui = getUi(); 73 if (status == LocationHelper.LOCATION_STATUS_OK) { 74 reverseGeocodeTask = new ReverseGeocodeTask(new WeakReference<>(ui)).execute(location); 75 if (ui != null) { 76 ui.setLocation(location); 77 } else { 78 LogUtil.i("LocationPresenter.updateLocation", "no Ui"); 79 } 80 } else if (status != LocationHelper.LOCATION_STATUS_NO_LOCATION) { 81 // Log impression indicating why the location is not valid 82 // Note: its possible for this to be called before the UI has been initialized. 83 Context context = (ui != null) ? ui.getContext() : null; 84 if (context != null) { 85 if (status == LocationHelper.LOCATION_STATUS_STALE) { 86 Logger.get(context).logImpression(DialerImpression.Type.EMERGENCY_STALE_LOCATION); 87 } else if (status == LocationHelper.LOCATION_STATUS_INACCURATE) { 88 Logger.get(context).logImpression(DialerImpression.Type.EMERGENCY_INACCURATE_LOCATION); 89 } 90 } 91 } 92 } 93 } 94 95 /** UI interface */ 96 public interface LocationUi extends Ui { 97 setAddress(String address)98 void setAddress(String address); 99 setLocation(Location location)100 void setLocation(Location location); 101 getContext()102 Context getContext(); 103 } 104 } 105