导航菜单

页面标题

页面副标题

Meine NEW v2.0.3616 - BeaconService.java 源代码

正在查看: Meine NEW v2.0.3616 应用的 BeaconService.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package org.altbeacon.beacon.service;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageItemInfo;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.os.Binder;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import com.microsoft.identity.common.internal.telemetry.TelemetryEventStrings;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconLocalBroadcastProcessor;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BuildConfig;
import org.altbeacon.beacon.Region;
import org.altbeacon.beacon.distance.ModelSpecificDistanceCalculator;
import org.altbeacon.beacon.logging.LogManager;
import org.altbeacon.beacon.service.scanner.CycledLeScanCallback;
import org.altbeacon.beacon.startup.StartupBroadcastReceiver;
import org.altbeacon.beacon.utils.ProcessUtils;
import org.altbeacon.bluetooth.BluetoothCrashResolver;

public class BeaconService extends Service {
    public static final int MSG_SET_SCAN_PERIODS = 6;
    public static final int MSG_START_MONITORING = 4;
    public static final int MSG_START_RANGING = 2;
    public static final int MSG_STOP_MONITORING = 5;
    public static final int MSG_STOP_RANGING = 3;
    public static final int MSG_SYNC_SETTINGS = 7;
    public static final String TAG = "BeaconService";
    private BluetoothCrashResolver bluetoothCrashResolver;
    private BeaconLocalBroadcastProcessor mBeaconNotificationProcessor;
    private ScanHelper mScanHelper;
    private final Handler handler = new Handler();
    final Messenger mMessenger = new Messenger(new IncomingHandler(this));

    public class BeaconBinder extends Binder {
        public BeaconBinder() {
        }

        public BeaconService getService() {
            LogManager.i(BeaconService.TAG, "getService of BeaconBinder called", new Object[0]);
            return BeaconService.this;
        }
    }

    public static class IncomingHandler extends Handler {
        private final WeakReference<BeaconService> mService;

        public IncomingHandler(BeaconService beaconService) {
            super(Looper.getMainLooper());
            this.mService = new WeakReference<>(beaconService);
        }

        @Override
        public void handleMessage(Message message) {
            BeaconService beaconService = this.mService.get();
            if (beaconService != null) {
                StartRMData fromBundle = StartRMData.fromBundle(message.getData());
                if (fromBundle == null) {
                    if (message.what != 7) {
                        LogManager.i(BeaconService.TAG, "Received unknown message from other process : " + message.what, new Object[0]);
                        return;
                    } else {
                        LogManager.i(BeaconService.TAG, "Received settings update from other process", new Object[0]);
                        SettingsData fromBundle2 = SettingsData.fromBundle(message.getData());
                        if (fromBundle2 != null) {
                            fromBundle2.apply(beaconService);
                            return;
                        } else {
                            LogManager.w(BeaconService.TAG, "Settings data missing", new Object[0]);
                            return;
                        }
                    }
                }
                int i = message.what;
                if (i == 2) {
                    LogManager.i(BeaconService.TAG, "start ranging received", new Object[0]);
                    beaconService.startRangingBeaconsInRegion(fromBundle.getRegionData(), new Callback(fromBundle.getCallbackPackageName()));
                    beaconService.setScanPeriods(fromBundle.getScanPeriod(), fromBundle.getBetweenScanPeriod(), fromBundle.getBackgroundFlag());
                    return;
                }
                if (i == 3) {
                    LogManager.i(BeaconService.TAG, "stop ranging received", new Object[0]);
                    beaconService.stopRangingBeaconsInRegion(fromBundle.getRegionData());
                    beaconService.setScanPeriods(fromBundle.getScanPeriod(), fromBundle.getBetweenScanPeriod(), fromBundle.getBackgroundFlag());
                    return;
                }
                if (i == 4) {
                    LogManager.i(BeaconService.TAG, "start monitoring received", new Object[0]);
                    beaconService.startMonitoringBeaconsInRegion(fromBundle.getRegionData(), new Callback(fromBundle.getCallbackPackageName()));
                    beaconService.setScanPeriods(fromBundle.getScanPeriod(), fromBundle.getBetweenScanPeriod(), fromBundle.getBackgroundFlag());
                } else if (i == 5) {
                    LogManager.i(BeaconService.TAG, "stop monitoring received", new Object[0]);
                    beaconService.stopMonitoringBeaconsInRegion(fromBundle.getRegionData());
                    beaconService.setScanPeriods(fromBundle.getScanPeriod(), fromBundle.getBetweenScanPeriod(), fromBundle.getBackgroundFlag());
                } else if (i != 6) {
                    super.handleMessage(message);
                } else {
                    LogManager.i(BeaconService.TAG, "set scan intervals received", new Object[0]);
                    beaconService.setScanPeriods(fromBundle.getScanPeriod(), fromBundle.getBetweenScanPeriod(), fromBundle.getBackgroundFlag());
                }
            }
        }
    }

