Exploit database

GH-14fb583d3d1bd2da10251bb1a37b93d6

gitchw/ghostlock-cve-2026-43499

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2026-43499

165 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source

Repository files3

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

workspace/decomp/HeyOTA/sources/com/heytap/wearable/ota/service/OTAService.java

Java · 50,540 bytes
package com.heytap.wearable.ota.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.PowerManager;
import android.os.RemoteException;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;
import com.heytap.accessory.CommonStatusCodes;
import com.heytap.wearable.ota.activity.NotificationActivity;
import com.heytap.wearable.ota.activity.OtaUpdateInfo;
import com.heytap.wearable.ota.bluetooth.OafProvider;
import com.heytap.wearable.ota.db.DbHelper;
import com.heytap.wearable.ota.db.SharedPrefManager;
import com.heytap.wearable.ota.download.DownloadInfo;
import com.heytap.wearable.ota.download.DownloadManager;
import com.heytap.wearable.ota.download.DownloadResultFeedbackRunnable;
import com.heytap.wearable.ota.download.IProgressObserver;
import com.heytap.wearable.ota.install.OTAInstallRunnable;
import com.heytap.wearable.ota.query.QueryOTAUpdateRunnable;
import com.heytap.wearable.ota.service.IOTAUICtrl;
import com.heytap.wearable.ota.strategy.OTAStrategy;
import com.heytap.wearable.ota.util.CommonUtil;
import com.heytap.wearable.ota.util.DialogHelper;
import com.heytap.wearable.ota.util.NearmeUploadUtil;
import com.heytap.wearable.ota.util.NotificationHelper;
import com.heytap.wearable.ota.util.OppoLog;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

