public static final String DB_KEY_BATTLEINFO = "key_battleinfo"; public static final String DB_KEY_BATTLENODE = "key_battlenode"; public static final String DB_KEY_FAIRYLOC = "key_fairyloc"; public static final String GOTO_FOREGROUND_ACTION = "com.antest1.gotobrowser.foreground"; public final static String GOTO_PACKAGE_NAME = "com.antest1.gotobrowser"; public static final int KCANOTIFY_DB_VERSION = 5; public static final String KCA_API_FAIRY_CHECKED = "/kca_api/fairy_checked"; public static final String KCA_API_FAIRY_HIDDEN = "/kca_api/fairy_hidden"; public static final String KCA_MSG_BATTLE_HDMG = "com.antest1.kcanotify.KcaService.KCA_MSG_BATTLE_HDMG"; public static final String KCA_MSG_BATTLE_INFO = "com.antest1.kcanotify.KcaService.KCA_MSG_BATTLE_INFO"; public static final String KCA_MSG_BATTLE_NODE = "com.antest1.kcanotify.KcaService.KCA_MSG_BATTLE_NODE"; public static final String KCA_MSG_BATTLE_VIEW_REFRESH = "com.antest1.kcanotify.KcaService.KCA_MSG_BATTLE_VIEW_REFRESH"; public static final String KCA_MSG_DATA = "com.antest1.kcanotify.KcaService.KCA_MSG_DATA"; public static final String KCA_MSG_QUEST_COMPLETE = "com.antest1.kcanotify.KcaViewButtonService.KCA_MSG_QUEST_COMPLETE"; public final static String KC_PACKAGE_NAME = "com.dmm.dmmlabo.kancolle"; public static final String PREF_FAIRY_AUTOHIDE = "fairy_auto_hide"; public static final String PREF_FAIRY_ICON = "fairy_icon"; public static final String PREF_FAIRY_NOTI_LONGCLICK = "notify_fairy_longclick"; public static final String PREF_FAIRY_RANDOM = "fairy_random"; public static final String PREF_FAIRY_REV = "fairy_rev"; public static final String PREF_FAIRY_SIZE = "fairy_size"; public static final String PREF_KCA_BATTLEVIEW_USE = "battleview_use"; public static final String PREF_KCA_NOTI_QUEST_FAIRY_GLOW = "noti_quest_fairy_glow"; public static final String PREF_KC_PACKAGE = "kc_package"; public static void doVibrate(Vibrator v, int time) {
    if (Build.VERSION.SDK_INT >= 26) {
        v.vibrate(VibrationEffect.createOneShot(time, VibrationEffect.DEFAULT_AMPLITUDE));
    } else {
        v.vibrate(time);
    }
} public static Boolean getBooleanPreferences(Context ctx, String key) {
    SharedPreferences pref = ctx.getSharedPreferences("pref", Context.MODE_PRIVATE);
    return pref.getBoolean(key, false);
} public static String getOrientationPrefix(int value) {
    if (value == Configuration.ORIENTATION_PORTRAIT) {
        return "ori_v_";
    } else {
        return "ori_h_";
    }
} public static String getStringFromException(Exception ex) {
    StringWriter errors = new StringWriter();
    ex.printStackTrace(new PrintWriter(errors));
    return errors.toString().replaceAll("\n", " / ").replaceAll("\t", "");
} public static String getStringPreferences(Context ctx, String key) {
    SharedPreferences pref = ctx.getSharedPreferences("pref", Context.MODE_PRIVATE);
    try {
        return String.valueOf(pref.getInt(key, 0));
    } catch (Exception e) {
        // Nothing to do
    }
    return pref.getString(key, "");
} public static int getWindowLayoutType() {
    int windowLayoutType = -1;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        windowLayoutType = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
    } else {
        windowLayoutType = WindowManager.LayoutParams.TYPE_PHONE;
    }
    return windowLayoutType;
}
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.Service;
import android.app.usage.UsageEvents;
import android.app.usage.UsageStatsManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Vibrator;
import android.provider.Settings;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import static com.antest1.kcanotify.KcaConstants.DB_KEY_BATTLEINFO;
import static com.antest1.kcanotify.KcaConstants.DB_KEY_BATTLENODE;
import static com.antest1.kcanotify.KcaConstants.DB_KEY_FAIRYLOC;
import static com.antest1.kcanotify.KcaConstants.GOTO_FOREGROUND_ACTION;
import static com.antest1.kcanotify.KcaConstants.GOTO_PACKAGE_NAME;
import static com.antest1.kcanotify.KcaConstants.KCANOTIFY_DB_VERSION;
import static com.antest1.kcanotify.KcaConstants.KCA_API_FAIRY_CHECKED;
import static com.antest1.kcanotify.KcaConstants.KCA_API_FAIRY_HIDDEN;
import static com.antest1.kcanotify.KcaConstants.KCA_MSG_BATTLE_HDMG;
import static com.antest1.kcanotify.KcaConstants.KCA_MSG_BATTLE_INFO;
import static com.antest1.kcanotify.KcaConstants.KCA_MSG_BATTLE_NODE;
import static com.antest1.kcanotify.KcaConstants.KCA_MSG_BATTLE_VIEW_REFRESH;
import static com.antest1.kcanotify.KcaConstants.KCA_MSG_DATA;
import static com.antest1.kcanotify.KcaConstants.KCA_MSG_QUEST_COMPLETE;
import static com.antest1.kcanotify.KcaConstants.KC_PACKAGE_NAME;
import static com.antest1.kcanotify.KcaConstants.PREF_FAIRY_AUTOHIDE;
import static com.antest1.kcanotify.KcaConstants.PREF_FAIRY_ICON;
import static com.antest1.kcanotify.KcaConstants.PREF_FAIRY_NOTI_LONGCLICK;
import static com.antest1.kcanotify.KcaConstants.PREF_FAIRY_RANDOM;
import static com.antest1.kcanotify.KcaConstants.PREF_FAIRY_REV;
import static com.antest1.kcanotify.KcaConstants.PREF_FAIRY_SIZE;
import static com.antest1.kcanotify.KcaConstants.PREF_KCA_BATTLEVIEW_USE;
import static com.antest1.kcanotify.KcaConstants.PREF_KCA_NOTI_QUEST_FAIRY_GLOW;
import static com.antest1.kcanotify.KcaConstants.PREF_KC_PACKAGE;
import static com.antest1.kcanotify.KcaUtils.doVibrate;
import static com.antest1.kcanotify.KcaUtils.getBooleanPreferences;
import static com.antest1.kcanotify.KcaUtils.getOrientationPrefix;
import static com.antest1.kcanotify.KcaUtils.getStringFromException;
import static com.antest1.kcanotify.KcaUtils.getStringPreferences;
import static com.antest1.kcanotify.KcaUtils.getWindowLayoutType;
                    mView.setVisibility(View.VISIBLE);
                    recentVisibility = View.VISIBLE;
                }
            }
            if (intent.getAction().equals(RETURN_FAIRY_ACTION) || intent.getAction().equals(REMOVE_FAIRY_ACTION)) {
                if (sHandler != null) {
                    Bundle bundle = new Bundle();
                    bundle.putString("url", KCA_API_FAIRY_CHECKED);
                    bundle.putString("data", "");
                    Message sMsg = sHandler.obtainMessage();
                    sMsg.setData(bundle);
                    sHandler.sendMessage(sMsg);
                    hiddenByUser = false;
                }
            }
            if (intent.getAction().equals(FAIRY_INVISIBLE)) {
                if (mView != null) {
                    mView.setVisibility(View.GONE);
                    recentVisibility = View.GONE;
                }
            }
            if (intent.getAction().equals(FAIRY_SIZE_CHANGE)) {
                setFairySize();
            }
            if (intent.getAction().equals(FAIRY_FORECHECK_ON)) {
                runForegroundCheck();
            }
            if (intent.getAction().equals(FAIRY_FORECHECK_OFF)) {
                stopForegroundCheck();
            }
            if (intent.getAction().equals(FAIRY_CHANGE)) {
                String fairyIdValue = getStringPreferences(getApplicationContext(), PREF_FAIRY_ICON);
                viewBitmapId = "noti_icon_".concat(fairyIdValue);
                setFairyImage();
                if (icon_info.size() > 0) {
                    int fairy_id = Integer.parseInt(fairyIdValue);
                    int rev_internal = 0;
                    if (fairy_id < icon_info.size()) {
                        JsonObject fairy_info = icon_info.get(fairy_id).getAsJsonObject();
                        rev_internal = fairy_info.has("rev") ? fairy_info.get("rev").getAsInt() : 0;
                    }
                    int rev_setting = Integer.parseInt(getStringPreferences(getApplicationContext(), PREF_FAIRY_REV));
                    if ((rev_internal + rev_setting) % 2 == 1) {
                        viewbutton.setScaleX(-1.0f);
                    } else {
                        viewbutton.setScaleX(1.0f);
                    }
                }
            }
            if (intent.getAction().equals(RESET_FAIRY_STATUS_ACTION)) {
                taiha_status = false;
                setFairyImage();
            }
            if (intent.getAction().equals(ACTIVATE_BATTLEVIEW_ACTION)) {
                Intent qintent = new Intent(getBaseContext(), KcaFleetViewService.class);
                qintent.setAction(KcaFleetViewService.CLOSE_FLEETVIEW_ACTION);
                startService(qintent);
                battleviewEnabled = true;
            }
            if (intent.getAction().equals(DEACTIVATE_BATTLEVIEW_ACTION)) {
                taiha_status = false;
                battleviewEnabled = false;
            }
            if (intent.getAction().equals(ACTIVATE_QUESTVIEW_ACTION)) {
                Intent qintent = new Intent(getBaseContext(), KcaFleetViewService.class);
                qintent.setAction(KcaFleetViewService.CLOSE_FLEETVIEW_ACTION);
                startService(qintent);
                questviewEnabled = true;
            }
            if (intent.getAction().equals(DEACTIVATE_QUESTVIEW_ACTION)) {
                questviewEnabled = false;
            }

        }
        return super.onStartCommand(intent, flags, startId);
    }

    private boolean isBattleViewEnabled() {
        return getBooleanPreferences(getApplicationContext(), PREF_KCA_BATTLEVIEW_USE);
    }

    private void setFairySize() {
        int fairy_size_id = Integer.parseInt(getStringPreferences(getApplicationContext(), PREF_FAIRY_SIZE));
        int size_dp;
        switch (fairy_size_id) {
            case 1:
                size_dp = getResources().getDimensionPixelSize(R.dimen.button_size_xsmall);
                break;
            case 2:
                size_dp = getResources().getDimensionPixelSize(R.dimen.button_size_small);
                break;
            case 3:
                size_dp = getResources().getDimensionPixelSize(R.dimen.button_size_normal);
                break;
            case 4:
                size_dp = getResources().getDimensionPixelSize(R.dimen.button_size_large);
                break;
            case 5:
                size_dp = getResources().getDimensionPixelSize(R.dimen.button_size_xlarge);
                break;
            default:
                size_dp = getResources().getDimensionPixelSize(R.dimen.button_size_normal);
                break;
        }

        if (viewbutton != null) {
            ViewGroup.LayoutParams params = viewbutton.getLayoutParams();
            params.width = size_dp;
            params.height = size_dp;
            viewbutton.setLayoutParams(params);
        }
    }

    private final int margin = 14;
    private final int halfMargin = margin / 2;
    private final int glowRadius = 20;
    private final int glowColor = Color.rgb(0, 192, 255);
    private final int glowColor2 = Color.rgb(230, 249, 255);

    private void setFairyImage() {
        boolean glow_available = fairy_glow_on && getBooleanPreferences(getApplicationContext(), PREF_KCA_NOTI_QUEST_FAIRY_GLOW);