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

UiUtils.java « util « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: aba596b243cbcb87cb6d4dc8670e612866e1f1cd (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package com.mapswithme.util;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Surface;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.mapswithme.maps.MWMApplication;
import com.mapswithme.maps.R;
import com.nineoldandroids.animation.Animator;

import static com.mapswithme.util.Utils.checkNotNull;

public final class UiUtils
{
  public static void hide(View view)
  {
    view.setVisibility(View.GONE);
  }

  public static void hide(View... views)
  {
    for (final View v : views)
      v.setVisibility(View.GONE);
  }

  public static void show(View view)
  {
    view.setVisibility(View.VISIBLE);
  }

  public static void show(View... views)
  {
    for (final View v : views)
      v.setVisibility(View.VISIBLE);
  }

  public static void invisible(View view)
  {
    view.setVisibility(View.INVISIBLE);
  }

  public static void invisible(View... views)
  {
    for (final View v : views)
      v.setVisibility(View.INVISIBLE);
  }

  public static void showIf(boolean condition, View view)
  {
    view.setVisibility(condition ? View.VISIBLE : View.GONE);
  }

  public static void showIf(boolean condition, View... views)
  {
    if (condition)
      show(views);
    else
      hide(views);
  }

  /*
    Views after alpha animations with 'setFillAfter' on 2.3 can't become GONE, until clearAnimationAfterAlpha is called.
   */
  public static void clearAnimationAfterAlpha(View... views)
  {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
      for (final View view : views)
        view.clearAnimation();
  }

  public static Drawable drawCircle(int color, int sizeResId, Resources res)
  {
    final int size = res.getDimensionPixelSize(sizeResId);
    final Bitmap bmp = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);

    final Paint paint = new Paint();
    paint.setColor(color);
    paint.setAntiAlias(true);

    final Canvas c = new Canvas(bmp);
    final float radius = size / 2.0f;
    c.drawCircle(radius, radius, radius, paint);

    return new BitmapDrawable(res, bmp);
  }

  public static class SimpleAnimationListener implements AnimationListener
  {
    @Override
    public void onAnimationStart(Animation animation)
    {}

    @Override
    public void onAnimationEnd(Animation animation)
    {}

    @Override
    public void onAnimationRepeat(Animation animation)
    {}
  }

  public static class SimpleNineoldAnimationListener implements Animator.AnimatorListener
  {
    @Override
    public void onAnimationStart(Animator animation) {}

    @Override
    public void onAnimationEnd(Animator animation) {}

    @Override
    public void onAnimationCancel(Animator animation) {}

    @Override
    public void onAnimationRepeat(Animator animation) {}
  }

  public static TextView findViewSetText(View root, int textViewId, CharSequence text)
  {
    checkNotNull(root);

    final TextView tv = (TextView) root.findViewById(textViewId);
    tv.setText(text);
    return tv;
  }

  public static ImageView findImageViewSetDrawable(View root, int imageViewId, Drawable drawable)
  {
    checkNotNull(root);

    final ImageView iv = (ImageView) root.findViewById(imageViewId);
    iv.setImageDrawable(drawable);
    return iv;
  }

  public static View findViewSetOnClickListener(View root, int viewId, OnClickListener listener)
  {
    checkNotNull(root);

    final View v = root.findViewById(viewId);
    v.setOnClickListener(listener);
    return v;
  }

  public static TextView setTextAndShow(TextView tv, CharSequence text)
  {
    checkNotNull(tv);

    tv.setText(text);
    show(tv);

    return tv;
  }

  public static TextView clearTextAndHide(TextView tv)
  {
    checkNotNull(tv);

    tv.setText(null);
    hide(tv);

    return tv;
  }

  public static void setTextAndHideIfEmpty(TextView tv, CharSequence text)
  {
    checkNotNull(tv);

    tv.setText(text);
    showIf(!TextUtils.isEmpty(text), tv);
  }

  public static void openAppInMarket(Activity activity, String marketUrl)
  {
    try
    {
      activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(marketUrl)));
    } catch (final Exception e)
    {
      e.printStackTrace();
    }
  }

  public static void showFacebookPage(Activity activity)
  {
    try
    {
      // Exception is thrown if we don't have installed Facebook application.
      activity.getPackageManager().getPackageInfo(Constants.Package.FB_PACKAGE, 0);

      activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(Constants.Url.FB_MAPSME_COMMUNITY_NATIVE)));
    } catch (final Exception e)
    {
      activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(Constants.Url.FB_MAPSME_COMMUNITY_HTTP)));
    }
  }

  public static void showTwitterPage(Activity activity)
  {
    Intent intent;
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse(Constants.Url.TWITTER_MAPSME_HTTP));
    activity.startActivity(intent);
  }

  public static String getDisplayDensityString()
  {
    final DisplayMetrics metrics = new DisplayMetrics();
    ((WindowManager) MWMApplication.get().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
    switch (metrics.densityDpi)
    {
    case DisplayMetrics.DENSITY_LOW:
      return "ldpi";
    case DisplayMetrics.DENSITY_MEDIUM:
      return "mdpi";
    case DisplayMetrics.DENSITY_HIGH:
      return "hdpi";
    case DisplayMetrics.DENSITY_XHIGH:
      return "xhdpi";
    case DisplayMetrics.DENSITY_XXHIGH:
      return "xxhdpi";
    case DisplayMetrics.DENSITY_XXXHIGH:
      return "xxxhdpi";
    }

    return "hdpi";
  }

  public static void checkConnectionAndShowAlert(final Activity activity, final String message)
  {
    if (!ConnectionState.isConnected())
    {
      activity.runOnUiThread(new Runnable()
      {
        @Override
        public void run()
        {
          new AlertDialog.Builder(activity)
              .setCancelable(false)
              .setMessage(message)
              .setPositiveButton(activity.getString(R.string.connection_settings), new DialogInterface.OnClickListener()
              {
                @Override
                public void onClick(DialogInterface dlg, int which)
                {
                  try
                  {
                    activity.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
                  } catch (final Exception ex)
                  {
                    ex.printStackTrace();
                  }

                  dlg.dismiss();
                }
              })
              .setNegativeButton(activity.getString(R.string.close), new DialogInterface.OnClickListener()
              {
                @Override
                public void onClick(DialogInterface dlg, int which)
                {
                  dlg.dismiss();
                }
              })
              .create()
              .show();
        }
      });
    }
  }

  public static void showHomeUpButton(Toolbar toolbar)
  {
    toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
  }

  public static AlertDialog buildAlertDialog(Activity activity, int titleId)
  {
    return new AlertDialog.Builder(activity)
        .setCancelable(false)
        .setMessage(titleId)
        .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener()
        {
          @Override
          public void onClick(DialogInterface dlg, int which) { dlg.dismiss(); }
        })
        .create();
  }

  public static void showAlertDialog(Activity activity, int titleId)
  {
    buildAlertDialog(activity, titleId).show();
  }


  public static AlertDialog buildAlertDialog(Activity activity, String title)
  {
    return new AlertDialog.Builder(activity)
        .setCancelable(false)
        .setMessage(title)
        .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener()
        {
          @Override
          public void onClick(DialogInterface dlg, int which) { dlg.dismiss(); }
        })
        .create();
  }

  public static void showAlertDialog(Activity activity, String title)
  {
    buildAlertDialog(activity, title).show();
  }

  public static String deviceOrientationAsString(Activity activity)
  {
    String rotation = activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT ? "|" : "-";
    switch (activity.getWindowManager().getDefaultDisplay().getRotation())
    {
    case Surface.ROTATION_0:
      rotation += "0";
      break;
    case Surface.ROTATION_90:
      rotation += "90";
      break;
    case Surface.ROTATION_180:
      rotation += "180";
      break;
    case Surface.ROTATION_270:
      rotation += "270";
      break;
    }
    return rotation;
  }

  public static boolean isSmallTablet()
  {
    return MWMApplication.get().getResources().getBoolean(R.bool.isSmallTablet);
  }

  public static boolean isBigTablet()
  {
    return MWMApplication.get().getResources().getBoolean(R.bool.isBigTablet);
  }

  /**
   * View's default getHitRect() had a bug and would not apply transforms properly.
   * More details : http://stackoverflow.com/questions/17750116/strange-view-gethitrect-behaviour
   *
   * @param v    view
   * @param rect rect
   */
  public static void getHitRect(View v, Rect rect)
  {
    rect.left = (int) com.nineoldandroids.view.ViewHelper.getX(v);
    rect.top = (int) com.nineoldandroids.view.ViewHelper.getY(v);
    rect.right = rect.left + v.getWidth();
    rect.bottom = rect.top + v.getHeight();
  }

  /**
   * Tests, whether views intercects each other in parent coordinates.
   *
   * @param firstView  base view
   * @param secondView covering view
   * @return intersects or not
   */
  public static boolean areViewsIntersecting(View firstView, View secondView)
  {
    if (firstView.getVisibility() == View.GONE)
      return false;

    final Rect baseRect = new Rect();
    final Rect testRect = new Rect();
    UiUtils.getHitRect(firstView, baseRect);
    UiUtils.getHitRect(secondView, testRect);

    return baseRect.intersect(testRect);
  }

  public static void appearSlidingDown(final View view, @Nullable final Runnable completionListener)
  {
    if (view.getVisibility() == View.VISIBLE)
    {
      if (completionListener != null)
        completionListener.run();
      return;
    }

    view.setVisibility(View.VISIBLE);

    Animation a = AnimationUtils.loadAnimation(view.getContext(), R.anim.slide_appear_down);
    if (completionListener != null)
      a.setAnimationListener(new UiUtils.SimpleAnimationListener()
      {
        @Override
        public void onAnimationEnd(Animation animation)
        {
          completionListener.run();
        }
      });

    view.startAnimation(a);
  }

  public static void disappearSlidingUp(final View view, @Nullable final Runnable completionListener)
  {
    if (view.getVisibility() != View.VISIBLE)
    {
      if (completionListener != null)
        completionListener.run();
      return;
    }

    Animation a = AnimationUtils.loadAnimation(view.getContext(), R.anim.slide_disappear_up);
    a.setAnimationListener(new UiUtils.SimpleAnimationListener()
    {
      @Override
      public void onAnimationEnd(Animation animation)
      {
        view.setVisibility(View.GONE);
        view.clearAnimation();

        if (completionListener != null)
          completionListener.run();
      }
    });

    view.startAnimation(a);
  }

  public static void exchangeViewsAnimatedDown(final View toHide, final View toShow, @Nullable final Runnable completionListener)
  {
    disappearSlidingUp(toHide, new Runnable()
    {
      @Override
      public void run()
      {
        appearSlidingDown(toShow, completionListener);
      }
    });
  }

  // utility class
  private UiUtils()
  {}
}