package com.javispedro.vndroid; import android.app.Activity; import android.content.Intent; import android.media.projection.MediaProjectionManager; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; public class SetupActivity extends AppCompatActivity { private static String TAG = SetupActivity.class.getSimpleName(); private static final int REQUEST_MEDIA_PROJECTION = 1; private boolean mirror = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_setup); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case REQUEST_MEDIA_PROJECTION: if (resultCode != Activity.RESULT_OK) { Log.w(TAG, "User cancelled media projection"); return; } notifyMediaProjectionResult(resultCode, data); startServer(); break; } } private void startServer() { Intent intent = new Intent(this, ServerService.class); intent.setAction(ServerService.ACTION_START); startService(intent); } private void stopServer() { Intent intent = new Intent(this, ServerService.class); intent.setAction(ServerService.ACTION_STOP); startService(intent); } private void notifyMediaProjectionResult(int resultCode, Intent resultData) { Intent intent = new Intent(this, ServerService.class); intent.setAction(ServerService.ACTION_NOTIFY_MEDIA_PROJECTION_RESULT); intent.putExtra("resultCode", resultCode); intent.putExtra("resultData", resultData); startService(intent); } public void onStartClick(View view) { Log.d(TAG, "onStartClick"); if (mirror) { MediaProjectionManager manager = getSystemService(MediaProjectionManager.class); startActivityForResult(manager.createScreenCaptureIntent(), REQUEST_MEDIA_PROJECTION); } else { startServer(); } } public void onStopClick(View view) { Log.d(TAG, "onStopClick"); stopServer(); } }