Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DownloaderStatusIcon.java « downloader « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 72f0a8a9c69aba7f3af421f1d5baa03d6c5e2874 (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
package com.mapswithme.maps.downloader;

import android.support.annotation.AttrRes;
import android.support.annotation.DrawableRes;
import android.util.SparseIntArray;
import android.view.View;
import android.widget.ImageView;

import com.mapswithme.maps.R;
import com.mapswithme.maps.widget.WheelProgressView;
import com.mapswithme.util.ThemeUtils;
import com.mapswithme.util.UiUtils;

public class DownloaderStatusIcon
{
  private final View mFrame;
  protected final ImageView mIcon;
  private final WheelProgressView mProgress;

  private static final SparseIntArray sIconsCache = new SparseIntArray();

  public DownloaderStatusIcon(View frame)
  {
    mFrame = frame;
    mIcon = (ImageView) mFrame.findViewById(R.id.downloader_status);
    mProgress = (WheelProgressView) mFrame.findViewById(R.id.downloader_progress_wheel);
  }

  public DownloaderStatusIcon setOnIconClickListener(View.OnClickListener listener)
  {
    mIcon.setOnClickListener(listener);
    return this;
  }

  public DownloaderStatusIcon setOnCancelClickListener(View.OnClickListener listener)
  {
    mProgress.setOnClickListener(listener);
    return this;
  }

  protected @AttrRes int selectIcon(CountryItem country)
  {
    switch (country.status)
    {
    case CountryItem.STATUS_DONE:
      return R.attr.status_done;

    case CountryItem.STATUS_DOWNLOADABLE:
    case CountryItem.STATUS_PARTLY:
      return R.attr.status_downloadable;

    case CountryItem.STATUS_FAILED:
      return R.attr.status_failed;

    case CountryItem.STATUS_UPDATABLE:
      return R.attr.status_updatable;

    default:
      throw new IllegalArgumentException("Inappropriate item status: " + country.status);
    }
  }

  private @DrawableRes int resolveIcon(@AttrRes int iconAttr)
  {
    int res = sIconsCache.get(iconAttr);
    if (res == 0)
    {
      res = ThemeUtils.getResource(mFrame.getContext(), R.attr.downloaderTheme, iconAttr);
      sIconsCache.put(iconAttr, res);
    }

    return res;
  }

  protected void updateIcon(CountryItem country)
  {
    @AttrRes int iconAttr = selectIcon(country);
    @DrawableRes int icon = resolveIcon(iconAttr);

    mIcon.setImageResource(icon);
  }

  public void update(CountryItem country)
  {
    boolean pending = (country.status == CountryItem.STATUS_ENQUEUED);
    boolean inProgress = (country.status == CountryItem.STATUS_PROGRESS ||
                          country.status == CountryItem.STATUS_APPLYING || pending);

    UiUtils.showIf(inProgress, mProgress);
    UiUtils.showIf(!inProgress, mIcon);
    mProgress.setPending(pending);

    if (inProgress)
    {
      if (!pending)
        mProgress.setProgress(country.progress);
      return;
    }

    updateIcon(country);
  }

  public void show(boolean show)
  {
    UiUtils.showIf(show, mFrame);
  }

  public static void clearCache()
  {
    sIconsCache.clear();
  }
}