/* loaded from: classes.dex */
public class OTAService extends Service {
    private Context mContext;
    private DownloadManager mDownLoadManager;
    private ThreadPoolExecutor mExecutor;
    private OTAHandler mOTAHandler;
    private PowerManager mPowerManager;
    private OTAUICtr mUICtrlStub;
    private static final ArrayList<Integer> OTA_RUNNING_STATUS = new ArrayList<>();
    private static long sNetworkSpeed = 0;
    private static long sLowSpeedCount = 8;
    private static long sOldTimeInMill = 0;
    private static long sOldTransfer = 0;
    private static Object mObserverLock = new Object();
    private static HashMap<String, IStatusObserver> mEntryObserver = new HashMap<>();
    private final HandlerThread mOTAWorkThread = new HandlerThread("OTAService", 10);
    private SharedPrefManager mSpDefault = null;
    private boolean mDownloadStarted = false;
    private boolean mIsVerifyDialogShowing = false;
    private final Map<String, ClientToken> mClientTokens = new HashMap();
    private IProgressObserver mListener = new IProgressObserver() { // from class: com.heytap.wearable.ota.service.OTAService.1
        @Override // android.os.IInterface
        public IBinder asBinder() {
            return null;
        }

        @Override // com.heytap.wearable.ota.download.IProgressObserver
        public void finished(boolean z, int i) {
        }

        @Override // com.heytap.wearable.ota.download.IProgressObserver
        public void reportHistory(int i, int i2) {
        }

        @Override // com.heytap.wearable.ota.download.IProgressObserver
        public void update(int i, long j, long j2) {
        }

        @Override // com.heytap.wearable.ota.download.IProgressObserver
        public void onDownloadUpdated(DownloadInfo downloadInfo) {
            OTAService.this.mOTAHandler.removeMessages(3002);
            OTAService.this.processUpdateDownload(downloadInfo);
        }
    };
    private BroadcastReceiver mReceiver = new BroadcastReceiver() { // from class: com.heytap.wearable.ota.service.OTAService.2
        @Override // android.content.BroadcastReceiver
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                if ("android.intent.action.BATTERY_CHANGED".equals(intent.getAction())) {
                    OTAService.this.processChargerConnect(context, false);
                    return;
                }
                if ("heytap.intent.action.LOG_COLLECT".equals(intent.getAction())) {
                    boolean z = intent.getIntExtra("collect", 0) == 1;
                    OppoLog.i("OTAService", "log switch change, enable:" + z);
                    OppoLog.setRomUpdateLog(z);
                    return;
                }
                if ("oppo.intent.action.ROM_UPDATE_CONFIG_SUCCESS".equals(intent.getAction())) {
                    ArrayList<String> arrayList = null;
                    try {
                        arrayList = intent.getStringArrayListExtra("ROM_UPDATE_CONFIG_LIST");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (arrayList == null || arrayList.isEmpty() || !arrayList.contains("ota_config_watch_list")) {
                        return;
                    }
                    OppoLog.d("OTAService", "start ota rus RomUpdateService");
                    OTAService.this.mOTAHandler.sendEmptyMessage(7007);
                }
            }
        }
    };
    private ContentObserver mSchoolModeObserver = new ContentObserver(new Handler()) { // from class: com.heytap.wearable.ota.service.OTAService.3
        @Override // android.database.ContentObserver
        public void onChange(boolean z, Uri uri) {
            super.onChange(z);
            if (Settings.Global.getUriFor("school_mode_status").equals(uri) && CommonUtil.isSchoolModeEnabled(OTAService.this.mContext) && DbHelper.getOTAUpdateStatus(OTAService.this.mContext) == 3) {
                OppoLog.i("OTAService", "enter school mode, pause download");
                DbHelper.changeUpdateStatus(OTAService.this.mContext, 6);
                OTAService.this.mDownLoadManager.pauseDownloadAuto();
            }
        }
    };

    static {
        OTA_RUNNING_STATUS.add(3);
        OTA_RUNNING_STATUS.add(2);
        OTA_RUNNING_STATUS.add(14);
    }

    @Override // android.app.Service
    public void onCreate() {
        super.onCreate();
        this.mContext = getApplicationContext();
        startForeground(hashCode(), NotificationHelper.getHelper(this.mContext).getForeSvcNotification());
        this.mExecutor = new ThreadPoolExecutor(0, 1, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue());
        this.mOTAWorkThread.start();
        Looper looper = this.mOTAWorkThread.getLooper();
        if (looper == null) {
            OppoLog.e("OTAService", "Unable to start OTAService, looper is null");
            return;
        }
        this.mOTAHandler = new OTAHandler(looper);
        this.mSpDefault = new SharedPrefManager(this.mContext, 0);
        DownloadManager downloadManager = DownloadManager.getInstance();
        this.mDownLoadManager = downloadManager;
        downloadManager.init(this.mContext, this.mListener);
        this.mPowerManager = (PowerManager) this.mContext.getSystemService("power");
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.intent.action.BATTERY_CHANGED");
        intentFilter.addAction("heytap.intent.action.LOG_COLLECT");
        intentFilter.addAction("oppo.intent.action.ROM_UPDATE_CONFIG_SUCCESS");
        this.mContext.registerReceiver(this.mReceiver, intentFilter);
        this.mContext.getContentResolver().registerContentObserver(Settings.Global.getUriFor("school_mode_status"), false, this.mSchoolModeObserver);
        initPublicDownloadState();
        updateLastUnexpectedState(this.mContext, DbHelper.getOTAUpdateStatus(this.mContext));
        this.mOTAHandler.sendEmptyMessageDelayed(2008, 30000L);
    }

    private void updateLastUnexpectedState(Context context, int i) {
        OppoLog.d("OTAService", "updateLastUnexpectedState lastState = " + i);
        if (i == 2) {
            DbHelper.changeUpdateStatus(this.mContext, 1);
            return;
        }
        if (i != 3) {
            return;
        }
        CommonUtil.getNetworkType(this.mContext);
        boolean isAllowAutoDownload = OTAStrategy.isAllowAutoDownload(this.mContext);
        boolean isAllowManualDownload = OTAStrategy.isAllowManualDownload(this.mContext);
        if (isAllowAutoDownload || isAllowManualDownload) {
            DownloadInfo oTADownloadInfo = DbHelper.getOTADownloadInfo(this.mContext);
            oTADownloadInfo.speedMode = 1;
            oTADownloadInfo.startType = 0;
            prepareStartDownload(oTADownloadInfo, true);
            return;
        }
        DbHelper.changeUpdateStatus(this.mContext, 6);
        DbHelper.setDownloadPauseReason(this.mContext, -6);
    }

    @Override // android.app.Service
    public boolean onUnbind(Intent intent) {
        synchronized (mObserverLock) {
            mEntryObserver.clear();
        }
        int oTAUpdateStatus = DbHelper.getOTAUpdateStatus(this.mContext);
        OppoLog.i("OTAService", "onUnbind " + oTAUpdateStatus);
        if (oTAUpdateStatus < 1 && !OTA_RUNNING_STATUS.contains(Integer.valueOf(oTAUpdateStatus))) {
            stopSelf();
        }
        return super.onUnbind(intent);
    }

    @Override // android.app.Service
    public int onStartCommand(Intent intent, int i, int i2) {
        if (intent != null && intent.getAction() != null) {
            String action = intent.getAction();
            OppoLog.i("OTAService", "onStartCommand action:" + action.substring(action.lastIndexOf(46) + 1));
            if ("oppo.intent.ota.CHECK_NEW_VERSION_UPDATE".equals(action)) {
                this.mOTAHandler.sendMessage(CommonUtil.createAutoUpdateStateMsg(this.mOTAHandler, CommonStatusCodes.AUTHENTICATE_SUCCESS, true));
            } else if ("oppo.intent.ota.MANUAL_OTA_UPDATE_CHECK".equals(action)) {
                this.mOTAHandler.sendMessage(CommonUtil.createAutoUpdateStateMsg(this.mOTAHandler, CommonStatusCodes.AUTHENTICATE_SUCCESS, false));
            } else if ("oppo.intent.ota.CURRENT_VERSION_DESCRIPTION_QUERY".equals(action)) {
                this.mOTAHandler.sendEmptyMessage(CommonStatusCodes.AUTHENTICATE_FAIL);
            } else if ("oppo.intent.action.OTA_AUTO_RESUME_DOWNLOAD".equals(action)) {
                this.mOTAHandler.removeMessages(3004);
                this.mOTAHandler.sendEmptyMessageDelayed(3004, 1000L);
            } else if ("oppo.intent.action.OTA_START_INSTALL".equals(action)) {
                String readString = new SharedPrefManager(this.mContext, 3).readString("ota_current_policy");
                String string = Settings.Secure.getString(getContentResolver(), "ota_current_policy");
                if (readString.equals(string)) {
                    if (OTAStrategy.isAllowInstallModeImmediately(this.mContext)) {
                        new SharedPrefManager(this.mContext, 1).writePref("install_by_manual", 1);
                        this.mOTAHandler.sendEmptyMessage(6001);
                    } else {
                        OppoLog.d("OTAService", "install condition is not satisfy, install later");
                        OTAStrategy.writeLaterInstallFlag(this.mContext, true);
                        this.mOTAHandler.sendEmptyMessage(7006);
                    }
                } else {
                    OppoLog.d("OTAService", readString + " " + string);
                    Intent intent2 = new Intent(this.mContext, (Class<?>) NotificationActivity.class);
                    intent2.addFlags(268468224);
                    intent2.putExtra("pageid", 3);
                    intent2.putExtra("install", true);
                    this.mContext.startActivity(intent2);
                }
            } else if ("oppo.intent.action.OTA_DELAY_AUTO_INSTALL".equals(action)) {
                OppoLog.d("OTAService", "install when plug again and battery level up (ACTION_OTA_DELAY_AUTO_INSTALL)");
                if (new SharedPrefManager(this.mContext, 3).readString("ota_current_policy").equals(Settings.Secure.getString(getContentResolver(), "ota_current_policy"))) {
                    OTAStrategy.writeLaterInstallFlag(this.mContext, true);
                } else {
                    Intent intent3 = new Intent(this.mContext, (Class<?>) NotificationActivity.class);
                    intent3.addFlags(268468224);
                    intent3.putExtra("pageid", 3);
                    this.mContext.startActivity(intent3);
                }
            } else if ("oppo.intent.action.OTA_REPORT_DOWNLOAD_RESULT".equals(action)) {
                this.mOTAHandler.sendEmptyMessage(10002);
            } else if ("heytap.intent.ota.POWER_CONNECT".equals(action)) {
                processChargerConnect(this.mContext, true);
            }
        }
        return 2;
    }

    @Override // android.app.Service
    public void onDestroy() {
        super.onDestroy();
        OppoLog.d("OTAService", "onDestroy");
        this.mContext.unregisterReceiver(this.mReceiver);
        this.mContext.getContentResolver().unregisterContentObserver(this.mSchoolModeObserver);
        this.mDownLoadManager.exit();
        synchronized (this.mExecutor) {
            this.mExecutor.shutdown();
        }
        this.mOTAWorkThread.quitSafely();
    }

    @Override // android.app.Service
    public IBinder onBind(Intent intent) {
        if (this.mUICtrlStub == null) {
            this.mUICtrlStub = new OTAUICtr();
        }
        return this.mUICtrlStub;
    }

    /* loaded from: classes.dex */
    private class ClientDeathRecipient implements IBinder.DeathRecipient {
        private String tag;

        public ClientDeathRecipient(String str) {
            this.tag = str;
        }

        @Override // android.os.IBinder.DeathRecipient
        public void binderDied() {
            synchronized (OTAService.mObserverLock) {
                OTAService.mEntryObserver.remove(this.tag);
            }
            ClientToken clientToken = (ClientToken) OTAService.this.mClientTokens.remove(this.tag);
            if (clientToken != null) {
                clientToken.binder.unlinkToDeath(clientToken.recipient, 0);
            }
        }
    }

    /* loaded from: classes.dex */
    private class ClientToken {
        private IBinder binder;
        private ClientDeathRecipient recipient;

        public ClientToken(OTAService oTAService, IBinder iBinder, ClientDeathRecipient clientDeathRecipient) {
            this.binder = iBinder;
            this.recipient = clientDeathRecipient;
        }
    }

    /* loaded from: classes.dex */
    final class OTAUICtr extends IOTAUICtrl.Stub {
        OTAUICtr() {
        }

        @Override // com.heytap.wearable.ota.service.IOTAUICtrl
        public void startDownload() {
            DownloadInfo oTADownloadInfo = DbHelper.getOTADownloadInfo(OTAService.this.mContext);
            oTADownloadInfo.speedMode = 1;
            oTADownloadInfo.startType = 1;
            new SharedPrefManager(OTAService.this.mContext, 1).remove("download_delayed_by_power_flag");
            OTAService.this.prepareStartDownload(oTADownloadInfo, false);
        }

        @Override // com.heytap.wearable.ota.service.IOTAUICtrl
        public void pauseDownload() {
            OTAService.this.mDownLoadManager.pauseDownload();
        }

        @Override // com.heytap.wearable.ota.service.IOTAUICtrl
        public void cancelDownload() {
            DbHelper.setDownloadProcess(OTAService.this.mContext, DbHelper.getOTAUpdateInfo(OTAService.this.mContext).name, 0L);
            DbHelper.changeUpdateStatus(OTAService.this.mContext, 1);
            OTAService.this.mDownLoadManager.cancelDownload(true);
        }

        @Override // com.heytap.wearable.ota.service.IOTAUICtrl
        public void startInstall() {
            OppoLog.d("OTAService", "startInstall()");
            OTAService.this.mOTAHandler.sendEmptyMessage(6001);
        }

        @Override // com.heytap.wearable.ota.service.IOTAUICtrl
        public void unregisterObserver(String str) {
            synchronized (OTAService.mObserverLock) {
                OTAService.mEntryObserver.remove(str);
            }
            ClientToken clientToken = (ClientToken) OTAService.this.mClientTokens.remove(str);
            if (clientToken != null) {
                clientToken.binder.unlinkToDeath(clientToken.recipient, 0);
            }
        }

        @Override // com.heytap.wearable.ota.service.IOTAUICtrl
        public boolean registerObserver(String str, IStatusObserver iStatusObserver) {
            if (iStatusObserver == null) {
                return false;
            }
            try {
                ClientDeathRecipient clientDeathRecipient = new ClientDeathRecipient(str);
                iStatusObserver.asBinder().linkToDeath(clientDeathRecipient, 0);
                OTAService.this.mClientTokens.put(str, new ClientToken(OTAService.this, iStatusObserver.asBinder(), clientDeathRecipient));
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            synchronized (OTAService.mObserverLock) {
                OTAService.mEntryObserver.put(str, iStatusObserver);
            }
            return true;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void processUpdateDownload(DownloadInfo downloadInfo) {
        int i = downloadInfo.state;
        long j = downloadInfo.downloadedSize;
        long j2 = downloadInfo.size;
        int i2 = downloadInfo.reason;
        boolean z = downloadInfo.autoDownload;
        String str = downloadInfo.path;
        switch (i) {
            case 2:
                OppoLog.d("OTAService", "size:" + j + "/" + j2 + " auto:" + z);
                DbHelper.setDownloadProcess(this.mContext, downloadInfo.name, j);
                int oTAUpdateStatus = DbHelper.getOTAUpdateStatus(this.mContext);
                if (!this.mDownloadStarted || oTAUpdateStatus < 3) {
                    this.mDownloadStarted = true;
                    DbHelper.changeUpdateStatus(this.mContext, 3);
                    NotificationHelper.getHelper(this.mContext).sendDownloadStartedNotification();
                    publishDownloadState(true);
                }
                notifyDownloadUpdate(j, j2, false);
                return;
            case 3:
                OppoLog.d("OTAService", "download paused! reason:" + i2);
                DbHelper.setDownloadPauseReason(this.mContext, i2);
                if (i2 == -5) {
                    DbHelper.changeUpdateStatus(this.mContext, 5);
                } else {
                    DbHelper.changeUpdateStatus(this.mContext, 6);
                }
                notifyDownloadPause(i2);
                publishDownloadState(false);
                NotificationHelper.getHelper(this.mContext).sendDownloadPausedNotification();
                return;
            case 4:
                PowerManager.WakeLock newWakeLock = this.mPowerManager.newWakeLock(1, "ota:download_finish");
                newWakeLock.acquire();
                OppoLog.i("OTAService", "download finished " + downloadInfo);
                publishDownloadState(false);
                DbHelper.setDownloadProcess(this.mContext, downloadInfo.name, j);
                DbHelper.changeUpdateStatus(this.mContext, 7);
                DbHelper.setInstallPath(this.mContext, downloadInfo.name, str);
                SharedPrefManager sharedPrefManager = new SharedPrefManager(this.mContext, 1);
                sharedPrefManager.remove("ota_user_agree_download_in_watch_mobile_flag");
                sharedPrefManager.remove("ota_user_agree_download_in_watch_phone_flag");
                sharedPrefManager.remove("is_manual_create_download_flag");
                sharedPrefManager.writePref("download_state_key_and_value", "1#success");
                notifyDownloadFinish(true);
                NearmeUploadUtil.sendNearmeDownloadSuccess(this.mContext);
                if (OTAStrategy.isAllowAutoInstall(this.mContext)) {
                    OppoLog.d("OTAService", "package download finish, start auto install.");
                    this.mOTAHandler.sendEmptyMessage(6001);
                } else {
                    OppoLog.d("OTAService", "package download finish, auto install is not allowed");
                    NotificationHelper.getHelper(this.mContext).sendDownloadFinishedNotification(true);
                }
                this.mOTAHandler.sendEmptyMessageDelayed(10002, 5000L);
                new SharedPrefManager(this.mContext, 0).writePref("ota_install_path", str);
                newWakeLock.release();
                return;
            case 5:
            case 7:
            case 8:
                OppoLog.d("OTAService", "download finished! reason=" + i2);
                SharedPrefManager sharedPrefManager2 = new SharedPrefManager(this.mContext, 1);
                sharedPrefManager2.remove("ota_user_agree_download_in_watch_mobile_flag");
                sharedPrefManager2.remove("ota_user_agree_download_in_watch_phone_flag");
                sharedPrefManager2.remove("is_manual_create_download_flag");
                DbHelper.changeUpdateStatus(this.mContext, 4);
                publishDownloadState(false);
                String str2 = i != 7 ? i != 8 ? "-1#other_failed" : "-3#no_space" : "-2#file_delete";
                sharedPrefManager2.writePref("download_state_key_and_value", str2);
                this.mOTAHandler.sendEmptyMessageDelayed(10002, 5000L);
                notifyDownloadFail();
                if (sharedPrefManager2.readInt("is_manual_create_download_flag", 0) == 1) {
                    NearmeUploadUtil.sendNearmeDownloadFail(this.mContext, str2.split("#")[1]);
                    return;
                }
                return;
            case 6:
            default:
                return;
            case 9:
                OppoLog.d("OTAService", "DOWNLOAD_STATE_CANCELED");
                return;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void prepareStartDownload(DownloadInfo downloadInfo, boolean z) {
        DownloadManager downloadManager = this.mDownLoadManager;
        if (downloadManager == null || downloadInfo == null) {
            return;
        }
        downloadInfo.autoDownload = z;
        sNetworkSpeed = 0L;
        sOldTimeInMill = 0L;
        sOldTransfer = 0L;
        this.mDownloadStarted = false;
        downloadManager.startDownload(downloadInfo);
        DbHelper.setDownloadPauseReason(this.mContext, 0);
    }

    private void publishDownloadState(boolean z) {
        Settings.Global.putInt(this.mContext.getContentResolver(), "ota_download", z ? 1 : 0);
    }

    private void initPublicDownloadState() {
        if (Settings.Global.getInt(this.mContext.getContentResolver(), "ota_download", 0) != 0) {
            Settings.Global.putInt(this.mContext.getContentResolver(), "ota_download", 0);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public boolean isDownloadFinished() {
        int oTAUpdateStatus = DbHelper.getOTAUpdateStatus(this.mContext);
        return 7 == oTAUpdateStatus || 14 == oTAUpdateStatus;
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public class OTAHandler extends Handler {
        public OTAHandler(Looper looper) {
            super(looper);
        }

        @Override // android.os.Handler
        public void handleMessage(Message message) {
            super.handleMessage(message);
            int i = message.what;
            boolean z = false;
            if (i == 1001) {
                long currentTimeMillis = System.currentTimeMillis();
                long readLong = currentTimeMillis - OTAService.this.mSpDefault.readLong("ota_last_check_time", 0L);
                if (readLong < 0 || readLong > 1000) {
                    OTAStrategy.setNextCheckOTAUpdateAlarm(OTAService.this.mContext);
                    OTAService.this.mSpDefault.writePref("ota_last_check_time", currentTimeMillis);
                    OtaUpdateInfo oTAUpdateInfo = DbHelper.getOTAUpdateInfo(OTAService.this.mContext);
                    if (OTAService.OTA_RUNNING_STATUS.contains(Integer.valueOf(oTAUpdateInfo.status))) {
                        OppoLog.d("OTAService", "ota status is : " + oTAUpdateInfo.status + " no need to query!");
                        if (14 == oTAUpdateInfo.status) {
                            OTAService.this.notifyQueryResult(0);
                            return;
                        } else {
                            OTAService.this.notifyQueryResult(-1);
                            return;
                        }
                    }
                    OTAService.this.executeInExecutor(new QueryOTAUpdateRunnable(OTAService.this.mContext, this, "query_version_update", CommonUtil.getAutoUpdateStateFormMsg(message)));
                    return;
                }
                OppoLog.d("OTAService", "The new update distance is 1 seconds less than last check, return!");
                return;
            }
            if (i == 1002) {
                if (new SharedPrefManager(OTAService.this.mContext, 2).readString(CommonUtil.getOtaVersion() + "_des").equals("")) {
                    OppoLog.d("OTAService", "start update new description!");
                    OTAService.this.executeInExecutor(new QueryOTAUpdateRunnable(OTAService.this.mContext, this, "query_decription", false));
                    return;
                } else {
                    OppoLog.d("OTAService", "start update new description!");
                    OTAService.this.mOTAHandler.sendEmptyMessage(2006);
                    return;
                }
            }
            if (i == 2001) {
                boolean autoUpdateStateFormMsg = CommonUtil.getAutoUpdateStateFormMsg(message);
                if (!autoUpdateStateFormMsg) {
                    OTAService.this.notifyQueryResult(0);
                }
                CommonUtil.setNewFlag(OTAService.this.mContext, false);
                CommonUtil.setMonitorNetworkState(OTAService.this.mContext, 0);
                NearmeUploadUtil.sendNearmeUpdateCheck(OTAService.this.mContext, autoUpdateStateFormMsg, false, "");
                return;
            }
            if (i == 3004) {
                if (!CommonUtil.isSchoolModeEnabled(OTAService.this.mContext)) {
                    DownloadInfo oTADownloadInfo = DbHelper.getOTADownloadInfo(OTAService.this.mContext);
                    if (OTAStrategy.isDownLoadSpaceEnough(OTAService.this.mContext, oTADownloadInfo.size)) {
                        if (!OTAStrategy.isAllowAutoDownload(OTAService.this.mContext) && !OTAStrategy.isAllowManualDownload(OTAService.this.mContext)) {
                            OppoLog.d("OTAService", "ota auto download not allow, check start condition next time");
                            return;
                        }
                        if (oTADownloadInfo.supportMobile != 0) {
                            oTADownloadInfo.speedMode = 1;
                        } else {
                            oTADownloadInfo.speedMode = 0;
                        }
                        oTADownloadInfo.startType = 0;
                        OTAService.this.prepareStartDownload(oTADownloadInfo, true);
                        return;
                    }
                    OppoLog.d("OTAService", "ota download space not enough!");
                    return;
                }
                OppoLog.i("OTAService", "school mode is enabled, skip download");
                return;
            }
            if (i == 7002) {
                if (CommonUtil.isScreenOn(OTAService.this.mContext)) {
                    OTAService.this.mIsVerifyDialogShowing = true;
                    DialogHelper.getInstance().showVerifyFailDialog(OTAService.this.mContext, new View.OnClickListener() { // from class: com.heytap.wearable.ota.service.OTAService.OTAHandler.1
                        @Override // android.view.View.OnClickListener
                        public void onClick(View view) {
                            OTAService.this.mIsVerifyDialogShowing = false;
                            OTAStrategy.reCheck(OTAService.this.mContext);
                        }
                    });
                    return;
                }
                return;
            }
            if (i == 10002) {
                String readString = new SharedPrefManager(OTAService.this.mContext, 1).readString("download_state_key_and_value", "");
                if (TextUtils.isEmpty(readString)) {
                    return;
                }
                OTAService.this.executeInExecutor(new DownloadResultFeedbackRunnable(OTAService.this.mContext, "download_feedback", readString));
                return;
            }
            if (i == 3001) {
                if (!CommonUtil.isSchoolModeEnabled(OTAService.this.mContext)) {
                    if (!OTAService.this.isDownloadFinished()) {
                        CommonUtil.setMonitorNetworkState(OTAService.this.mContext, 1);
                        DownloadInfo oTADownloadInfo2 = DbHelper.getOTADownloadInfo(OTAService.this.mContext);
                        if (OTAStrategy.isDownLoadSpaceEnough(OTAService.this.mContext, oTADownloadInfo2.size)) {
                            if (OTAStrategy.isAllowAutoDownloadWaitWifi(OTAService.this.mContext)) {
                                int networkType = CommonUtil.getNetworkType(OTAService.this.mContext);
                                if (networkType != 1) {
                                    WifiManager wifiManager = (WifiManager) OTAService.this.mContext.getSystemService("wifi");
                                    if (!wifiManager.isWifiEnabled()) {
                                        wifiManager.setWifiEnabled(true);
                                        OppoLog.d("OTAService", "enable wifi " + networkType);
                                        if (networkType != 7) {
                                            OTAService.this.mOTAHandler.sendEmptyMessageDelayed(3001, 30000L);
                                            return;
                                        }
                                    } else if (networkType != 7) {
                                        return;
                                    }
                                }
                                oTADownloadInfo2.speedMode = 0;
                                oTADownloadInfo2.startType = 0;
                                OTAService.this.prepareStartDownload(oTADownloadInfo2, true);
                                return;
                            }
                            OppoLog.d("OTAService", "ota auto download not allow, check start condition next time");
                            return;
                        }
                        OppoLog.d("OTAService", "ota download space not enough, check start condition next time");
                        return;
                    }
                    OppoLog.d("OTAService", "already download finished, check install condition");
                    if (OTAStrategy.isAllowAutoInstall(OTAService.this.mContext)) {
                        OTAService.this.mOTAHandler.sendEmptyMessage(6001);
                        return;
                    } else {
                        OppoLog.d("OTAService", "skip auto install");
                        return;
                    }
                }
                OppoLog.i("OTAService", "school mode is enabled, skip download");
                return;
            }
            if (i == 3002) {
                OtaUpdateInfo oTAUpdateInfo2 = DbHelper.getOTAUpdateInfo(OTAService.this.mContext);
                if (oTAUpdateInfo2.status == 3) {
                    OTAService.this.notifyDownloadUpdate(oTAUpdateInfo2.downloadedSize, oTAUpdateInfo2.size, true);
                    return;
                }
                return;
            }
            if (i == 7006) {
                Intent intent = new Intent(OTAService.this.mContext, (Class<?>) NotificationActivity.class);
                intent.addFlags(268468224);
                intent.putExtra("pageid", 2);
                OTAService.this.mContext.startActivity(intent);
                return;
            }
            if (i == 7007) {
                OTAService.this.processRusUpdate();
                return;
            }
            switch (i) {
                case 2003:
                case 2004:
                    if (2003 == i) {
                        OppoLog.d("OTAService", "has update, package type:" + DbHelper.getOTAUpdateType(OTAService.this.mContext));
                        CommonUtil.setNewFlag(OTAService.this.mContext, true);
                    } else {
                        OppoLog.d("OTAService", "has same, package type:" + DbHelper.getOTAUpdateType(OTAService.this.mContext));
                    }
                    boolean autoUpdateStateFormMsg2 = CommonUtil.getAutoUpdateStateFormMsg(message);
                    boolean isDownloadFinished = OTAService.this.isDownloadFinished();
                    NearmeUploadUtil.sendNearmeUpdateCheck(OTAService.this.mContext, autoUpdateStateFormMsg2, true, DbHelper.getOTAUpdateInfo(OTAService.this.mContext).newVersion);
                    if (autoUpdateStateFormMsg2) {
                        OTAService.this.mOTAHandler.sendEmptyMessage(3001);
                    } else {
                        OTAService.this.notifyQueryResult(2);
                    }
                    synchronized (OTAService.mObserverLock) {
                        if (OTAService.mEntryObserver.get("DiagnosisHelper") == null) {
                            z = true;
                        }
                    }
                    if (isDownloadFinished) {
                        NotificationHelper.getHelper(OTAService.this.mContext).sendDownloadFinishedNotification(z);
                        return;
                    } else {
                        NotificationHelper.getHelper(OTAService.this.mContext).sendFoundUpdateNotification(z);
                        return;
                    }
                case 2005:
                    if (CommonUtil.getAutoUpdateStateFormMsg(message)) {
                        return;
                    }
                    OTAService.this.notifyQueryResult(0);
                    return;
                case 2006:
                    OTAService.this.notifyQueryResult(3);
                    return;
                case 2007:
                    int oTAUpdateStatus = DbHelper.getOTAUpdateStatus(OTAService.this.mContext);
                    if (1 > oTAUpdateStatus || 7 < oTAUpdateStatus) {
                        OTAService.this.notifyQueryResult(0);
                        return;
                    } else {
                        OTAService.this.mOTAHandler.sendMessage(CommonUtil.createAutoUpdateStateMsg(OTAService.this.mOTAHandler, 2004, CommonUtil.getAutoUpdateStateFormMsg(message)));
                        return;
                    }
                case 2008:
                    if (OTAService.this.isServiceIdle()) {
                        OTAService.this.stopSelf();
                        return;
                    } else {
                        sendEmptyMessageDelayed(2008, 30000L);
                        return;
                    }
                default:
                    switch (i) {
                        case 6001:
                            OTAService.this.mOTAHandler.removeMessages(6001);
                            PowerManager.WakeLock newWakeLock = OTAService.this.mPowerManager.newWakeLock(1, "ota:start_install");
                            newWakeLock.acquire();
                            NotificationHelper.getHelper(OTAService.this.mContext).cancelNotify();
                            SharedPrefManager sharedPrefManager = new SharedPrefManager(OTAService.this.mContext, 1);
                            sharedPrefManager.remove("install_later_flag");
                            sharedPrefManager.remove("later_install_power_flag");
                            sharedPrefManager.remove("download_delayed_by_power_flag");
                            OTAService.this.executeInExecutor(new OTAInstallRunnable(OTAService.this.mContext, this));
                            newWakeLock.release();
                            return;
                        case 6002:
                            DialogHelper.getInstance().showVerifyView(OTAService.this.mContext);
                            return;
                        case 6003:
                            DialogHelper.getInstance().dismissVerifyView();
                            return;
                        case 6004:
                            DialogHelper.getInstance().setProgress(message.arg1, message.arg2);
                            return;
                        default:
                            return;
                    }
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public boolean isServiceIdle() {
        if (DbHelper.getOTAUpdateStatus(this.mContext) >= 1 || new SharedPrefManager(this.mContext, 1).readInt("install_later_flag", 0) == 1) {
            return false;
        }
        synchronized (mObserverLock) {
            if (mEntryObserver.size() != 0) {
                return false;
            }
            if (OafProvider.isConnected() || this.mIsVerifyDialogShowing) {
                return false;
            }
            OppoLog.d("OTAService", "idle true ");
            return true;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Code restructure failed: missing block: B:10:0x006e, code lost:
    
        if (android.text.TextUtils.isEmpty(r1) != false) goto L32;
     */
    /* JADX WARN: Code restructure failed: missing block: B:11:0x0070, code lost:
    
        parserXml(r1);
        com.heytap.wearable.ota.util.CommonUtil.updateVersionForRus(r8.mContext, r0);
     */
    /* JADX WARN: Code restructure failed: missing block: B:12:0x0078, code lost:
    
        return;
     */
    /* JADX WARN: Code restructure failed: missing block: B:14:?, code lost:
    
        return;
     */
    /* JADX WARN: Code restructure failed: missing block: B:15:?, code lost:
    
        return;
     */
    /* JADX WARN: Code restructure failed: missing block: B:22:0x0041, code lost:
    
        if (r2 == null) goto L20;
     */
    /* JADX WARN: Code restructure failed: missing block: B:5:0x0033, code lost:
    
        if (r2 != null) goto L12;
     */
    /* JADX WARN: Code restructure failed: missing block: B:6:0x0035, code lost:
    
        r2.close();
     */
    /* JADX WARN: Code restructure failed: missing block: B:7:0x0044, code lost:
    
        r2 = com.heytap.wearable.ota.util.CommonUtil.getVersionForRus(r8.mContext);
        com.heytap.wearable.ota.util.OppoLog.d("OTAService", "update version=" + r0 + " localVersion=" + r2);
     */
    /* JADX WARN: Code restructure failed: missing block: B:8:0x0068, code lost:
    
        if (r0 <= r2) goto L31;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public void processRusUpdate() {
        /*
            r8 = this;
            java.lang.String r0 = "version"
            java.lang.String r1 = "xml"
            java.lang.String[] r4 = new java.lang.String[]{r0, r1}
            android.content.Context r0 = r8.mContext
            android.content.ContentResolver r2 = r0.getContentResolver()
            r0 = 0
            r1 = 0
            java.lang.String r3 = "content://com.heytap.wearable.romupdate.provider.db/update_list"
            android.net.Uri r3 = android.net.Uri.parse(r3)     // Catch: java.lang.Throwable -> L39 java.lang.Exception -> L40
            java.lang.String r5 = "filtername=\"ota_config_watch_list\""
            r6 = 0
            r7 = 0
            android.database.Cursor r2 = r2.query(r3, r4, r5, r6, r7)     // Catch: java.lang.Throwable -> L39 java.lang.Exception -> L40
            if (r2 == 0) goto L33
            boolean r3 = r2.moveToFirst()     // Catch: java.lang.Throwable -> L30 java.lang.Exception -> L41
            if (r3 == 0) goto L33
            int r0 = r2.getInt(r0)     // Catch: java.lang.Throwable -> L30 java.lang.Exception -> L41
            r3 = 1
            java.lang.String r1 = r2.getString(r3)     // Catch: java.lang.Throwable -> L30 java.lang.Exception -> L41
            goto L33
        L30:
            r8 = move-exception
            r1 = r2
            goto L3a
        L33:
            if (r2 == 0) goto L44
        L35:
            r2.close()
            goto L44
        L39:
            r8 = move-exception
        L3a:
            if (r1 == 0) goto L3f
            r1.close()
        L3f:
            throw r8
        L40:
            r2 = r1
        L41:
            if (r2 == 0) goto L44
            goto L35
        L44:
            android.content.Context r2 = r8.mContext
            int r2 = com.heytap.wearable.ota.util.CommonUtil.getVersionForRus(r2)
            java.lang.StringBuilder r3 = new java.lang.StringBuilder
            r3.<init>()
            java.lang.String r4 = "update version="
            r3.append(r4)
            r3.append(r0)
            java.lang.String r4 = " localVersion="
            r3.append(r4)
            r3.append(r2)
            java.lang.String r3 = r3.toString()
            java.lang.String r4 = "OTAService"
            com.heytap.wearable.ota.util.OppoLog.d(r4, r3)
            if (r0 <= r2) goto L78
            boolean r2 = android.text.TextUtils.isEmpty(r1)
            if (r2 != 0) goto L78
            r8.parserXml(r1)
            android.content.Context r8 = r8.mContext
            com.heytap.wearable.ota.util.CommonUtil.updateVersionForRus(r8, r0)
        L78:
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: com.heytap.wearable.ota.service.OTAService.processRusUpdate():void");
    }

    private void parserXml(String str) {
        if (TextUtils.isEmpty(str)) {
            return;
        }
        StringReader stringReader = new StringReader(str);
        try {
            try {
                XmlPullParser newPullParser = XmlPullParserFactory.newInstance().newPullParser();
                newPullParser.setInput(stringReader);
                newPullParser.nextTag();
                for (int eventType = newPullParser.getEventType(); eventType != 1; eventType = newPullParser.next()) {
                    if (eventType == 2) {
                        String name = newPullParser.getName();
                        if ("min_battery_for_install".equals(name)) {
                            newPullParser.next();
                            OTAStrategy.updateMinBatteryLevelForInstall(this.mContext, Integer.parseInt(newPullParser.getText()));
                        } else if ("min_battery_for_auto_download".equals(name)) {
                            newPullParser.next();
                            OTAStrategy.updateMinBatteryLevelForAutoDownload(this.mContext, Integer.parseInt(newPullParser.getText()));
                        } else if ("auto_check_interval_time".equals(name)) {
                            newPullParser.next();
                            OTAStrategy.updateAutoCheckIntervalMS(this.mContext, Long.parseLong(newPullParser.getText()));
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e2) {
                e2.printStackTrace();
            }
        } finally {
            stringReader.close();
        }
    }

    private long updateDownloadSpeed(long j) {
        if (0 == sOldTimeInMill || j < sOldTransfer) {
            sNetworkSpeed = 0L;
            sOldTransfer = j;
            sLowSpeedCount = 0L;
            sOldTimeInMill = System.currentTimeMillis();
        } else {
            long currentTimeMillis = System.currentTimeMillis();
            long j2 = currentTimeMillis - sOldTimeInMill;
            if (0 != j2) {
                sNetworkSpeed = ((j - sOldTransfer) * 1000) / j2;
                sOldTransfer = j;
                sOldTimeInMill = currentTimeMillis;
                OppoLog.d("OTAService", "NetworkSpeed " + (sNetworkSpeed / 1024) + "KB/s");
                if (sNetworkSpeed < 51200) {
                    if (sLowSpeedCount == 20) {
                        NearmeUploadUtil.sendNearmeDownloadFail(this.mContext, "download speed slow " + sNetworkSpeed);
                    }
                    long j3 = sLowSpeedCount;
                    if (j3 <= 20) {
                        j3++;
                    }
                    sLowSpeedCount = j3;
                } else {
                    sLowSpeedCount = 0L;
                }
            }
        }
        return sNetworkSpeed;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void notifyDownloadUpdate(long j, long j2, boolean z) {
        long updateDownloadSpeed = z ? 0L : updateDownloadSpeed(j);
        if (updateDownloadSpeed > 0) {
            long j3 = ((j2 - j) * 1000) / updateDownloadSpeed;
            NotificationHelper.getHelper(this.mContext).sendDownloadUpdateNotification(CommonUtil.formatDuration(this.mContext, j3).toString(), j3 < 172800000);
        } else {
            NotificationHelper.getHelper(this.mContext).sendDownloadUpdateNotification(null, false);
        }
        try {
            synchronized (mObserverLock) {
                Iterator<String> it = mEntryObserver.keySet().iterator();
                while (it.hasNext()) {
                    mEntryObserver.get(it.next()).update(3, j, j2, updateDownloadSpeed);
                }
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        if (updateDownloadSpeed > 0) {
            this.mOTAHandler.sendEmptyMessageDelayed(3002, 5000L);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void notifyQueryResult(int i) {
        try {
            synchronized (mObserverLock) {
                Iterator<String> it = mEntryObserver.keySet().iterator();
                while (it.hasNext()) {
                    mEntryObserver.get(it.next()).hasUpdate(i);
                }
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private void notifyDownloadFail() {
        try {
            synchronized (mObserverLock) {
                Iterator<String> it = mEntryObserver.keySet().iterator();
                while (it.hasNext()) {
                    mEntryObserver.get(it.next()).downloadFailed();
                }
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private void notifyDownloadPause(int i) {
        try {
            synchronized (mObserverLock) {
                Iterator<String> it = mEntryObserver.keySet().iterator();
                while (it.hasNext()) {
                    mEntryObserver.get(it.next()).downloadPause(i);
                }
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private void notifyDownloadFinish(boolean z) {
        try {
            synchronized (mObserverLock) {
                Iterator<String> it = mEntryObserver.keySet().iterator();
                while (it.hasNext()) {
                    mEntryObserver.get(it.next()).downLoadFinished(z);
                }
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void processChargerConnect(Context context, boolean z) {
        SharedPrefManager sharedPrefManager = new SharedPrefManager(context, 1);
        int readInt = sharedPrefManager.readInt("install_later_flag", 0);
        int readInt2 = sharedPrefManager.readInt("download_delayed_by_power_flag", 0);
        if (readInt == 1) {
            int readInt3 = sharedPrefManager.readInt("later_install_power_flag", 1);
            if (z && readInt3 != 1) {
                sharedPrefManager.writePref("later_install_power_flag", 1);
                readInt3 = 1;
            }
            if (readInt3 == 1 && OTAStrategy.isAllowInstallModeLater(context)) {
                this.mOTAHandler.sendEmptyMessage(6001);
                return;
            }
            return;
        }
        if (z) {
            if (OTAStrategy.shouldCheckUpdateByCharge(this.mContext)) {
                OppoLog.i("OTAService", "start auto query when power connect");
                this.mOTAHandler.sendMessage(CommonUtil.createAutoUpdateStateMsg(this.mOTAHandler, CommonStatusCodes.AUTHENTICATE_SUCCESS, true));
                return;
            } else {
                if (isServiceIdle()) {
                    stopSelf();
                    return;
                }
                return;
            }
        }
        if (readInt2 == 1) {
            if (isDownloadFinished() && OTAStrategy.isAllowAutoInstall(this.mContext)) {
                this.mOTAHandler.sendEmptyMessage(6001);
            } else if (OTAStrategy.isAllowAutoDownloadWaitWifi(context)) {
                if (OTAStrategy.isAllowAutoDownload(context)) {
                    sharedPrefManager.remove("download_delayed_by_power_flag");
                }
                this.mOTAHandler.sendEmptyMessage(3001);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void executeInExecutor(Runnable runnable) {
        synchronized (this.mExecutor) {
            if (!this.mExecutor.isShutdown()) {
                this.mExecutor.execute(runnable);
            } else {
                OppoLog.e("OTAService", "already shutdown");
            }
        }
    }
}