Procházet zdrojové kódy

fix: implemented options for notes

beautusg před 7 roky
rodič
revize
0d01131a03

+ 9 - 0
app/src/main/AndroidManifest.xml

@@ -29,6 +29,15 @@
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
+        <provider
+            android:name="androidx.core.content.FileProvider"
+            android:authorities="@string/app_authority_for_file_provider"
+            android:exported="false"
+            android:grantUriPermissions="true">
+            <meta-data
+                android:name="android.support.FILE_PROVIDER_PATHS"
+                android:resource="@xml/filepaths" />
+        </provider>
     </application>
 
 </manifest>

+ 4 - 1
app/src/main/java/com/sduduzog/slimlauncher/MainActivity.kt

@@ -12,6 +12,7 @@ import com.sduduzog.slimlauncher.utils.BaseFragment
 import com.sduduzog.slimlauncher.utils.HomeWatcher
 import com.sduduzog.slimlauncher.utils.Permissions
 import com.sduduzog.slimlauncher.utils.VoiceRecorder
+import java.io.File
 
 
 class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceChangeListener, HomeWatcher.OnHomePressedListener {
@@ -95,7 +96,9 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
         when (requestCode) {
             Permissions.RECORD_AUDIO -> {
                 if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
-                    VoiceRecorder.getInstance().startRecording(filesDir.absolutePath)
+                    val file = File(filesDir, getString(R.string.audio_file_path))
+                    val dir = file.canonicalPath
+                    VoiceRecorder.getInstance().startRecording(dir)
                 }
                 return
             }

+ 1 - 1
app/src/main/java/com/sduduzog/slimlauncher/data/BaseDatabase.kt

@@ -72,7 +72,7 @@ abstract class BaseDatabase : RoomDatabase() {
                 database.execSQL("CREATE TABLE IF NOT EXISTS `notes` (`id` INTEGER PRIMARY KEY NOT NULL, `body` TEXT NOT NULL, `title` TEXT, `edited` INTEGER NOT NULL)")
                 database.execSQL("INSERT INTO `notes` (`id`, `body`, `edited`, `title`) SELECT `id`, `body`, `edited`, `title` FROM `notes_old`")
                 database.execSQL("ALTER TABLE `notes` ADD COLUMN `type` INTEGER NOT NULL DEFAULT 0")
-                database.execSQL("ALTER TABLE `notes` ADD COLUMN `path` TEXT")
+                database.execSQL("ALTER TABLE `notes` ADD COLUMN `filename` TEXT")
             }
         }
     }

+ 2 - 2
app/src/main/java/com/sduduzog/slimlauncher/data/model/Note.kt

