Android Open Source - AmbientAlarmClock Ambient Alarm Manager






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.ambientalarm;
/*from w  ww . j av a  2  s.c om*/
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Environment;

import org.joda.time.DateTime;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import de.lukeslog.alarmclock.actions.AmbientAction;
import de.lukeslog.alarmclock.datatabse.AmbientAlarmDatabase;
import de.lukeslog.alarmclock.main.ClockWorkService;
import de.lukeslog.alarmclock.main.NotificationManagement;
import de.lukeslog.alarmclock.startup.NotificationService;
import de.lukeslog.alarmclock.support.AlarmClockConstants;
import de.lukeslog.alarmclock.support.AlarmState;
import de.lukeslog.alarmclock.support.Logger;
import de.lukeslog.alarmclock.ui.AmbientAlarmActivity;

/**
 * Created by lukas on 31.03.14.
 *
 * Manages the set of alarms generated by the user, also manages access to the database.
 */
public class AmbientAlarmManager
{

    private static ArrayList<AmbientAlarm> registeredAlarms;

    public static final String PREFS_NAME = AlarmClockConstants.PREFS_NAME;
    public static String TAG = AlarmClockConstants.TAG;

    /**
     *
     * This method informs all currently active alarms of the current time, this time may then
     * be transformed into actions by these alarms
     *
     * @param currentTime to inform all alerts of an identical time the current Time is provided by
     *                    the calling method
     */
    public static void notifyActiveAlerts(DateTime currentTime)
    {
        if(registeredAlarms!=null)
        {
            //Log.d(TAG, "--tick--" + registeredAlarms.size());
            for (AmbientAlarm alarm : registeredAlarms)
            {
                if (alarm.isActive())
                {
                    alarm.notifyOfCurrentTime(currentTime);
                }
            }
        }
        else
        {
            updateListFromDataBase();
        }
    }

    /**
     * add a new ambient alarm to the registry
     *
     * @param ambientAlarm
     */
    public static void addNewAmbientAlarm(AmbientAlarm ambientAlarm)
    {
        Logger.d(TAG, "  addNewAmbientAlarm(AmbientAlarm)");
        if(registeredAlarms!=null)
        {
            registeredAlarms.add(ambientAlarm);
        }
        else
        {
            updateListFromDataBase();
            registeredAlarms.add(ambientAlarm);
        }
        AmbientAlarmDatabase.updateAmbientAlarm(ambientAlarm);
    }

    public static void updateDataBaseEntry(AmbientAlarm ambientAlarm)
    {
        Logger.d(TAG, "  updateDatabaseEntry(AmbientAlarm)");
        AmbientAlarmDatabase.updateAmbientAlarm(ambientAlarm);
    }

    public static ArrayList<AmbientAlarm> getListOfAmbientAlarms()
    {
        Logger.d(TAG, "  getListOfAmbientAlarms()");
        return registeredAlarms;
    }

    public static void updateListFromDataBase()
    {
        Logger.d(TAG, "  update Alarm List from Database");
        //Log.d(TAG, "  Listsize="+registeredAlarms.size());
        registeredAlarms = AmbientAlarmDatabase.getAllAlarmsFromDatabase();
        Logger.d(TAG, "  Listsize="+registeredAlarms.size());
    }

