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

TrafficButton.java « widget « traffic « maplayer « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f704bb02c71bd5d59326ba9cd9fc5cd80d02e227 (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
113
114
package com.mapswithme.maps.maplayer.traffic.widget;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import com.mapswithme.maps.R;
import com.mapswithme.util.ThemeUtils;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.Utils;

public class TrafficButton
{
  @NonNull
  private final AnimationDrawable mLoadingAnim;
  @NonNull
  private final ImageButton mButton;

  public TrafficButton(@NonNull ImageButton trafficBtn)
  {
    mButton = trafficBtn;
    mLoadingAnim = getLoadingAnim(trafficBtn);

    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) trafficBtn.getLayoutParams();
    params.setMargins(0, UiUtils.getStatusBarHeight(trafficBtn.getContext()), 0, 0);
  }

  @NonNull
  private static AnimationDrawable getLoadingAnim(@NonNull ImageButton trafficBtn)
  {
    Context context = trafficBtn.getContext();
    Resources res = context.getResources();
    final int animResId = ThemeUtils.getResource(context, R.attr.trafficLoadingAnimation);
    return Utils.isLollipopOrLater()
           ? (AnimationDrawable) res.getDrawable(animResId, context.getTheme())
           : (AnimationDrawable) res.getDrawable(animResId);
  }

  void turnOff()
  {
    stopWaitingAnimation();
    mButton.setImageResource(ThemeUtils.isNightTheme() ? R.drawable.bg_subway_night_default
                                                       : R.drawable.bg_subway_light_default);
  }

  void turnOn()
  {
    stopWaitingAnimation();
    mButton.setImageResource(ThemeUtils.isNightTheme() ? R.drawable.ic_traffic_on_night
                                                       : R.drawable.ic_traffic_on);
  }

  void markAsOutdated()
  {
    stopWaitingAnimation();
    mButton.setImageResource(ThemeUtils.isNightTheme() ? R.drawable.ic_traffic_outdated_night
                                                       : R.drawable.ic_traffic_outdated);
  }

  void startWaitingAnimation()
  {
    mButton.setImageDrawable(mLoadingAnim);
    AnimationDrawable anim = (AnimationDrawable) mButton.getDrawable();
    anim.start();
  }

  private void stopWaitingAnimation()
  {
    Drawable drawable = mButton.getDrawable();
    if (drawable instanceof AnimationDrawable)
    {
      AnimationDrawable animation = (AnimationDrawable) drawable;
      animation.stop();
      mButton.setImageDrawable(null);
    }
  }

  public void setOffset(int offsetX, int offsetY)
  {
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mButton.getLayoutParams();
    params.setMargins(offsetX, offsetY, 0, 0);
    mButton.requestLayout();
  }

  public void show()
  {
    UiUtils.show(mButton);
  }

  public void hide()
  {
    UiUtils.hide(mButton);
  }

  public void hideImmediately()
  {
    mButton.setVisibility(View.GONE);
  }

  public void showImmediately()
  {
    mButton.setVisibility(View.VISIBLE);
  }

  public void setOnclickListener(View.OnClickListener onclickListener)
  {
    mButton.setOnClickListener(onclickListener);
  }
}