@@ -19,8 +19,8 @@ data class Note(
         var edited: Long = -1L,
         @field:ColumnInfo(name = "type")
         var type: Int = 0,
-        @field:ColumnInfo(name = "path")
-        var path: String? = null
+        @field:ColumnInfo(name = "filename")
+        var filename: String? = null
 ) : Serializable {
     companion object {
         const val TYPE_TEXT = 0

+ 4 - 2
app/src/main/java/com/sduduzog/slimlauncher/ui/main/NotesFragment.kt

@@ -114,7 +114,9 @@ class NotesFragment : BaseFragment(), OnShitDoneToNotesListener {
             val permissionCheck = ContextCompat.checkSelfPermission(activity!!,
                     Manifest.permission.RECORD_AUDIO)
             val isPermissionGranted = permissionCheck == PackageManager.PERMISSION_GRANTED
-            if (isPermissionGranted) voiceRecorder.startRecording(context!!.filesDir.absolutePath)
+            val file = File(context!!.filesDir, getString(R.string.audio_file_path))
+            val dir = file.canonicalPath
+            if (isPermissionGranted) voiceRecorder.startRecording(dir)
             else ActivityCompat.requestPermissions(activity!!,
                     arrayOf(Manifest.permission.RECORD_AUDIO),
                     Permissions.RECORD_AUDIO)
@@ -146,7 +148,7 @@ class NotesFragment : BaseFragment(), OnShitDoneToNotesListener {
     }
 
     private fun deleteNote(note: Note) {
-        if (note.type == Note.TYPE_VOICE) File(note.path).delete()
+        if (note.type == Note.TYPE_VOICE) context?.deleteFile(note.filename)
         viewModel.remove(note)
     }
 

+ 40 - 10
app/src/main/java/com/sduduzog/slimlauncher/ui/notes/VoiceNoteFragment.kt

@@ -1,22 +1,28 @@
 package com.sduduzog.slimlauncher.ui.notes
 
+import android.content.Intent
 import android.media.MediaPlayer
 import android.os.Bundle
 import android.os.Handler
 import android.os.SystemClock
 import android.text.format.DateUtils
+import android.util.Log
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
 import androidx.appcompat.widget.PopupMenu
+import androidx.core.content.FileProvider
 import androidx.lifecycle.Observer
 import androidx.lifecycle.ViewModelProviders
+import androidx.navigation.Navigation
 import com.sduduzog.slimlauncher.R
 import com.sduduzog.slimlauncher.data.MainViewModel
 import com.sduduzog.slimlauncher.data.model.Note
 import com.sduduzog.slimlauncher.dialogs.RenameNoteDialog
 import com.sduduzog.slimlauncher.utils.BaseFragment
 import kotlinx.android.synthetic.main.voice_note_fragment.*
+import java.io.File
+
 
 class VoiceNoteFragment : BaseFragment() {
 
@@ -31,11 +37,15 @@ class VoiceNoteFragment : BaseFragment() {
     private var pauseTime = 0L
     private val updateTimerThread = object : Runnable {
         override fun run() {
-            timeInMilliseconds = SystemClock.uptimeMillis()
-            updateTime = (timeInMilliseconds + pauseTime) - startTime
-            val time = DateUtils.formatElapsedTime(updateTime / 1000)
-            voice_note_fragment_counter.text = getString(R.string.notes_fragment_counter, time)
-            customHandler.postDelayed(this, 10)
+            try {
+                timeInMilliseconds = SystemClock.uptimeMillis()
+                updateTime = (timeInMilliseconds + pauseTime) - startTime
+                val time = DateUtils.formatElapsedTime(updateTime / 1000)
+                voice_note_fragment_counter.text = getString(R.string.notes_fragment_counter, time)
+                customHandler.postDelayed(this, 10)
+            } finally {
+                // Do something
+            }
         }
     }
 
@@ -113,7 +123,10 @@ class VoiceNoteFragment : BaseFragment() {
             showDuration()
             voice_note_fragment_play.text = getString(R.string.voice_note_fragment_play)
         }
-        mediaPlayer?.setDataSource(note.path)
+        val imagePath = File(context!!.filesDir, getString(R.string.audio_file_path))
+        val newFile = File(imagePath, note.filename)
+        Log.d("check", newFile.absolutePath)
+        mediaPlayer?.setDataSource(newFile.absolutePath)
         mediaPlayer?.prepare()
         showDuration()
         voice_note_fragment_options.setOnClickListener { showPopupMenu(it, note) }
@@ -128,13 +141,30 @@ class VoiceNoteFragment : BaseFragment() {
 
 
     private fun showPopupMenu(view: View, note: Note) {
+        stopTimer()
+        showDuration()
+        voice_note_fragment_play.text = getString(R.string.voice_note_fragment_play)
         val popup = PopupMenu(context!!, view)
         popup.menuInflater.inflate(R.menu.voice_note_popup_menu, popup.menu)
         popup.setOnMenuItemClickListener {
-            if (it.itemId == R.id.vn_menu_rename) {
-                RenameNoteDialog.getInstance(note, viewModel).show(fragmentManager, "note_dialog")
-            } else if (it.itemId == R.id.vn_menu_share) {
-            } else if (it.itemId == R.id.vn_menu_delete) {
+            when {
+                it.itemId == R.id.vn_menu_rename -> RenameNoteDialog.getInstance(note, viewModel)
+                        .show(fragmentManager, "note_dialog")
+                it.itemId == R.id.vn_menu_share -> {
+                    val intent = Intent(Intent.ACTION_SEND)
+                    val imagePath = File(context!!.filesDir, getString(R.string.audio_file_path))
+                    val newFile = File(imagePath, note.filename)
+                    val contentUri = FileProvider.getUriForFile(context!!, getString(R.string.app_authority_for_file_provider), newFile)
+                    val type = context!!.contentResolver.getType(contentUri)
+                    intent.setDataAndType(contentUri, type)
+                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
+                    startActivity(Intent.createChooser(intent, "Send audio note"))
+                }
+                it.itemId == R.id.vn_menu_delete -> {
+                    Navigation.findNavController(voice_note_fragment).popBackStack()
+                    context!!.deleteFile(note.filename)
+                    viewModel.remove(note)
+                }
             }
             true
         }

+ 6 - 2
app/src/main/java/com/sduduzog/slimlauncher/utils/VoiceRecorder.kt

@@ -5,6 +5,7 @@ import android.util.Log
 import androidx.lifecycle.MutableLiveData
 import com.sduduzog.slimlauncher.data.MainViewModel
 import com.sduduzog.slimlauncher.data.model.Note
+import java.io.File
 import java.io.IOException
 import java.text.DateFormat
 import java.util.*
@@ -22,11 +23,13 @@ class VoiceRecorder private constructor() {
     private var mediaRecorder: MediaRecorder? = null
 
     fun startRecording(fileDir: String) {
+        File(fileDir).mkdir()
         val timestamp = Date().time
         val title = "VN-$timestamp"
         val body = "Recorded at ${DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT).format(Date(timestamp))}"
-        val path = "$fileDir/$title.m4a"
-        note = Note(timestamp, body, title, timestamp, Note.TYPE_VOICE, path)
+        val filename = "$title.m4a"
+        val path = "$fileDir/$filename"
+        note = Note(timestamp, body, title, timestamp, Note.TYPE_VOICE, filename)
         mediaRecorder?.setAudioSource(MediaRecorder.AudioSource.MIC)
         mediaRecorder?.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
         mediaRecorder?.setOutputFile(path)
@@ -41,6 +44,7 @@ class VoiceRecorder private constructor() {
         } catch (e: IOException) {
             note = null
             state = ERROR
+            Log.e("check", "$e")
         }
     }
 

+ 2 - 0
app/src/main/res/values/strings.xml

@@ -1,8 +1,10 @@
 <resources>
     <string name="app_name">Slim Launcher</string>
+    <string name="app_authority_for_file_provider">com.sduduzog.slimlauncher.fileprovider</string>
     <string name="slim_url">http://sduduzog.github.io/slim-launcher/</string>
     <string name="main_placeholder_clock">00:00</string>
     <string name="main_placeholder_date">Sat, Apr 20</string>
+    <string name="audio_file_path">audio</string>
 
     <string-array name="themes_array">
         <item>Default</item>

+ 6 - 0
app/src/main/res/xml/filepaths.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Paths>
+    <files-path
+        name="vn"
+        path="audio/" />
+</Paths>