<commit_before>package com.brianco.materialcards;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.IBinder;
import android.os.Vibrator;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import android.widget.ImageView;

import com.brianco.materialcards.model.PaletteColor;

public class HoverService extends Service {

    public static final String COLOR_VALUE_EXTRA = "COLOR_VALUE_EXTRA";
    public static final String COLOR_NAME_EXTRA = "COLOR_NAME_EXTRA";
    public static final String COLOR_BASE_NAME_EXTRA = "COLOR_BASE_NAME_EXTRA";
    public static final String COLOR_PARENT_NAME_EXTRA = "COLOR_PARENT_NAME_EXTRA";
    public static final String COLOR_PARENT_VALUE_EXTRA = "COLOR_PARENT_VALUE_EXTRA";

    private static final String COPY_FILTER = "com.brianco.materialcards.HoverService.COPY_FILTER";
    private static final String STOP_FILTER = "com.brianco.materialcards.HoverService.STOP_FILTER";
    private static final int NOTIFICATION_ID = 1;
    private static final int START_REQUEST_CODE = 2;
    private static final int COPY_REQUEST_CODE = 3;
    private static final int STOP_REQUEST_CODE = 4;
    private static final int LONG_PRESS_MILLIS = ViewConfiguration.getLongPressTimeout();

    private WindowManager.LayoutParams params;
    private WindowManager windowManager;
    private View hoverView;
    private Drawable drawable;

    private boolean isPressed;
    private final Handler handler = new Handler();
    private final Runnable runnable = new Runnable() {
        public void run() {
            checkLongPress();
        }
    };

