Optimizing your app

Modifying apps for Samsung DeX

This section provides an overview of how to optimize apps for Samsung DeX.

There are very few special implementation requirements needed to get an app ready for Samsung DeX. Generally, if an app adheres to the best practices of Android programming, it will successfully operate in Samsung DeX without any additional coding. Apps should follow the best practices for the following topics:

  • Enabling Multi-Window support

  • Enabling keyboard and mouse support

  • Handling runtime configuration changes

  • Implementing a responsive UI

 

The following section provides references to the most relevant Android coding practices for Samsung DeX.

Enabling Multi-Window support

To run in Samsung DeX, apps should support Android Multi-Window – this enables minimizing, maximizing and resizing. If Multi-Window is not supported, the app opens in a fixed-size window. Follow Androids guide on Multi-Window support for complete information on how to configure apps.

To enable Multi-Window for Samsung DeX, paste this code into the Manifest:

 
<activity>
    android:resizeableActivity="true"
</activity>

The image below shows the different window resize options available when apps have Multi-Window enabled

Figure 6: Target SDK >= 24, android:resizeableActivity="true"

Enabling Mouse and keyboard

Do not explicitly declare the touchscreen support as it may disable the mouse and the keyboard interactions. This explicit declaration is shown in the code below.

 
<uses-configuration android:reqTouchScreen="finger">
<uses-feature android:name="android.hardware.touchscreen">
<android:required="true">

If you explicitly declare touchscreen support, the app won't launch in Desktop mode and will display the message below.

 
Figure 5: touchscreen disabled error

Handle runtime configuration changes

The switch between mobile and Samsung DeX mode causes runtime configuration changes – similar to an orientation change from portrait to landscape. For optimal results when running apps in Desktop Mode, follow Android's runtime changes best practices. Also, see Follow Android's best practices for User Interface for information building responsive designs.

 
Samsung DeX 7 runtime changes to support:
  • Density change between xxxhdpi and mdpi.

  • Resolution change between WQHD and FHD.

  • Orientation change between portrait to landscape.

  • Screen layout change.

  • Screen size change.

  • Smallest screen size change.

  • UI mode change between mobile and desktop.

 

You can use the following qualifiers to provide alternative Android resources in Samsung DeX mode:

  • UI mode : desk

  • Screen layout : xlarge

  • Density : mdpi

  • Resolution : FHD

For example, this image below shows an example of using different resources for different screen densities.

Figure 7: Android resource folder

This image demonstrates the correct dpi handling for fonts on different screen sizes – when the Samsung DeX app is resized, the size and scale of font remains appropriate to the screen size.

Figure 7: Runtime configuration changes in Samsung DeX

If you choose to handle configuration changes yourself (the least recommended), you need to provide the following declaration in the Manifest. It will prevent the application from restarting when the screen density changes between mobile and Samsung DeX modes:

 
Keepalive declaration

<application>
    <meta-data android:name=“com.samsung.android.keepalive.density” android:value=“true”/>
</application>
Detect Samsung DeX mode

You can use the following logic to detect the Samsung DeX mode in your handler:


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) {
          } catch(IllegalAccessException e) {
          } catch(IllegalArgumentException e) {
}

If you prefer, you can also use this technique to detect the Samsung DeX mode in your handler:

1. Register the receiver in the Android manifest.


receiver android:name=".MainActivity$DesktopModeReceiver"
    intent-filter
        action android:name="android.app.action.ENTER_KNOX_DESKTOP_MODE" 
      action android:name="android.app.action.EXIT_KNOX_DESKTOP_MODE" 
    intent-filter
receiver

2. In your app code, implement a broadcast receiver to detect entering and exiting Samsung DeX mode.


public 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)) {
            Toast.makeText(context, "Desktop mode ON", Toast.LENGTH_LONG).show();
            
        } else if ("android.app.action.EXIT_KNOX_DESKTOP_MODE".equals(action)) {
            Toast.makeText(context, "Desktop mode OFF", Toast.LENGTH_LONG).show();
            
        }
    }
}