Profiles API

Overview

A Profile is a device state which can be created, modified, or activated for a user based off of an environmental trigger. When you tell the system to create a profile, it will be populated within the system settings application’s Profile viewer. This hub can both be controlled by the Settings application and by a 3rd party application.

Creating a Profile

You must specify the UI information, actions, and optional environmental triggers for a Profile utilizing the methods available within the Profile object. To add your created Profile to the system, invoke a call to ProfileManager.addProfile(Profile)

Required Profile contents

A Profile object must contain the following:

To use these Profiles API, your application must first declare the write settings permission in AndroidManifest.xml.

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

Creating and adding a simple Profile

The snippet below details how to create a Profile object that overrides the devices ring mode settings to be muted.

Kotlin

fun createProfile(context: Context) {
    // Create a new Profile object with a label
    val profile = Profile("My New Profile!")

    // We're a toggle!
    profile.profileType = Type.TOGGLE

    // Now create a RingModeSettings object and make set the mode to MUTE with an override of TRUE
    val ringSettings = RingModeSettings(RingModeSettings.RING_MODE_MUTE, true)
    profile.ringMode = ringSettings

    // Add the Profile to the internal service
    ProfileManager.getInstance(context).addProfile(profile)
}

Java

void createProfile(Context context) {
    // Create a new Profile object with a label
    Profile profile = new Profile("My New Profile!");

    // We're a toggle!
    profile.setProfileType(Type.TOGGLE);

    // Now create a RingModeSettings object and make set the mode to MUTE with an override of TRUE
    RingModeSettings ringSettings = new RingModeSettings(RingModeSettings.RING_MODE_MUTE, true);
    profile.setRingMode(ringSettings);

    // Add the Profile to the internal service
    ProfileManager.getInstance(context).addProfile(profile);
}

To set the profile to be the primary active profile, you can trigger this by calling setActiveProfile(UUID) by passing the UUID of the created Profile object.

Kotlin

fun enableProfile(profile: Profile, context: Context) {
    val profileUuid = profile.uuid
    ProfileManager.getInstance(context).setActiveProfile(profileUuid)
}

Java

void enableProfile(Profile profile, Context context) {
    UUID profileUuid = profile.getUuid();
    ProfileManager.getInstance(context).setActiveProfile(profileUuid);
}

Leveraging a ProfileTrigger

For the instances where you want to trigger a Profile based off of an environment change as defined by Profile.TriggerType you can set a specific Profile.ProfileTrigger on your Profile.

To create a ProfileTrigger you’ll need to populate 4 parameters, for something like a trigger based off of a WiFi access point, you can follow the model below:

Kotlin

fun getTrigger(triggerName: String): Profile.ProfileTrigger {
    val triggerType = Profile.TriggerType.WIFI          // This is a wifi trigger
    val triggerId = trigger.getSSID()                   // Use the AP's SSID as identifier
    val triggerState = Profile.TriggerState.ON_CONNECT  // On Connect of this, trigger

    return Profile.ProfileTrigger(triggerType, triggerId, triggerState, triggerName)
}

Java

Profile.ProfileTrigger getTrigger(String triggerName) {
    int triggerType = Profile.TriggerType.WIFI;         // This is a wifi trigger
    int triggerId = trigger.getSSID();                  // Use the AP's SSID as identifier
    int triggerState = Profile.TriggerState.ON_CONNECT; // On Connect of this, trigger

    return new Profile.ProfileTrigger(triggerType, triggerId, triggerState, triggerName);
}

As you can see we set the TriggerType as WiFI, set the SSID as the triggerId (for tracking), set the title of the access point as the triggerName, and then finally declare that we want to be “triggered” when an ON_CONNECT is given for the access point.

Get assistance

If you have any questions or get stuck on any of the steps, feel free to ask on our subreddit or in #LineageOS on freenode.

You can also find more information in the LineageSDK javadoc.