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

SearchWheel.java « routing « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1f550a8802cf5bc11defe34cb130564063ff4387 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package com.mapswithme.maps.routing;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;

import com.mapswithme.maps.MwmActivity;
import com.mapswithme.maps.R;
import com.mapswithme.maps.search.SearchEngine;
import com.mapswithme.util.Graphics;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.concurrency.UiThread;

class SearchWheel implements View.OnClickListener
{
  private final View mFrame;

  private final View mSearchLayout;
  private final ImageView mSearchButton;
  private final View mTouchInterceptor;

  private boolean mIsExpanded;
  private SearchOption mCurrentOption;

  private static final long CLOSE_DELAY_MILLIS = 5000L;
  private Runnable mCloseRunnable = new Runnable() {
    @Override
    public void run()
    {
      toggleSearchLayout();
    }
  };

  private enum SearchOption
  {
    FUEL(R.id.search_fuel, R.drawable.ic_routing_fuel_off, R.drawable.ic_routing_fuel_on, "fuel"),
    PARKING(R.id.search_parking, R.drawable.ic_routing_parking_off, R.drawable.ic_routing_parking_on, "parking"),
    FOOD(R.id.search_food, R.drawable.ic_routing_food_off, R.drawable.ic_routing_food_on, "food"),
    SHOP(R.id.search_shop, R.drawable.ic_routing_shop_off, R.drawable.ic_routing_shop_on, "shop"),
    ATM(R.id.search_atm, R.drawable.ic_routing_atm_off, R.drawable.ic_routing_atm_on, "atm");

    private int mResId;
    private int mDrawableOff;
    private int mDrawableOn;
    private String mSearchQuery;

    SearchOption(@IdRes int resId, @DrawableRes int drawableOff, @DrawableRes int drawableOn, String searchQuery)
    {
      this.mResId = resId;
      this.mDrawableOff = drawableOff;
      this.mDrawableOn = drawableOn;
      this.mSearchQuery = searchQuery;
    }

    @NonNull
    public static SearchOption FromResId(@IdRes int resId)
    {
      for (SearchOption searchOption : SearchOption.values())
      {
        if (searchOption.mResId == resId)
          return searchOption;
      }
      throw new IllegalArgumentException("No navigation search for id " + resId);
    }

    @Nullable
    public static SearchOption FromSearchQuery(@NonNull String query)
    {
      final String normalizedQuery = query.trim().toLowerCase();
      for (SearchOption searchOption : SearchOption.values())
      {
        if (searchOption.mSearchQuery.equals(normalizedQuery))
          return searchOption;
      }
      return null;
    }
  }

  SearchWheel(View frame)
  {
    mFrame = frame;

    mTouchInterceptor = mFrame.findViewById(R.id.touch_interceptor);
    mTouchInterceptor.setOnClickListener(this);
    mSearchButton = (ImageView) mFrame.findViewById(R.id.btn_search);
    mSearchButton.setOnClickListener(this);
    mSearchLayout = mFrame.findViewById(R.id.search_frame);
    if (UiUtils.isLandscape(mFrame.getContext()))
    {
      UiUtils.waitLayout(mSearchLayout, new ViewTreeObserver.OnGlobalLayoutListener()
      {
        @Override
        public void onGlobalLayout()
        {
          mSearchLayout.setPivotX(0);
          mSearchLayout.setPivotY(mSearchLayout.getMeasuredHeight() / 2);
        }
      });
    }
    for (SearchOption searchOption : SearchOption.values())
      mFrame.findViewById(searchOption.mResId).setOnClickListener(this);
    refreshSearchVisibility();
  }

  public void reset()
  {
    mIsExpanded = false;
    mCurrentOption = null;
    SearchEngine.cancelSearch();
  }

  public void onResume()
  {
    final String query = SearchEngine.getQuery();
    if (TextUtils.isEmpty(query))
    {
      resetSearchButtonImage();
      return;
    }

    mCurrentOption = SearchOption.FromSearchQuery(query);
    refreshSearchButtonImage();
  }

  private void toggleSearchLayout()
  {
    final int animRes;
    if (mIsExpanded)
    {
      animRes = R.animator.show_zoom_out_alpha;
    }
    else
    {
      animRes = R.animator.show_zoom_in_alpha;
      UiUtils.show(mSearchLayout);
    }
    mIsExpanded = !mIsExpanded;
    final Animator animator = AnimatorInflater.loadAnimator(mSearchLayout.getContext(), animRes);
    animator.setTarget(mSearchLayout);
    animator.start();
    UiUtils.visibleIf(mIsExpanded, mTouchInterceptor);
    animator.addListener(new UiUtils.SimpleAnimatorListener()
    {
      @Override
      public void onAnimationEnd(Animator animation)
      {
        refreshSearchVisibility();
      }
    });
  }

  private void refreshSearchVisibility()
  {
    for (SearchOption searchOption : SearchOption.values())
      UiUtils.visibleIf(mIsExpanded, mSearchLayout.findViewById(searchOption.mResId));

    UiUtils.visibleIf(mIsExpanded, mSearchLayout, mTouchInterceptor);

    if (mIsExpanded)
    {
      UiThread.cancelDelayedTasks(mCloseRunnable);
      UiThread.runLater(mCloseRunnable, CLOSE_DELAY_MILLIS);
    }
  }

  private void resetSearchButtonImage()
  {
    mSearchButton.setImageDrawable(Graphics.tint(mSearchButton.getContext(),
                                                 R.drawable.ic_routing_search_on));
  }

  private void refreshSearchButtonImage()
  {
    mSearchButton.setImageDrawable(Graphics.tint(mSearchButton.getContext(),
                                                 mCurrentOption == null ?
                                                 R.drawable.ic_routing_search_off :
                                                 mCurrentOption.mDrawableOff,
                                                 R.attr.colorAccent));
  }

  @Override
  public void onClick(View v)
  {
    switch (v.getId())
    {
    case R.id.btn_search:
      if (mCurrentOption != null || !TextUtils.isEmpty(SearchEngine.getQuery()))
      {
        SearchEngine.cancelSearch();
        mCurrentOption = null;
        mIsExpanded = false;
        resetSearchButtonImage();
        refreshSearchVisibility();
        return;
      }

      if (mIsExpanded)
      {
        showSearchInParent();
        return;
      }

      toggleSearchLayout();
      break;
    case R.id.touch_interceptor:
      toggleSearchLayout();
      break;
    case R.id.search_fuel:
    case R.id.search_parking:
    case R.id.search_food:
    case R.id.search_shop:
    case R.id.search_atm:
      startSearch(SearchOption.FromResId(v.getId()));
    }
  }

  private void showSearchInParent()
  {
    final MwmActivity parent = (MwmActivity) mFrame.getContext();
    parent.showSearch();
    mIsExpanded = false;
    refreshSearchVisibility();
  }

  private void startSearch(SearchOption searchOption)
  {
    mCurrentOption = searchOption;
    SearchEngine.searchInteractive(searchOption.mSearchQuery, System.nanoTime(), false /* isMapAndTable */);
    refreshSearchButtonImage();

    toggleSearchLayout();
  }
}