智能工厂.apk(点击下载) / LocationManager.java


package com.unarin.cordova.beacon;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.AdvertiseCallback;
import android.bluetooth.le.AdvertiseSettings;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Handler;
import android.os.RemoteException;
import android.util.Log;
import androidx.core.view.MotionEventCompat;
import androidx.core.view.inputmethod.EditorInfoCompat;
import com.nordnetab.chcp.main.js.PluginResultHelper;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.InvalidKeyException;
import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.BeaconTransmitter;
import org.altbeacon.beacon.BleNotAvailableException;
import org.altbeacon.beacon.Identifier;
import org.altbeacon.beacon.MonitorNotifier;
import org.altbeacon.beacon.RangeNotifier;
import org.altbeacon.beacon.Region;
import org.altbeacon.beacon.service.ArmaRssiFilter;
import org.altbeacon.beacon.service.RangedBeacon;
import org.altbeacon.beacon.service.RunningAverageRssiFilter;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

@TargetApi(16)
public class LocationManager extends CordovaPlugin implements BeaconConsumer {
    private static final int BUILD_VERSION_CODES_M = 23;
    private static int CDV_LOCATION_MANAGER_DOM_DELEGATE_TIMEOUT = 30;
    private static final boolean DEFAULT_ENABLE_ARMA_FILTER = false;
    private static final int DEFAULT_FOREGROUND_BETWEEN_SCAN_PERIOD = 0;
    private static final int DEFAULT_FOREGROUND_SCAN_PERIOD = 1100;
    private static final boolean DEFAULT_REQUEST_BT_PERMISSION = true;
    private static final int DEFAULT_SAMPLE_EXPIRATION_MILLISECOND = 20000;
    private static final String ENABLE_ARMA_FILTER_NAME = "com.unarin.cordova.beacon.android.altbeacon.EnableArmaFilter";
    private static final String FOREGROUND_BETWEEN_SCAN_PERIOD_NAME = "com.unarin.cordova.beacon.android.altbeacon.ForegroundBetweenScanPeriod";
    private static final String FOREGROUND_SCAN_PERIOD_NAME = "com.unarin.cordova.beacon.android.altbeacon.ForegroundScanPeriod";
    private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;
    private static final String REQUEST_BT_PERMISSION_NAME = "com.unarin.cordova.beacon.android.altbeacon.RequestBtPermission";
    private static final String SAMPLE_EXPIRATION_MILLISECOND = "com.unarin.cordova.beacon.android.altbeacon.SampleExpirationMilliseconds";
    public static final String TAG = "com.unarin.beacon";
    private IBeaconServiceNotifier beaconServiceNotifier;
    private BeaconTransmitter beaconTransmitter;
    private BluetoothAdapter bluetoothAdapter;
    private BroadcastReceiver broadcastReceiver;
    private boolean debugEnabled = true;
    private BeaconManager iBeaconManager;
    private BlockingQueue<Runnable> queue;
    private PausableThreadPoolExecutor threadPoolExecutor;

