diff --git a/app/build.gradle b/app/build.gradle index b32d9f3..b91a0c1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,11 +1,11 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 31 + compileSdkVersion 33 defaultConfig { applicationId "com.hbisoft.hbrecorderexample" minSdkVersion 17 - targetSdkVersion 31 + targetSdkVersion 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" diff --git a/app/release/HBRecorderDemo.apk b/app/release/HBRecorderDemo.apk index 3b55f38..2fb1bc4 100644 Binary files a/app/release/HBRecorderDemo.apk and b/app/release/HBRecorderDemo.apk differ diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 56fd3f0..c00918d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -6,6 +6,7 @@ + = Build.VERSION_CODES.LOLLIPOP) { - //first check if permissions was granted - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - if (checkSelfPermission(Manifest.permission.RECORD_AUDIO, PERMISSION_REQ_ID_RECORD_AUDIO)) { - hasPermissions = true; - } - } else { - if (checkSelfPermission(Manifest.permission.RECORD_AUDIO, PERMISSION_REQ_ID_RECORD_AUDIO) && checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE)) { - hasPermissions = true; - } + startbtn.setOnClickListener(v -> { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + //first check if permissions was granted + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS, PERMISSION_REQ_POST_NOTIFICATIONS) && checkSelfPermission(Manifest.permission.RECORD_AUDIO, PERMISSION_REQ_ID_RECORD_AUDIO)) { + hasPermissions = true; } - - if (hasPermissions) { - //check if recording is in progress - //and stop it if it is - if (hbRecorder.isBusyRecording()) { - hbRecorder.stopScreenRecording(); - startbtn.setText(R.string.start_recording); - } - //else start recording - else { - startRecordingScreen(); - } + } + else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + if (checkSelfPermission(Manifest.permission.RECORD_AUDIO, PERMISSION_REQ_ID_RECORD_AUDIO)) { + hasPermissions = true; } } else { - showLongToast("This library requires API 21>"); + if (checkSelfPermission(Manifest.permission.RECORD_AUDIO, PERMISSION_REQ_ID_RECORD_AUDIO) && checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE)) { + hasPermissions = true; + } + } + + if (hasPermissions) { + //check if recording is in progress + //and stop it if it is + if (hbRecorder.isBusyRecording()) { + hbRecorder.stopScreenRecording(); + startbtn.setText(R.string.start_recording); + } + //else start recording + else { + startRecordingScreen(); + } } + } else { + showLongToast("This library requires API 21>"); } }); } @@ -258,6 +261,7 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { }); } + // Called when recording starts @Override public void HBRecorderOnStart() { Log.e("HBRecorder", "HBRecorderOnStart called"); @@ -284,25 +288,7 @@ public void HBRecorderOnComplete() { } - @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) - private void refreshGalleryFile() { - MediaScannerConnection.scanFile(this, - new String[]{hbRecorder.getFilePath()}, null, - new MediaScannerConnection.OnScanCompletedListener() { - public void onScanCompleted(String path, Uri uri) { - Log.i("ExternalStorage", "Scanned " + path + ":"); - Log.i("ExternalStorage", "-> uri=" + uri); - } - }); - } - - @RequiresApi(api = Build.VERSION_CODES.Q) - private void updateGalleryUri(){ - contentValues.clear(); - contentValues.put(MediaStore.Video.Media.IS_PENDING, 0); - getContentResolver().update(mUri, contentValues, null, null); - } - + // Called when error occurs @Override public void HBRecorderOnError(int errorCode, String reason) { // Error 38 happens when @@ -325,6 +311,37 @@ public void HBRecorderOnError(int errorCode, String reason) { } + // Called when recording has been paused + @Override + public void HBRecorderOnPause() { + // Called when recording was paused + } + + // Calld when recording has resumed + @Override + public void HBRecorderOnResume() { + // Called when recording was resumed + } + + @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) + private void refreshGalleryFile() { + MediaScannerConnection.scanFile(this, + new String[]{hbRecorder.getFilePath()}, null, + new MediaScannerConnection.OnScanCompletedListener() { + public void onScanCompleted(String path, Uri uri) { + Log.i("ExternalStorage", "Scanned " + path + ":"); + Log.i("ExternalStorage", "-> uri=" + uri); + } + }); + } + + @RequiresApi(api = Build.VERSION_CODES.Q) + private void updateGalleryUri(){ + contentValues.clear(); + contentValues.put(MediaStore.Video.Media.IS_PENDING, 0); + getContentResolver().update(mUri, contentValues, null, null); + } + //Start recording screen //It is important to call it like this //hbRecorder.startScreenRecording(data); should only be called in onActivityResult @@ -347,6 +364,7 @@ private void startRecordingScreen() { } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) + // Example of how to set custom settings private void customSettings() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); @@ -557,6 +575,14 @@ private boolean checkSelfPermission(String permission, int requestCode) { public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { + case PERMISSION_REQ_POST_NOTIFICATIONS: + if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { + checkSelfPermission(Manifest.permission.RECORD_AUDIO, PERMISSION_REQ_ID_RECORD_AUDIO); + } else { + hasPermissions = false; + showLongToast("No permission for " + Manifest.permission.POST_NOTIFICATIONS); + } + break; case PERMISSION_REQ_ID_RECORD_AUDIO: if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE); diff --git a/hbrecorder/build.gradle b/hbrecorder/build.gradle index e8649c5..d593504 100644 --- a/hbrecorder/build.gradle +++ b/hbrecorder/build.gradle @@ -1,11 +1,11 @@ apply plugin: 'com.android.library' android { - compileSdkVersion 31 + compileSdkVersion 33 defaultConfig { minSdkVersion 17 - targetSdkVersion 31 + targetSdkVersion 33 versionCode 1 versionName "1.0" diff --git a/hbrecorder/src/main/java/com/hbisoft/hbrecorder/Constants.java b/hbrecorder/src/main/java/com/hbisoft/hbrecorder/Constants.java index 397ec52..a1a6c52 100644 --- a/hbrecorder/src/main/java/com/hbisoft/hbrecorder/Constants.java +++ b/hbrecorder/src/main/java/com/hbisoft/hbrecorder/Constants.java @@ -7,6 +7,10 @@ public class Constants { public final static String ON_COMPLETE_KEY = "onComplete"; public final static String ON_START_KEY = "onStart"; public final static String ON_COMPLETE = "Uri was passed"; + public final static String ON_PAUSE_KEY = "onPause"; + public final static String ON_RESUME_KEY = "onResume"; + public final static String ON_PAUSE = "Paused"; + public final static String ON_RESUME = "Resumed"; public final static int SETTINGS_ERROR = 38; public final static int MAX_FILE_SIZE_REACHED_ERROR = 48; public final static int GENERAL_ERROR = 100; diff --git a/hbrecorder/src/main/java/com/hbisoft/hbrecorder/HBRecorder.java b/hbrecorder/src/main/java/com/hbisoft/hbrecorder/HBRecorder.java index 63acd82..b271d7d 100644 --- a/hbrecorder/src/main/java/com/hbisoft/hbrecorder/HBRecorder.java +++ b/hbrecorder/src/main/java/com/hbisoft/hbrecorder/HBRecorder.java @@ -34,6 +34,8 @@ import static com.hbisoft.hbrecorder.Constants.NO_SPECIFIED_MAX_SIZE; import static com.hbisoft.hbrecorder.Constants.ON_COMPLETE_KEY; import static com.hbisoft.hbrecorder.Constants.ON_START_KEY; +import static com.hbisoft.hbrecorder.Constants.ON_PAUSE_KEY; +import static com.hbisoft.hbrecorder.Constants.ON_RESUME_KEY; /** * Created by HBiSoft on 13 Aug 2019 @@ -374,6 +376,7 @@ protected void onReceiveResult(int resultCode, Bundle resultData) { String onComplete = resultData.getString(ON_COMPLETE_KEY); int onStartCode = resultData.getInt(ON_START_KEY); int errorCode = resultData.getInt(ERROR_KEY); + // There was an error if (errorListener != null) { //Stop countdown if it was set stopCountDown(); @@ -393,7 +396,9 @@ protected void onReceiveResult(int resultCode, Bundle resultData) { // Can be ignored } - }else if (onComplete != null){ + } + // OnComplete was called + else if (onComplete != null){ //Stop countdown if it was set stopCountDown(); //OnComplete for when Uri was passed @@ -401,13 +406,25 @@ protected void onReceiveResult(int resultCode, Bundle resultData) { hbRecorderListener.HBRecorderOnComplete(); } wasOnErrorCalled = false; - }else if (onStartCode != 0){ + } + // OnStart was called + else if (onStartCode != 0){ hbRecorderListener.HBRecorderOnStart(); //Check if max duration was set and start count down if (isMaxDurationSet){ startCountdown(); } } + // OnPause/onResume was called + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + String onPause = resultData.getString(ON_PAUSE_KEY); + String onResume = resultData.getString(ON_RESUME_KEY); + if (onPause != null) { + hbRecorderListener.HBRecorderOnPause(); + } else if (onResume != null) { + hbRecorderListener.HBRecorderOnResume(); + } + } } } }); diff --git a/hbrecorder/src/main/java/com/hbisoft/hbrecorder/HBRecorderListener.java b/hbrecorder/src/main/java/com/hbisoft/hbrecorder/HBRecorderListener.java index 0848873..cc1e715 100644 --- a/hbrecorder/src/main/java/com/hbisoft/hbrecorder/HBRecorderListener.java +++ b/hbrecorder/src/main/java/com/hbisoft/hbrecorder/HBRecorderListener.java @@ -1,7 +1,15 @@ package com.hbisoft.hbrecorder; +import android.os.Build; + +import androidx.annotation.RequiresApi; + public interface HBRecorderListener { void HBRecorderOnStart(); void HBRecorderOnComplete(); void HBRecorderOnError(int errorCode, String reason); + @RequiresApi(api = Build.VERSION_CODES.N) + void HBRecorderOnPause(); + @RequiresApi(api = Build.VERSION_CODES.N) + void HBRecorderOnResume(); } diff --git a/hbrecorder/src/main/java/com/hbisoft/hbrecorder/ScreenRecordService.java b/hbrecorder/src/main/java/com/hbisoft/hbrecorder/ScreenRecordService.java index 62535b7..485b852 100755 --- a/hbrecorder/src/main/java/com/hbisoft/hbrecorder/ScreenRecordService.java +++ b/hbrecorder/src/main/java/com/hbisoft/hbrecorder/ScreenRecordService.java @@ -42,6 +42,10 @@ import static com.hbisoft.hbrecorder.Constants.NO_SPECIFIED_MAX_SIZE; import static com.hbisoft.hbrecorder.Constants.ON_COMPLETE; import static com.hbisoft.hbrecorder.Constants.ON_COMPLETE_KEY; +import static com.hbisoft.hbrecorder.Constants.ON_PAUSE; +import static com.hbisoft.hbrecorder.Constants.ON_PAUSE_KEY; +import static com.hbisoft.hbrecorder.Constants.ON_RESUME; +import static com.hbisoft.hbrecorder.Constants.ON_RESUME_KEY; import static com.hbisoft.hbrecorder.Constants.ON_START; import static com.hbisoft.hbrecorder.Constants.ON_START_KEY; import static com.hbisoft.hbrecorder.Constants.SETTINGS_ERROR; @@ -341,12 +345,24 @@ public void onInfo(MediaRecorder mr, int what, int extra) { @RequiresApi(api = Build.VERSION_CODES.N) private void pauseRecording(){ mMediaRecorder.pause(); + ResultReceiver receiver = mIntent.getParcelableExtra(ScreenRecordService.BUNDLED_LISTENER); + Bundle bundle = new Bundle(); + bundle.putString(ON_PAUSE_KEY, ON_PAUSE); + if (receiver != null) { + receiver.send(Activity.RESULT_OK, bundle); + } } //Resume Recording @RequiresApi(api = Build.VERSION_CODES.N) private void resumeRecording(){ mMediaRecorder.resume(); + ResultReceiver receiver = mIntent.getParcelableExtra(ScreenRecordService.BUNDLED_LISTENER); + Bundle bundle = new Bundle(); + bundle.putString(ON_RESUME_KEY, ON_RESUME); + if (receiver != null) { + receiver.send(Activity.RESULT_OK, bundle); + } } //Set output format as int based on what developer has provided