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; } } } }