Beispiel #1
0
  @CalledByNative
  public void startCapture() {
    Log.d(TAG, "startCapture");
    synchronized (mCaptureStateLock) {
      if (mCaptureState != CaptureState.ALLOWED) {
        Log.e(TAG, "startCapture() invoked without user permission.");
        return;
      }
    }
    mMediaProjection = mMediaProjectionManager.getMediaProjection(mResultCode, mResultData);
    if (mMediaProjection == null) {
      Log.e(TAG, "mMediaProjection is null");
      return;
    }
    mMediaProjection.registerCallback(new MediaProjectionCallback(), null);

    mThread = new HandlerThread("ScreenCapture");
    mThread.start();
    mBackgroundHandler = new Handler(mThread.getLooper());

    // On Android M and above, YUV420 is prefered. Some Android L devices will silently
    // fail with YUV420, so keep with RGBA_8888 on L.
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
      mFormat = PixelFormat.RGBA_8888;
    } else {
      mFormat = ImageFormat.YUV_420_888;
    }
    maybeDoRotation();
    createImageReaderWithFormat();
    createVirtualDisplay();

    changeCaptureStateAndNotify(CaptureState.STARTED);
  }
 private void shareScreen() {
   mScreenSharing = true;
   if (mSurface == null) {
     return;
   }
   if (mMediaProjection == null) {
     startActivityForResult(mProjectionManager.createScreenCaptureIntent(), PERMISSION_CODE);
     return;
   }
   mVirtualDisplay = createVirtualDisplay();
 }
 @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode != PERMISSION_CODE) {
     Log.e(TAG, "Unknown request code: " + requestCode);
     return;
   }
   if (resultCode != RESULT_OK) {
     Toast.makeText(this, "User denied screen sharing permission", Toast.LENGTH_SHORT).show();
     return;
   }
   mMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);
   mMediaProjection.registerCallback(new MediaProjectionCallback(), null);
   mVirtualDisplay = createVirtualDisplay();
 }
  // region Media Projection
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE) {
      mMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);

      if (mMediaProjection != null) {
        store_directory =
            Environment.getExternalStorageDirectory().getAbsolutePath() + "/MediaProjection/";
        File storeDirectory = new File(store_directory);
        if (!storeDirectory.exists()) {
          storeDirectory.mkdirs();
          if (!storeDirectory.isDirectory()) {
            Log.e(TAG, "failed to create file storage directory.");
            return;
          }
        }

        DisplayMetrics metrics = getResources().getDisplayMetrics();
        int density = metrics.densityDpi;
        int flags =
            DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR
                | DisplayManager
                    .VIRTUAL_DISPLAY_FLAG_PUBLIC; // DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        mWidth = size.x;
        mHeight = size.y;

        mImageReader =
            ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888 /*ImageFormat.JPEG*/, 2);
        mImageReader.setOnImageAvailableListener(new TestImageAvailableListener(), null);
        mVirtualDisplay =
            mMediaProjection.createVirtualDisplay(
                "screencap",
                mWidth,
                mHeight,
                density,
                flags,
                mImageReader.getSurface(),
                mVirtualDisplayCallback,
                null);
      }
    }
  }
  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  private void takeScreenShot() {
    // call for the projection manager
    mProjectionManager =
        (MediaProjectionManager)
            trainingActivity.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
    // start capture handling thread
    new Thread() {
      @Override
      public void run() {
        Looper.prepare();
        mImageHandler = new Handler();
        Looper.loop();
      }
    }.start();

    trainingActivity.startActivityForResult(
        mProjectionManager.createScreenCaptureIntent(), TrainingActivity.IMAGE_REQUEST_CODE);
  }
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE) {

      mediaProjection = projectionManager.getMediaProjection(resultCode, data);
      if (mediaProjection != null) {

        projectionStarted = true;

        // Initialize the media projection
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        int density = metrics.densityDpi;
        int flags =
            DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY
                | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);

        displayWidth = size.x;
        displayHeight = size.y;

        imageReader =
            ImageReader.newInstance(displayWidth, displayHeight, PixelFormat.RGBA_8888, 2);
        mediaProjection.createVirtualDisplay(
            "screencap",
            displayWidth,
            displayHeight,
            density,
            flags,
            imageReader.getSurface(),
            null,
            handler);
        imageReader.setOnImageAvailableListener(new ImageAvailableListener(), handler);
      }
    }
  }
Beispiel #7
0
  @CalledByNative
  public boolean startPrompt() {
    Log.d(TAG, "startPrompt");
    Activity activity = ApplicationStatus.getLastTrackedFocusedActivity();
    if (activity == null) {
      Log.e(TAG, "activity is null");
      return false;
    }
    FragmentManager fragmentManager = activity.getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(this, "screencapture");
    try {
      fragmentTransaction.commit();
    } catch (RuntimeException e) {
      Log.e(TAG, "ScreenCaptureExcaption " + e);
      return false;
    }

    synchronized (mCaptureStateLock) {
      while (mCaptureState != CaptureState.ATTACHED) {
        try {
          mCaptureStateLock.wait();
        } catch (InterruptedException ex) {
          Log.e(TAG, "ScreenCaptureException: " + ex);
        }
      }
    }

    try {
      startActivityForResult(
          mMediaProjectionManager.createScreenCaptureIntent(), REQUEST_MEDIA_PROJECTION);
    } catch (android.content.ActivityNotFoundException e) {
      Log.e(TAG, "ScreenCaptureException " + e);
      return false;
    }
    return true;
  }
 private void startProjection() {
   startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);
 }