    private String nameOfProximity(double d) {
        return d < 0.0d ? "ProximityUnknown" : d < 0.5d ? "ProximityImmediate" : d <= 4.0d ? "ProximityNear" : "ProximityFar";
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private String nameOfRegionState(int i) {
        switch (i) {
            case 0:
                return "CLRegionStateOutside";
            case 1:
                return "CLRegionStateInside";
            default:
                return "ErrorUnknownCLRegionStateObjectReceived";
        }
    }

    @Override // org.apache.cordova.CordovaPlugin
    public void initialize(CordovaInterface cordovaInterface, CordovaWebView cordovaWebView) {
        super.initialize(cordovaInterface, cordovaWebView);
        Activity activity = cordovaInterface.getActivity();
        int integer = this.preferences.getInteger(FOREGROUND_BETWEEN_SCAN_PERIOD_NAME, 0);
        int integer2 = this.preferences.getInteger(FOREGROUND_SCAN_PERIOD_NAME, DEFAULT_FOREGROUND_SCAN_PERIOD);
        Log.i(TAG, "Determined config value FOREGROUND_SCAN_PERIOD: " + String.valueOf(integer2));
        this.iBeaconManager = BeaconManager.getInstanceForApplication(activity);
        this.iBeaconManager.setForegroundBetweenScanPeriod((long) integer);
        this.iBeaconManager.setForegroundScanPeriod((long) integer2);
        int integer3 = this.preferences.getInteger(SAMPLE_EXPIRATION_MILLISECOND, DEFAULT_SAMPLE_EXPIRATION_MILLISECOND);
        Log.i(TAG, "Determined config value SAMPLE_EXPIRATION_MILLISECOND: " + String.valueOf(integer3));
        if (this.preferences.getBoolean(ENABLE_ARMA_FILTER_NAME, false)) {
            BeaconManager beaconManager = this.iBeaconManager;
            BeaconManager.setRssiFilterImplClass(ArmaRssiFilter.class);
        } else {
            BeaconManager beaconManager2 = this.iBeaconManager;
            BeaconManager.setRssiFilterImplClass(RunningAverageRssiFilter.class);
            RunningAverageRssiFilter.setSampleExpirationMilliseconds((long) integer3);
        }
        RangedBeacon.setSampleExpirationMilliseconds((long) integer3);
        initBluetoothListener();
        initEventQueue();
        pauseEventPropagationToDom();
        initLocationManager();
        this.debugEnabled = true;
        if (Build.VERSION.SDK_INT >= 18) {
            initBluetoothAdapter();
        }
        if (this.preferences.getBoolean(REQUEST_BT_PERMISSION_NAME, true)) {
            tryToRequestMarshmallowLocationPermission();
        }
    }

    @Override // org.apache.cordova.CordovaPlugin
    public void onDestroy() {
        this.iBeaconManager.unbind(this);
        if (this.broadcastReceiver != null) {
            this.f0cordova.getActivity().unregisterReceiver(this.broadcastReceiver);
            this.broadcastReceiver = null;
        }
        super.onDestroy();
    }

    @Override // org.apache.cordova.CordovaPlugin
    public boolean execute(String str, JSONArray jSONArray, CallbackContext callbackContext) throws JSONException {
        if (str.equals("onDomDelegateReady")) {
            onDomDelegateReady(callbackContext);
            return true;
        } else if (str.equals("disableDebugNotifications")) {
            disableDebugNotifications(callbackContext);
            return true;
        } else if (str.equals("enableDebugNotifications")) {
            enableDebugNotifications(callbackContext);
            return true;
        } else if (str.equals("disableDebugLogs")) {
            disableDebugLogs(callbackContext);
            return true;
        } else if (str.equals("enableDebugLogs")) {
            enableDebugLogs(callbackContext);
            return true;
        } else if (str.equals("appendToDeviceLog")) {
            appendToDeviceLog(jSONArray.optString(0), callbackContext);
            return true;
        } else if (str.equals("startMonitoringForRegion")) {
            startMonitoringForRegion(jSONArray.optJSONObject(0), callbackContext);
            return true;
        } else if (str.equals("stopMonitoringForRegion")) {
            stopMonitoringForRegion(jSONArray.optJSONObject(0), callbackContext);
            return true;
        } else if (str.equals("startRangingBeaconsInRegion")) {
            startRangingBeaconsInRegion(jSONArray.optJSONObject(0), callbackContext);
            return true;
        } else if (str.equals("stopRangingBeaconsInRegion")) {
            stopRangingBeaconsInRegion(jSONArray.optJSONObject(0), callbackContext);
            return true;
        } else if (str.equals("isRangingAvailable")) {
            isRangingAvailable(callbackContext);
            return true;
        } else if (str.equals("getAuthorizationStatus")) {
            getAuthorizationStatus(callbackContext);
            return true;
        } else if (str.equals("requestWhenInUseAuthorization")) {
            requestWhenInUseAuthorization(callbackContext);
            return true;
        } else if (str.equals("requestAlwaysAuthorization")) {
            requestAlwaysAuthorization(callbackContext);
            return true;
        } else if (str.equals("getMonitoredRegions")) {
            getMonitoredRegions(callbackContext);
            return true;
        } else if (str.equals("getRangedRegions")) {
            getRangedRegions(callbackContext);
            return true;
        } else if (str.equals("requestStateForRegion")) {
            requestStateForRegion(jSONArray.optJSONObject(0), callbackContext);
            return true;
        } else if (str.equals("registerDelegateCallbackId")) {
            registerDelegateCallbackId(jSONArray.optJSONObject(0), callbackContext);
            return true;
        } else if (str.equals("isMonitoringAvailableForClass")) {
            isMonitoringAvailableForClass(jSONArray.optJSONObject(0), callbackContext);
            return true;
        } else if (str.equals("isAdvertisingAvailable")) {
            isAdvertisingAvailable(callbackContext);
            return true;
        } else if (str.equals("isAdvertising")) {
            isAdvertising(callbackContext);
            return true;
        } else if (str.equals("startAdvertising")) {
            startAdvertising(jSONArray, callbackContext);
            return true;
        } else if (str.equals("stopAdvertising")) {
            stopAdvertising(callbackContext);
            return true;
        } else if (str.equals("isBluetoothEnabled")) {
            isBluetoothEnabled(callbackContext);
            return true;
        } else if (str.equals("enableBluetooth")) {
            enableBluetooth(callbackContext);
            return true;
        } else if (!str.equals("disableBluetooth")) {
            return false;
        } else {
            disableBluetooth(callbackContext);
            return true;
        }
    }

    private void initLocationManager() {
        this.iBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
        this.iBeaconManager.bind(this);
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private BeaconTransmitter createOrGetBeaconTransmitter() {
        if (this.beaconTransmitter == null) {
            this.beaconTransmitter = new BeaconTransmitter(getApplicationContext(), new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
        }
        return this.beaconTransmitter;
    }

    @TargetApi(23)
    private void tryToRequestMarshmallowLocationPermission() {
        if (Build.VERSION.SDK_INT < 23) {
            Log.i(TAG, "tryToRequestMarshmallowLocationPermission() skipping because API code is below criteria: " + String.valueOf(Build.VERSION.SDK_INT));
            return;
        }
        final Activity activity = this.f0cordova.getActivity();
        Method checkSelfPermissionMethod = getCheckSelfPermissionMethod();
        if (checkSelfPermissionMethod == null) {
            Log.e(TAG, "Could not obtain the method Activity.checkSelfPermission method. Will not check for ACCESS_COARSE_LOCATION even though we seem to be on a supported version of Android.");
            return;
        }
        try {
            Integer num = (Integer) checkSelfPermissionMethod.invoke(activity, "android.permission.ACCESS_COARSE_LOCATION");
            Log.i(TAG, "Permission check result for ACCESS_COARSE_LOCATION: " + String.valueOf(num));
            if (num.intValue() == 0) {
                Log.i(TAG, "Permission for ACCESS_COARSE_LOCATION has already been granted.");
                return;
            }
            final Method requestPermissionsMethod = getRequestPermissionsMethod();
            if (requestPermissionsMethod == null) {
                Log.e(TAG, "Could not obtain the method Activity.requestPermissions. Will not ask for ACCESS_COARSE_LOCATION even though we seem to be on a supported version of Android.");
                return;
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setTitle("This app needs location access");
            builder.setMessage("Please grant location access so this app can detect beacons.");
            builder.setPositiveButton(17039370, (DialogInterface.OnClickListener) null);
            builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass1 */

                @SuppressLint({"NewApi"})
                public void onDismiss(DialogInterface dialogInterface) {
                    try {
                        requestPermissionsMethod.invoke(activity, new String[]{"android.permission.ACCESS_COARSE_LOCATION"}, 1);
                    } catch (IllegalAccessException e) {
                        Log.e(LocationManager.TAG, "IllegalAccessException while requesting permission for ACCESS_COARSE_LOCATION:", e);
                    } catch (InvocationTargetException e2) {
                        Log.e(LocationManager.TAG, "InvocationTargetException while requesting permission for ACCESS_COARSE_LOCATION:", e2);
                    }
                }
            });
            builder.show();
        } catch (IllegalAccessException e) {
            Log.w(TAG, "IllegalAccessException while checking for ACCESS_COARSE_LOCATION:", e);
        } catch (InvocationTargetException e2) {
            Log.w(TAG, "InvocationTargetException while checking for ACCESS_COARSE_LOCATION:", e2);
        }
    }

    private Method getCheckSelfPermissionMethod() {
        try {
            return Activity.class.getMethod("checkSelfPermission", String.class);
        } catch (Exception unused) {
            return null;
        }
    }

    private Method getRequestPermissionsMethod() {
        try {
            return Activity.class.getMethod("requestPermissions", String[].class, Integer.TYPE);
        } catch (Exception unused) {
            return null;
        }
    }

    @TargetApi(MotionEventCompat.AXIS_RTRIGGER)
    private void initBluetoothAdapter() {
        this.bluetoothAdapter = ((BluetoothManager) this.f0cordova.getActivity().getSystemService("bluetooth")).getAdapter();
    }

    private void pauseEventPropagationToDom() {
        checkEventQueue();
        this.threadPoolExecutor.pause();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void resumeEventPropagationToDom() {
        checkEventQueue();
        this.threadPoolExecutor.resume();
    }

    private void initBluetoothListener() {
        if (!hasBlueToothPermission()) {
            debugWarn("Cannot listen to Bluetooth service when BLUETOOTH permission is not added");
            return;
        }
        try {
            this.iBeaconManager.checkAvailability();
            if (this.broadcastReceiver != null) {
                debugWarn("Already listening to Bluetooth service, not adding again");
                return;
            }
            this.broadcastReceiver = new BroadcastReceiver() {
                /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass2 */

                public void onReceive(Context context, Intent intent) {
                    if (intent.getAction().equals("android.bluetooth.adapter.action.STATE_CHANGED")) {
                        int intExtra = intent.getIntExtra("android.bluetooth.adapter.extra.STATE", EditorInfoCompat.IME_FLAG_FORCE_ASCII);
                        int intExtra2 = intent.getIntExtra("android.bluetooth.adapter.extra.PREVIOUS_STATE", EditorInfoCompat.IME_FLAG_FORCE_ASCII);
                        LocationManager locationManager = LocationManager.this;
                        locationManager.debugLog("Bluetooth Service state changed from " + getStateDescription(intExtra2) + " to " + getStateDescription(intExtra));
                        if (intExtra != Integer.MIN_VALUE) {
                            if (intExtra != 10) {
                                switch (intExtra) {
                                    case 12:
                                        LocationManager.this.beaconServiceNotifier.didChangeAuthorizationStatus("AuthorizationStatusAuthorized");
                                        return;
                                    case 13:
                                        break;
                                    default:
                                        return;
                                }
                            }
                            if (intExtra2 == 12) {
                                LocationManager.this.beaconServiceNotifier.didChangeAuthorizationStatus("AuthorizationStatusDenied");
                                return;
                            }
                            return;
                        }
                        LocationManager.this.beaconServiceNotifier.didChangeAuthorizationStatus("AuthorizationStatusNotDetermined");
                    }
                }

                private String getStateDescription(int i) {
                    if (i == Integer.MIN_VALUE) {
                        return "ERROR";
                    }
                    switch (i) {
                        case 10:
                            return "STATE_OFF";
                        case 11:
                            return "STATE_TURNING_ON";
                        case 12:
                            return "STATE_ON";
                        case 13:
                            return "STATE_TURNING_OFF";
                        default:
                            return "ERROR" + i;
                    }
                }
            };
            this.f0cordova.getActivity().registerReceiver(this.broadcastReceiver, new IntentFilter("android.bluetooth.adapter.action.STATE_CHANGED"));
        } catch (BleNotAvailableException e) {
            debugWarn("Cannot listen to Bluetooth service: " + e.getMessage());
        } catch (Exception e2) {
            debugWarn("Unexpected exception checking for Bluetooth service: " + e2.getMessage());
        }
    }

    private void initEventQueue() {
        this.queue = new LinkedBlockingQueue();
        this.threadPoolExecutor = new PausableThreadPoolExecutor(this.queue);
        new Handler().postDelayed(new Runnable() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass3 */

            public void run() {
                LocationManager.this.checkIfDomSignaldDelegateReady();
            }
        }, (long) (CDV_LOCATION_MANAGER_DOM_DELEGATE_TIMEOUT * 1000));
    }

    private void checkEventQueue() {
        if (this.threadPoolExecutor == null || this.queue == null) {
            debugWarn("WARNING event queue should not be null.");
            this.queue = new LinkedBlockingQueue();
            this.threadPoolExecutor = new PausableThreadPoolExecutor(this.queue);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void checkIfDomSignaldDelegateReady() {
        PausableThreadPoolExecutor pausableThreadPoolExecutor = this.threadPoolExecutor;
        if (pausableThreadPoolExecutor == null || pausableThreadPoolExecutor.isPaused()) {
            String str = "WARNING did not receive delegate ready callback from DOM after " + CDV_LOCATION_MANAGER_DOM_DELEGATE_TIMEOUT + " seconds!";
            debugWarn(str);
            this.webView.sendJavascript("console.warn('" + str + "')");
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void createMonitorCallbacks(final CallbackContext callbackContext) {
        this.iBeaconManager.setMonitorNotifier(new MonitorNotifier() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass4 */

            @Override // org.altbeacon.beacon.MonitorNotifier
            public void didEnterRegion(Region region) {
                LocationManager locationManager = LocationManager.this;
                locationManager.debugLog("didEnterRegion INSIDE for " + region.getUniqueId());
                dispatchMonitorState("didEnterRegion", 1, region, callbackContext);
            }

            @Override // org.altbeacon.beacon.MonitorNotifier
            public void didExitRegion(Region region) {
                LocationManager locationManager = LocationManager.this;
                locationManager.debugLog("didExitRegion OUTSIDE for " + region.getUniqueId());
                dispatchMonitorState("didExitRegion", 0, region, callbackContext);
            }

            @Override // org.altbeacon.beacon.MonitorNotifier
            public void didDetermineStateForRegion(int i, Region region) {
                LocationManager locationManager = LocationManager.this;
                locationManager.debugLog("didDetermineStateForRegion '" + LocationManager.this.nameOfRegionState(i) + "' for region: " + region.getUniqueId());
                dispatchMonitorState("didDetermineStateForRegion", i, region, callbackContext);
            }

            private void dispatchMonitorState(final String str, final int i, final Region region, final CallbackContext callbackContext) {
                LocationManager.this.threadPoolExecutor.execute(new Runnable() {
                    /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass4.AnonymousClass1 */

                    public void run() {
                        try {
                            JSONObject jSONObject = new JSONObject();
                            jSONObject.put("eventType", str);
                            jSONObject.put("region", LocationManager.this.mapOfRegion(region));
                            if (str.equals("didDetermineStateForRegion")) {
                                jSONObject.put("state", LocationManager.this.nameOfRegionState(i));
                            }
                            PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, jSONObject);
                            pluginResult.setKeepCallback(true);
                            callbackContext.sendPluginResult(pluginResult);
                        } catch (Exception e) {
                            Log.e(LocationManager.TAG, "'monitoringDidFailForRegion' exception " + e.getCause());
                            LocationManager.this.beaconServiceNotifier.monitoringDidFailForRegion(region, e);
                        }
                    }
                });
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void createRangingCallbacks(final CallbackContext callbackContext) {
        this.iBeaconManager.setRangeNotifier(new RangeNotifier() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass5 */

            @Override // org.altbeacon.beacon.RangeNotifier
            public void didRangeBeaconsInRegion(final Collection<Beacon> collection, final Region region) {
                LocationManager.this.threadPoolExecutor.execute(new Runnable() {
                    /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass5.AnonymousClass1 */

                    public void run() {
                        try {
                            JSONObject jSONObject = new JSONObject();
                            JSONArray jSONArray = new JSONArray();
                            for (Beacon beacon : collection) {
                                jSONArray.put(LocationManager.this.mapOfBeacon(beacon));
                            }
                            jSONObject.put("eventType", "didRangeBeaconsInRegion");
                            jSONObject.put("region", LocationManager.this.mapOfRegion(region));
                            jSONObject.put("beacons", jSONArray);
                            LocationManager locationManager = LocationManager.this;
                            locationManager.debugLog("didRangeBeacons: " + jSONObject.toString());
                            PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, jSONObject);
                            pluginResult.setKeepCallback(true);
                            callbackContext.sendPluginResult(pluginResult);
                        } catch (Exception e) {
                            Log.e(LocationManager.TAG, "'rangingBeaconsDidFailForRegion' exception " + e.getCause());
                            LocationManager.this.beaconServiceNotifier.rangingBeaconsDidFailForRegion(region, e);
                        }
                    }
                });
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void createManagerCallbacks(final CallbackContext callbackContext) {
        this.beaconServiceNotifier = new IBeaconServiceNotifier() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass6 */

            @Override // com.unarin.cordova.beacon.IBeaconServiceNotifier
            public void rangingBeaconsDidFailForRegion(final Region region, final Exception exc) {
                LocationManager.this.threadPoolExecutor.execute(new Runnable() {
                    /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass6.AnonymousClass1 */

                    public void run() {
                        AnonymousClass6 r0 = AnonymousClass6.this;
                        r0.sendFailEvent("rangingBeaconsDidFailForRegion", region, exc, callbackContext);
                    }
                });
            }

            @Override // com.unarin.cordova.beacon.IBeaconServiceNotifier
            public void monitoringDidFailForRegion(final Region region, final Exception exc) {
                LocationManager.this.threadPoolExecutor.execute(new Runnable() {
                    /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass6.AnonymousClass2 */

                    public void run() {
                        AnonymousClass6 r0 = AnonymousClass6.this;
                        r0.sendFailEvent("monitoringDidFailForRegionWithError", region, exc, callbackContext);
                    }
                });
            }

            @Override // com.unarin.cordova.beacon.IBeaconServiceNotifier
            public void didStartMonitoringForRegion(final Region region) {
                LocationManager.this.threadPoolExecutor.execute(new Runnable() {
                    /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass6.AnonymousClass3 */

                    public void run() {
                        try {
                            JSONObject jSONObject = new JSONObject();
                            jSONObject.put("eventType", "didStartMonitoringForRegion");
                            jSONObject.put("region", LocationManager.this.mapOfRegion(region));
                            LocationManager locationManager = LocationManager.this;
                            locationManager.debugLog("didStartMonitoringForRegion: " + jSONObject.toString());
                            PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, jSONObject);
                            pluginResult.setKeepCallback(true);
                            callbackContext.sendPluginResult(pluginResult);
                        } catch (Exception e) {
                            Log.e(LocationManager.TAG, "'startMonitoringForRegion' exception " + e.getCause());
                            AnonymousClass6.this.monitoringDidFailForRegion(region, e);
                        }
                    }
                });
            }

            @Override // com.unarin.cordova.beacon.IBeaconServiceNotifier
            public void didChangeAuthorizationStatus(final String str) {
                LocationManager.this.threadPoolExecutor.execute(new Runnable() {
                    /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass6.AnonymousClass4 */

                    public void run() {
                        try {
                            JSONObject jSONObject = new JSONObject();
                            jSONObject.put("eventType", "didChangeAuthorizationStatus");
                            jSONObject.put("authorizationStatus", str);
                            LocationManager locationManager = LocationManager.this;
                            locationManager.debugLog("didChangeAuthorizationStatus: " + jSONObject.toString());
                            PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, jSONObject);
                            pluginResult.setKeepCallback(true);
                            callbackContext.sendPluginResult(pluginResult);
                        } catch (Exception e) {
                            CallbackContext callbackContext = callbackContext;
                            callbackContext.error("didChangeAuthorizationStatus error: " + e.getMessage());
                        }
                    }
                });
            }

            /* access modifiers changed from: private */
            /* access modifiers changed from: public */
            private void sendFailEvent(String str, Region region, Exception exc, CallbackContext callbackContext) {
                try {
                    JSONObject jSONObject = new JSONObject();
                    jSONObject.put("eventType", str);
                    jSONObject.put("region", LocationManager.this.mapOfRegion(region));
                    jSONObject.put(PluginResultHelper.JsParams.General.ERROR, exc.getMessage());
                    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, jSONObject);
                    pluginResult.setKeepCallback(true);
                    callbackContext.sendPluginResult(pluginResult);
                } catch (Exception e) {
                    Log.e(LocationManager.TAG, str + " error " + e.getMessage());
                    callbackContext.error(str + " error " + e.getMessage());
                }
            }
        };
    }

    private void onDomDelegateReady(CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass7 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                LocationManager.this.resumeEventPropagationToDom();
                return new PluginResult(PluginResult.Status.OK);
            }
        });
    }

    private void isBluetoothEnabled(CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass8 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                try {
                    return new PluginResult(PluginResult.Status.OK, LocationManager.this.bluetoothAdapter != null && LocationManager.this.bluetoothAdapter.isEnabled());
                } catch (Exception e) {
                    LocationManager locationManager = LocationManager.this;
                    locationManager.debugWarn("'isBluetoothEnabled' exception " + e.getMessage());
                    return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
                }
            }
        });
    }

