Android Open Source - AmbientAlarmClock Clock Work Service






From Project

Back to project page AmbientAlarmClock.

License

The source code is released under:

<p align="center"><a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/"> <img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-sa/4.0...

If you think the Android project AmbientAlarmClock listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package de.lukeslog.alarmclock.main;
/*  w w w .j a va  2  s .  c  o  m*/
import android.app.AlarmManager;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;
import android.preference.PreferenceManager;

import org.joda.time.DateTime;
import org.joda.time.Seconds;

import java.util.ArrayList;
import java.util.Calendar;

import de.lukeslog.alarmclock.actions.ActionManager;
import de.lukeslog.alarmclock.ambientalarm.AmbientAlarmManager;
import de.lukeslog.alarmclock.support.AlarmClockConstants;
import de.lukeslog.alarmclock.support.Logger;

/**
 * Created by lukas on 29.03.14.
 */
public class ClockWorkService  extends IntentService
{
    public static final String PREFS_NAME = AlarmClockConstants.PREFS_NAME;
    public static String TAG = AlarmClockConstants.TAG;

    public static SharedPreferences settings;

    private static int currentMinute=-1;
    private static DateTime lasttime;
    private static Context context=null;
    private static boolean running = true;

    private static ArrayList<Timable> notifications = new ArrayList<Timable>();

    public ClockWorkService()
    {
        super("ClockWorkService");
    }

    public static Context getClockworkContext()
    {
        running=true;
        if(context!=null)
        {
            return context;
        }
        else
        {
            return null;
        }
    }

    public static void registerForNotofication(Timable x)
    {
        notifications.add(x);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        super.onStartCommand(intent, flags, startId);

        return START_NOT_STICKY;
    }

    @Override
    public void onCreate()
    {
        running=true;
        super.onCreate();
        context=this;
        //Log.d(TAG, "ClockWorkService onCreate()");
        settings = PreferenceManager.getDefaultSharedPreferences(this);
    }

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

    @Override
    protected void onHandleIntent(Intent intent)
    {
        if(running)
        {
            Logger.d(TAG, ".");
            DateTime currentTime = new DateTime();
            if (lasttime == null)
            {
                lasttime = currentTime;
            }
            if (newSecondHasStarted())
            {
                //sometimes the handler can skip one Minute, this would mean missing the
                //alarm if we only called with the current time so we have a while loop calling for all times
                //since the last time
                currentTime = currentTime.withMillisOfSecond(0);
                currentTime = currentTime.withSecondOfMinute(0);
                lasttime = lasttime.withMillisOfSecond(0);
                lasttime = lasttime.withSecondOfMinute(0);
                while (lasttime.isBefore(currentTime))
                {
                    lasttime = lasttime.plusMinutes(1);
                    //since lasttime is a global object which may change during execution on the other thread
                    final DateTime time = new DateTime(lasttime);

                    Handler mainHandler = new Handler(context.getMainLooper());

                    // This is your code
                    mainHandler.post(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            tick(time);
                        }
                    });
                }
                lasttime = currentTime;
            }
            scheduleNext();
        }
    }

    private void scheduleNext()
    {
        Calendar cal = Calendar.getInstance();
        DateTime now = new DateTime();
        DateTime inaminute = new DateTime();
        inaminute = inaminute.plusMinutes(1);
        inaminute = inaminute.withSecondOfMinute(0);
        int secondstoadd = Seconds.secondsBetween(now, inaminute).getSeconds();
        Logger.d(TAG, ""+secondstoadd);
        cal.add(Calendar.SECOND, secondstoadd);
        Intent intent = new Intent(this, ClockWorkService.class);
        PendingIntent pendingIntent = PendingIntent.getService(this, AlarmClockConstants.TICK, intent, 0);
        AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
    }

    private void tick(DateTime time)
    {
        Logger.d(TAG + "_time", ". "+time.getHourOfDay()+":"+time.getMinuteOfHour()+":"+time.getSecondOfMinute()+":"+time.getMillisOfSecond());
        AmbientAlarmManager.notifyActiveAlerts(time);
        ActionManager.notifyOfCurrentTime(time);
    }

    /**
     *
     * @return returns true if a new second has started since the last time this method has been called
     */
    private boolean newSecondHasStarted()
    {
        DateTime currentTime = new DateTime();
        if(currentTime.getMinuteOfHour()-currentMinute>1)
        {
            Logger.e(TAG, "WE JUST SKIPPED A MINUTE! WE JUST SKIPPED A MINUTE! THIS IS WORST THING. EVER.");
        }

        if(currentMinute!=currentTime.getMinuteOfHour())
        {
            currentMinute=currentTime.getMinuteOfHour();
            return true;
        }
        return false;
    }

    public static void stopService()
    {
        running=false;
    }

    public static Context getContext()
    {
        if(context!=null)
        {
            return context;
        }
        throw new NullPointerException();
    }
}




