handle file storing on Android Q and before differently

This commit is contained in:
tibbi
2020-03-28 23:57:30 +01:00
parent d591ee211a
commit 882aa98c46
2 changed files with 42 additions and 10 deletions

View File

@ -5,6 +5,9 @@
android:installLocation="auto"> android:installLocation="auto">
<uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />
<uses-feature <uses-feature
android:name="android.hardware.faketouch" android:name="android.hardware.faketouch"
@ -19,6 +22,7 @@
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:label="@string/app_launcher_name" android:label="@string/app_launcher_name"
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">

View File

@ -4,12 +4,16 @@ import android.content.ContentValues
import android.content.Intent import android.content.Intent
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.media.MediaRecorder import android.media.MediaRecorder
import android.media.MediaScannerConnection
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.provider.MediaStore import android.provider.MediaStore
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import androidx.annotation.RequiresApi
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_RECORD_AUDIO import com.simplemobiletools.commons.helpers.PERMISSION_RECORD_AUDIO
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.helpers.isQPlus import com.simplemobiletools.commons.helpers.isQPlus
import com.simplemobiletools.commons.models.FAQItem import com.simplemobiletools.commons.models.FAQItem
@ -34,7 +38,7 @@ class MainActivity : SimpleActivity() {
handlePermission(PERMISSION_RECORD_AUDIO) { handlePermission(PERMISSION_RECORD_AUDIO) {
if (it) { if (it) {
initVoiceRecorder() tryInitVoiceRecorder()
} else { } else {
finish() finish()
} }
@ -69,6 +73,20 @@ class MainActivity : SimpleActivity() {
return true return true
} }
private fun tryInitVoiceRecorder() {
if (isQPlus()) {
initVoiceRecorder()
} else {
handlePermission(PERMISSION_WRITE_STORAGE) {
if (it) {
initVoiceRecorder()
} else {
finish()
}
}
}
}
private fun initVoiceRecorder() { private fun initVoiceRecorder() {
toggle_recording_button.setOnClickListener { toggle_recording_button.setOnClickListener {
toggleRecording() toggleRecording()
@ -97,11 +115,10 @@ class MainActivity : SimpleActivity() {
try { try {
prepare() prepare()
start()
} catch (e: IOException) { } catch (e: IOException) {
showErrorToast(e) showErrorToast(e)
} }
start()
} }
} }
@ -109,19 +126,21 @@ class MainActivity : SimpleActivity() {
recorder?.apply { recorder?.apply {
stop() stop()
release() release()
ensureBackgroundThread { ensureBackgroundThread {
addFileInMediaStore() if (isQPlus()) {
addFileInNewMediaStore()
} else {
addFileInLegacyMediaStore()
}
} }
} }
recorder = null recorder = null
} }
private fun addFileInMediaStore() { @RequiresApi(Build.VERSION_CODES.Q)
val audioCollection = if (isQPlus()) { private fun addFileInNewMediaStore() {
MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY) val audioCollection = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
} else {
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
}
val storeFilename = currFilePath.getFilenameFromPath() val storeFilename = currFilePath.getFilenameFromPath()
val newSongDetails = ContentValues().apply { val newSongDetails = ContentValues().apply {
@ -141,6 +160,15 @@ class MainActivity : SimpleActivity() {
inputStream!!.copyTo(outputStream!!, DEFAULT_BUFFER_SIZE) inputStream!!.copyTo(outputStream!!, DEFAULT_BUFFER_SIZE)
} }
private fun addFileInLegacyMediaStore() {
MediaScannerConnection.scanFile(
this,
arrayOf(currFilePath),
arrayOf(currFilePath.getMimeType()),
null
)
}
private fun getToggleButtonIcon(): Drawable { private fun getToggleButtonIcon(): Drawable {
val drawable = if (isRecording) R.drawable.ic_stop_vector else R.drawable.ic_mic_vector val drawable = if (isRecording) R.drawable.ic_stop_vector else R.drawable.ic_mic_vector
return resources.getColoredDrawableWithColor(drawable, getFABIconColor()) return resources.getColoredDrawableWithColor(drawable, getFABIconColor())