    private void enableBluetooth(CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass9 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                try {
                    LocationManager.this.bluetoothAdapter.enable();
                    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK);
                    pluginResult.setKeepCallback(true);
                    return pluginResult;
                } catch (Exception e) {
                    Log.e(LocationManager.TAG, "'enableBluetooth' service error: " + e.getCause());
                    return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
                }
            }
        });
    }

    private void disableBluetooth(CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass10 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                try {
                    LocationManager.this.bluetoothAdapter.disable();
                    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK);
                    pluginResult.setKeepCallback(true);
                    return pluginResult;
                } catch (Exception e) {
                    Log.e(LocationManager.TAG, "'disableBluetooth' service error: " + e.getCause());
                    return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
                }
            }
        });
    }

    private void disableDebugNotifications(CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass11 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                LocationManager.this.debugEnabled = false;
                BeaconManager.setDebug(false);
                return new PluginResult(PluginResult.Status.OK);
            }
        });
    }

    private void enableDebugNotifications(CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass12 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                LocationManager.this.debugEnabled = true;
                BeaconManager.setDebug(true);
                return new PluginResult(PluginResult.Status.OK);
            }
        });
    }

    private void disableDebugLogs(CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass13 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                LocationManager.this.debugEnabled = false;
                BeaconManager.setDebug(false);
                return new PluginResult(PluginResult.Status.OK);
            }
        });
    }

    private void enableDebugLogs(CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass14 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                LocationManager.this.debugEnabled = true;
                BeaconManager.setDebug(true);
                return new PluginResult(PluginResult.Status.OK);
            }
        });
    }

    private void appendToDeviceLog(final String str, CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass15 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                String str = str;
                if (str == null || str.isEmpty()) {
                    return new PluginResult(PluginResult.Status.ERROR, "Log message not provided");
                }
                LocationManager locationManager = LocationManager.this;
                locationManager.debugLog("[DOM] " + str);
                return new PluginResult(PluginResult.Status.OK, str);
            }
        });
    }

    private void startMonitoringForRegion(final JSONObject jSONObject, CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass16 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                Region region = null;
                try {
                    region = LocationManager.this.parseRegion(jSONObject);
                    LocationManager.this.iBeaconManager.startMonitoringBeaconsInRegion(region);
                    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK);
                    pluginResult.setKeepCallback(true);
                    LocationManager.this.beaconServiceNotifier.didStartMonitoringForRegion(region);
                    return pluginResult;
                } catch (RemoteException e) {
                    Log.e(LocationManager.TAG, "'startMonitoringForRegion' service error: " + e.getCause());
                    LocationManager.this.beaconServiceNotifier.monitoringDidFailForRegion(region, e);
                    return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
                } catch (Exception e2) {
                    Log.e(LocationManager.TAG, "'startMonitoringForRegion' exception " + e2.getCause());
                    LocationManager.this.beaconServiceNotifier.monitoringDidFailForRegion(region, e2);
                    return new PluginResult(PluginResult.Status.ERROR, e2.getMessage());
                }
            }
        });
    }

    private void stopMonitoringForRegion(final JSONObject jSONObject, CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass17 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                try {
                    LocationManager.this.iBeaconManager.stopMonitoringBeaconsInRegion(LocationManager.this.parseRegion(jSONObject));
                    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK);
                    pluginResult.setKeepCallback(true);
                    return pluginResult;
                } catch (RemoteException e) {
                    Log.e(LocationManager.TAG, "'stopMonitoringForRegion' service error: " + e.getCause());
                    return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
                } catch (Exception e2) {
                    Log.e(LocationManager.TAG, "'stopMonitoringForRegion' exception " + e2.getCause());
                    return new PluginResult(PluginResult.Status.ERROR, e2.getMessage());
                }
            }
        });
    }

    private void startRangingBeaconsInRegion(final JSONObject jSONObject, CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass18 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                try {
                    LocationManager.this.iBeaconManager.startRangingBeaconsInRegion(LocationManager.this.parseRegion(jSONObject));
                    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK);
                    pluginResult.setKeepCallback(true);
                    return pluginResult;
                } catch (RemoteException e) {
                    Log.e(LocationManager.TAG, "'startRangingBeaconsInRegion' service error: " + e.getCause());
                    return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
                } catch (Exception e2) {
                    Log.e(LocationManager.TAG, "'startRangingBeaconsInRegion' exception " + e2.getCause());
                    return new PluginResult(PluginResult.Status.ERROR, e2.getMessage());
                }
            }
        });
    }

    private void stopRangingBeaconsInRegion(final JSONObject jSONObject, CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass19 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                try {
                    LocationManager.this.iBeaconManager.stopRangingBeaconsInRegion(LocationManager.this.parseRegion(jSONObject));
                    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK);
                    pluginResult.setKeepCallback(true);
                    return pluginResult;
                } catch (RemoteException e) {
                    Log.e(LocationManager.TAG, "'stopRangingBeaconsInRegion' service error: " + e.getCause());
                    return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
                } catch (Exception e2) {
                    Log.e(LocationManager.TAG, "'stopRangingBeaconsInRegion' exception " + e2.getCause());
                    return new PluginResult(PluginResult.Status.ERROR, e2.getMessage());
                }
            }
        });
    }

    private void getAuthorizationStatus(CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass20 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                try {
                    if (!LocationManager.this.hasBlueToothPermission()) {
                        return new PluginResult(PluginResult.Status.ERROR, "Application does not BLUETOOTH or BLUETOOTH_ADMIN permissions");
                    }
                    String str = LocationManager.this.iBeaconManager.checkAvailability() ? "AuthorizationStatusAuthorized" : "AuthorizationStatusDenied";
                    JSONObject jSONObject = new JSONObject();
                    jSONObject.put("authorizationStatus", str);
                    return new PluginResult(PluginResult.Status.OK, jSONObject);
                } catch (BleNotAvailableException e) {
                    LocationManager locationManager = LocationManager.this;
                    locationManager.debugLog("'getAuthorizationStatus' Device not supported: " + e.getMessage());
                    return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
                } catch (Exception e2) {
                    LocationManager locationManager2 = LocationManager.this;
                    locationManager2.debugWarn("'getAuthorizationStatus' exception " + e2.getMessage());
                    return new PluginResult(PluginResult.Status.ERROR, e2.getMessage());
                }
            }
        });
    }

    private void requestWhenInUseAuthorization(CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass21 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                return new PluginResult(PluginResult.Status.OK);
            }
        });
    }

    private void requestAlwaysAuthorization(CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass22 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                return new PluginResult(PluginResult.Status.OK);
            }
        });
    }

    private void getMonitoredRegions(CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass23 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                try {
                    Collection<Region> monitoredRegions = LocationManager.this.iBeaconManager.getMonitoredRegions();
                    JSONArray jSONArray = new JSONArray();
                    for (Region region : monitoredRegions) {
                        jSONArray.put(LocationManager.this.mapOfRegion(region));
                    }
                    return new PluginResult(PluginResult.Status.OK, jSONArray);
                } catch (JSONException e) {
                    LocationManager locationManager = LocationManager.this;
                    locationManager.debugWarn("'getMonitoredRegions' exception: " + e.getMessage());
                    return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
                }
            }
        });
    }

    private void getRangedRegions(CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass24 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                try {
                    Collection<Region> rangedRegions = LocationManager.this.iBeaconManager.getRangedRegions();
                    JSONArray jSONArray = new JSONArray();
                    for (Region region : rangedRegions) {
                        jSONArray.put(LocationManager.this.mapOfRegion(region));
                    }
                    return new PluginResult(PluginResult.Status.OK, jSONArray);
                } catch (JSONException e) {
                    LocationManager locationManager = LocationManager.this;
                    locationManager.debugWarn("'getRangedRegions' exception: " + e.getMessage());
                    return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
                }
            }
        });
    }

    private void requestStateForRegion(JSONObject jSONObject, CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass25 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                PluginResult pluginResult = new PluginResult(PluginResult.Status.ERROR, "Manual request for monitoring update is not supported on Android");
                pluginResult.setKeepCallback(true);
                return pluginResult;
            }
        });
    }

    private void isRangingAvailable(CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass26 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                try {
                    return new PluginResult(PluginResult.Status.OK, LocationManager.this.iBeaconManager.checkAvailability());
                } catch (BleNotAvailableException e) {
                    LocationManager locationManager = LocationManager.this;
                    locationManager.debugLog("'isRangingAvailable' Device not supported: " + e.getMessage());
                    return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
                } catch (Exception e2) {
                    LocationManager locationManager2 = LocationManager.this;
                    locationManager2.debugWarn("'isRangingAvailable' exception " + e2.getMessage());
                    return new PluginResult(PluginResult.Status.ERROR, e2.getMessage());
                }
            }
        });
    }

    private void registerDelegateCallbackId(JSONObject jSONObject, final CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass27 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                LocationManager locationManager = LocationManager.this;
                locationManager.debugLog("Registering delegate callback ID: " + callbackContext.getCallbackId());
                LocationManager.this.createMonitorCallbacks(callbackContext);
                LocationManager.this.createRangingCallbacks(callbackContext);
                LocationManager.this.createManagerCallbacks(callbackContext);
                PluginResult pluginResult = new PluginResult(PluginResult.Status.OK);
                pluginResult.setKeepCallback(true);
                return pluginResult;
            }
        });
    }

    private void isMonitoringAvailableForClass(final JSONObject jSONObject, CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass28 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                boolean z;
                try {
                    LocationManager.this.parseRegion(jSONObject);
                    z = true;
                } catch (Exception unused) {
                    z = false;
                }
                PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, z);
                pluginResult.setKeepCallback(true);
                return pluginResult;
            }
        });
    }

    private void isAdvertisingAvailable(CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass29 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                if (Build.VERSION.SDK_INT >= 21) {
                    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, true);
                    pluginResult.setKeepCallback(true);
                    return pluginResult;
                }
                PluginResult pluginResult2 = new PluginResult(PluginResult.Status.OK, false);
                pluginResult2.setKeepCallback(true);
                return pluginResult2;
            }
        });
    }

    private void isAdvertising(CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass30 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, false);
                pluginResult.setKeepCallback(true);
                return pluginResult;
            }
        });
    }

    private void startAdvertising(JSONArray jSONArray, CallbackContext callbackContext) throws JSONException {
        debugLog("Advertisement start START BEACON ");
        debugLog(jSONArray.toString(4));
        JSONObject optJSONObject = jSONArray.optJSONObject(0);
        optJSONObject.getString("identifier");
        final String str = null;
        final String string = (!optJSONObject.has("uuid") || optJSONObject.isNull("uuid")) ? null : optJSONObject.getString("uuid");
        final String string2 = (!optJSONObject.has("major") || optJSONObject.isNull("major")) ? null : optJSONObject.getString("major");
        if (optJSONObject.has("minor") && !optJSONObject.isNull("minor")) {
            str = optJSONObject.getString("minor");
        }
        final int i = jSONArray.length() > 1 ? jSONArray.getInt(1) : -55;
        if (string2 != null || str == null) {
            _handleCallSafely(callbackContext, new ILocationManagerCommand() {
                /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass31 */

                @Override // com.unarin.cordova.beacon.ILocationManagerCommand
                public PluginResult run() {
                    LocationManager.this.debugLog("Advertisement start STEP Beacon.Builder ");
                    Beacon build = new Beacon.Builder().setId1(string).setId2(string2).setId3(str).setManufacturer(76).setTxPower(i).setDataFields(Arrays.asList(0L)).build();
                    LocationManager locationManager = LocationManager.this;
                    locationManager.debugLog("[DEBUG] Beacon.Builder: " + build);
                    LocationManager.this.debugLog("Advertisement start STEP BeaconParser ");
                    LocationManager.this.debugLog("Advertisement start STEP BeaconTransmitter ");
                    BeaconTransmitter createOrGetBeaconTransmitter = LocationManager.this.createOrGetBeaconTransmitter();
                    LocationManager locationManager2 = LocationManager.this;
                    locationManager2.debugLog("[DEBUG] BeaconTransmitter: " + createOrGetBeaconTransmitter);
                    createOrGetBeaconTransmitter.startAdvertising(build, new AdvertiseCallback() {
                        /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass31.AnonymousClass1 */

                        public void onStartFailure(int i) {
                            LocationManager locationManager = LocationManager.this;
                            locationManager.debugWarn("Advertisement start failed with code: " + i);
                        }

                        public void onStartSuccess(AdvertiseSettings advertiseSettings) {
                            LocationManager.this.debugWarn("startAdvertising start succeeded.");
                        }
                    });
                    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, false);
                    pluginResult.setKeepCallback(true);
                    return pluginResult;
                }
            });
            return;
        }
        throw new UnsupportedOperationException("Unsupported combination of 'major' and 'minor' parameters.");
    }

    private void stopAdvertising(CallbackContext callbackContext) {
        _handleCallSafely(callbackContext, new ILocationManagerCommand() {
            /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass32 */

            @Override // com.unarin.cordova.beacon.ILocationManagerCommand
            public PluginResult run() {
                LocationManager.this.debugInfo("LocationManager::stopAdvertising::STOPPING...");
                LocationManager.this.createOrGetBeaconTransmitter().stopAdvertising();
                LocationManager.this.debugInfo("LocationManager::stopAdvertising::DONE");
                PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, "iBeacon Advertising stopped.");
                pluginResult.setKeepCallback(true);
                return pluginResult;
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private Region parseRegion(JSONObject jSONObject) throws JSONException, InvalidKeyException, UnsupportedOperationException {
        if (!jSONObject.has("typeName")) {
            throw new InvalidKeyException("'typeName' is missing, cannot parse Region.");
        } else if (jSONObject.has("identifier")) {
            String string = jSONObject.getString("typeName");
            if (string.equals("BeaconRegion")) {
                return parseBeaconRegion(jSONObject);
            }
            if (string.equals("CircularRegion")) {
                return parseCircularRegion(jSONObject);
            }
            throw new UnsupportedOperationException("Unsupported region type");
        } else {
            throw new InvalidKeyException("'identifier' is missing, cannot parse Region.");
        }
    }

    private Region parseCircularRegion(JSONObject jSONObject) throws JSONException, InvalidKeyException, UnsupportedOperationException {
        if (!jSONObject.has("latitude")) {
            throw new InvalidKeyException("'latitude' is missing, cannot parse CircularRegion.");
        } else if (!jSONObject.has("longitude")) {
            throw new InvalidKeyException("'longitude' is missing, cannot parse CircularRegion.");
        } else if (!jSONObject.has("radius")) {
            throw new InvalidKeyException("'radius' is missing, cannot parse CircularRegion.");
        } else {
            throw new UnsupportedOperationException("Circular regions are not supported at present");
        }
    }

    private Region parseBeaconRegion(JSONObject jSONObject) throws JSONException, UnsupportedOperationException {
        String string = jSONObject.getString("identifier");
        Identifier identifier = null;
        String string2 = (!jSONObject.has("uuid") || jSONObject.isNull("uuid")) ? null : jSONObject.getString("uuid");
        String string3 = (!jSONObject.has("major") || jSONObject.isNull("major")) ? null : jSONObject.getString("major");
        String string4 = (!jSONObject.has("minor") || jSONObject.isNull("minor")) ? null : jSONObject.getString("minor");
        if (string3 != null || string4 == null) {
            Identifier parse = string2 != null ? Identifier.parse(string2) : null;
            Identifier parse2 = string3 != null ? Identifier.parse(string3) : null;
            if (string4 != null) {
                identifier = Identifier.parse(string4);
            }
            return new Region(string, parse, parse2, identifier);
        }
        throw new UnsupportedOperationException("Unsupported combination of 'major' and 'minor' parameters.");
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private JSONObject mapOfRegion(Region region) throws JSONException {
        return mapOfBeaconRegion(region);
    }

    private JSONObject mapOfBeaconRegion(Region region) throws JSONException {
        JSONObject jSONObject = new JSONObject();
        if (region.getUniqueId() != null) {
            jSONObject.put("identifier", region.getUniqueId());
        }
        jSONObject.put("uuid", region.getId1());
        if (region.getId2() != null) {
            jSONObject.put("major", region.getId2());
        }
        if (region.getId3() != null) {
            jSONObject.put("minor", region.getId3());
        }
        jSONObject.put("typeName", "BeaconRegion");
        return jSONObject;
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private JSONObject mapOfBeacon(Beacon beacon) throws JSONException {
        JSONObject jSONObject = new JSONObject();
        jSONObject.put("uuid", beacon.getId1());
        jSONObject.put("major", beacon.getId2());
        jSONObject.put("minor", beacon.getId3());
        jSONObject.put("proximity", nameOfProximity(beacon.getDistance()));
        jSONObject.put("rssi", beacon.getRssi());
        jSONObject.put("tx", beacon.getTxPower());
        double round = (double) Math.round(beacon.getDistance() * 100.0d);
        Double.isNaN(round);
        jSONObject.put("accuracy", round / 100.0d);
        return jSONObject;
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private boolean hasBlueToothPermission() {
        Activity activity = this.f0cordova.getActivity();
        return activity.checkCallingOrSelfPermission("android.permission.BLUETOOTH") == 0 && activity.checkCallingOrSelfPermission("android.permission.BLUETOOTH_ADMIN") == 0;
    }

    private void _handleCallSafely(CallbackContext callbackContext, ILocationManagerCommand iLocationManagerCommand) {
        _handleCallSafely(callbackContext, iLocationManagerCommand, true);
    }

    private void _handleCallSafely(final CallbackContext callbackContext, final ILocationManagerCommand iLocationManagerCommand, boolean z) {
        if (z) {
            new AsyncTask<Void, Void, Void>() {
                /* class com.unarin.cordova.beacon.LocationManager.AnonymousClass33 */

                /* access modifiers changed from: protected */
                public Void doInBackground(Void... voidArr) {
                    try {
                        LocationManager.this._sendResultOfCommand(callbackContext, iLocationManagerCommand.run());
                        return null;
                    } catch (Exception e) {
                        LocationManager.this._handleExceptionOfCommand(callbackContext, e);
                        return null;
                    }
                }
            }.execute(new Void[0]);
            return;
        }
        try {
            _sendResultOfCommand(callbackContext, iLocationManagerCommand.run());
        } catch (Exception e) {
            _handleExceptionOfCommand(callbackContext, e);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void _handleExceptionOfCommand(CallbackContext callbackContext, Exception exc) {
        Log.e(TAG, "Uncaught exception: " + exc.getMessage());
        String arrays = Arrays.toString(exc.getStackTrace());
        Log.e(TAG, "Stack trace: " + arrays);
        if (callbackContext != null) {
            callbackContext.error(exc.getMessage());
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void _sendResultOfCommand(CallbackContext callbackContext, PluginResult pluginResult) {
        if (pluginResult.getStatus() != PluginResult.Status.OK.ordinal()) {
            debugWarn("WARNING: " + PluginResult.StatusMessages[pluginResult.getStatus()]);
        }
        if (callbackContext != null) {
            callbackContext.sendPluginResult(pluginResult);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void debugInfo(String str) {
        if (this.debugEnabled) {
            Log.i(TAG, str);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void debugLog(String str) {
        if (this.debugEnabled) {
            Log.d(TAG, str);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void debugWarn(String str) {
        if (this.debugEnabled) {
            Log.w(TAG, str);
        }
    }

    @Override // org.altbeacon.beacon.BeaconConsumer
    public void onBeaconServiceConnect() {
        debugLog("Connected to IBeacon service");
    }

    @Override // org.altbeacon.beacon.BeaconConsumer
    public Context getApplicationContext() {
        return this.f0cordova.getActivity();
    }

    @Override // org.altbeacon.beacon.BeaconConsumer
    public void unbindService(ServiceConnection serviceConnection) {
        debugLog("Unbind from IBeacon service");
        this.f0cordova.getActivity().unbindService(serviceConnection);
    }

    @Override // org.altbeacon.beacon.BeaconConsumer
    public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
        debugLog("Bind to IBeacon service");
        return this.f0cordova.getActivity().bindService(intent, serviceConnection, i);
    }
}