Java Source Code List

de.lukeslog.alarmclock.MediaPlayer.MediaPlayerService.java
de.lukeslog.alarmclock.actions.ActionActivity.java
de.lukeslog.alarmclock.actions.ActionConfigBundle.java
de.lukeslog.alarmclock.actions.ActionManager.java
de.lukeslog.alarmclock.actions.AmbientAction.java
de.lukeslog.alarmclock.actions.CountdownActionConfigurationFragment.java
de.lukeslog.alarmclock.actions.CountdownAction.java
de.lukeslog.alarmclock.actions.EZControlHeatActionConfigurationFragment.java
de.lukeslog.alarmclock.actions.EZControlHeatAction.java
de.lukeslog.alarmclock.actions.EZControlPlugActionConfigurationFragment.java
de.lukeslog.alarmclock.actions.EZControlPlugAction.java
de.lukeslog.alarmclock.actions.MusicActionConfigurationFragment.java
de.lukeslog.alarmclock.actions.MusicAction.java
de.lukeslog.alarmclock.actions.PhilipsHueActionConfigurationFragment.java
de.lukeslog.alarmclock.actions.PhilipsHueAction.java
de.lukeslog.alarmclock.actions.SendMailActionFragment.java
de.lukeslog.alarmclock.actions.SendMailAction.java
de.lukeslog.alarmclock.actions.WebsiteActionConfigurationFragment.java
de.lukeslog.alarmclock.actions.WebsiteAction.java
de.lukeslog.alarmclock.ambientService.dropbox.DropBox.java
de.lukeslog.alarmclock.ambientService.lastfm.Scrobbler.java
de.lukeslog.alarmclock.ambientService.mail.BackgroundMail.java
de.lukeslog.alarmclock.ambientService.mail.Mail.java
de.lukeslog.alarmclock.ambientalarm.AmbientAlarmManager.java
de.lukeslog.alarmclock.ambientalarm.AmbientAlarm.java
de.lukeslog.alarmclock.datatabse.AmbientAlarmDatabase.java
de.lukeslog.alarmclock.datatabse.DatabaseConstants.java
de.lukeslog.alarmclock.main.ClockWorkService.java
de.lukeslog.alarmclock.main.NotificationManagement.java
de.lukeslog.alarmclock.main.Timable.java
de.lukeslog.alarmclock.startup.NotificationService.java
de.lukeslog.alarmclock.startup.ServiceStarter.java
de.lukeslog.alarmclock.startup.StartUp.java
de.lukeslog.alarmclock.support.AlarmClockConstants.java
de.lukeslog.alarmclock.support.AlarmState.java
de.lukeslog.alarmclock.support.Day.java
de.lukeslog.alarmclock.support.Logger.java
de.lukeslog.alarmclock.support.Radiostations.java
de.lukeslog.alarmclock.support.Settings.java
de.lukeslog.alarmclock.support.UISupport.java
de.lukeslog.alarmclock.teardown.Teardown.java
de.lukeslog.alarmclock.ui.ActionListAdapter.java
de.lukeslog.alarmclock.ui.AlarmClockMainActivity.java
de.lukeslog.alarmclock.ui.AlarmListAdapter.java
de.lukeslog.alarmclock.ui.AmbientAlarmActivity.java
de.lukeslog.alarmclock.ui.AmbientAlarmConfigurationActivity.java
de.lukeslog.alarmclock.ui.NewAmbientAction.java
de.lukeslog.alarmclock.ui.NewSettingsFragment.java
de.lukeslog.alarmclock.ui.SettingsActivity.java
org.json2.JSONArray.java
org.json2.JSONException.java
org.json2.JSONObject.java
org.json2.JSONString.java
org.json2.JSONStringer.java
org.json2.JSONTokener.java
org.json2.JSONWriter.java