Skip to content

Commit

Permalink
Added option to pass Uri (For Android 10>)
Browse files Browse the repository at this point in the history
  • Loading branch information
HBiSoft committed Jul 29, 2020
1 parent 17f91c1 commit 4c7c52e
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 37 deletions.
7 changes: 3 additions & 4 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file removed app/release/HBRecorderDemo.apk
Binary file not shown.
1 change: 0 additions & 1 deletion app/release/output.json

This file was deleted.

78 changes: 66 additions & 12 deletions app/src/main/java/com/hbisoft/hbrecorderexample/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.hbisoft.hbrecorderexample;

import android.Manifest;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
Expand All @@ -14,6 +16,7 @@
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -37,6 +40,9 @@

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Locale;


/**
Expand Down Expand Up @@ -69,6 +75,7 @@
*
* */

@SuppressWarnings({"deprecation", "SameParameterValue"})
public class MainActivity extends AppCompatActivity implements HBRecorderListener {
//Permissions
private static final int SCREEN_RECORD_REQUEST_CODE = 777;
Expand Down Expand Up @@ -101,18 +108,14 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

createFolder();
initViews();
mOnClickListeners();
radioGroupCheckListener();
recordAudioCheckBoxListener();
setOnClickListeners();
setRadioGroupCheckListener();
setRecordAudioCheckBoxListener();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//Init HBRecorder
hbRecorder = new HBRecorder(this, this);
hbRecorder.setOutputPath(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) +"/HBRecorder");
hbRecorder.setAudioBitrate(128000);
hbRecorder.setAudioSamplingRate(44100);

//When the user returns to the application, some UI changes might be necessary,
//check if recording is in progress and make changes accordingly
Expand All @@ -124,6 +127,8 @@ protected void onCreate(Bundle savedInstanceState) {
}

