# Automatically Updating the Host App

Use the DTIS SDK to automatically update your Host App on a device. Device users must opt in for Auto Updates, and the Host App must capture and send the user's Auto Update preference to DT. By default, automatic app updates through Android OS require an idle device state. Auto Updates of the Host App through DT Ignite Services require the same idle device state:

* Device is connected to an unmetered connection (`NETWORK_TYPE_UNMETERED`).
* Device is charging.
* Device is not actively in use.

{% hint style="info" %}
When testing your Auto Update implementation, ensure that the test device meets these idle state conditions. For more information, see [Testing Auto Updates](#testing).
{% endhint %}

The following diagram shows the general flow for implementing automatic updates of the Host App through the DTIS SDK:

{% @mermaid/diagram content="sequenceDiagram
participant Host as Host App
participant SDK as Ignite
autonumber

```
Host ->> SDK: Initialize and Authenticate
Host ->> SDK: getIsAutoUpdateEnabled()

alt IsAutoUpdateEnabled=null
    Host ->> Host:  Capture user opt-in for Auto Updates
    Host ->> SDK: setIsAutoUpdateEnabled()
else IsAutoUpdateEnabled=true
    SDK -->> Host: App Update Broadcasts
end

Host ->> SDK: Disconnect" %}
```

| SEQUENCE ITEM | DESCRIPTION                                                                                                                                                                                                                                                                                                                                                            |
| ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 1             | <p><strong>Initialization and Connection</strong><br>The Host App initializes the DTIS SDK and connects to Ignite. This action establishes a secure connection between the two components and authenticates the Host App with Ignite. For more information, see <a href="../sdk-reference/connect-to-ignite">Connect to Ignite</a>.</p>                                |
| 2             | <p><strong>Check for Auto Update Enabled</strong><br>The Host App calls the <a href="../sdk-reference/check-for-auto-update-preference"><code>getIsAutoUpdateEnabled()</code> method</a> to determine whether or not the device user has opted in for automatic updates of the Host App.</p>                                                                           |
| 3             | <p><strong>Capture User Preference</strong><br>If the user has not specified their Auto Update preference, the Host App must surface a UI to capture their preference.</p>                                                                                                                                                                                             |
| 4             | <p><strong>Send Auto Update Preference</strong><br>Send the device user's Auto Update preference to DT using the <a href="../sdk-reference/send-auto-update-preference"><code>setIsAutoUpdateEnabled()</code> method</a>.</p>                                                                                                                                          |
| 5             | <p><strong>Listen for Auto Updates</strong><br>If a user has opted in for Auto Updates, Ignite schedules tasks to update the Host App on devices. Ignite broadcasts notifications when the updates are scheduled and completed. For more information, see <a href="../../sdk-reference/send-auto-update-preference#appupdatebroadcast">Auto Update Broadcasts</a>.</p> |
| 6             | <p><strong>Disconnect from Ignite</strong><br>Disconnect from Ignite Services. For more information, see <a href="../sdk-reference/disconnect-from-ignite">Disconnect from Ignite</a>.</p>                                                                                                                                                                             |

## Testing Automatic Updates <a href="#testing" id="testing"></a>

When testing your Auto Update implementation, ensure that the test device state is in an idle state:

* The device is connected to an unmetered connection (`NETWORK_TYPE_UNMETERED`).
* The device is charging.
* The device is not actively in use.

Use the following commands to set a device to the required idle state:

{% code title="ADB" %}

```bash
$adb shell dumpsys deviceidle enabled deep								
#C1 (If output is 0 then run C2 command else run C3)

$adb shell dumpsys deviceidle enable deep								
#C2 (Run above command and check it should not be 0 now)

adb shell dumpsys deviceidle tempwhitelist -d 3000000 com.your.packagename		
#C3 (This commands set demo app jobs to run in device idle mode)

adb shell dumpsys deviceidle tempwhitelist								
#C4 (It should show output like "UID=SOME_NUMBER: SOME_TIME - shell")

adb shell dumpsys activity idle-maintenance								
#C5 (Invoke maintenance mode when device is idle, this will start the App Update Job)
```

{% endcode %}
