Development

Step 1: Preparing the area

Open the Android Studio IDE, and open the starting version of the Remote Desktop sample app.

  1. Go to File > Open to access the Open File or Project window.

  2. In the Open Project window, select the remotedesktop Android app project, and press OK.

  3. The project should automatically kickoff a Gradle build when opened and display results in the Build Output toolbar. You can also start the build by navigating to Build > Rebuild Project.

  4. Project structure should look like below (Project structure window at the left side of the window).

  5. Open Constants.java from the project structure by navigating to java > Constants.

  6. Fill in String KPE_LICENSE_KEY = with your license key

    To get a license refer to:
    https://docs.samsungknox.com/dev/common/tutorial-get-a-license.htm


Step 2: Setting up the start and stop button behaviors

We will first set up the code for the buttons that start and stop remote capture.

  1. Open the main activity’s behavior file (MainActivity.java) from the project structure by navigating to java > com.samsung.knox.example.remotedesktopsampleapp > MainActivity.java.

  2. At the beginning of the onCreate(Bundle savedInstanceState) method, add the code that chooses the correct version of the RemoteDesktop library:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {  
        System.loadLibrary("remote-desktop-sample-jni-pie");  
    } else {  
        System.loadLibrary("remote-desktop-sample-jni");  
    }
    
  3. In the list of defined variables and onClick listeners at the end of onCreate(), initialize the start and stop buttons:

    Button startRemoteCaptureButton =
    findViewById(R.id.startRemoteCaptureBtn);  
    startRemoteCaptureButton.setOnClickListener(view ->
    startRemoteCapture());  
      
    Button stopRemoteCaptureButton =
    findViewById(R.id.stopRemoteCaptureBtn);  
    stopRemoteCaptureButton.setOnClickListener(view ->
    stopRemoteCapture());
    
  4. Look for the startRemoteCapture() method and write the following code:

    private void startRemoteCapture() {  
        try {  
            boolean result = startRemoteDesktop(rawFilePath);
            mUtils.log(getResources().getString(R.string.remote_control_start, result));  
        } catch (Exception e) {  
            mUtils.processException(e,TAG);  
        }  
    }
    
  5. Look for the stopRemoteCapture() method and write the following code:

    private void stopRemoteCapture() {  
        try {
            boolean result = stopRemoteDesktop();
            mUtils.log(getResources().getString(R.string.remote_control_stop, result));  
        } catch (Exception e) {  
            mUtils.processException(e,TAG);  
        }  
    }
    

    Take note of the implemented methods above.


Step 3: Transitioning from Java to C++

  1. Open the folder that holds all the native code for remote viewing: cpp > remote-desktop-sample-jni-pie.

  2. Open remote_desktop_sample_jni.cpp, and notice the startRemoteDesktop() and stopRemoteDesktop() methods:

    These are the methods that let our Java functions startRemoteDesktop() and stopRemoteDesktop() call the C++ native functions start(onScreenChanged, rawPath) and stop(). We will look at these methods next.


Step 4: Setting up the different remote viewing behaviors

  1. Open remote viewing’s behavior file (RemoteControl.cpp) from the project structure by navigating the same folder: cpp > remote-desktop-sample-jni-pie > RemoteControl.cpp.

  2. Inside the constructor RemoteControl::RemoteControl(), initialize the remote desktop object:

    mRemoteDesktop = NULL;
    

    The following steps have missing but important methods (or public and static public member functions) that you need to look for. You can look for these methods in the Knox IRemoteDesktop class reference.

  3. At the start of RemoteControl::start(), inside if(!mIsRemoteControlRunning), retrieve the instance of the remote desktop object:

    if (!mRemoteDesktop) {  
        mRemoteDesktop = IRemoteDesktop::getInstance();  
        // Initialize Remote Desktop  
    }
    
  4. Next, after getDefaultScreenInfo and setScreenInfo, initialize the remote desktop:

    ret = mRemoteDesktop->init();  
    if(!ret){  
        LOGE(TAG, "Failed: mRemoteDesktop->init();");  
        return false;  
    }  
    mIsRemoteControlRunning = true;  
    
  5. Next, after getFrameBufInfo(), set the listener for our remote desktop object:

    mRemoteDesktop->setListener(this); // It uses the “this” object as its parameter.`
    
  6. Inside RemoteControl::stop(), write the following code to delete the object:

    if (mRemoteDesktop) {  
        LOGD(TAG, "Deleting mRemoteDesktop");  
        delete mRemoteDesktop;  
        mRemoteDesktop = NULL;  
    }
    
  7. Lastly, inside RemoteControl::screenChanged(), write the code that captures screen changes:

    bool err = mRemoteDesktop->captureScreen(dirtyRegion);
    // It uses “dirtyRegion” as its parameter.
    

Step 5: Running and using the app

Now, let’s see how the app works.

Building and running the app

  1. Clean and rebuild the project by going to Build > Rebuild Project.

  2. On your device (e.g. Galaxy Note10 or S10),

    a. Activate developer mode by going to Settings > About phone > Software information and tapping the Build number 7 times.

    b. Navigate back to the main Settings page and go to the Developer options category. Toggle on Stay awake and USB debugging.

  3. Back in Android Studio, select the device that you want to run your app on from the target device drop-down menu.

  4. Run the app by clicking Play button, pressing Shift + F10, or going to Run > Run ‘app’. Wait for it to load, and you should see this screen:

Using the app

Let’s look at how to run the complete version of the app.

  1. Tap Activate Admin. A pop-up screen will ask you to activate the device admin app, letting you to have admin control over your device. Tap Activate.

  2. Tap Activate License. Wait for a few seconds for a message at the bottom of the app to prompt you that license activation is successful.

  3. Tap Start Remote Capture. Play around with this feature by moving and going into different apps and pages on your phone.

  4. Once you’re finished, go back to the app and tap Stop Remote Capture.

  5. Return to your home screen and search for the My Files > Remote Capture folder. In there, you will see all the screen captures that happened between the moment you tapped Start Remote Capture and Stop Remote Capture.


You're done!

Congratulations! You have successfully achieved the goal of this Code Lab topic. Now, you can remotely view a device’s display using Knox SDK by yourself! You can learn more about remote viewing and how it caters to your business needs in our Knox SDK developer guide.

If you're having any trouble with the activity, you may check out the link below.

Remote Management Complete Code12.39 MB