// 包路径
// const package_Activity = 'android.app.Activity'; 
// const package_Bundle = 'android.os.Bundle'; 
// const package_MotionEvent = 'android.view.MotionEvent'; 
// const package_KeyEvent = 'android.view.KeyEvent'; 


const SYSTEM_REASON = "reason";
//Home键
const SYSTEM_HOME_KEY = "homekey";
//最近使用的应用键
const SYSTEM_RECENT_APPS = "recentapps";

let receiver, Intent = false;
export default {
	startListen: function (needStopSystem = false) {//开始监听，初始化
		//参数needStopSystem默认不阻止系统响应，如需阻止调用startListen(true)
		/*特别提醒，全面屏手势Home和recent部分机型是监听不到的，自行测试，如遇卡死电脑控制台直接重新运行项目即可解决！*/
		let that = this;
		that.openListen();
		let main = plus.android.runtimeMainActivity();
		if (needStopSystem) {
			main.startLockTask()//阻止系统home建和近期任务键
			plus.key.addEventListener("backbutton", function () {
				// main.stopLockTask()//按返回键恢复
				// main.unregisterReceiver(receiver); //同时停止接收home和recent点击
			});
		} else {
			plus.globalEvent.addEventListener('resume', function (e) {
				that.openListen();//应用切换到前台时需要重新开启监听
				main.startLockTask()//阻止系统home建和近期任务键
			});
			plus.key.addEventListener("backbutton", function () {
				// main.unregisterReceiver(receiver); //同时停止监听home和recent
			});
		}
	},
	openListen: function () {//注册监听
		let that = this;
		try {
			let main = plus.android.runtimeMainActivity();
			Intent = plus.android.importClass('android.content.Intent');
			let IntentFilter = plus.android.importClass('android.content.IntentFilter');
			var filter = new IntentFilter();
			filter.addAction(Intent.ACTION_SCREEN_ON);
			filter.addAction(Intent.ACTION_SCREEN_OFF);
			filter.addAction(Intent.ACTION_USER_PRESENT);
			filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
			filter.addAction(Intent.GLOBAL_ACTION_BACK);
			receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
				onReceive: function (context, intent) { //实现onReceiver回调函数  
					let act = intent.getAction();
					if (act == Intent.ACTION_SCREEN_ON) {
						console.log('开屏')
					} else if (act == Intent.ACTION_SCREEN_OFF) {
						console.log('锁屏')
					} else if (act == Intent.ACTION_USER_PRESENT) {
						console.log('解锁')
					} else if (act == Intent.ACTION_CLOSE_SYSTEM_DIALOGS) {
						let systemReason = intent.getStringExtra(SYSTEM_REASON);
						// console.log("关闭原因", systemReason);
						if (systemReason != null) {
							if (systemReason == SYSTEM_HOME_KEY) {
								// System.out.println("按下HOME键");
								console.log('按下HOME键')
							} else if (systemReason == SYSTEM_RECENT_APPS) {
								// System.out.println("按下多任务键");
								console.log('按下多任务键')
							}
						}
					} else if (act == Intent.GLOBAL_ACTION_BACK) {
						console.log('返回')
					}
					// main.unregisterReceiver(receiver);
					/*在这里取消会出现第二次监听无效,可根据不同业务要求
					在不同时刻停止接收两个按键的广播，我的处理是在返回的时候停*/
					// context.unregisterReceiver(receiver);
				}
			});
			main.registerReceiver(receiver, filter); //注册监听  
		} catch (e) {
			console.error(e);
			toast('初始化错误');
		}
	},
	// 全屏函数
	fullScreen() {
		// Android全屏
		if (uni.getSystemInfoSync().platform === "android") {
			plus.android.invoke("setFullScreen", true);
		}

		// iOS全屏
		if (uni.getSystemInfoSync().platform === "ios") {
			// iOS无法通过API直接全屏，可以尝试隐藏所有导航栏
			plus.navigator.hideSystemBar();
		}
	}
}
function toast(content) {
	uni.showToast({
		title: content,
		icon: 'none'
	})
}