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;
        }
        Paint color_paint = new Paint();
        if (taiha_status) {
            color_paint.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(getApplicationContext(),
                    R.color.colorHeavyDmgStateWarn), PorterDuff.Mode.MULTIPLY));
        } else if (glow_available) {
            color_paint.setColorFilter(new PorterDuffColorFilter(glowColor2, PorterDuff.Mode.MULTIPLY));
        }
        canvas.drawBitmap(src, halfMargin, halfMargin, color_paint);
        viewbutton.setImageBitmap(bmp);
    }

    @Override
    public void onDestroy() {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(battleinfo_receiver);
        LocalBroadcastManager.getInstance(this).unregisterReceiver(battlenode_receiver);
        LocalBroadcastManager.getInstance(this).unregisterReceiver(battlehdmg_receiver);
        LocalBroadcastManager.getInstance(this).unregisterReceiver(questcmpl_receiver);
        if (gotoFgReceiver != null) unregisterReceiver(gotoFgReceiver);
        if (checkForegroundScheduler != null && !checkForegroundScheduler.isShutdown()) {
            checkForegroundScheduler.shutdown();
        }
        if (mManager != null) mManager.removeView(mView);
        super.onDestroy();
    }

    private float mTouchX, mTouchY;
    private int mViewX, mViewY;

    private View.OnTouchListener mViewTouchListener = new View.OnTouchListener() {
        private static final int MAX_CLICK_DURATION = 200;
        private static final int LONG_CLICK_DURATION = 800;

        private long startClickTime;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int id = v.getId();
            if (id == viewbutton.getId()) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mTouchX = event.getRawX();
                        mTouchY = event.getRawY();
                        mViewX = mParams.x;
                        mViewY = mParams.y;
                        Log.e("KCA", KcaUtils.format("mView: %d %d", mViewX, mViewY));
                        startClickTime = Calendar.getInstance().getTimeInMillis();
                        mHandler.postDelayed(mRunnable, LONG_CLICK_DURATION);
                        break;

                    case MotionEvent.ACTION_UP:
                        Log.e("KCA", "Callback Canceled");
                        mHandler.removeCallbacks(mRunnable);
                        long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
                        if (clickDuration < MAX_CLICK_DURATION) {
                            clickcount += 1;
                            if (battleviewEnabled && isBattleViewEnabled()) {
                                Intent qintent = new Intent(getBaseContext(), KcaBattleViewService.class);
                                qintent.setAction(KcaBattleViewService.SHOW_BATTLEVIEW_ACTION);
                                startService(qintent);
                            } else if (questviewEnabled) {
                                Intent qintent = new Intent(getBaseContext(), KcaQuestViewService.class);
                                qintent.setAction(KcaQuestViewService.SHOW_QUESTVIEW_ACTION_NEW);
                                startService(qintent);
                            } else {
                                Intent qintent = new Intent(getBaseContext(), KcaFleetViewService.class);
                                qintent.setAction(KcaFleetViewService.SHOW_FLEETVIEW_ACTION);
                                startService(qintent);
                            }
                        }

                        int[] locations = new int[2];
                        mView.getLocationOnScreen(locations);
                        int xx = locations[0];
                        int yy = locations[1];
                        Log.e("KCA", KcaUtils.format("Coord: %d %d", xx, yy));
                        if (mParams.x < 0) mParams.x = 0;
                        else if (mParams.x > screenWidth - buttonWidth / 2)
                            mParams.x = screenWidth - buttonWidth / 2;
                        if (mParams.y < 0) mParams.y = 0;
                        else if (mParams.y > screenHeight - buttonHeight / 2)
                            mParams.y = screenHeight - buttonHeight / 2;

                        JsonObject locdata = dbHelper.getJsonObjectValue(DB_KEY_FAIRYLOC);
                        String ori_prefix = getOrientationPrefix(getResources().getConfiguration().orientation);
                        if (locdata != null && locdata.toString().length() > 0) {
                            locdata.addProperty(ori_prefix.concat("x"), mParams.x);
                            locdata.addProperty(ori_prefix.concat("y"), mParams.y);
                        } else {
                            locdata = new JsonObject();
                        }
                        dbHelper.putValue(DB_KEY_FAIRYLOC, locdata.toString());
                        break;

                    case MotionEvent.ACTION_MOVE:
                        int x = (int) (event.getRawX() - mTouchX);
                        int y = (int) (event.getRawY() - mTouchY);

                        mParams.x = mViewX + x;
                        mParams.y = mViewY + y;
                        if (mParams.x < 0) mParams.x = 0;
                        else if (mParams.x > screenWidth - buttonWidth / 2) mParams.x = screenWidth - buttonWidth / 2;
                        if (mParams.y < 0) mParams.y = 0;
                        else if (mParams.y > screenHeight - buttonHeight / 2) mParams.y = screenHeight - buttonHeight / 2;
                        mManager.updateViewLayout(mView, mParams);
                        if (Math.abs(x) > 20 || Math.abs(y) > 20) {
                            Log.e("KCA", "Callback Canceled");
                            mHandler.removeCallbacks(mRunnable);
                        }
                        break;
                }
            }
            return true;
        }
    };


    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            if (getBooleanPreferences(getApplicationContext(), PREF_FAIRY_NOTI_LONGCLICK)) {