    private void ensureNotificationProcessorSetup() {
        if (this.mBeaconNotificationProcessor == null) {
            BeaconLocalBroadcastProcessor beaconLocalBroadcastProcessor = BeaconLocalBroadcastProcessor.getInstance(this);
            this.mBeaconNotificationProcessor = beaconLocalBroadcastProcessor;
            beaconLocalBroadcastProcessor.register();
        }
    }

    private String getManifestMetadataValue(String str) {
        Bundle bundle;
        try {
            ServiceInfo serviceInfo = getPackageManager().getServiceInfo(new ComponentName(this, (Class<?>) BeaconService.class), 128);
            if (serviceInfo == null || (bundle = ((PackageItemInfo) serviceInfo).metaData) == null) {
                return null;
            }
            return bundle.get(str).toString();
        } catch (PackageManager.NameNotFoundException unused) {
            return null;
        }
    }

    private PendingIntent getRestartIntent() {
        return PendingIntent.getBroadcast(getApplicationContext(), 1, new Intent(getApplicationContext(), (Class<?>) StartupBroadcastReceiver.class), 1140850688);
    }

    private void startForegroundIfConfigured() {
        BeaconManager instanceForApplication = BeaconManager.getInstanceForApplication(getApplicationContext());
        Notification foregroundServiceNotification = instanceForApplication.getForegroundServiceNotification();
        int foregroundServiceNotificationId = instanceForApplication.getForegroundServiceNotificationId();
        if (foregroundServiceNotification != null) {
            startForeground(foregroundServiceNotificationId, foregroundServiceNotification);
        }
    }

    public CycledLeScanCallback getCycledLeScanCallback() {
        return this.mScanHelper.getCycledLeScanCallback();
    }

    @Override
    public IBinder onBind(Intent intent) {
        LogManager.i(TAG, "binding", new Object[0]);
        return this.mMessenger.getBinder();
    }

    @Override
    public void onCreate() {
        startForegroundIfConfigured();
        ScanHelper scanHelper = new ScanHelper(this);
        this.mScanHelper = scanHelper;
        if (scanHelper.getCycledScanner() == null) {
            this.mScanHelper.createCycledLeScanner(false, this.bluetoothCrashResolver);
        }
        this.mScanHelper.setMonitoringStatus(MonitoringStatus.getInstanceForApplication(this));
        this.mScanHelper.setRangedRegionState(new HashMap());
        this.mScanHelper.setBeaconParsers(new HashSet());
        this.mScanHelper.setExtraDataBeaconTracker(new ExtraDataBeaconTracker());
        BeaconManager instanceForApplication = BeaconManager.getInstanceForApplication(getApplicationContext());
        instanceForApplication.setScannerInSameProcess(true);
        if (instanceForApplication.isMainProcess()) {
            LogManager.i(TAG, "beaconService version %s is starting up on the main process", BuildConfig.VERSION_NAME);
            ensureNotificationProcessorSetup();
        } else {
            LogManager.i(TAG, "beaconService version %s is starting up on a separate process", BuildConfig.VERSION_NAME);
            ProcessUtils processUtils = new ProcessUtils(this);
            LogManager.i(TAG, "beaconService PID is " + processUtils.getPid() + " with process name " + processUtils.getProcessName(), new Object[0]);
        }
        String manifestMetadataValue = getManifestMetadataValue("longScanForcingEnabled");
        if (manifestMetadataValue != null && manifestMetadataValue.equals(TelemetryEventStrings.Value.TRUE)) {
            LogManager.i(TAG, "longScanForcingEnabled to keep scans going on Android N for > 30 minutes", new Object[0]);
            if (this.mScanHelper.getCycledScanner() != null) {
                this.mScanHelper.getCycledScanner().setLongScanForcingEnabled(true);
            }
        }
        this.mScanHelper.reloadParsers();
        Beacon.setDistanceCalculator(new ModelSpecificDistanceCalculator(this, BeaconManager.getDistanceModelUpdateUrl()));
        try {
            this.mScanHelper.setSimulatedScanData((List) Class.forName("org.altbeacon.beacon.SimulatedScanData").getField("beacons").get(null));
        } catch (ClassNotFoundException unused) {
            LogManager.d(TAG, "No org.altbeacon.beacon.SimulatedScanData class exists.", new Object[0]);
        } catch (Exception e) {
            LogManager.e(e, TAG, "Cannot get simulated Scan data.  Make sure your org.altbeacon.beacon.SimulatedScanData class defines a field with the signature 'public static List<Beacon> beacons'", new Object[0]);
        }
    }

