/*
 * Copyright (C) 2016 The Android Open Source Project
 * Copyright (C) 2017 Christopher Blay <chris.b.blay@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.covertbagel.neko;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.job.JobInfo;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.app.job.JobService;
import android.content.ComponentName;
import android.content.Context;

import java.util.List;
import java.util.Random;

public class NekoService extends JobService {

    private static final int JOB_ID = 42;
    private static final int CAT_NOTIFICATION = 1;
    private static final float CAT_CAPTURE_PROB = 1.0f; // generous
    private static final long SECONDS = 1000;
    private static final long MINUTES = 60 * SECONDS;
    private static final long INTERVAL_FLEX = 5 * MINUTES;
    private static final float INTERVAL_JITTER_FRAC = 0.25f;

    @Override
    public boolean onStartJob(JobParameters params) {
        final PrefState prefs = new PrefState(this);
        final int food = prefs.getFoodState();
        if (food != 0) {
            prefs.setFoodState(0); // nom
            final Random rng = new Random();
            if (rng.nextFloat() <= CAT_CAPTURE_PROB) {
                final Cat cat;
                final List<Cat> cats = prefs.getCats();
                final int[] probs = getResources().getIntArray(R.array.food_new_cat_prob);
                final float new_cat_prob = (float)((food < probs.length) ? probs[food] : 50) / 100f;
                if (cats.size() == 0 || rng.nextFloat() <= new_cat_prob) {
                    cat = Cat.create(this);
                    prefs.addCat(cat);
                } else {
                    cat = cats.get(rng.nextInt(cats.size()));
                }
                final Notification.Builder builder = cat.buildNotification(this);
                getSystemService(NotificationManager.class)
                        .notify(CAT_NOTIFICATION, builder.build());
            }
        }
        cancelJob(this);
        return false;
    }

    @Override
    public boolean onStopJob(JobParameters jobParameters) {
        return false;
    }

    public static void registerJob(Context context, long intervalMinutes) {
        final JobScheduler jss = context.getSystemService(JobScheduler.class);
        jss.cancel(JOB_ID);
        long interval = intervalMinutes * MINUTES;
        final long jitter = (long)(INTERVAL_JITTER_FRAC * interval);
        interval += (long)(Math.random() * (2 * jitter)) - jitter;
        final JobInfo jobInfo =
                new JobInfo.Builder(JOB_ID, new ComponentName(context, NekoService.class))
                        .setPeriodic(interval, INTERVAL_FLEX).build();
        jss.schedule(jobInfo);
    }

    public static void cancelJob(Context context) {
        context.getSystemService(JobScheduler.class).cancel(JOB_ID);
    }
}

@@ -1182,76 +1182,8 @@
  1;%0A
-    private static final float CAT_CAPTURE_PROB = 1.0f; // generous%0A
     
@@ -1224,16 +1224,16 @@
 = 1000;%0A
+
     priv
@@ -1683,67 +1683,8 @@
 ();%0A
-            if (rng.nextFloat() %3C= CAT_CAPTURE_PROB) %7B%0A    
     
@@ -1706,20 +1706,16 @@
 at cat;%0A
-    
         
@@ -1754,28 +1754,24 @@
 .getCats();%0A
-    
             
@@ -1845,20 +1845,16 @@
 _prob);%0A
-    
         
@@ -1950,28 +1950,24 @@
             
-    
 if (cats.siz
@@ -2025,28 +2025,24 @@
             
-    
 cat = Cat.cr
@@ -2065,28 +2065,24 @@
             
- 
-   
 prefs.addCat
@@ -2100,20 +2100,16 @@
         
-    
 %7D else %7B
@@ -2121,28 +2121,24 @@
             
-  
-  
 cat = cats.g
@@ -2175,33 +2175,26 @@
             
-    %7D%0A   
+%7D%0A
             
@@ -2189,25 +2189,24 @@
             
- 
 final Notifi
@@ -2271,20 +2271,16 @@
         
-    
 getSyste
@@ -2318,33 +2318,8 @@
 ass)
-%0A                        
 .not
@@ -2354,38 +2354,24 @@
 r.build());%0A
-            %7D%0A
         %7D%0A  

Simplify some more "dead code" in NekoService