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