From 0bcadef77319729973c134ac8a5412c8e445dd63 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 25 Jan 2018 23:49:52 +0100 Subject: [PATCH] fix #155, properly handle View file intent, create a new note --- .../notes/activities/MainActivity.kt | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/notes/activities/MainActivity.kt index a4f67efb..0b62b70e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/activities/MainActivity.kt @@ -1,10 +1,12 @@ package com.simplemobiletools.notes.activities import android.content.Intent +import android.net.Uri import android.os.Bundle import android.support.v4.view.ViewPager import android.text.method.ArrowKeyMovementMethod import android.text.method.LinkMovementMethod +import android.util.Log import android.util.TypedValue import android.view.ActionMode import android.view.Gravity @@ -63,7 +65,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener { } if (action == Intent.ACTION_VIEW) { - handleFile(data.path) + handleUri(data) intent.removeCategory(Intent.CATEGORY_DEFAULT) intent.action = null } @@ -172,8 +174,8 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener { } } - private fun handleFile(path: String) { - val id = dbHelper.getNoteId(path) + private fun handleUri(uri: Uri) { + val id = dbHelper.getNoteId(uri.path) if (dbHelper.isValidId(id)) { updateSelectedNote(id) @@ -182,7 +184,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener { handlePermission(PERMISSION_WRITE_STORAGE) { if (it) { - importFileWithSync(path) + importFileWithSync(uri) } } } @@ -269,7 +271,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener { private fun openFile(path: String, checkTitle: Boolean, onChecksPassed: (file: File) -> Unit) { val file = File(path) - if (file.isImageVideoGif()) { + if (path.isImageVideoGif()) { toast(R.string.invalid_file_format) } else if (file.length() > 10 * 1000 * 1000) { toast(R.string.file_too_large) @@ -280,7 +282,21 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener { } } - private fun importFileWithSync(path: String) { + private fun importFileWithSync(uri: Uri) { + when (uri.scheme) { + "file" -> openPath(uri.path) + "content" -> { + val realPath = getRealPathFromURI(uri) + if (realPath != null) { + openPath(realPath) + } else { + R.string.unknown_error_occurred + } + } + } + } + + private fun openPath(path: String) { openFile(path, false) { var title = path.getFilenameFromPath() if (dbHelper.doesTitleExist(title))