Display running app when plugging into Samsung DeX

1.1k Views Asked by At

When I plug my phone into the DeX dock, my app window is minimized in the DeX taskbar. This is default behaviour.

I am using a Galaxy S8 running DeX 2.5.

I want my app to display (full-screen or windowed) immediately when plugged into the DeX.


What I have tried so far (as per advice on the Samsung DeX website)...

1 - I have applied the manifest meta-data that keeps the app process alive:

<meta-data
    android:name="com.samsung.android.keepalive.density"
    android:value="true"/>

2 - I have applied the configChanges property to intercept config changes:

android:configChanges="orientation|screenSize|smallestScreenSize|density|screenLayout|uiMode|keyboard|keyboardHidden|navigation"

This works as expected when the device is rotated, or when the screen is resized within the DeX interface i.e. Activity.onConfigurationChanged(Configuration) runs.

But this does not get triggered by plugging the phone into the DeX.

3 - My activity has been set to resize in the manifest:

android:resizeableActivity="true"
android:supportsPictureInPicture="true"

  • Is there a way to get the window to automatically display when plugged into the Dex?
  • Is there a way to get a callback when plugged into the DeX, and then launch my app from that calllback?
1

There are 1 best solutions below

4
Richard Le Mesurier On BEST ANSWER

This info is only valid for DeX v2.5. Updates to DeX v3 broke the below code.


When a device is docked into, or undocked from, the DeX, the following intents are broadcast by the system:

  • android.app.action.ENTER_KNOX_DESKTOP_MODE
  • android.app.action.EXIT_KNOX_DESKTOP_MODE

Any app can register a receiver in the manifest to be notified when the device is docked:

<receiver android:name=".DexReceiver">
    <intent-filter>
        <action android:name="android.app.action.ENTER_KNOX_DESKTOP_MODE"/>
    </intent-filter>
</receiver>

Inside the receiver the app can relaunch itself.

public void onReceive(Context context, Intent intent) {
    Intent relaunch = context.getPackageManager().getLaunchIntentForPackage(BuildConfig.APPLICATION_ID);
    relaunch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(relaunch);
}

This causes the app to launch in a normal window when docked.


To ensure full-screen launching, there are 2 changes to make in the manifest.

1) Add DeX meta-data inside the application block:

<!--DeX FULL SCREEN LAUNCH SIZE-->
<meta-data
    android:name="com.samsung.android.dex.launchwidth"
    android:value="0"/>
<meta-data
    android:name="com.samsung.android.dex.launchheight"
    android:value="0"/>

Here the 0 values tell DeX to use the maximum width/height possible.

2) Add a layout block to the activity that is being launched:

<!--USE BIG ENOUGH DIMENSIONS TO FORCE FULL-SCREEN ON LAUNCH-->
<layout
    android:defaultWidth="5000dp"
    android:defaultHeight="5000dp"
    android:gravity="center"/>

Here the 5000dp values must be bigger than the screen that you are plugging into the DeX.

Both launch size blocks are recommended by Samsung in different places - my experience was that the second option worked on the S8 (DeX v2.5).