package com.hh.arome.services;

import android.accessibilityservice.AccessibilityService;
import android.content.Context;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.accessibility.AccessibilityEvent;
import android.widget.Toast;

import com.hh.arome.utlis.log.AppLogUtils;


/**
 * 扫码枪的 AccessibilityService
 */
public class QrcodeScannerService extends AccessibilityService {

    private static boolean isServiceCreated = false;


    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();

        isServiceCreated = true;

        initBarcodeScanner();
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {

    }

    @Override
    protected boolean onKeyEvent(KeyEvent event) {
        if(BarcodeScannerHelper.getInstance().dispatchKeyEvent(event)) {
            return true;
        }
        return super.onKeyEvent(event);
    }

    @Override
    public void onInterrupt() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        isServiceCreated = false;
    }

    public static boolean isServiceRunning(){
        return isServiceCreated;
    }

    /**
     * 初始化扫码
     */
    private void initBarcodeScanner() {
        BarcodeScannerHelper.getInstance().setScanSuccessListener(new BarcodeScannerHelper.OnScanSuccessListener() {
            @Override
            public void onScanSuccess(String barcode) {
                Toast.makeText(QrcodeScannerService.this.getApplicationContext(), "--->扫码内容：" + barcode, Toast.LENGTH_LONG).show();
                System.out.println("--->扫码内容：" + barcode);
            }
        });
    }

    /**
     * 判断当前应用的辅助功能在设置中是否打开
     * @param context
     * @return true辅助功能开 false辅助功能关
     */
    public static boolean isAccessibilitySettingsOn(Context context) {
        int accessibilityEnabled = 0;
        final String service = context.getPackageName() + "/" + QrcodeScannerService.class.getCanonicalName();
        System.out.println("--->isAccessibilitySettingsOn：" + service);

        AppLogUtils.e("--->isAccessibilitySettingsOn：" + service);

        try {
            // 获取setting里辅助功能的开启状态
            accessibilityEnabled = Settings.Secure.getInt(context.getApplicationContext().getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED);
        } catch (Settings.SettingNotFoundException e) {
        }
        TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
        if (accessibilityEnabled == 1) {
            // 获取辅助功能里所有开启的服务 包名列表
            String settingValue = Settings.Secure.getString(context.getApplicationContext().getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
            if (settingValue != null) {
                // 转换程集合
                mStringColonSplitter.setString(settingValue);
                while (mStringColonSplitter.hasNext()) {
                    String accessibilityService = mStringColonSplitter.next();
                    //判断当前包名是否在服务集合里
                    if (accessibilityService.equalsIgnoreCase(service)) {
                        AppLogUtils.e("--->isAccessibilitySettingsOn---true");
                        return true;
                    }
                }
            }
        }
        AppLogUtils.e("--->isAccessibilitySettingsOn---false");
        return false;
    }
}
