summaryrefslogtreecommitdiffstats
path: root/java/com/android/incallui/calllocation/impl/LocationPresenter.java
blob: 155d9fdfd4f167f47e3d31549bfebbfd770e6869 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.incallui.calllocation.impl;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.AsyncTask;
import com.android.dialer.common.LogUtil;
import com.android.dialer.logging.Logger;
import com.android.dialer.logging.nano.DialerImpression;
import com.android.incallui.baseui.Presenter;
import com.android.incallui.baseui.Ui;
import com.google.android.gms.location.LocationListener;
import java.lang.ref.WeakReference;
import java.util.Objects;

/**
 * Presenter for the {@code LocationFragment}.
 *
 * <p>Performs lookup for the address and map image to show.
 */
public class LocationPresenter extends Presenter<LocationPresenter.LocationUi>
    implements LocationListener {

  private Location mLastLocation;
  private AsyncTask mDownloadMapTask;
  private AsyncTask mReverseGeocodeTask;

  LocationPresenter() {}

  @Override
  public void onUiReady(LocationUi ui) {
    LogUtil.i("LocationPresenter.onUiReady", "");
    super.onUiReady(ui);
    updateLocation(mLastLocation, true);
  }

  @Override
  public void onUiUnready(LocationUi ui) {
    LogUtil.i("LocationPresenter.onUiUnready", "");
    super.onUiUnready(ui);

    if (mDownloadMapTask != null) {
      mDownloadMapTask.cancel(true);
    }
    if (mReverseGeocodeTask != null) {
      mReverseGeocodeTask.cancel(true);
    }
  }

  @Override
  public void onLocationChanged(Location location) {
    LogUtil.i("LocationPresenter.onLocationChanged", "");
    updateLocation(location, false);
  }

  private void updateLocation(Location location, boolean forceUpdate) {
    LogUtil.i("LocationPresenter.updateLocation", "location: " + location);
    if (forceUpdate || !Objects.equals(mLastLocation, location)) {
      mLastLocation = location;
      int status = LocationHelper.checkLocation(location);
      LocationUi ui = getUi();
      if (status == LocationHelper.LOCATION_STATUS_OK) {
        mDownloadMapTask = new DownloadMapImageTask(new WeakReference<>(ui)).execute(location);
        mReverseGeocodeTask = new ReverseGeocodeTask(new WeakReference<>(ui)).execute(location);
        if (ui != null) {
          ui.setLocation(location);
        } else {
          LogUtil.i("LocationPresenter.updateLocation", "no Ui");
        }
      } else if (status != LocationHelper.LOCATION_STATUS_NO_LOCATION) {
        // Log impression indicating why the location is not valid
        // Note: its possible for this to be called before the UI has been initialized.
        Context context = (ui != null) ? ui.getContext() : null;
        if (context != null) {
          if (status == LocationHelper.LOCATION_STATUS_STALE) {
            Logger.get(context).logImpression(DialerImpression.Type.EMERGENCY_STALE_LOCATION);
          } else if (status == LocationHelper.LOCATION_STATUS_INACCURATE) {
            Logger.get(context).logImpression(DialerImpression.Type.EMERGENCY_INACCURATE_LOCATION);
          }
        }
      }
    }
  }

  /** UI interface */
  public interface LocationUi extends Ui {

    void setAddress(String address);

    void setMap(Drawable mapImage);

    void setLocation(Location location);

    Context getContext();
  }
}