Skip to content

Commit 1f0d2a0

Browse files
committed
io.appium.settings: recording: Revert SAF update
Signed-off-by: sirmordred <[email protected]>
1 parent 9177d64 commit 1f0d2a0

File tree

3 files changed

+31
-43
lines changed

3 files changed

+31
-43
lines changed

app/src/main/java/io/appium/settings/RecorderService.java

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,15 @@
1717
package io.appium.settings;
1818

1919
import android.app.Service;
20-
import android.content.ContentResolver;
21-
import android.content.ContentValues;
2220
import android.content.Context;
2321
import android.content.Intent;
2422
import android.media.projection.MediaProjection;
2523
import android.media.projection.MediaProjectionManager;
26-
import android.net.Uri;
2724
import android.os.Build;
2825
import android.os.IBinder;
29-
import android.provider.MediaStore;
3026
import android.util.DisplayMetrics;
3127
import android.util.Log;
3228

33-
import java.io.FileDescriptor;
34-
import java.io.FileNotFoundException;
35-
3629
import androidx.annotation.Nullable;
3730
import androidx.annotation.RequiresApi;
3831
import io.appium.settings.helpers.NotificationHelpers;
@@ -124,16 +117,9 @@ private void startScreenRecord(MediaProjectionManager mediaProjectionManager,
124117
return;
125118
}
126119

127-
String outputFileName = intent.getStringExtra(ACTION_RECORDING_FILENAME);
128-
if (outputFileName == null) {
129-
Log.i(TAG, "outputFileName is null");
130-
return;
131-
}
132-
133-
FileDescriptor outputVideoFd =
134-
createOutputVideoDescriptor(getApplicationContext(), outputFileName);
135-
if (outputVideoFd == null) {
136-
Log.i(TAG, "outputVideoFd is null");
120+
String outputFilePath = intent.getStringExtra(ACTION_RECORDING_FILENAME);
121+
if (outputFilePath == null) {
122+
Log.i(TAG, "outputFilePath is null");
137123
return;
138124
}
139125

@@ -167,7 +153,7 @@ private void startScreenRecord(MediaProjectionManager mediaProjectionManager,
167153
metrics.widthPixels, metrics.heightPixels, rawWidth, rawHeight));
168154
}
169155

170-
recorderThread = new RecorderThread(projection, outputVideoFd,
156+
recorderThread = new RecorderThread(projection, outputFilePath,
171157
rawWidth, rawHeight, recordingRotation);
172158
recorderThread.startRecording();
173159
}
@@ -188,21 +174,4 @@ private void showNotification() {
188174
startForeground(NotificationHelpers.APPIUM_NOTIFICATION_IDENTIFIER,
189175
NotificationHelpers.getNotification(this));
190176
}
191-
192-
@RequiresApi(api = Build.VERSION_CODES.Q)
193-
public FileDescriptor createOutputVideoDescriptor(Context context, String videoFileName) {
194-
ContentValues values = new ContentValues();
195-
values.put(MediaStore.Video.Media.TITLE, videoFileName);
196-
values.put(MediaStore.Video.Media.DISPLAY_NAME, videoFileName);
197-
values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
198-
ContentResolver resolver = context.getContentResolver();
199-
Uri collection = MediaStore.Downloads.EXTERNAL_CONTENT_URI;
200-
Uri videoOutputUri = resolver.insert(collection, values);
201-
try {
202-
return resolver.openFileDescriptor(videoOutputUri, "rw").getFileDescriptor();
203-
} catch (FileNotFoundException e) {
204-
e.printStackTrace();
205-
}
206-
return null;
207-
}
208177
}

app/src/main/java/io/appium/settings/RecorderThread.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import android.util.Log;
3535
import android.view.Surface;
3636

37-
import java.io.FileDescriptor;
3837
import java.io.IOException;
3938
import java.nio.ByteBuffer;
4039

