package com.aragaer.jtt.core;

import android.app.*;
import android.content.*;
import android.util.Log;

public class Clockwork extends IntentService {
    public static final String ACTION_JTT_TICK = "com.aragaer.jtt.action.TICK";
    private static final Intent TickAction = new Intent(ACTION_JTT_TICK);
    private static final int INTENT_FLAGS = PendingIntent.FLAG_UPDATE_CURRENT;

    public Clockwork() {
	super("CLOCKWORK");
    }

    public static void schedule(final Context context) {
	long now = System.currentTimeMillis();
	ThreeIntervals intervals = Calculator.getSurroundingTransitions(context, now);
	Interval currentInterval = intervals.getMiddleInterval();
	long tickLength = Math.round(currentInterval.getLength()/Hour.TICKS_PER_INTERVAL);

	Intent TickActionInternal = new Intent(context, Clockwork.class)
	    .putExtra("intervals", intervals);

	AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
	am.setRepeating(AlarmManager.RTC, currentInterval.start, tickLength,
			PendingIntent.getService(context, 0, TickActionInternal, INTENT_FLAGS));
    }

    public static void unschedule(final Context context) {
	AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
	am.cancel(PendingIntent.getService(context, 0, new Intent(context, Clockwork.class), 0));
    }

    @Override
    protected void onHandleIntent(Intent intent) {
	ThreeIntervals intervals = (ThreeIntervals) intent.getSerializableExtra("intervals");
	long now = System.currentTimeMillis();
	Hour hour = Hour.fromInterval(intervals.getMiddleInterval(), now, null);

	if (intervals.surrounds(now)) {
	    TickAction.putExtra("intervals", intervals)
		.putExtra("hour", hour.num)
		.putExtra("jtt", hour.wrapped);
	    sendStickyBroadcast(TickAction);
	} else
	    try {
		schedule(this);
	    } catch (IllegalStateException e) {
		Log.i("JTT CLOCKWORK", "Transition passed while service is not running, ignore");
	    }

	stopSelf();
    }
}

@@ -1,8 +1,107 @@
+// -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; -*-%0A// vim: et ts=4 sts=4 sw=4 syntax=java%0A
 package 
@@ -401,87 +401,8 @@
 CK);
-%0A    private static final int INTENT_FLAGS = PendingIntent.FLAG_UPDATE_CURRENT;
 %0A%0A  
@@ -424,17 +424,24 @@
 ork() %7B%0A
-%09
+        
 super(%22C
@@ -516,17 +516,24 @@
 text) %7B%0A
-%09
+        
 long now
@@ -563,17 +563,24 @@
 llis();%0A
-%09
+        
 ThreeInt
@@ -650,17 +650,24 @@
 , now);%0A
-%09
+        
 Interval
@@ -716,17 +716,24 @@
 rval();%0A
-%09
+        
 long tic
@@ -808,17 +808,24 @@
 RVAL);%0A%0A
-%09
+        
 Intent T
@@ -881,17 +881,24 @@
 .class)%0A
-%09
+        
     .put
@@ -929,17 +929,24 @@
 vals);%0A%0A
-%09
+        
 AlarmMan
@@ -1019,17 +1019,24 @@
 RVICE);%0A
-%09
+        
 am.setRe
@@ -1100,11 +1100,32 @@
 th,%0A
-%09%09%09
+                        
 Pend
@@ -1181,20 +1181,41 @@
 al, 
-INTENT_FLAGS
+PendingIntent.FLAG_UPDATE_CURRENT
 ));%0A
@@ -1280,17 +1280,24 @@
 text) %7B%0A
-%09
+        
 AlarmMan
@@ -1370,17 +1370,24 @@
 RVICE);%0A
-%09
+        
 am.cance
@@ -1540,17 +1540,24 @@
 tent) %7B%0A
-%09
+        
 ThreeInt
@@ -1634,17 +1634,24 @@
 vals%22);%0A
-%09
+        
 long now
@@ -1681,17 +1681,24 @@
 llis();%0A
-%09
+        
 Hour hou
@@ -1763,17 +1763,24 @@
 null);%0A%0A
-%09
+        
 if (inte
@@ -1803,17 +1803,24 @@
 now)) %7B%0A
-%09
+        
     Tick
@@ -1859,18 +1859,32 @@
 ervals)%0A
-%09%09
+                
 .putExtr
@@ -1903,18 +1903,32 @@
 ur.num)%0A
-%09%09
+                
 .putExtr
@@ -1951,17 +1951,24 @@
 apped);%0A
-%09
+        
     send
@@ -2000,29 +2000,57 @@
 n);%0A
-%09%7D else%0A%09    try %7B%0A%09%09
+        %7D else%0A            try %7B%0A                
 sche
@@ -2061,21 +2061,28 @@
 (this);%0A
-%09
+      
     
+  
 %7D catch 
@@ -2113,10 +2113,24 @@
 ) %7B%0A
-%09%09
+                
 Log.
@@ -2211,17 +2211,31 @@
 %22);%0A
-%09    %7D%0A%0A%09
+            %7D%0A%0A        
 stop

Fix lint error and formatting