Blame view

src/com/ectrip/cyt/ui/BaseActivity.java 8.56 KB
3c2353cd   杜方   1、畅游通核销app源码提交;
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
  package com.ectrip.cyt.ui;
  
  import java.util.Timer;
  import java.util.TimerTask;
  import java.util.concurrent.atomic.AtomicBoolean;
  
  import android.annotation.SuppressLint;
  import android.app.Dialog;
  import android.app.Service;
  import android.content.Context;
  import android.content.pm.ActivityInfo;
  import android.os.Bundle;
  import android.os.Handler;
  import android.os.Message;
  import android.os.PowerManager;
  import android.os.PowerManager.WakeLock;
  import android.support.v4.app.FragmentActivity;
  import android.view.View;
  import android.widget.Toast;
  
  import com.ectrip.cyt.config.BindService;
  import com.ectrip.cyt.config.DevicTool;
  import com.ectrip.cyt.config.MyApp;
  import com.ectrip.cyt.constant.constant;
  import com.ectrip.cyt.shield_home.LockLayer;
  import com.ectrip.cyt.shield_home.LockLayer.MToast;
  import com.ectrip.cyt.utils.LogUtil;
  import com.ectrip.trips.check.R;
  
  /**
   * @author jigo activity父类
   */
  public abstract class BaseActivity extends FragmentActivity {
      public final String TAG = "BaseActivity";
      private boolean isTablet;
      public final int NETSUCESS = 88;
      public final int NETFAILRE = 89;
      public final int NETDIALOG = 90;
      public NetHandler netHandler;
      public AtomicBoolean isStop = new AtomicBoolean(false);
      public PowerManager powerManager = null;
      public WakeLock wakeLock = null;
      private AtomicBoolean isPause = new AtomicBoolean(false);
      private Timer timer;
      public Dialog netDialog;
  
      @SuppressWarnings("deprecation")
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          isTablet = DevicTool.getInstance().isTablet(this);
          MyApp.getInstance().addActivity(this);
          if (netHandler == null) {
              netHandler = new NetHandler();
          }
          try {
              powerManager = (PowerManager) this
                      .getSystemService(Service.POWER_SERVICE);
              wakeLock = this.powerManager.newWakeLock(
                      PowerManager.SCREEN_DIM_WAKE_LOCK, "My Lock");
              // 是否需计算锁的数量
              wakeLock.setReferenceCounted(false);
          } catch (Exception e) {
              e.printStackTrace();
          } catch (Throwable e) {
              e.printStackTrace();
          }
  
          // 请求常亮,onResume()
          try {
              if (wakeLock != null) {
                  wakeLock.acquire();
              }
          } catch (Exception e) {
              e.printStackTrace();
          } catch (Throwable e) {
              e.printStackTrace();
          }
          /**
           * 设置为横屏
           */
          LogUtil.i(TAG, "isTablet == " + isTablet);
          if (isTablet) {
              if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
              }
          } else {
              if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
                  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
          }
      }
  
      @Override
      protected void onResume() {
          super.onResume();
          initNet();
      }
  
      protected void initNet() {
          timer = new Timer();
          isPause.set(false);
          TimerTask task = new TimerTask() {
              @Override
              public void run() {
                  if (!MyApp.getInstance().checkNetwork() && !isPause.get()) {
                      try {
                          netHandler.sendMessage(netHandler
                                  .obtainMessage(NETDIALOG));
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                  }
              }
          };
  
          timer.schedule(task, 4000);// 2秒后启动任务
  
          new Thread(new Runnable() {
              @Override
              public void run() {
                  while (!isStop.get()) {
                      if (MyApp.getInstance().checkNetwork()) {
                          isPause.set(true);
                          netHandler.sendMessage(netHandler
                                  .obtainMessage(NETSUCESS));
                      }
                      sleep(800);
                  }
              }
          }).start();
      }
  
      @SuppressLint("HandlerLeak")
      public class NetHandler extends Handler {
          @Override
          public void handleMessage(Message msg) {
              super.handleMessage(msg);
              switch (msg.what) {
                  case NETSUCESS:
                      if (netDialog != null && netDialog.isShowing()) {
                          try {
                              netDialog.dismiss();
                          } catch (Exception e) {
                              e.printStackTrace();
                          }
                          MToast(BaseActivity.this, getString(R.string.connect_net),
                                  MToast.LENGTH_SHORT);
                      }
                      break;
                  case NETDIALOG:
                      if (netDialog == null) {
                          netDialog = MyApp.getInstance().initNetWork(
                                  BaseActivity.this);
                      } else if (!netDialog.isShowing()) {
                          netDialog.show();
                      }
                      break;
                  default:
                      break;
              }
          }
      }
  
      private void sleep(int ms) {
  
          try {
              java.lang.Thread.sleep(ms);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public static Context getBasecontext() {
          return BaseActivity.getBasecontext();
      }
  
      /*
       * ...........................屏蔽home..................................
       */
  
      public LockLayer lockLayer;
      private View lock;
      public boolean isHandset;
  
      public void setContentView(int layoutResID) {
          isHandset = ((MyApp) getApplication()).isHandset;
          if (isHandset) {
              lock = View.inflate(this, layoutResID, null);
              lockLayer = new LockLayer(this);
              lockLayer.setLockView(lock);
              lockLayer.lock();
  
          } else {
              super.setContentView(layoutResID);
          }
      }
  
      public View findViewById(int id) {
          if (isHandset) {
              return lock.findViewById(id);
          } else {
              return super.findViewById(id);
          }
      }
  
      private long lastClick;
  
      public void MToast(Context context, CharSequence charSequence, int duration) {
          try {
              if (charSequence == null) {
                  return;
              }
              if (System.currentTimeMillis() - lastClick <= constant.mToastTime) {
                  return;
              }
              lastClick = System.currentTimeMillis();
              if (isHandset) {
                  lockLayer.setToast(context, charSequence, duration);
              } else {
                  Toast.makeText(context, charSequence, duration).show();
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      @Override
      public void onBackPressed() {
          if (timer != null) {
              timer.cancel();
          }
          isPause.set(true);
          super.onBackPressed();
72876d86   杜方   畅游通核销app: 1.增加550...
234
          LogUtil.i(TAG,getString(R.string.back_pressed));
3c2353cd   杜方   1、畅游通核销app源码提交;
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
          if (netDialog != null && netDialog.isShowing()) {
              try {
                  netDialog.dismiss();
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }
  
      @Override
      protected void onPause() {
          if (timer != null) {
              timer.cancel();
          }
          isPause.set(true);
          super.onPause();
          // 取消屏幕常亮,onPause()
          try {
              if (wakeLock != null) {
                  wakeLock.release();
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
          if (netDialog != null && netDialog.isShowing()) {
              try {
                  netDialog.dismiss();
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }
  
      /***************************** 2.0 *************************************/
  
      @Override
      protected void onDestroy() {
          if (timer != null) {
              timer.cancel();
          }
          if (((MyApp) getApplication()).isHandset) {
              lockLayer.unlock();
          }
          MyApp.getInstance().removeActivity(this);
          isPause.set(true);
          isStop.set(true);
          super.onDestroy();
          if (netDialog != null && netDialog.isShowing()) {
              try {
                  netDialog.dismiss();
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }
  }