Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 625a00b5 authored by Michael W's avatar Michael W
Browse files

Recorder: Set result and pass last recorded URI

* Apps starting the recorder with
  https://developer.android.com/reference/android/provider/MediaStore.Audio.Media#RECORD_SOUND_ACTION
  expect a proper output, which is
  a) a RESULT_OK in case the user successfully recorded something
  b) the uri of the recording so they can work with it
     (example: attach it to a mail)

Tested:
- Start Recorder with startActivityForResult (worked before)
- Record audio, press back: Got a proper result and uri
- Start again, don't record, go back: no RESULT_OK, no URI
- Start again, record, go to last recordings and delete it, then
  go back: no RESULT_OK, no URI
- Start again, record, delete recording from share notification:
  no RESULT_OK, no URI

Fixes: https://gitlab.com/LineageOS/issues/android/-/issues/3039
Change-Id: I20493e067cecd3631b301ec48783670681d5fcc2
parent 519ce7dd
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton;
import org.lineageos.recorder.service.RecorderBinder;
import org.lineageos.recorder.service.SoundRecorderService;
import org.lineageos.recorder.ui.WaveFormView;
import org.lineageos.recorder.utils.LastRecordHelper;
import org.lineageos.recorder.utils.LocationHelper;
import org.lineageos.recorder.utils.OnBoardingHelper;
import org.lineageos.recorder.utils.Utils;
@@ -140,6 +141,15 @@ public class RecorderActivity extends AppCompatActivity implements
        unregisterReceiver(mTelephonyReceiver);
    }

    @Override
    public void onBackPressed() {
        if (LastRecordHelper.getLastItemUri(this) != null) {
            setResult(RESULT_OK, new Intent().setData(LastRecordHelper.getLastItemUri(this)));
            LastRecordHelper.setLastItem(this, null); // forget what the last recording was
        }
        super.onBackPressed();
    }

    @Override
    protected void onResume() {
        super.onResume();
+2 −2
Original line number Diff line number Diff line
@@ -45,7 +45,7 @@ public class LastRecordHelper {
                .setTitle(R.string.delete_title)
                .setMessage(context.getString(R.string.delete_recording_message))
                .setPositiveButton(R.string.delete, (dialog, which) -> {
                    MediaProviderHelper.remove(context.getContentResolver(), uri);
                    MediaProviderHelper.remove(context, uri);
                    NotificationManager nm = context.getSystemService(NotificationManager.class);
                    if (nm == null) {
                        return;
@@ -70,7 +70,7 @@ public class LastRecordHelper {
                    }

                    nm.cancel(SoundRecorderService.NOTIFICATION_ID);
                    MediaProviderHelper.remove(context.getContentResolver(), uri);
                    MediaProviderHelper.remove(context, uri);
                    onDelete.run();
                })
                .setNegativeButton(R.string.cancel, null)
+14 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package org.lineageos.recorder.utils;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
@@ -95,8 +96,20 @@ public final class MediaProviderHelper {
        runTask(new LoaderTask(cr), listener);
    }

    public static void remove(@NonNull ContentResolver cr, @NonNull Uri uri) {
    public static void remove(@NonNull Context context, @NonNull Uri uri) {
        ContentResolver cr = context.getContentResolver();
        cr.delete(uri, null, null);

        // We want to clear the last item marker when we actually deleted it
        // but we have to handle differences in URIs sometimes containing external_primary
        // and sometimes "external" -> always make it "external"
        Uri lastItem = LastRecordHelper.getLastItemUri(context);
        String lastItemStr = lastItem != null
                ? lastItem.toString().replace("external_primary", "external") : "";
        String uriString = uri.toString().replace("external_primary", "external");
        if (uriString.equals(lastItemStr)) {
            LastRecordHelper.setLastItem(context, null);
        }
    }

    public static void rename(@NonNull ContentResolver cr,