Rate This Document
Findability
Accuracy
Completeness
Readability

Calling the API for Starting the Engine

Application Scenario

Start the instruction stream engine to receive and render screens of the cloud phone.

Prerequisites

  • The instruction stream engine has been created and initialized.
  • The logic thread has been started.
  • The SurfaceView control has been initialized.

Development Process

  1. Record the width and height in the SurfaceView control callback and start the connection thread.
  2. Call the startup function of the instruction stream engine in the connection thread.

Encoding Instance

public class Activity implements BaseActivity {
    private VmiSurfaceView surfaceView;
    private Boolean newlyCreated = true;
    private int guestWidth = 1080;
    private int guestHeight = 1920;
    DisplayMetrics metric = new DisplayMetrics();

    protected void onCreate(Bundle savedInstanceState) {
        WindowManager wm = (WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE);
        wm.getDefaultDisplay().getRealMetrics(metric);
        Log.i(TAG, "width pixels:" + metric.widthPixels + ", height pixels:" + metric.heightPixels
            + ",densityDpi:" + metric.densityDpi);
        guestWidth = metric.widthPixels;
        guestHeight = metric.heightPixels;
        initSurfaceHolderCallback(surfaceView.getHolder());
    }
    private void initSurfaceHolderCallback(SurfaceHolder surfaceHolder) {
       surfaceHolder.addCallback(new SurfaceHolder.Callback() {
           @Override
           public void surfaceCreated(SurfaceHolder surfaceHolder) {
               // Create a project for the first time.
               newlyCreated = true;
           }
           @Override
           public void surfaceChanged(SurfaceHolder inSurfaceHolder, int format, 
              int width, int height) {
               if (!newlyCreated) {
                   return;
               }
               newlyCreated = false;
               // The startup API involves socket connections. It is recommended that the API be called in a thread.
               threadPool.submit(new ConnectRunable());
           }
           @Override
           public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
               newlyCreated = false;
           }
      });
  }
  class ConnectRunable implements Runnable {
        @Override
        public void run() {
            // Call the startup API of the instruction stream engine.
            final int startResult = engine.start(surfaceView.getHolder().getSurface(),
                guestWidth, guestHeight, metric.densityDpi);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (startResult == InstructionEngine.VMI_CLIENT_START_FAIL) {
                      // Connection failed.
                    } else {
                        //  The connection is successful or has been established.
                    }
                }
            });
       }
  }