//Create Folder
//Only call this on Android 9 and lower (getExternalStoragePublicDirectory is deprecated)
//This can still be used on Android 10> but you will have to add android:requestLegacyExternalStorage="true" in your Manifest
private void createFolder() {
File f1 = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), "HBRecorder");
if (!f1.exists()) {
Expand All @@ -142,7 +147,7 @@ private void initViews() {
}

//Start Button OnClickListener
private void mOnClickListeners() {
private void setOnClickListeners() {
startbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand Down Expand Up @@ -171,7 +176,7 @@ public void onClick(View v) {
}

//Check if HD/SD Video should be recorded
private void radioGroupCheckListener() {
private void setRadioGroupCheckListener() {
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
Expand All @@ -191,7 +196,7 @@ public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
}

//Check if audio should be recorded
private void recordAudioCheckBoxListener() {
private void setRecordAudioCheckBoxListener() {
recordAudioCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
Expand All @@ -208,13 +213,18 @@ public void HBRecorderOnComplete() {
startbtn.setText(R.string.start_recording);
showLongToast("Saved Successfully");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
refreshGallery();
//Update gallery depending on SDK Level
if (hbRecorder.wasUriSet()) {
updateGalleryUri();
}else{
refreshGalleryFile();
}
}

}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void refreshGallery() {
private void refreshGalleryFile() {
MediaScannerConnection.scanFile(this,
new String[]{hbRecorder.getFilePath()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
Expand All @@ -225,6 +235,12 @@ public void onScanCompleted(String path, Uri uri) {
});
}

private void updateGalleryUri(){
contentValues.clear();
contentValues.put(MediaStore.Video.Media.IS_PENDING, 0);
getContentResolver().update(mUri, contentValues, null, null);
}

@Override
public void HBRecorderOnError(int errorCode, String reason) {
// Error 38 happens when
Expand Down Expand Up @@ -419,6 +435,8 @@ private void customSettings() {
//Get/Set the selected settings
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void quickSettings() {
hbRecorder.setAudioBitrate(128000);
hbRecorder.setAudioSamplingRate(44100);
hbRecorder.recordHDVideo(wasHDSelected);
hbRecorder.isAudioEnabled(isAudioEnabled);
//Customise Notification
Expand Down Expand Up @@ -492,6 +510,8 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (requestCode == SCREEN_RECORD_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
//Set file path or Uri depending on SDK version
setOutputPath();
//Start screen recording
hbRecorder.startScreenRecording(data, resultCode, this);

Expand All @@ -500,6 +520,40 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
}

//For Android 10> we will pass a Uri to HBRecorder
//This is not necessary - You can still use getExternalStoragePublicDirectory
//But then you will have to add android:requestLegacyExternalStorage="true" in your Manifest
//IT IS IMPORTANT TO SET THE FILE NAME THE SAME AS THE NAME YOU USE FOR TITLE AND DISPLAY_NAME
ContentResolver resolver;
ContentValues contentValues;
Uri mUri;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void setOutputPath() {
String filename = generateFileName();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
resolver = getContentResolver();
contentValues = new ContentValues();
contentValues.put(MediaStore.Video.Media.RELATIVE_PATH, "Movies/" + "HBRecorder");
contentValues.put(MediaStore.Video.Media.TITLE, filename);
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, filename);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "video/mp4");
mUri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentValues);
//FILE NAME SHOULD BE THE SAME
hbRecorder.setFileName(filename);
hbRecorder.setOutputUri(mUri);
}else{
createFolder();
hbRecorder.setOutputPath(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) +"/HBRecorder");
}
}

//Generate a timestamp to be used as a file name
private String generateFileName() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.getDefault());
Date curDate = new Date(System.currentTimeMillis());
return formatter.format(curDate).replace(" ", "");
}

//Show Toast
private void showLongToast(final String msg) {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
Expand Down
1 change: 0 additions & 1 deletion hbrecorder/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package="com.hbisoft.hbrecorder">

<application
android:requestLegacyExternalStorage="true"
tools:targetApi="q">

<service android:name="com.hbisoft.hbrecorder.ScreenRecordService"
Expand Down
53 changes: 38 additions & 15 deletions hbrecorder/src/main/java/com/hbisoft/hbrecorder/HBRecorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
Expand All @@ -28,6 +29,7 @@
* 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 @@ -72,6 +74,18 @@ public void setOutputPath(String path) {
outputPath = path;
}

Uri mUri;
boolean mWasUriSet = false;
@RequiresApi(api = Build.VERSION_CODES.Q)
public void setOutputUri(Uri uri){
mWasUriSet = true;
mUri = uri;
}

public boolean wasUriSet(){
return mWasUriSet;
}

/*Set file name*/
public void setFileName(String fileName) {
this.fileName = fileName;
Expand Down Expand Up @@ -212,16 +226,21 @@ public void setNotificationButtonText(String string){
/*Start recording service*/
private void startService(Intent data) {
try {
if (outputPath!=null){
File file = new File(outputPath);
String parent = file.getParent();
observer = new FileObserver(parent, activity, HBRecorder.this);
}else {
observer = new FileObserver(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES)), activity, HBRecorder.this);
if (!mWasUriSet) {
if (outputPath != null) {
File file = new File(outputPath);
String parent = file.getParent();
observer = new FileObserver(parent, activity, HBRecorder.this);
} else {
observer = new FileObserver(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES)), activity, HBRecorder.this);
}
observer.startWatching();
}
observer.startWatching();

Intent service = new Intent(context, ScreenRecordService.class);
if (mWasUriSet) {
service.putExtra("mUri", mUri.toString());
}
service.putExtra("code", resultCode);
service.putExtra("data", data);
service.putExtra("audio", isAudioEnabled);
Expand Down Expand Up @@ -250,24 +269,28 @@ private void startService(Intent data) {
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
if (resultCode == Activity.RESULT_OK) {
String result = resultData.getString("errorReason");
if (result != null) {
Log.e("CALLED", "CALLED");
observer.stopWatching();
hbRecorderListener.HBRecorderOnError(100, result);
String errorListener = resultData.getString("errorReason");
String onComplete = resultData.getString("onComplete");

if (errorListener != null) {
if (!mWasUriSet) {
observer.stopWatching();
}
hbRecorderListener.HBRecorderOnError(100, errorListener);
try {
Intent mservice = new Intent(context, ScreenRecordService.class);
context.stopService(mservice);
}catch (Exception e){

// Can be ignored
}

}else if (onComplete != null){
//OnComplete for when Uri was passed
hbRecorderListener.HBRecorderOnComplete();
}
}
}
});


context.startService(service);
}catch (Exception e){
hbRecorderListener.HBRecorderOnError(0, Log.getStackTraceString(e));
Expand Down
Loading

0 comments on commit 4c7c52e

Please sign in to comment.