Developer API

The developer API is currently under construction. Currently it supports retrieving sleep data, alarm data and starting sleep tracking. The aim of the API is to allow third party add-ons to Sleep as Android. 

Sleep tracking start intent:

com.urbandroid.sleep.alarmclock.START_SLEEP_TRACK

Sleep tracking started 

com.urbandroid.sleep.alarmclock.SLEEP_TRACKING_STARTED

Time to bed notification:

com.urbandroid.sleep.alarmclock.TIME_TO_BED_ALARM_ALERT

Alarm triggered:

com.urbandroid.sleep.alarmclock.ALARM_ALERT_START

Alarm dismissed:

com.urbandroid.sleep.alarmclock.ALARM_ALERT_DISMISS

Lullaby

Stop:
com.urbandroid.sleep.ACTION_LULLABY_STOP_PLAYBACK

Start:
start a Service with package "com.urbandroid.sleep" and class "com.urbandroid.sleep.media.lullaby.LullabyService"
EXTRA: extra_lullaby = "lullaby name"...
Lullaby names:
NONE, WHALE, STORM, STREAM, CAVE, FIREPLACE, SEA, WIND, CLOCK, TIBET, NIGHT, FROGS, HORSE, SHEEP, CHIMES, OM, BELLS, FLUTE, PIANO, CAT, TRAIN, MARCH, MUSICBOX, BABY, GIRL...

Using Tasker with Sleep as Android

React on Sleep as Android events in Tasker
Misc -> Intent received and add one of the Intents described above.
For example you can start you weather app when alarm is dismissed, by listening for the com.urbandroid.sleep.alarmclock.ALARM_ALERT_DISMISS intent.

Start an Sleep as Android action from Tasker
Misc -> Send intent
For example, create a new task, click + and choose "Action Intent" from the Misc category and there you fill in com.urbandroid.sleep.alarmclock.START_SLEEP_TRACK into the action text field. So you can start sleep tracking when you plug your phone to charger for example.

Adding alarm

android.intent.action.SET_ALARM
EXTRA_MESSAGE = "android.intent.extra.alarm.MESSAGE";
EXTRA_HOUR = "android.intent.extra.alarm.HOUR";
EXTRA_MINUTES = "android.intent.extra.alarm.MINUTES";
EXTRA_SKIP_UI = "android.intent.extra.alarm.SKIP_UI";


Tip: use Reactor to handle Sleep as Android Intents. As an example the following Reactor Script starts a news Reader after you dismiss the alarm to synchronize latest latest news:

while (true) {
  waitForIntent("com.urbandroid.sleep.alarmclock.ALARM_ALERT_DISMISS");
  waitForConnectivity();
  // TODO change package to start my app
  startActivity("com.urbandroid.reader");
}
The following script will start FM Radio as your alarm using (FMAlarm) you can setup an alarm with silnce ringtone and no vibration and let Reactor start FMAlarm after the alarm triggers. 
while(true) {  
  waitForIntent("com.urbandroid.sleep.alarmclock.ALARM_ALERT_START");
  con = hasConnectivity();
  if (!con) {
    enableAirplane(false);
  }
  // start the FMAlarm
  sendIntent("com.rejh.fmalarm2.intents.ALARM");
}













Reactor is for FREE is you use one script at a time. For more Reactor script for Sleep as Android, please have a look here

Sleep data content provider:

1. Use the following class to access the content provider
public class Record {
    public static final String AUTHORITY = "com.urbandroid.sleep.history";
    public static final String RECORDS_TABLE = "records";

    public static class Records implements BaseColumns {
            public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + RECORDS_TABLE);
            public static final String CONTENT_TYPE = "vnd.android.cursor.dir/com.urbandroid.sleep.history";
            public static final String RECORD_ID = "_id";
            public static final String START_TIME = "startTime";
            public static final String LATEST_TO_TIME = "latestToTime";
            public static final String TO_TIME = "toTime";
            public static final String FRAMERATE = "framerate";
            public static final String RATING = "rating";
            public static final String COMMENT = "comment";
            public static final String RECORD_DATA = "recordData";
            public static final String LEN_ADJUST = "lenAdjust";
    }
}

2. Query the content provider (only select is currently supporter - no update or delete)

private final String[] projection = new String[] {                                Record.Records.START_TIME,Record.Records.TO_TIME, Record.Records.RATING};
Cursor cursor = activity.managedQuery(Record.Records.CONTENT_URI, projection, Record.Records.START_TIME + " > " + yearAgo, null, Record.Records.START_TIME + " ASC");

Alarm data content provider:

Alarm content provider allows all provider operations.

public final class Alarm {

    public static class Columns implements BaseColumns {
        public static final Uri CONTENT_URI = Uri.parse("content://com.urbandroid.sleep.alarmclock/alarm");

        public static final String HOUR = "hour";
        public static final String MINUTES = "minutes";
        public static final String DAYS_OF_WEEK = "daysofweek";
        public static final String ALARM_TIME = "alarmtime";
        public static final String SUSPEND_TIME = "suspendtime";
        public static final String NON_DEEPSLEEP_WAKEUP_WINDOWN = "ndswakeupwindow";
        public static final String ENABLED = "enabled";
        public static final String VIBRATE = "vibrate";
        public static final String MESSAGE = "message";
        public static final String ALERT = "alert";
        public static final String DEFAULT_SORT_ORDER = HOUR + ", " + MINUTES + " ASC";

        public static final int ALARM_ID_INDEX = 0;
        public static final int ALARM_HOUR_INDEX = 1;
        public static final int ALARM_MINUTES_INDEX = 2;
        public static final int ALARM_DAYS_OF_WEEK_INDEX = 3;
        public static final int ALARM_TIME_INDEX = 4;
        public static final int ALARM_ENABLED_INDEX = 5;
        public static final int ALARM_VIBRATE_INDEX = 6;
        public static final int ALARM_MESSAGE_INDEX = 7;
        public static final int ALARM_ALERT_INDEX = 8;
        public static final int ALARM_SUSPEND_TIME_INDEX = 9;
        public static final int ALARM_NON_DEEPSLEEP_WAKEUP_WINDOW_INDEX = 10;
    }
}

Starting sleep tracking from another process

Intent intent = new Intent();
intent.setAction("com.urbandroid.sleep.alarmclock.START_SLEEP_TRACK");
intent.putExtra("START_AIRPLANE", true);
intent.setClassName("com.urbandroid.sleep", "com.urbandroid.sleep.alarmclock.StartTrackReceiver");
sendBroadcast(intent);



Comments