实现权限最高AlertDialog

package com.xiaozhuo.locksim.utils;


import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;

import com.xiaozhuo.locksim.R;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

public class SimStatusDialog{
	private Context context;
	private  AlertDialog alertDialog;
	private static boolean isOpen= false;
	private TitleListener listener;
	private boolean isFive = false;
	private int tick = 0;
	private PopupWindow popupWindow;
	private OnDialogListener dialogListener;
	private ArrayList<String> nums;
	private OnBtnListener btnListener;
	private static String dialogMessage = "";

	public SimStatusDialog(Context context) {
		this.context = context;
		init();
		listener = new TitleListener();
		btnListener = new OnBtnListener();
		if(nums==null){
			nums= new ArrayList<>();
			nums.add("0");nums.add("1");nums.add("2");nums.add("3");
			nums.add("4");nums.add("5");nums.add("6");nums.add("7");
			nums.add("8");nums.add("9");nums.add("*");nums.add("#");
		}
	}

	private void init() {
		alertDialog = new AlertDialog.Builder(context)
				.setTitle("提示").create();
		/*.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Intent intent = new Intent(Intent.ACTION_REBOOT);
						intent.putExtra("nowait", 1);
						intent.putExtra("interval", 1);
						intent.putExtra("window", 0);
						context.sendBroadcast(intent);
					}
				})*/
		/*.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						closeDialog();
					}
				})*/
	}

	public  void  openDialog(String message){
		alertDialog.setMessage(message);
		alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
		alertDialog.setCancelable(false);
		alertDialog.setCanceledOnTouchOutside(false);
		try {
			java.lang.reflect.Field field = alertDialog.getClass().getSuperclass().getDeclaredField("mShowing");
			field.setAccessible(true);
			field.set(alertDialog, false);
		} catch (Exception e) {
			e.printStackTrace();
		}
		alertDialog.show();
		isOpen= true;
		onTitleLisener();
		alertDialog.getWindow().getDecorView().setSystemUiVisibility(
				View.SYSTEM_UI_FLAG_LAYOUT_STABLE
				//| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
				| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
				//| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
				| View.SYSTEM_UI_FLAG_FULLSCREEN
				| View.SYSTEM_UI_FLAG_IMMERSIVE
				| View.INVISIBLE);
	}

