12

Dual Messenger is a feature on Samsung Android phones

[Screenshot from Samsung A70 (Android 9]

For example I can launch the first copy of WhatsApp using intent by calling package name "com.whatsapp".

        Intent waIntent = new Intent(Intent.ACTION_SEND);
        waIntent.setType("text/plain");
        String text = "THIS IS MY TEXT";

        PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);

        waIntent.setPackage("com.whatsapp");

        waIntent.putExtra(Intent.EXTRA_TEXT, text);
        startActivity(Intent.createChooser(waIntent, "Share with"));

What would be the package name or other Intent options required to launch the second WhatsApp?

Ehab Reda
  • 555
  • 5
  • 9

1 Answers1

0

It seems Samsung utilizes a separate user profile mechanism for dual messenger apps. To access these apps, we can gather the app list from all user profiles, filtering out the ones we already have. This will leave us with the dual messenger apps that we can then launch using the LauncherApps service.

Here's how we get the list of all apps (includes dual messenger apps)

suspend fun getAppsList(context: Context): MutableList<App> {
  return withContext(Dispatchers.IO) {
    val appList: MutableList<App> = mutableListOf()

    try {
      val userManager = context.getSystemService(Context.USER_SERVICE) as UserManager
      val launcherApps = context.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
      val collator = Collator.getInstance()

      for (profile in userManager.userProfiles) {
        for (app in launcherApps.getActivityList(null, profile)) {
          val appLabelShown = app.label.toString()
          val appModel = App(app.applicationInfo.packageName, appLabelShown, profile)

          val appAlreadyExists =
              appList.any { existingApp ->
                existingApp.packageName == appModel.packageName && existingApp.user == appModel.user
              }

          if (app.applicationInfo.packageName != BuildConfig.APPLICATION_ID && !appAlreadyExists) {
            appList.add(appModel)
          }
        }
      }
      appList.sortBy { it.appName.lowercase() }
    } catch (e: Exception) {
      e.printStackTrace()
    }
    appList
  }
}

And here's how we launch an app

private fun launchApp(packageName: String, userHandle: UserHandle) {
  val launcher = appContext.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps

  val activityInfo = launcher.getActivityList(packageName, userHandle)

  val component =
      when (activityInfo.size) {
        0 -> {
          // Handle app not found case
          return
        }
        1 -> ComponentName(packageName, activityInfo[0].name)
        else -> ComponentName(packageName, activityInfo.last().name)
      }

  try {
    launcher.startMainActivity(component, userHandle, null, null)
  } catch (e: SecurityException) {
    try {
      launcher.startMainActivity(component, android.os.Process.myUserHandle(), null, null)
    } catch (e: Exception) {
      // Handle unable to open app case
    }
  } catch (e: Exception) {
    // Handle unable to open app case
  }
}

I found this method in Olauncher(Github), does exactly what I want lists and launch all apps including Dual messenger apps

Umair Ayub
  • 74
  • 2