Blame view

app/src/main/java/com/dinhcv/lifelogpedometer/activity/ActivityBase.java 3.1 KB
7f095a929   chudinhbka@gmail.com   Create GIT Project
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
  package com.dinhcv.lifelogpedometer.activity;
  
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.app.ProgressDialog;
  import android.content.Context;
  import android.content.Intent;
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.view.MotionEvent;
  import android.view.View;
  import android.view.inputmethod.InputMethodManager;
  import android.widget.EditText;
  
  import com.dinhcv.lifelogpedometer.R;
  
  
  abstract class ActivityBase extends AppCompatActivity {
  
      public void showAlerDialog(Context context, String message){
          AlertDialog.Builder alert = new AlertDialog.Builder(context);
  
          alert.setTitle(R.string.error_title);
          alert.setMessage(message);
          alert.setPositiveButton(R.string.ok, null);
          alert.show();
      }
  
      @Override
      public boolean dispatchTouchEvent(MotionEvent ev) {
          View v = getCurrentFocus();
  
          if (v != null &&
                  (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_MOVE) &&
                  v instanceof EditText &&
                  !v.getClass().getName().startsWith("android.webkit.")) {
              int scrcoords[] = new int[2];
              v.getLocationOnScreen(scrcoords);
              float x = ev.getRawX() + v.getLeft() - scrcoords[0];
              float y = ev.getRawY() + v.getTop() - scrcoords[1];
  
              if (x < v.getLeft() || x > v.getRight() || y < v.getTop() || y > v.getBottom())
                  hideKeyboard(this);
          }
          return super.dispatchTouchEvent(ev);
      }
  
      public static void hideKeyboard(Activity activity) {
          if (activity != null && activity.getWindow() != null && activity.getWindow().getDecorView() != null) {
              InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
          }
      }
  
      private static ProgressDialog progressDialog;
      public static void showDialog(Context context){
          progressDialog = new ProgressDialog(context);
          progressDialog.setMessage(context.getResources().getString(R.string.waite_some_minute));
          progressDialog.setCancelable(false);
          progressDialog.show();
      }
  
      public static void hiddenDialog(){
          if (progressDialog != null && progressDialog.isShowing()) {
              progressDialog.dismiss();
          }
      }
  
      public void gotoActivity(Class c) {
          Intent intent = new Intent(this, c);
          startActivity(intent);
      }
  
      public void gotoActivity(Class c, Bundle b) {
          Intent intent = new Intent(this, c);
          intent.putExtras(b);
          startActivity(intent);
      }
  
      public void gotoActivityForResult(Class c, int requestCode) {
          Intent intent = new Intent(this, c);
          startActivityForResult(intent, requestCode);
      }
  
      public void gotoActivityAndFinish(Class c) {
          Intent intent = new Intent(this, c);
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
          startActivity(intent);
          finish();
      }
  
  }