	public void closeDialog(){
		try {
			java.lang.reflect.Field field = alertDialog.getClass().getSuperclass().getDeclaredField("mShowing");
			field.setAccessible(true);
			field.set(alertDialog, true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(popupWindow!=null){
			if(popupWindow.isShowing()){
				popupWindow.dismiss();
				popupWindow=null;
			}
		}
		alertDialog.dismiss();
		isOpen=false;
	}

	public void closeThis() {
		try {
			java.lang.reflect.Field field = alertDialog.getClass().getSuperclass().getDeclaredField("mShowing");
			field.setAccessible(true);
			field.set(alertDialog, true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		alertDialog.dismiss();
	}

	public static boolean getSimDialogAilve(){
		return isOpen;
	}

	public void onTitleLisener(){
		if(alertDialog.isShowing()){
			try {
				Field alert = alertDialog.getClass().getDeclaredField("mAlert");
				alert.setAccessible(true);
				Object alertController = alert.get(alertDialog);
				Field mTitleView = alertController.getClass().getDeclaredField("mTitleView");
				mTitleView.setAccessible(true);
				Field mMessageView = alertController.getClass().getDeclaredField("mMessageView");
				mMessageView.setAccessible(true);
				TextView tvTitle = (TextView) mTitleView.get(alertController);
				TextView tvMessage = (TextView) mMessageView.get(alertController);
				//if((context.getResources().getString(R.string.use_sim)).equals(tvMessage.getText())){
				dialogMessage = (String) tvMessage.getText();
				tvTitle.setOnClickListener(listener);
				//}
			}catch (IllegalAccessException e) {
				e.printStackTrace();
			}catch (IllegalArgumentException e) {
				e.printStackTrace();
			}catch (NoSuchFieldException e) {
				e.printStackTrace();
			}  
		}
	}


	private class TitleListener implements View.OnClickListener{
		private Handler handler = new Handler();
		private Runnable runnable = new Runnable() {
			@Override
			public void run() {
				isFive= false;
				tick = 0;
			}
		};
		@Override
		public void onClick(View v) {
			if(isFive) {
				handler.removeCallbacks(runnable);
				isFive = false;
				openPassword();
				closeThis();
			}else {
				tick++;
				if(tick==4){
					isFive = true;
					tick = 0;
				}
				handler.postDelayed(runnable, 2000);
			}
		}
	}
	private StringBuffer strBuffer;
	@SuppressLint("NewApi")
	private void openPassword() {
		if(strBuffer!=null){
			strBuffer=null;
		}
		strBuffer=new StringBuffer();
		Collections.shuffle(nums);
		if(popupWindow==null){
			View view= LayoutInflater.from(context).inflate(R.layout.popup_window_layout, null);
			initButton(view);
			DisplayMetrics dm = context.getResources().getDisplayMetrics(); 
			float dpi = dm.densityDpi;
			if(dpi==160.0) {
				popupWindow = new PopupWindow(view,400, WindowManager.LayoutParams.MATCH_PARENT);
			}else if(dpi==240.0) {
				popupWindow = new PopupWindow(view,556, WindowManager.LayoutParams.MATCH_PARENT);
			}
			popupWindow.update();  
			//popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);  
			//popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);  
			popupWindow.setTouchable(true); 
			popupWindow.setOutsideTouchable(false); 
			popupWindow.setFocusable(true); 
			popupWindow.setWindowLayoutType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
			popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0); 
		}
	}
	private ArrayList<Button> btns;
	private Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12,cancel,delete;
	private EditText etPassword;
	private CharSequence pass;
	private void initButton(View view){
		if(btns!=null){
			btns = null;
		}
		btns = new ArrayList<>();
		etPassword = (EditText) view.findViewById(R.id.et_password);
		btn1 = (Button) view.findViewById(R.id.button1);
		btn2 = (Button) view.findViewById(R.id.button2);
		btn3 = (Button) view.findViewById(R.id.button3);
		btn4 = (Button) view.findViewById(R.id.button4);
		btn5 = (Button) view.findViewById(R.id.button5);
		btn6 = (Button) view.findViewById(R.id.button6);
		btn7 = (Button) view.findViewById(R.id.button7);
		btn8 = (Button) view.findViewById(R.id.button8);
		btn9 = (Button) view.findViewById(R.id.button9);
		btn10 = (Button) view.findViewById(R.id.button10);
		btn11 = (Button) view.findViewById(R.id.button11);
		btn12 = (Button) view.findViewById(R.id.button12);
		cancel = (Button) view.findViewById(R.id.bt_cancel);
		delete = (Button) view.findViewById(R.id.bt_delete);
		btns.add(btn1);btns.add(btn2);btns.add(btn3);btns.add(btn4);
		btns.add(btn5);btns.add(btn6);btns.add(btn7);btns.add(btn8);
		btns.add(btn9);btns.add(btn10);btns.add(btn11);btns.add(btn12);
		for (int i = 0; i < btns.size(); i++) {
			btns.get(i).setText(nums.get(i));
			btns.get(i).setOnClickListener(btnListener);
		}
		cancel.setOnClickListener(btnListener);
		delete.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				if(strBuffer.length()>=1){
					strBuffer.delete(0, strBuffer.length());
					updateSB();
				}
				return false;
			}
		});
		delete.setOnClickListener(btnListener);
		etPassword.setFocusable(false);
		etPassword.setSingleLine();
		etPassword.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				pass = s;
			}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}
			@Override
			public void afterTextChanged(Editable s) {
				if("".equals(pass.toString())){
					//do something
					context.startActivity(intent);
					dialogListener.setOnTimeTick(false);
					isOpen = false;
					popupWindow.dismiss();
					popupWindow = null;
				}
			}
		});

	}

	private class OnBtnListener implements OnClickListener{
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.button1:
				if(strBuffer.length()<=14){
					strBuffer.append(btn1.getText());
					updateSB();
				}
				break;
			case R.id.button2:
				if(strBuffer.length()<=14){
					strBuffer.append(btn2.getText());
					updateSB();
				}
				break;
			case R.id.button3:
				if(strBuffer.length()<=14){
					strBuffer.append(btn3.getText());
					updateSB();
				}
				break;
			case R.id.button4:
				if(strBuffer.length()<=14){
					strBuffer.append(btn4.getText());
					updateSB();
				}
				break;
			case R.id.button5:
				if(strBuffer.length()<=14){
					strBuffer.append(btn5.getText());
					updateSB();
				}
				break;
			case R.id.button6:
				if(strBuffer.length()<=14){
					strBuffer.append(btn6.getText());
					updateSB();
				}
				break;
			case R.id.button7:
				if(strBuffer.length()<=14){
					strBuffer.append(btn7.getText());
					updateSB();
				}
				break;
			case R.id.button8:
				if(strBuffer.length()<=14){
					strBuffer.append(btn8.getText());
					updateSB();
				}
				break;
			case R.id.button9:
				if(strBuffer.length()<=14){
					strBuffer.append(btn9.getText());
					updateSB();
				}
				break;
			case R.id.button10:
				if(strBuffer.length()<=14){
					strBuffer.append(btn10.getText());
					updateSB();
				}
				break;
			case R.id.button11:
				if(strBuffer.length()<=14){
					strBuffer.append(btn11.getText());
					updateSB();
				}
				break;
			case R.id.button12:
				if(strBuffer.length()<=14){
					strBuffer.append(btn12.getText());
					updateSB();
				}
				break;
			case R.id.bt_cancel:
				openDialog(context.getResources().getString(R.string.use_sim));
				popupWindow.dismiss();
				popupWindow=null;
				if(strBuffer!=null){
					strBuffer=null;
				}
				break;
			case R.id.bt_delete:
				if(strBuffer.length()>=1){
					strBuffer.deleteCharAt(strBuffer.length()-1);
					updateSB();
				}
				break;
			}
		}
	}
	
	public static String getDialogMessage() {
		return dialogMessage;
	}
	private void updateSB(){
		String password = strBuffer.toString();
		etPassword.setText(password);
	}
	public interface OnDialogListener {
		void setOnTimeTick(boolean isNeedTick);
	}

	public void setOnDialogListener(OnDialogListener dialogListener) {
		this.dialogListener = dialogListener; 
	}

	public void removeDialogListener() {
		this.dialogListener = null;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值