Intent and Content provider API

Who is this API for

This API is intended for individuals who want to automate their sleep related tasks as well as developers who like to implement apps or services integration with Sleep as Android.

You can automate most aspects of Sleep as Android and integrate it with other services or apps on your phone using common automation software such as Tasker for example. As an example of what you can do please follow Jeoren’s guide on how to Let Google Text to Speech say on which time your alarm goes.

TIP: No idea about programming or intents? Just use the Sleep as Android Tasker plug-in (built within the app) and you get all the automation without any need for coding!

Actions

Sleep tracking start intent (Broadcast)

com.urbandroid.sleep.alarmclock.START_SLEEP_TRACK

Optional EXTRA to start in battery saving mode: START_IN_BATTERY_SAVING_MODE = true

Sleep tracking start + set an alarm for ideal sleep length (Broadcast)

com.urbandroid.sleep.alarmclock.START_SLEEP_TRACK_WITH_IDEAL_ALARM_ACTION

Sleep tracking stop intent (Broadcast)

com.urbandroid.sleep.alarmclock.STOP_SLEEP_TRACK

Sleep tracking 5 minute pause intent (Broadcast)

com.urbandroid.sleep.ACTION_PAUSE_TRACKING

Snooze Alarm (Broadcast)

com.urbandroid.sleep.alarmclock.ALARM_SNOOZE

You can send an integer extra to set the snooze duration in minutes (in range from 1 to 60).

EXTRA NAME: extra_snooze_time
 
Dismiss Alarm (Broadcast)

com.urbandroid.sleep.alarmclock.ALARM_DISMISS_CAPTCHA

Lullaby Stop (Broadcast)

com.urbandroid.sleep.ACTION_LULLABY_STOP_PLAYBACK
 
Lullaby start (Service)
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, TIBER, NIGHT, FROGS, HORSE, SHEEP, CHIMES, OM, BELLS, FLUTE, PIANO, CAT, TRAIN, MARCH, MUSICBOX, BABY, GIRL, SUB, NASA, LAVA, JUNGLE, TIBET, BABY, SCIFI, CHOR, BREATH…

Request Backup (Broadcast)
“com.urbandroid.sleep.REQUEST_SYNC” will start the backup sync – export a CSV file, backup to Sleepcloud, Dropbox and Google Drive if connected

Events

Sleep tracking started 
 
com.urbandroid.sleep.alarmclock.SLEEP_TRACKING_STARTED
 
Sleep tracking stopped
 
com.urbandroid.sleep.alarmclock.SLEEP_TRACKING_STOPPED

Snoozed by user
 
com.urbandroid.sleep.alarmclock.ALARM_SNOOZE_CLICKED_ACTION
 
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 started

com.urbandroid.sleep.ACTION_LULLABY_START_PLAYBACK

Lullaby stopped

com.urbandroid.sleep.ACTION_LULLABY_STOPPED_PLAYBACK

Lucid dreaming cue:

com.urbandroid.sleep.LUCID_CUE_ACTION
NOTE: enable settings-lucid dreaming
 
Antisnoring sound:
 
com.urbandroid.sleep.ANTISNORING_ACTION
 
45 minutes before smart period:
 
com.urbandroid.sleep.alarmclock.AUTO_START_SLEEP_TRACK

Using Tasker with Sleep as Android
React on Sleep as Android events in Tasker
Event -> System -> Intent (previously 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
Event -> System -> Intent (previously Misc -> Intent send)
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”;

Enabling/disabling alarm by label

com.urbandroid.sleep.alarmclock.ALARM_STATE_CHANGE
Extras:
“alarm_label”: String representing a label of alarm to be changed. If there are more such alarms, only one of them is going to be changed (no guarantees which).

“alarm_enabled”: Boolean saying whether alarm should be enabled or disabled.

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.

Content providers

To access Sleep as Android content providers your app needs the com.urbandroid.sleep.READ permission

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 TIMEZONE = "timezone";
public static final String LEN_ADJUST = "lenAdjust";
public static final String QUALITY = "quality";
public static final String SNORE = "snore";
public static final String CYCLES = "cycles";
public static final String EVENT_LABELS = "eventLabels";
public static final String EVENTS = "events";
public static final String RECORD_FULL_DATA = "recordFullData";
public static final String RECORD_NOISE_DATA = "recordNoiseData";
public static final String NOISE_LEVEL = "noiseLevel";
public static final String FINISHED = "finished";
public static final String GEO = "geo";
public static final String LENGTH = "length";
	}
}

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);

Share

14 thoughts on “Intent and Content provider API

  1. So far I have found these additional lullaby names: SUB, NASA, LAVA, JUNGLE, TIBET, BABY. I can’t find the ones for: choir, slow breath, spaceship and north wind. I have tried some possible variants for all of them. Any help would be appreciated.

    1. Hello, the com.urbandroid.sleep.LUCID_CUE_ACTION will trigger when we detect REM sleep, but you havge to have lucid dreaming enabled. If you don’t want lucid dreaming, you can just set the sound to silence…

  2. I’m trying to detect if sleep has finished being recorded. Is there a reason “finished” attribute of the Content Provider is nonexistent?

      1. It just said that it wasn’t a field. I decided to not include that functionality anyways though.
        Another question: I’m occasionally getting this error when building my cursor to query the content provider (Though sometimes it doesn’t happen):

        Attempt to invoke virtual method ‘android.database.Cursor com.urbandroid.sleep.persistence.DbSleepRecordRepository.executeRead(com.urbandroid.sleep.persistence.IPersistentReadOperation)’ on a null object reference

        Any ideas?

    1. Hello, many thanks for this question, absolutely there is an intent “com.urbandroid.sleep.ACTION_LULLABY_STOP_PLAYBACK” when we decide to stop our lullaby based on your sleep phases and the lullaby turn off after asleep time..

Leave a Reply

Your email address will not be published. Required fields are marked *