Skip to content

Commit

Permalink
Merge pull request #4 from zero-meta/master
Browse files Browse the repository at this point in the history
Android:add delete button.
  • Loading branch information
labolado authored Sep 2, 2024
2 parents ca0b334 + b626ec0 commit 8ad2954
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 28 deletions.
Binary file modified plugins/2020.3620/android/plugin.screenRecorder.aar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public class LuaLoader implements JavaFunction, CoronaRuntimeListener, HBRecorde
private int fListener;
private HBRecorder fRecorder;
private boolean fHasPermissions = false;
private boolean fIdleEnabled = true;

private int fSCREEN_RECORD_REQUEST_CODE = -1;
private int fSHARE_REQUEST_CODE = -2;
private int fPERMISSION_REQ_ID_RECORD_AUDIO = 22;
Expand Down Expand Up @@ -132,10 +134,30 @@ public int invoke(LuaState L) {
Log.d("Corona", "requestCode4: " + fPERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE);
}

getCurrentSystemIdleTimeFlag(L);

// Returning 1 indicates that the Lua require() function will return the above Lua library.
return 1;
}

private void getCurrentSystemIdleTimeFlag(LuaState L) {
L.getGlobal("system");
L.getField(-1, "getIdleTimer");
L.call(0, 1);
if (L.isBoolean(-1)) {
fIdleEnabled = L.toBoolean(-1);
}
L.pop(2);
}

private void setCurrentSystemIdleTimeFlag(LuaState L, boolean enable) {
L.getGlobal("system");
L.getField(-1, "setIdleTimer");
L.pushBoolean(enable);
L.call(1, 0);
L.pop(1);
}

private void quickSettings() {
fRecorder.setAudioBitrate(128000);
fRecorder.setAudioSamplingRate(44100);
Expand Down Expand Up @@ -447,14 +469,17 @@ private void startRecordingScreen() {
if (activity == null) {
return;
}
quickSettings();
MediaProjectionManager mediaProjectionManager = (MediaProjectionManager)activity.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
Intent permissionIntent = mediaProjectionManager != null ? mediaProjectionManager.createScreenCaptureIntent() : null;
activity.startActivityForResult(permissionIntent, fSCREEN_RECORD_REQUEST_CODE);
}

@SuppressWarnings("WeakerAccess")
public int start(LuaState L) {
if (fIdleEnabled) {
setCurrentSystemIdleTimeFlag(L, false);
}

int listenerIndex = 1;
if ( CoronaLua.isListener( L, listenerIndex, EVENT_NAME ) ) {
fListener = CoronaLua.newRef( L, listenerIndex );
Expand Down Expand Up @@ -497,6 +522,10 @@ public int start(LuaState L) {
public int stop(LuaState L) {
fRecorder.stopScreenRecording();

if (fIdleEnabled) {
setCurrentSystemIdleTimeFlag(L, true);
}

return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.MediaController;
import android.net.Uri;
Expand All @@ -15,46 +16,57 @@
import android.widget.TextView;
import android.widget.VideoView;

import android.content.ContentResolver;
import android.provider.MediaStore;
import java.io.File;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class VideoPreviewActivity extends AppCompatActivity {
private static final int SHARE_REQUEST_CODE = 77;

private int mOriginOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
private MediaController mMediaController;
private CustomVideoView mVideoView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

makeFullScreen();
hideStatusBar();
// keepScreenActive();

Intent intent = getIntent();
// Uri videoUri = (Uri)intent.getSerializableExtra("videoUri");
String videoFile = intent.getStringExtra("videoFile");
Uri videoUri = Uri.parse(videoFile);

mOriginOrientation = intent.getIntExtra("originOrientation", getRequestedOrientation());
setRequestedOrientation(mOriginOrientation);

setContentView(R.layout.video_preview);
CustomVideoView videoView = findViewById(R.id.videoPreview);
videoView.setVideoURI(videoUri);
mVideoView = findViewById(R.id.videoPreview);
mVideoView.setVideoURI(videoUri);

MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
mMediaController = new MediaController(this);
mMediaController.setAnchorView(mVideoView);
mVideoView.setMediaController(mMediaController);

final boolean[] firstStart = {false};
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
calculateView(videoView, mp.getVideoWidth(), mp.getVideoHeight());
calculateView(mVideoView, mp.getVideoWidth(), mp.getVideoHeight());
firstStart[0] = true;
videoView.start();
videoView.pause();
videoView.requestFocus();
mediaController.show();
if (mVideoView != null) {
mVideoView.start();
mVideoView.pause();
mVideoView.requestFocus();
}
if (mMediaController != null) {
mMediaController.show();
}
}
});

Expand All @@ -69,10 +81,6 @@ public void onClick(View v) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("video/mp4");
shareIntent.putExtra(Intent.EXTRA_STREAM, videoUri);
// shareIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, mFilename);
// shareIntent.putExtra(Intent.EXTRA_TITLE, mFilename);
// shareIntent.putExtra(Intent.EXTRA_TITLE, mFilename);
// activity.setRequestedOrientation(originOrientation);
startActivityForResult(
Intent.createChooser(shareIntent, getResources().getText(R.string.share_chooser_title)), SHARE_REQUEST_CODE);
// Intent chooserIntent = Intent.createChooser(shareIntent, getResources().getText(R.string.share_chooser_title));
Expand All @@ -94,6 +102,17 @@ public void onClick(View v) {
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clearResource();
finish();
}
});

ImageButton deleteButton = findViewById(R.id.deleteButton);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteVideo(videoUri);
clearResource();
finish();
}
});
Expand Down Expand Up @@ -130,26 +149,36 @@ public void onClick(View v) {
// }
// });

videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
titleBar.setVisibility(View.VISIBLE);
videoView.pause();
if (mVideoView != null) {
mVideoView.pause();
}
}
});

// popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
// @Override
// public void onDismiss() {
// videoView.stopPlayback();
// activity.setRequestedOrientation(activity.getOrientationFromManifest());
// }
// });

TextView title = findViewById(R.id.previewTitle);
title.setText(R.string.preview_title);
}

private void clearResource() {
if (mMediaController != null) {
mMediaController.hide();
mMediaController.setAnchorView(null);
mMediaController = null;
}

if (mVideoView != null) {
mVideoView.setMediaController(null);
mVideoView.setOnPreparedListener(null);
mVideoView.setOnCompletionListener(null);
mVideoView.stopPlayback();
mVideoView = null;
}
}

private void makeFullScreen() {
View decorView = getWindow().getDecorView();
int uiOptions = 0;
Expand Down Expand Up @@ -177,7 +206,19 @@ public void onSystemUiVisibilityChange(int visibilityInt)
}
}
});
}

private void hideStatusBar() {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
if (Build.VERSION.SDK_INT >= 28){
getWindow().getAttributes().layoutInDisplayCutoutMode
= WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}
}

private void keepScreenActive() {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

private void calculateView(VideoView videoView, int videoWidth, int videoHeight) {
Expand All @@ -195,6 +236,34 @@ private void reSetVideoViewWidth(VideoView videoView, int newWidth) {
videoView.setLayoutParams(lp);
}

private boolean deleteVideo(Uri videoUri) {
ContentResolver contentResolver = getContentResolver();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
try {
// For Android 10 (API 29) and above
return contentResolver.delete(videoUri, null, null) > 0;
} catch (SecurityException e) {
// If we don't have permission, try using MediaStore API
return deleteVideoUsingMediaStore(videoUri);
}
} else {
// For Android 5.0 (API 21) to Android 9 (API 28)
if (videoUri.getPath() != null) {
File file = new File(videoUri.getPath());
return file.exists() && file.delete();
}
}
return false;
}

private boolean deleteVideoUsingMediaStore(Uri videoUri) {
ContentResolver contentResolver = getContentResolver();
String selection = MediaStore.Video.Media._ID + "=?";
String[] selectionArgs = new String[]{videoUri.getLastPathSegment()};
return contentResolver.delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, selection, selectionArgs) > 0;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Expand All @@ -208,6 +277,7 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten

@Override
protected void onDestroy() {
clearResource();
super.onDestroy();
}
}
}
10 changes: 10 additions & 0 deletions src/android/plugin/src/main/res/layout/video_preview.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@
android:paddingTop="16dp"
android:src="@android:drawable/ic_menu_share" />

<ImageButton
android:id="@+id/deleteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:background="@android:color/transparent"
android:paddingTop="16dp"
android:paddingEnd="64dp"
android:src="@android:drawable/ic_menu_delete" />

<ImageButton
android:id="@+id/closeButton"
android:layout_width="wrap_content"
Expand Down

0 comments on commit 8ad2954

Please sign in to comment.