[Samsung DeX] How to detect the Samsung DeX mode

Samsung Developer Program

Going into Samsung DeX Mode

Samsung DeX is a new type of launcher – transferring the known Android’s phone mode UI to desktop-like, bigger screen and experience. Upon opening Samsung DeX Mode, every app that stayed open in Phone mode, undergo certain changes before launching in Samsung DeX. What is done and how can we detect this change?

Your app during transfer into Samsung DeX Mode

During the transfer into Samsung DeX Mode opened activities are killed and recreated in new, android desk UI mode. Handling the runtime configuration changes is explained exhaustively on Android developer webpage ( https://developer.android.com/guide/topics/resources/runtime-changes.html ). Worth mentioning is that your app should support at least preserving the app state and two of the UI modes – initial phone density and size and Samsung DeX desk mode UI. Below you can see the table set for S8 initial phone mode and Samsung DeX Mode.

Phone Mode

Samsung DeX Mode

UI mode

mobile

desk

Layout qualifier

layout-xxxhdpi

layout-desk

Size

large

xlarge

Density

xxxhdpi

mdpi

To prevent the activity restart and handle the configuration change yourself you can declare (as described in Android developer guide):

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

`

This way of keeping the app or activity alive is not recommended for most applications.

Detecting Samsung DeX Mode

Changes often should be applied to Android components that are not Activities. For this you can detect Samsung DeX Mode in three possible ways:

System configuration check

When mode is changed from phone to Samsung DeX, system configuration is also changed as below.

UI MODE

desk

Density

mdpi

(optional) Screen layout

xlarge

(optional) Resolution

FHD

When you want to detect the current mode, you can try with the following sample code.

`private boolean isDesktopMode(Context context){
UiModeManager uiModeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
//Density for mdpi is 160
float density = context.getResources().getDisplayMetrics().densityDpi;

    return (uiModeManager.getCurrentModeType()== Configuration.UI_MODE_TYPE_DESK) && density == 160;  
}`
Set up Android Broadcast Receiver

App can receive the notification when Samsung DeX mode (or Phone mode) changes via Broadcast Receiver. If it is registered via manifest, app can get the notification after process is killed. Timing for getting this notification is not accurate because of limitation of Broadcast.

Enter Samsung DeX mode:

UiModeManager.SEM_ACTION_ENTER_KNOX_DESKTOP_MODE = "android.app.action.ENTER_KNOX_DESKTOP_MODE"

Exit Samsung DeX mode:

UiModeManager.SEM_ACTION_EXIT_KNOX_DESKTOP_MODE = "android.app.action.EXIT_KNOX_DESKTOP_MODE"

Code sample:

`class DesktopModeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();

        if ("android.app.action.ENTER_KNOX_DESKTOP_MODE".equals(action)) {  
            Log.d(DEBUG_TAG, "Desktop mode ON");  

        } else if ("android.app.action.EXIT_KNOX_DESKTOP_MODE".equals(action)) {  
            Log.d(DEBUG_TAG, "Desktop mode OFF");  
        }  
    }  
}`
Query the current mode

App can also query the current mode as below. This way needs handling the possible exceptions arise while accessing config.

`import android.content.res.Configuration;
import java.lang.reflect.Field;
import java.lang.Class;

//* *//

Configuration config = getResources().getConfiguration();
try {
Class configClass = config.getClass();
if(configClass.getField("SEM_DESKTOP_MODE_ENABLED").getInt(configClass)
== configClass.getField("semDesktopModeEnabled").getInt(config)) {

      // Samsung DeX mode enabled  
     }  
  } catch(NoSuchFieldException e) {  

//Handle the NoSuchFieldException
} catch(IllegalAccessException e) {
//Handle the IllegalAccessException
} catch(IllegalArgumentException e) {
//Handle the IllegalArgumentException
}`

More info about Samsung DeX on: https://developer.samsung.com/samsung-dex and other blog posts on https://developer.samsung.com/blog.

Preferences Submitted

You have successfully updated your cookie preferences.