// region Launching app
  private void launchApp(String appLaunchIconText) throws UiAutomatorDaemonException {
    d(
        uiaDaemon_logcatTag,
        "Launching app by navigating to and clicking icon with text " + appLaunchIconText);

    boolean clickResult;
    try {
      UiObject app = navigateToAppLaunchIcon(appLaunchIconText);
      Log.v(uiaDaemon_logcatTag, "Pressing the " + appLaunchIconText + " app icon to launch it.");
      clickResult = app.clickAndWaitForNewWindow();
      waitForGuiToStabilize();
    } catch (UiObjectNotFoundException e) {
      Log.w(
          uiaDaemon_logcatTag,
          String.format(
              "Attempt to navigate to and click on the icon labeled '%s' to launch the app threw an exception: %s: %s",
              appLaunchIconText, e.getClass().getSimpleName(), e.getLocalizedMessage()));
      d(uiaDaemon_logcatTag, "Pressing 'home' button after failed app launch.");
      this.device.pressHome();
      waitForGuiToStabilize();
      return;
    }

    if (clickResult) waitForGuiToStabilize();
    else
      Log.w(
          uiaDaemon_logcatTag,
          (String.format(
              "A click on the icon labeled '%s' to launch the app returned false",
              appLaunchIconText)));
  }
  private UiObject navigateToAppLaunchIcon(String appLaunchIconName)
      throws UiObjectNotFoundException {
    // Simulate a short press on the HOME button.
    this.device.pressHome();

    // We’re now in the home screen. Next, we want to simulate
    // a user bringing up the All Apps screen.
    // If you use the uiautomatorviewer tool to capture a snapshot
    // of the Home screen, notice that the All Apps button’s
    // content-description property has the value "Apps".  We can
    // use this property to create a UiSelector to find the button.
    UiObject allAppsButton = this.device.findObject(new UiSelector().description("Apps"));
    // Simulate a click to bring up the All Apps screen.
    allAppsButton.clickAndWaitForNewWindow();

    // In Android 6 default apps menu (Nexus 5X) there is a search box to locate the application
    // Use it to search for the application
    UiObject searchTextbox =
        this.device.findObject(
            new UiSelector()
                .resourceId("com.google.android.googlequicksearchbox:id/search_box_proxy"));
    searchTextbox.setText(appLaunchIconName);
    waitForGuiToStabilize();

    // The application (if found), will be the first item in the application list container
    // Type: com.google.android.googlequicksearchbox:id/icon
    UiObject appListview =
        this.device.findObject(
            new UiSelector()
                .resourceId("com.google.android.googlequicksearchbox:id/apps_list_view"));

    UiObject selectedItem = appListview.getChild(new UiSelector().index(0));
    d(uiaDaemon_logcatTag, selectedItem.getContentDescription());
    return selectedItem;
  }
  private void loadXPrivacyConfig(String actionCommand) {
    // There is an intent to open the import configuration screen,
    // however there is a bug on XPrivacy, when that intent is called,
    // after a file is selected its contents are not displayed and no
    // operation is performed
    Log.d(uiaDaemon_logcatTag, "Loading XPrivacy configuration file.");

    // Extract the configuration file name
    String fileName = actionCommand.split("=")[1];
    Log.d(uiaDaemon_logcatTag, "Filename = " + fileName);

    try {
      // launchApp("XPrivacy");

      String XPrivacy = "biz.bokhorst.xprivacy";
      String XPrivacyToolbar = XPrivacy + ":id/widgetToolbar";
      String XPrivacyOkButton = XPrivacy + ":id/btnOk";
      UiObject toolbar = this.device.findObject(new UiSelector().resourceId(XPrivacyToolbar));

      // Locate the toolbar
      UiObject buttonSet = toolbar.getChild(new UiSelector().index(2));
      UiObject menuButton = buttonSet.getChild(new UiSelector().index(1));
      // Click to open the menu
      menuButton.click();
      waitForGuiToStabilize();

      // Select "Operations …" item from menu
      UiObject operationsButton = this.device.findObject(new UiSelector().text("Operations …"));
      operationsButton.click();
      waitForGuiToStabilize();

      // Select "Import"
      UiObject importButton = this.device.findObject(new UiSelector().text("Import"));
      importButton.click();
      waitForGuiToStabilize();

      // Select configuration file
      UiObject fileManagerButton = this.device.findObject(new UiSelector().text("OI File Manager"));
      fileManagerButton.click();
      waitForGuiToStabilize();

      UiObject textInput = this.device.findObject(new UiSelector().text("File name"));
      textInput.setText(fileName);

      UiObject pickFileButton = this.device.findObject(new UiSelector().text("Pick file"));
      pickFileButton.click();
      waitForGuiToStabilize();

      UiObject okButton = this.device.findObject(new UiSelector().resourceId(XPrivacyOkButton));
      okButton.click();

      do {
        okButton = this.device.findObject(new UiSelector().resourceId(XPrivacyOkButton));
        waitForGuiToStabilize();
      } while (!okButton.isEnabled());

      waitForGuiToStabilize();

      okButton.click();
      this.device.waitForIdle();
      waitForGuiToStabilize();

      // Return to home
      this.device.pressHome();
      this.device.waitForIdle();
    } catch (UiObjectNotFoundException ex) {
      Log.e(uiaDaemon_logcatTag, "Failed to import XPrivacy configuration.");
    }
    // catch (UiAutomatorDaemonException ex)
    // {
    //  Log.e(uiaDaemon_logcatTag, "Failed to start XPrivacy.");
    // }

    Log.d(uiaDaemon_logcatTag, "XPrivacy configuration file loaded.");
  }