Blame view

src/com/ectrip/cyt/shield_home/LockLayer.java 11.2 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
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
  package com.ectrip.cyt.shield_home;
  
  import android.app.Activity;
  import android.content.Context;
  import android.graphics.Color;
  import android.graphics.PixelFormat;
  import android.os.Handler;
  import android.os.Message;
  import android.view.Gravity;
  import android.view.View;
  import android.view.WindowManager;
  import android.view.WindowManager.LayoutParams;
  import android.widget.LinearLayout;
  import android.widget.TextView;
  
  import com.ectrip.trips.check.R;
  
  public class LockLayer {
      private static Activity mActivty;
      public WindowManager mWindowManager;
      private View mLockView;
      private LayoutParams mLockViewLayoutParams;
  
      public LockLayer(Activity act) {
          mActivty = act;
          init();
      }
  
      private void init(){
  //        mWindowManager = (WindowManager) mActivty.getSystemService(Context.WINDOW_SERVICE);
          mWindowManager = (WindowManager) mActivty.getSystemService("window");
          mLockViewLayoutParams = new LayoutParams();
          mLockViewLayoutParams.width = LayoutParams.MATCH_PARENT;
          mLockViewLayoutParams.height = LayoutParams.MATCH_PARENT;
          //实现关键
          mLockViewLayoutParams.type = LayoutParams.TYPE_SYSTEM_ALERT;
          //apktool value,这个值具体是哪个变量还请网友帮忙
          mLockViewLayoutParams.flags = 1280;
  //        mLockViewLayoutParams.flags =WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
  //        ad.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); 
  //		ad.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
      }
      public void lock() {
          if(mLockView!=null){
              mWindowManager.addView(mLockView, mLockViewLayoutParams);
          }
      }
      public void unlock() {
  //        if(mWindowManager!=null){
  //            mWindowManager.removeView(mLockView);  
  //        } 
          if (mLockView != null)
          {
              if (mLockView.getParent() != null)
              {
                  mWindowManager.removeView(mLockView);
              }
              mLockView = null;
          }
      }
      public void setLockView(View v){
          mLockView = v;
      }
  
      private MToast mCToast;
  
      public void setToast(Context context,CharSequence content, int duration){
          mCToast=MToast.makeText(context,content,duration);
          mCToast.show();
      }
  
      public static class MToast{
  
          public static MToast makeText(Context context, CharSequence text, int duration)
          {
              MToast result = new MToast(context);
  
              LinearLayout mLayout=new LinearLayout(context);
              TextView tv = new TextView(context);
              tv.setText(text);
              tv.setTextColor(Color.WHITE);
              tv.setGravity(Gravity.CENTER);
              mLayout.setBackgroundResource(R.drawable.toast_img);
  
              int w=context.getResources().getDisplayMetrics().widthPixels / 2;
              int h=context.getResources().getDisplayMetrics().widthPixels / 10;
              mLayout.addView(tv, w, h);
              result.mNextView = mLayout;
              result.mDuration = duration;
  
              return result;
          }
  
          public static final int LENGTH_SHORT = 0;
          public static final int LENGTH_LONG = 1;
          private int mDuration=LENGTH_SHORT;
          private int mGravity = Gravity.CENTER;
          private int mX, mY;
          private float mHorizontalMargin;
          private float mVerticalMargin;
          private View mView;
          private View mNextView;
  
          private final int SHOW=10;
          private final int HIDE=11;
          private Handler mHandler = new Handler(mActivty.getMainLooper()) {
  
              @Override
              public void handleMessage(Message msg) {
                  switch (msg.what) {
                      case SHOW:
                          handleShow();
                          break;
                      case HIDE:
                          handleHide();
                          break;
                      default:
                          break;
                  }
              }
          };
  
          private WindowManager mWM;
          private final WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();
  
  
          public MToast(Context context) {
              init(context);
          }
  
          /**
           * Set the view to show.
           * @see #getView
           */
          public void setView(View view) {
              mNextView = view;
          }
  
          /**
           * Return the view.
           * @see #setView
           */
          public View getView() {
              return mNextView;
          }
  
          /**
           * Set how long to show the view for.
           * @see #LENGTH_SHORT
           * @see #LENGTH_LONG
           */
          public void setDuration(int duration) {
              mDuration = duration;
          }
  
          /**
           * Return the duration.
           * @see #setDuration
           */
          public int getDuration() {
              return mDuration;
          }
  
          /**
           * Set the margins of the view.
           *
           * @param horizontalMargin The horizontal margin, in percentage of the
           *        container width, between the container's edges and the
           *        notification
           * @param verticalMargin The vertical margin, in percentage of the
           *        container height, between the container's edges and the
           *        notification
           */
          public void setMargin(float horizontalMargin, float verticalMargin) {
              mHorizontalMargin = horizontalMargin;
              mVerticalMargin = verticalMargin;
          }
  
          /**
           * Return the horizontal margin.
           */
          public float getHorizontalMargin() {
              return mHorizontalMargin;
          }
  
          /**
           * Return the vertical margin.
           */
          public float getVerticalMargin() {
              return mVerticalMargin;
          }
  
          /**
           * Set the location at which the notification should appear on the screen.
           * @see android.view.Gravity
           * @see #getGravity
           */
          public void setGravity(int gravity, int xOffset, int yOffset) {
              mGravity = gravity;
              mX = xOffset;
              mY = yOffset;
          }
  
          /**
           * Get the location at which the notification should appear on the screen.
           * @see android.view.Gravity
           * @see #getGravity
           */
          public int getGravity() {
              return mGravity;
          }
  
          /**
           * Return the X offset in pixels to apply to the gravity's location.
           */
          public int getXOffset() {
              return mX;
          }
  
          /**
           * Return the Y offset in pixels to apply to the gravity's location.
           */
          public int getYOffset() {
              return mY;
          }
  
          /**
           * schedule handleShow into the right thread
           */
          public void show() {
  //            mHandler.post(mShow);
              mHandler.sendEmptyMessage(SHOW);
  
              if(mDuration==LENGTH_SHORT)
              {
                  new Thread(new Runnable(){
                      public void run(){
                          try {
                              Thread.sleep(2000);
                          } catch (InterruptedException e) {
                              e.printStackTrace();
                          }
                          mHandler.sendEmptyMessage(HIDE);
                      }
                  }).start();
  //                 mHandler.postDelayed(mHide, 2000);
              }else if(mDuration==LENGTH_LONG){
                  new Thread(new Runnable(){
                      public void run(){
                          try {
                              Thread.sleep(3500);
                          } catch (InterruptedException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
                          mHandler.sendEmptyMessage(HIDE);
                      }
                  }).start();
  //            	mHandler.postDelayed(mHide, 3500);
              }else{
                  new Thread(new Runnable(){
                      public void run(){
                          try {
                              Thread.sleep(mDuration);
                          } catch (InterruptedException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
                          mHandler.sendEmptyMessage(HIDE);
                      }
                  }).start();
  //            	mHandler.postDelayed(mHide,mDuration);
              }
          }
  
          /**
           * schedule handleHide into the right thread
           */
          public void hide() {
  //            mHandler.post(mHide);
          }
  
  //        private final Runnable mShow = new Runnable() {
  //            public void run() {
  //                handleShow();
  //            }
  //        };
  //     
  //        private final Runnable mHide = new Runnable() {
  //            public void run() {
  //                handleHide();
  //            }
  //        };
  
          private void init(Context context)
          {
              final WindowManager.LayoutParams params = mParams;
              params.height = WindowManager.LayoutParams.WRAP_CONTENT;
              params.width = WindowManager.LayoutParams.WRAP_CONTENT;
              params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                      | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                      | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
              params.format = PixelFormat.TRANSLUCENT;
              params.windowAnimations = android.R.style.Animation_Toast;
              params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
              params.setTitle("Toast");
  
              mWM = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
          }
  
  
          private void handleShow() {
  
              if (mView != mNextView) {
                  // remove the old view if necessary
                  handleHide();
                  mView = mNextView;
  //                mWM = WindowManagerImpl.getDefault();
                  final int gravity = mGravity;
                  mParams.gravity = gravity;
                  if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.FILL_HORIZONTAL)
                  {
                      mParams.horizontalWeight = 1.0f;
                  }
                  if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.FILL_VERTICAL)
                  {
                      mParams.verticalWeight = 1.0f;
                  }
                  mParams.x = mX;
                  mParams.y = mY;
                  mParams.verticalMargin = mVerticalMargin;
                  mParams.horizontalMargin = mHorizontalMargin;
                  if (mView.getParent() != null)
                  {
                      mWM.removeView(mView);
                  }
                  try {
                      if(mView!=null){
                          mWM.addView(mView, mParams);
                      }
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          }
  
          private void handleHide()
          {
              if (mView != null)
              {
                  if (mView.getParent() != null)
                  {
                      mWM.removeView(mView);
                  }
                  mView = null;
              }
          }
      }
  }