package com.dowse.base.util;

import android.app.AlarmManager;
import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;
import android.text.TextUtils;
import java.util.HashMap;

/* loaded from: ds_base_2.0.9_23030112.aar:classes.jar:com/dowse/base/util/DSSchedulers.class */
public class DSSchedulers {
    private static final String TAG = DSSchedulers.class.getSimpleName();
    private AlarmManager mAlarmManager;
    private Context mContext;
    private Handler mHandler;
    private boolean isInit;
    private HashMap<String, AlarmManager.OnAlarmListener> taskInfoMap;
    private HandlerThread mHandlerThread;

    private DSSchedulers() {
        this.isInit = false;
        this.taskInfoMap = new HashMap<>();
    }

    /* loaded from: ds_base_2.0.9_23030112.aar:classes.jar:com/dowse/base/util/DSSchedulers$Holder.class */
    private static class Holder {
        public static DSSchedulers INSTANCE = new DSSchedulers();

        private Holder() {
        }
    }

    public static DSSchedulers getInstance() {
        return Holder.INSTANCE;
    }

    public boolean init(Context mContext) {
        if (!this.isInit) {
            this.mContext = mContext;
            this.mHandlerThread = new HandlerThread(TAG);
            this.mHandlerThread.start();
            this.mHandler = new Handler(this.mHandlerThread.getLooper());
            this.mAlarmManager = (AlarmManager) mContext.getSystemService("alarm");
            this.isInit = true;
            return true;
        }
        return false;
    }

    public void addTask(String taskId, long delay, AlarmManager.OnAlarmListener listener) {
        long triggerAtTime = System.currentTimeMillis() + delay;
        this.mAlarmManager.setExact(0, triggerAtTime, TAG, listener, this.mHandler);
        this.taskInfoMap.put(taskId, listener);
    }

    public void cancelTask(String taskId) {
        AlarmManager.OnAlarmListener listener;
        if (!TextUtils.isEmpty(taskId) && (listener = this.taskInfoMap.get(taskId)) != null) {
            this.mAlarmManager.cancel(listener);
        }
    }
}