@@ -47,7 +46,7 @@ public class RecorderThread implements Runnable {
4746
private static final String TAG = "RecorderThread";
4847

4948
private final MediaProjection mediaProjection;
50-
private final FileDescriptor outputFileFd;
49+
private final String outputFilePath;
5150
private final int videoWidth;
5251
private final int videoHeight;
5352
private final int recordingRotation;
@@ -81,10 +80,10 @@ public void onStopped() {
8180
}
8281
};
8382

84-
public RecorderThread(MediaProjection mediaProjection, FileDescriptor outputFileFd,
83+
public RecorderThread(MediaProjection mediaProjection, String outputFilePath,
8584
int videoWidth, int videoHeight, int recordingRotation) {
8685
this.mediaProjection = mediaProjection;
87-
this.outputFileFd = outputFileFd;
86+
this.outputFilePath = outputFilePath;
8887
this.videoWidth = videoWidth;
8988
this.videoHeight = videoHeight;
9089
this.recordingRotation = recordingRotation;
@@ -376,7 +375,7 @@ public void run() {
376375

377376
audioRecord = initAudioRecord(this.mediaProjection, sampleRate);
378377

379-
muxer = new MediaMuxer(this.outputFileFd,
378+
muxer = new MediaMuxer(this.outputFilePath,
380379
MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
381380

382381
// set output file orientation info

app/src/main/java/io/appium/settings/Settings.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import android.view.Surface;
3333
import android.view.WindowManager;
3434

35+
import java.io.File;
3536
import java.text.SimpleDateFormat;
3637
import java.util.Arrays;
3738
import java.util.Date;
@@ -63,7 +64,7 @@
6364
public class Settings extends Activity {
6465
private static final String TAG = "APPIUM SETTINGS";
6566

66-
private String recordingFilename = RecorderConstant.DEFAULT_RECORDING_FILENAME;
67+
private String recordingOutputPath = "";
6768
private int recordingRotation = -1;
6869

6970
@Override
@@ -128,7 +129,7 @@ private void handleRecording(Intent intent) {
128129
return;
129130
}
130131

131-
recordingFilename = intent.getStringExtra(ACTION_RECORDING_FILENAME);
132+
String recordingFilename = intent.getStringExtra(ACTION_RECORDING_FILENAME);
132133

133134
if (isValidFileName(recordingFilename)) {
134135
String timeStamp = new SimpleDateFormat(
@@ -139,6 +140,25 @@ private void handleRecording(Intent intent) {
139140
" using default one: " + recordingFilename);
140141
}
141142

143+
/*
144+
External Storage File Directory for app
145+
(i.e /storage/emulated/0/Android/data/io.appium.settings/files) may not be created
146+
so we need to call getExternalFilesDir() method twice
147+
source:https://www.androidbugfix.com/2021/10/getexternalfilesdirnull-returns-null-in.html
148+
*/
149+
File externalStorageFile = getExternalFilesDir(null);
150+
if (externalStorageFile == null) {
151+
externalStorageFile = getExternalFilesDir(null);
152+
}
153+
// if path is still null despite calling method twice, early exit
154+
if (externalStorageFile == null) {
155+
Log.e(TAG, "handleRecording: external storage file path returns null");
156+
finishActivity();
157+
return;
158+
}
159+
recordingOutputPath = externalStorageFile.getAbsolutePath()
160+
+ File.separator + recordingFilename;
161+
142162
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
143163

144164
if (display != null) {
@@ -268,7 +288,7 @@ protected void onActivityResult(final int requestCode, final int resultCode, fin
268288
final Intent intent = new Intent(this, RecorderService.class);
269289
intent.setAction(ACTION_RECORDING_START);
270290
intent.putExtra(ACTION_RECORDING_RESULT_CODE, resultCode);
271-
intent.putExtra(ACTION_RECORDING_FILENAME, recordingFilename);
291+
intent.putExtra(ACTION_RECORDING_FILENAME, recordingOutputPath);
272292
intent.putExtra(ACTION_RECORDING_ROTATION, recordingRotation);
273293
intent.putExtras(data);
274294

0 commit comments

Comments
 (0)