package com.bc_chat.bc_base.utils;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.bc_chat.bc_base.BCApplication;
import com.zhaohaoting.framework.utils.Logger;

import me.leolin.shortcutbadger.ShortcutBadger;

 /**
 * author：Created by zhaohaoting on 2019/4/22
 * email：526309416@qq.com
 * desc：APP图标未读数红点更新接口
 */
public class Badger {
    private static final String TAG = "Badger";

    private static Handler handler;

    private static boolean support = true;

    public static void updateBadgerCount(final int unreadCount) {
        if (!support) {
            return;
        }

        if (handler == null) {
            handler = new Handler(Looper.myLooper());
        }

        handler.removeCallbacksAndMessages(null);
        handler.postDelayed(() -> {
            int badgerCount = unreadCount;
            if (badgerCount < 0) {
                badgerCount = 0;
            } else if (badgerCount > 99) {
                badgerCount = 99;
            }

            boolean res = ShortcutBadger.applyCount(BCApplication.getContext(), badgerCount);
            if (!res) {
                support = false; // 如果失败就不要再使用了!
            }
            Logger.e(TAG, "update badger count " + (res ? "success" : "failed"));
        }, 200);
    }
}