    private final BroadcastReceiver copyReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            PaletteColor.copyColorToClipboard(context,
                    intent.getStringExtra(COLOR_PARENT_NAME_EXTRA),
                    intent.getStringExtra(COLOR_BASE_NAME_EXTRA),
                    intent.getStringExtra(COLOR_NAME_EXTRA));
        }
    };

    private final BroadcastReceiver stopReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            context.stopService(new Intent(context, HoverService.class));
        }
    };

    private void checkLongPress() {
        if (isPressed) {
            final Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            vibrator.vibrate(getResources().getInteger(R.integer.vibrate_long_press));
            stopSelf();
        } else {
            throw new RuntimeException("The handler should remove its callback" +
                    " when the view is not being pressed anymore.");
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(final Intent intent, final int flags, final int startId) {
        if (intent == null) {
            return START_STICKY;
        }
        initHoverLocation();
        final int color = intent.getIntExtra(COLOR_VALUE_EXTRA, 0);
        final String colorName = intent.getStringExtra(COLOR_NAME_EXTRA);
        final String colorBaseName = intent.getStringExtra(COLOR_BASE_NAME_EXTRA);
        final String colorParentName = intent.getStringExtra(COLOR_PARENT_NAME_EXTRA);
        final int parentColor = intent.getIntExtra(COLOR_PARENT_VALUE_EXTRA, 0);
        drawable.setColorFilter(color, PorterDuff.Mode.SRC);
        hoverView.setBackground(drawable);
        final Intent startIntent = new Intent(this, PaletteActivity.class);
        startIntent.setAction(PaletteActivity.ACTION_START_COLOR);
        startIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startIntent.putExtra(PaletteActivity.COLOR_SECTION_VALUE_EXTRA, parentColor);
        final PendingIntent startPendingIntent = PendingIntent.getActivity(this, START_REQUEST_CODE,
                startIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        final Intent copyIntent = new Intent(COPY_FILTER);
        copyIntent.putExtra(COLOR_NAME_EXTRA, colorName);
        copyIntent.putExtra(COLOR_BASE_NAME_EXTRA, colorBaseName);
        copyIntent.putExtra(COLOR_PARENT_NAME_EXTRA, colorParentName);
        final PendingIntent copyPendingIntent = PendingIntent.getBroadcast(this, COPY_REQUEST_CODE,
                copyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        final Intent stopIntent = new Intent(STOP_FILTER);
        final PendingIntent stopPendingIntent = PendingIntent.getBroadcast(this, STOP_REQUEST_CODE,
                stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        final Notification.Builder builder = new Notification.Builder(this)
                .setSmallIcon(R.drawable.hover)
                .setLargeIcon(drawableToBitmap(drawable))
                .setContentTitle(getString(R.string.app_name))
                .setContentText(colorName)
                .setLights(color, 500, 500)
                .setPriority(Notification.PRIORITY_MIN)
                .addAction(R.drawable.ic_action_copy_light,
                        getString(R.string.copy_color_description), copyPendingIntent)
                .addAction(R.drawable.ic_action_close, getString(R.string.close), stopPendingIntent)
                .setContentIntent(startPendingIntent);
        final Notification notification = builder.build();
        notification.flags = Notification.FLAG_NO_CLEAR;
        startForeground(NOTIFICATION_ID, notification);
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        final float MOVE_THRESHOLD = getResources().getDimension(R.dimen.hover_move_threshold);

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        drawable = getResources().getDrawable(R.drawable.hover);

        hoverView = new ImageView(this);

        params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,<commit_msg>Use NotificationCompat for ICS compat<commit_after>package com.brianco.materialcards;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.IBinder;
import android.os.Vibrator;
import android.support.v4.app.NotificationCompat;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import android.widget.ImageView;

import com.brianco.materialcards.model.PaletteColor;

public class HoverService extends Service {

    public static final String COLOR_VALUE_EXTRA = "COLOR_VALUE_EXTRA";
    public static final String COLOR_NAME_EXTRA = "COLOR_NAME_EXTRA";
    public static final String COLOR_BASE_NAME_EXTRA = "COLOR_BASE_NAME_EXTRA";
    public static final String COLOR_PARENT_NAME_EXTRA = "COLOR_PARENT_NAME_EXTRA";
    public static final String COLOR_PARENT_VALUE_EXTRA = "COLOR_PARENT_VALUE_EXTRA";

    private static final String COPY_FILTER = "com.brianco.materialcards.HoverService.COPY_FILTER";
    private static final String STOP_FILTER = "com.brianco.materialcards.HoverService.STOP_FILTER";
    private static final int NOTIFICATION_ID = 1;
    private static final int START_REQUEST_CODE = 2;
    private static final int COPY_REQUEST_CODE = 3;
    private static final int STOP_REQUEST_CODE = 4;
    private static final int LONG_PRESS_MILLIS = ViewConfiguration.getLongPressTimeout();

    private WindowManager.LayoutParams params;
    private WindowManager windowManager;
    private View hoverView;
    private Drawable drawable;

    private boolean isPressed;
    private final Handler handler = new Handler();
    private final Runnable runnable = new Runnable() {
        public void run() {
            checkLongPress();
        }
    };

    private final BroadcastReceiver copyReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            PaletteColor.copyColorToClipboard(context,
                    intent.getStringExtra(COLOR_PARENT_NAME_EXTRA),
                    intent.getStringExtra(COLOR_BASE_NAME_EXTRA),
                    intent.getStringExtra(COLOR_NAME_EXTRA));
        }
    };

    private final BroadcastReceiver stopReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            context.stopService(new Intent(context, HoverService.class));
        }
    };

    private void checkLongPress() {
        if (isPressed) {
            final Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            vibrator.vibrate(getResources().getInteger(R.integer.vibrate_long_press));
            stopSelf();
        } else {
            throw new RuntimeException("The handler should remove its callback" +
                    " when the view is not being pressed anymore.");
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(final Intent intent, final int flags, final int startId) {
        if (intent == null) {
            return START_STICKY;
        }
        initHoverLocation();
        final int color = intent.getIntExtra(COLOR_VALUE_EXTRA, 0);
        final String colorName = intent.getStringExtra(COLOR_NAME_EXTRA);
        final String colorBaseName = intent.getStringExtra(COLOR_BASE_NAME_EXTRA);
        final String colorParentName = intent.getStringExtra(COLOR_PARENT_NAME_EXTRA);
        final int parentColor = intent.getIntExtra(COLOR_PARENT_VALUE_EXTRA, 0);
        drawable.setColorFilter(color, PorterDuff.Mode.SRC);
        hoverView.setBackground(drawable);
        final Intent startIntent = new Intent(this, PaletteActivity.class);
        startIntent.setAction(PaletteActivity.ACTION_START_COLOR);
        startIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startIntent.putExtra(PaletteActivity.COLOR_SECTION_VALUE_EXTRA, parentColor);
        final PendingIntent startPendingIntent = PendingIntent.getActivity(this, START_REQUEST_CODE,
                startIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        final Intent copyIntent = new Intent(COPY_FILTER);
        copyIntent.putExtra(COLOR_NAME_EXTRA, colorName);
        copyIntent.putExtra(COLOR_BASE_NAME_EXTRA, colorBaseName);
        copyIntent.putExtra(COLOR_PARENT_NAME_EXTRA, colorParentName);
        final PendingIntent copyPendingIntent = PendingIntent.getBroadcast(this, COPY_REQUEST_CODE,
                copyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        final Intent stopIntent = new Intent(STOP_FILTER);
        final PendingIntent stopPendingIntent = PendingIntent.getBroadcast(this, STOP_REQUEST_CODE,
                stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        final NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.hover)
                .setLargeIcon(drawableToBitmap(drawable))
                .setContentTitle(getString(R.string.app_name))
                .setContentText(colorName)
                .setLights(color, 500, 500)
                .setPriority(Notification.PRIORITY_MIN)
                .addAction(R.drawable.ic_action_copy_light,
                        getString(R.string.copy_color_description), copyPendingIntent)
                .addAction(R.drawable.ic_action_close, getString(R.string.close), stopPendingIntent)
                .setContentIntent(startPendingIntent);
        final Notification notification = builder.build();
        notification.flags = Notification.FLAG_NO_CLEAR;
        startForeground(NOTIFICATION_ID, notification);
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        final float MOVE_THRESHOLD = getResources().getDimension(R.dimen.hover_move_threshold);

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        drawable = getResources().getDrawable(R.drawable.hover);

        hoverView = new ImageView(this);

        params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
{'fraction_non_alphanumeric': 0.07479203802733214, 'fraction_numerical': 0.0018568033273915626, 'mean_word_length': 3.4104159842777597, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3313862', 'n_tokens_mistral': 3714, 'n_tokens_neox': 3570, 'n_words': 749}