Skip to content

Commit

Permalink
Added new Constants.java file to capture values used within HBRecorder
Browse files Browse the repository at this point in the history
Modified HBRecorder to utilize the above mentioned Constants

Added maxFileSize property to HBRecorder, allowing it to be set from calling programs and having it passed along to ScreenRecordService.

Updated HBRecorder to utilize int values for error codes as specified in Constants.java, including a new code indicating that the specified max file size has been reached (and have it automatically stop recording).

Updated ScreenRecordService to utilize values from Constants.java

Updated ScreenRecordService to implement setting a max file size on the media recorder.

Added a string.xml resource to the hbrecorder module to capture strings used there.

Updated gradle to target version 30.
  • Loading branch information
tfrysinger committed Apr 6, 2021
1 parent 78d5967 commit b21fbe1
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 35 deletions.
12 changes: 6 additions & 6 deletions hbrecorder/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 29
compileSdkVersion 30


defaultConfig {
minSdkVersion 17
targetSdkVersion 29
targetSdkVersion 30
versionCode 1
versionName "1.0"

Expand All @@ -26,8 +26,8 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'androidx.appcompat:appcompat:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
15 changes: 15 additions & 0 deletions hbrecorder/src/main/java/com/hbisoft/hbrecorder/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.hbisoft.hbrecorder;

public class Constants {
public final static String MAX_FILE_SIZE_KEY = "maxFileSize";
public final static String ERROR_REASON_KEY = "errorReason";
public final static String ERROR_KEY = "error";
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 int SETTINGS_ERROR = 38;
public final static int MAX_FILE_SIZE_REACHED_ERROR = 48;
public final static int GENERAL_ERROR = 100;
public final static int ON_START = 111;
public final static int NO_SPECIFIED_MAX_SIZE = 0;
}
36 changes: 27 additions & 9 deletions hbrecorder/src/main/java/com/hbisoft/hbrecorder/HBRecorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,19 @@
import java.io.ByteArrayOutputStream;
import java.io.File;

import static com.hbisoft.hbrecorder.Constants.ERROR_KEY;
import static com.hbisoft.hbrecorder.Constants.ERROR_REASON_KEY;
import static com.hbisoft.hbrecorder.Constants.GENERAL_ERROR;
import static com.hbisoft.hbrecorder.Constants.MAX_FILE_SIZE_KEY;
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;

/**
* Created by HBiSoft on 13 Aug 2019
* Copyright (c) 2019 . All rights reserved.
*/

@SuppressWarnings("deprecation")
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class HBRecorder implements MyListener {
private int mScreenWidth;
Expand Down Expand Up @@ -57,6 +64,7 @@ public class HBRecorder implements MyListener {
private int videoBitrate = 40000000;
private String outputFormat = "DEFAULT";
private int orientation;
private long maxFileSize = NO_SPECIFIED_MAX_SIZE; // Default no max size
boolean wasOnErrorCalled = false;
Intent service;
boolean isPaused = false;
Expand Down Expand Up @@ -85,6 +93,10 @@ public void setOutputUri(Uri uri){
mUri = uri;
}

public void setMaxFileSize(long fileSize) {
maxFileSize = fileSize;
}

public boolean wasUriSet(){
return mWasUriSet;
}
Expand Down Expand Up @@ -311,19 +323,23 @@ private void startService(Intent data) {
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
if (resultCode == Activity.RESULT_OK) {
String errorListener = resultData.getString("errorReason");
String onComplete = resultData.getString("onComplete");
String onStart = resultData.getString("onStart");

String errorListener = resultData.getString(ERROR_REASON_KEY);
String onComplete = resultData.getString(ON_COMPLETE_KEY);
int onStartCode = resultData.getInt(ON_START_KEY);
int errorCode = resultData.getInt(ERROR_KEY);
if (errorListener != null) {
if (!mWasUriSet) {
observer.stopWatching();
}
wasOnErrorCalled = true;
hbRecorderListener.HBRecorderOnError(100, errorListener);
if ( errorCode > 0 ) {
hbRecorderListener.HBRecorderOnError(errorCode, errorListener);
} else {
hbRecorderListener.HBRecorderOnError(GENERAL_ERROR, errorListener);
}
try {
Intent mservice = new Intent(context, ScreenRecordService.class);
context.stopService(mservice);
Intent mService = new Intent(context, ScreenRecordService.class);
context.stopService(mService);
}catch (Exception e){
// Can be ignored
}
Expand All @@ -334,12 +350,14 @@ protected void onReceiveResult(int resultCode, Bundle resultData) {
hbRecorderListener.HBRecorderOnComplete();
}
wasOnErrorCalled = false;
}else if (onStart != null){
}else if (onStartCode != 0){
hbRecorderListener.HBRecorderOnStart();
}
}
}
});
// Max file size
service.putExtra(MAX_FILE_SIZE_KEY, maxFileSize);
context.startService(service);
}catch (Exception e){
hbRecorderListener.HBRecorderOnError(0, Log.getStackTraceString(e));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,27 @@
import java.util.Locale;
import java.util.Objects;

import static com.hbisoft.hbrecorder.Constants.ERROR_KEY;
import static com.hbisoft.hbrecorder.Constants.ERROR_REASON_KEY;
import static com.hbisoft.hbrecorder.Constants.MAX_FILE_SIZE_REACHED_ERROR;
import static com.hbisoft.hbrecorder.Constants.MAX_FILE_SIZE_KEY;
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_START;
import static com.hbisoft.hbrecorder.Constants.ON_START_KEY;
import static com.hbisoft.hbrecorder.Constants.SETTINGS_ERROR;

/**
* Created by HBiSoft on 13 Aug 2019
* Copyright (c) 2019 . All rights reserved.
*/

@SuppressWarnings("deprecation")
public class ScreenRecordService extends Service {

private static final String TAG = "ScreenRecordService";

private long maxFileSize = NO_SPECIFIED_MAX_SIZE;
private boolean hasMaxFileBeenReached = false;
private int mScreenWidth;
private int mScreenHeight;
private int mScreenDensity;
Expand Down Expand Up @@ -93,7 +104,9 @@ else if (pauseResumeAction != null && pauseResumeAction.equals("resume")){
//Start Recording
else {
//Get intent extras
hasMaxFileBeenReached = false;
mIntent = intent;
maxFileSize = intent.getLongExtra(MAX_FILE_SIZE_KEY, NO_SPECIFIED_MAX_SIZE);
byte[] notificationSmallIcon = intent.getByteArrayExtra("notificationSmallBitmap");
String notificationTitle = intent.getStringExtra("notificationTitle");
String notificationDescription = intent.getStringExtra("notificationDescription");
Expand Down Expand Up @@ -137,7 +150,7 @@ else if (pauseResumeAction != null && pauseResumeAction.equals("resume")){
audioSamplingRate = intent.getIntExtra("audioSamplingRate", 44100);
String outputFormat = intent.getStringExtra("outputFormat");
if (outputFormat != null) {
setOutputformatAsInt(outputFormat);
setOutputFormatAsInt(outputFormat);
}

isCustomSettingsEnabled = intent.getBooleanExtra("enableCustomSettings", false);
Expand All @@ -156,11 +169,11 @@ else if (pauseResumeAction != null && pauseResumeAction.equals("resume")){
}
//Set notification title if developer did not
if (notificationTitle == null || notificationTitle.equals("")) {
notificationTitle = "Recording your screen";
notificationTitle = getString(R.string.stop_recording_notification_title);
}
//Set notification description if developer did not
if (notificationDescription == null || notificationDescription.equals("")) {
notificationDescription = "Drag down to stop the recording";
notificationDescription = getString(R.string.stop_recording_notification_message);
}

//Notification
Expand Down Expand Up @@ -212,7 +225,7 @@ else if (pauseResumeAction != null && pauseResumeAction.equals("resume")){
} catch (Exception e) {
ResultReceiver receiver = intent.getParcelableExtra(ScreenRecordService.BUNDLED_LISTENER);
Bundle bundle = new Bundle();
bundle.putString("errorReason", Log.getStackTraceString(e));
bundle.putString(ERROR_REASON_KEY, Log.getStackTraceString(e));
if (receiver != null) {
receiver.send(Activity.RESULT_OK, bundle);
}
Expand All @@ -224,7 +237,7 @@ else if (pauseResumeAction != null && pauseResumeAction.equals("resume")){
} catch (Exception e) {
ResultReceiver receiver = intent.getParcelableExtra(ScreenRecordService.BUNDLED_LISTENER);
Bundle bundle = new Bundle();
bundle.putString("errorReason", Log.getStackTraceString(e));
bundle.putString(ERROR_REASON_KEY, Log.getStackTraceString(e));
if (receiver != null) {
receiver.send(Activity.RESULT_OK, bundle);
}
Expand All @@ -236,40 +249,61 @@ else if (pauseResumeAction != null && pauseResumeAction.equals("resume")){
} catch (Exception e) {
ResultReceiver receiver = intent.getParcelableExtra(ScreenRecordService.BUNDLED_LISTENER);
Bundle bundle = new Bundle();
bundle.putString("errorReason", Log.getStackTraceString(e));
bundle.putString(ERROR_REASON_KEY, Log.getStackTraceString(e));
if (receiver != null) {
receiver.send(Activity.RESULT_OK, bundle);
}
}

mMediaRecorder.setOnErrorListener(new MediaRecorder.OnErrorListener() {
@Override
public void onError(MediaRecorder mediaRecorder, int i, int i1) {
public void onError(MediaRecorder mediaRecorder, int what, int extra) {
if ( what == 268435556 && hasMaxFileBeenReached) {
// Benign error b/c recording is too short and has no frames. See SO: https://stackoverflow.com/questions/40616466/mediarecorder-stop-failed-1007
return;
}
ResultReceiver receiver = intent.getParcelableExtra(ScreenRecordService.BUNDLED_LISTENER);
Bundle bundle = new Bundle();
bundle.putString("error", "38");
bundle.putString("errorReason", String.valueOf(i));
bundle.putInt(ERROR_KEY, SETTINGS_ERROR);
bundle.putString(ERROR_REASON_KEY, String.valueOf(what));
if (receiver != null) {
receiver.send(Activity.RESULT_OK, bundle);
}
}
});

mMediaRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED) {
hasMaxFileBeenReached = true;
Log.i(TAG,String.format(Locale.US,"onInfoListen what : %d | extra %d", what, extra));
ResultReceiver receiver = intent.getParcelableExtra(ScreenRecordService.BUNDLED_LISTENER);
Bundle bundle = new Bundle();
bundle.putInt(ERROR_KEY, MAX_FILE_SIZE_REACHED_ERROR);
bundle.putString(ERROR_REASON_KEY, getString(R.string.max_file_reached));
if (receiver != null) {
receiver.send(Activity.RESULT_OK, bundle);
}
}
}
});

//Start Recording
try {
mMediaRecorder.start();
ResultReceiver receiver = intent.getParcelableExtra(ScreenRecordService.BUNDLED_LISTENER);
Bundle bundle = new Bundle();
bundle.putString("onStart", "111");
bundle.putInt(ON_START_KEY, ON_START);
if (receiver != null) {
receiver.send(Activity.RESULT_OK, bundle);
}
} catch (Exception e) {
// From the tests I've done, this can happen if another application is using the mic or if an unsupported video encoder was selected
ResultReceiver receiver = intent.getParcelableExtra(ScreenRecordService.BUNDLED_LISTENER);
Bundle bundle = new Bundle();
bundle.putString("error", "38");
bundle.putString("errorReason", Log.getStackTraceString(e));
bundle.putInt(ERROR_KEY, SETTINGS_ERROR);
bundle.putString(ERROR_REASON_KEY, Log.getStackTraceString(e));
if (receiver != null) {
receiver.send(Activity.RESULT_OK, bundle);
}
Expand All @@ -294,17 +328,14 @@ private void resumeRecording(){

//Set output format as int based on what developer has provided
//It is important to provide one of the following and nothing else.
private void setOutputformatAsInt(String outputFormat) {
private void setOutputFormatAsInt(String outputFormat) {
switch (outputFormat) {
case "DEFAULT":
outputFormatAsInt = 0;
break;
case "THREE_GPP":
outputFormatAsInt = 1;
break;
case "MPEG_4":
outputFormatAsInt = 2;
break;
case "AMR_NB":
outputFormatAsInt = 3;
break;
Expand All @@ -323,6 +354,7 @@ private void setOutputformatAsInt(String outputFormat) {
case "OGG":
outputFormatAsInt = 11;
break;
case "MPEG_4":
default:
outputFormatAsInt = 2;
}
Expand Down Expand Up @@ -455,7 +487,7 @@ private void initRecorder() throws Exception {
} catch (Exception e) {
ResultReceiver receiver = mIntent.getParcelableExtra(ScreenRecordService.BUNDLED_LISTENER);
Bundle bundle = new Bundle();
bundle.putString("errorReason", Log.getStackTraceString(e));
bundle.putString(ERROR_REASON_KEY, Log.getStackTraceString(e));
if (receiver != null) {
receiver.send(Activity.RESULT_OK, bundle);
}
Expand All @@ -478,6 +510,10 @@ private void initRecorder() throws Exception {
mMediaRecorder.setVideoFrameRate(videoFrameRate);
}

// Catch approaching file limit
if ( maxFileSize > NO_SPECIFIED_MAX_SIZE) {
mMediaRecorder.setMaxFileSize(maxFileSize); // in bytes
}

mMediaRecorder.prepare();

Expand All @@ -500,7 +536,7 @@ public void onDestroy() {
private void callOnComplete() {
ResultReceiver receiver = mIntent.getParcelableExtra(ScreenRecordService.BUNDLED_LISTENER);
Bundle bundle = new Bundle();
bundle.putString("onComplete", "Uri was passed");
bundle.putString(ON_COMPLETE_KEY, ON_COMPLETE);
if (receiver != null) {
receiver.send(Activity.RESULT_OK, bundle);
}
Expand Down
3 changes: 3 additions & 0 deletions hbrecorder/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<resources>
<string name="app_name">HBRecorder</string>
<string name="max_file_reached">File size max has been reached.</string>
<string name="stop_recording_notification_message">Drag down to stop the recording</string>
<string name="stop_recording_notification_title">Recording your screen</string>
</resources>

0 comments on commit b21fbe1

Please sign in to comment.