    public static void startAlarmActivity(final AmbientAlarm ambientAlarm)
    {
        Logger.d(TAG, "  startAlarmActivity!");
        final Context ctx = ClockWorkService.getContext();
        if(ctx!=null)
        {
            new Thread(new Runnable()
            {
                @SuppressWarnings("unchecked")
                public void run()
                {
                    Logger.d(TAG, "  ctx is not null");
                    String alarmID = ambientAlarm.getAlarmID();
                    Logger.d(TAG, "  alarmid is " + alarmID);
                    Intent intent = new Intent(ctx, ambientAlarm.getAlarmActivity());
                    Logger.d(TAG, "  Intent created....");
                    // intent.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
                    Logger.d(TAG, "  strage stuff...");
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    Logger.d(TAG, "  now start stuff....");
                    intent.putExtra("ambientAlarmID", alarmID);
                    while(ambientAlarm.getStatus()== AlarmState.ALARM)
                    {
                        //check if the activity is running...
                        if (!AmbientAlarmActivity.running)
                        {
                            Logger.d(TAG, "apparently it is not running....");
                            if(ambientAlarm.getStatus()== AlarmState.ALARM)//making the race condition as small as possible...
                            {
                                ctx.startActivity(intent);
                            }
                        }
                        else
                        {
                            Logger.d(TAG, "running...");
                        }
                        try
                        {
                            Thread.sleep(3000);
                        }
                        catch (InterruptedException e)
                        {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();

        }
        else
        {
            Logger.e(TAG, "CONTEXT WAS NULL!!!!! ALARMACTIVITY COULD NOT BE STARTED!");
        }
    }

    public static void deleteAmbientAlarm(int position)
    {
        Logger.d(TAG, "  delete Ambient Alarm!");
        if(registeredAlarms!=null)
        {
            Logger.d(TAG, "registeredalarm "+registeredAlarms.size());
            //TODO: have a Folder Management that can do this better
            String alarmID = registeredAlarms.get(position).getAlarmID();
            File folder = new File(Environment.getExternalStorageDirectory().getPath() + "/"+AlarmClockConstants.BASE_FOLDER+"/"+alarmID+"/");
            deleteDirectory(folder);
            //TODO: this is the stuff that belongs here... the above stuff should go into storage management... which does not exist.
            AmbientAlarmDatabase.removeAmbientAlarm(registeredAlarms.get(position));
            registeredAlarms.remove(position);
        }
        Logger.d(TAG, "registeredalarm "+registeredAlarms.size());
    }

    static public boolean deleteDirectory(File path)
    {
        Logger.d(TAG, "DELETE "+path.getAbsolutePath());
        if (path.exists())
        {
            File[] files = path.listFiles();
            for (int i = 0; i < files.length; i++)
            {
                if (files[i].isDirectory())
                {
                    deleteDirectory(files[i]);
                }
                else
                {
                    Logger.d(TAG, "delete-->"+files[i].getAbsolutePath());
                    files[i].delete();
                    final SharedPreferences settings = ClockWorkService.settings;
                    SharedPreferences.Editor edit = settings.edit();
                    edit.putString(AlarmClockConstants.LAST_CHANGE_DATE+files[i].getName(), "");
                    edit.commit();
                }
            }
        }
        else
        {
            Logger.d(TAG, "does not exist...");
        }
        return (path.delete());
    }

    public static AmbientAlarm getAlarmById(String alarmID)
    {
        if(registeredAlarms!=null)
        {
            Logger.d(TAG, "  getAlarmByID " + alarmID);
            Logger.d(TAG, "  size of AlarmList " + registeredAlarms.size());
            for (AmbientAlarm alarm : registeredAlarms)
            {
                Logger.d(TAG, "    -->" + alarm.getAlarmID());
                if (alarm.getAlarmID().equals(alarmID))
                {
                    return alarm;
                }
            }
            return null;
        }
        else
        {
            updateListFromDataBase();
            return getAlarmById(alarmID);
        }
    }

    public static AmbientAlarm getAlarmByRegisteredAction(String actionID)
    {
        Logger.d(TAG, "getAlarmByRegsiteredAction");
        if(registeredAlarms!=null)
        {
            Logger.d(TAG, "  size of AlarmList " + registeredAlarms.size());
            for (AmbientAlarm alarm : registeredAlarms)
            {
                Logger.d(TAG, "    -->" + alarm.getAlarmID());
                HashMap<String, ArrayList<AmbientAction>> actions = alarm.getRegisteredActions();
                Collection<ArrayList<AmbientAction>> actionlists = actions.values();
                Iterator<ArrayList<AmbientAction>> itte = actionlists.iterator();
                while(itte.hasNext())
                {
                    ArrayList<AmbientAction> actionlist = itte.next();
                    for(AmbientAction action: actionlist)
                    {
                        Logger.d(TAG, "ACTION "+action.getActionID());
                        if(action.getActionID().equals(actionID))
                        {
                            return alarm;
                        }
                    }
                }
            }
            return null;
        }
        return null;
    }
}




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