    @Override
    public void onDestroy() {
        LogManager.e(TAG, "onDestroy()", new Object[0]);
        BeaconLocalBroadcastProcessor beaconLocalBroadcastProcessor = this.mBeaconNotificationProcessor;
        if (beaconLocalBroadcastProcessor != null) {
            beaconLocalBroadcastProcessor.unregister();
        }
        BluetoothCrashResolver bluetoothCrashResolver = this.bluetoothCrashResolver;
        if (bluetoothCrashResolver != null) {
            bluetoothCrashResolver.stop();
        }
        LogManager.i(TAG, "onDestroy called.  stopping scanning", new Object[0]);
        this.handler.removeCallbacksAndMessages(null);
        if (this.mScanHelper.getCycledScanner() != null) {
            this.mScanHelper.getCycledScanner().stop();
            this.mScanHelper.getCycledScanner().destroy();
        }
        this.mScanHelper.getMonitoringStatus().stopStatusPreservation();
        this.mScanHelper.terminateThreads();
    }

    @Override
    public int onStartCommand(Intent intent, int i, int i2) {
        String str;
        if (intent == null) {
            str = "starting with null intent";
        } else {
            str = "starting with intent " + intent.toString();
        }
        LogManager.i(TAG, str, new Object[0]);
        return super.onStartCommand(intent, i, i2);
    }

    @Override
    public void onTaskRemoved(Intent intent) {
        super.onTaskRemoved(intent);
        LogManager.d(TAG, "task removed", new Object[0]);
        String str = Build.VERSION.RELEASE;
        if (str.contains("4.4.1") || str.contains("4.4.2") || str.contains("4.4.3")) {
            ((AlarmManager) getApplicationContext().getSystemService("alarm")).set(0, System.currentTimeMillis() + 1000, getRestartIntent());
            LogManager.d(TAG, "Setting a wakeup alarm to go off due to Android 4.4.2 service restarting bug.", new Object[0]);
        }
    }

    @Override
    public boolean onUnbind(Intent intent) {
        LogManager.i(TAG, "unbinding so destroying self", new Object[0]);
        stopForeground(true);
        stopSelf();
        return false;
    }

    public void reloadParsers() {
        this.mScanHelper.reloadParsers();
    }

    public void setScanPeriods(long j, long j2, boolean z) {
        if (this.mScanHelper.getCycledScanner() != null) {
            this.mScanHelper.getCycledScanner().setScanPeriods(j, j2, z);
        }
    }

    public void startMonitoringBeaconsInRegion(Region region, Callback callback) {
        LogManager.d(TAG, "startMonitoring called", new Object[0]);
        this.mScanHelper.getMonitoringStatus().addRegion(region, callback);
        LogManager.d(TAG, "Currently monitoring %s regions.", Integer.valueOf(this.mScanHelper.getMonitoringStatus().regionsCount()));
        if (this.mScanHelper.getCycledScanner() != null) {
            this.mScanHelper.getCycledScanner().start();
        }
    }

    public void startRangingBeaconsInRegion(Region region, Callback callback) {
        synchronized (this.mScanHelper.getRangedRegionState()) {
            if (this.mScanHelper.getRangedRegionState().containsKey(region)) {
                LogManager.i(TAG, "Already ranging that region -- will replace existing region.", new Object[0]);
                this.mScanHelper.getRangedRegionState().remove(region);
            }
            this.mScanHelper.getRangedRegionState().put(region, new RangeState(callback));
            LogManager.d(TAG, "Currently ranging %s regions.", Integer.valueOf(this.mScanHelper.getRangedRegionState().size()));
        }
        if (this.mScanHelper.getCycledScanner() != null) {
            this.mScanHelper.getCycledScanner().start();
        }
    }

    public void stopMonitoringBeaconsInRegion(Region region) {
        LogManager.d(TAG, "stopMonitoring called", new Object[0]);
        this.mScanHelper.getMonitoringStatus().removeRegion(region);
        LogManager.d(TAG, "Currently monitoring %s regions.", Integer.valueOf(this.mScanHelper.getMonitoringStatus().regionsCount()));
        if (this.mScanHelper.getMonitoringStatus().regionsCount() == 0 && this.mScanHelper.getRangedRegionState().size() == 0 && this.mScanHelper.getCycledScanner() != null) {
            this.mScanHelper.getCycledScanner().stop();
        }
    }

    public void stopRangingBeaconsInRegion(Region region) {
        int size;
        synchronized (this.mScanHelper.getRangedRegionState()) {
            this.mScanHelper.getRangedRegionState().remove(region);
            size = this.mScanHelper.getRangedRegionState().size();
            LogManager.d(TAG, "Currently ranging %s regions.", Integer.valueOf(this.mScanHelper.getRangedRegionState().size()));
        }
        if (size == 0 && this.mScanHelper.getMonitoringStatus().regionsCount() == 0 && this.mScanHelper.getCycledScanner() != null) {
            this.mScanHelper.getCycledScanner().stop();
        }
    }
}