Skip to content

Commit ddbfbeb

Browse files
Allow rename for internally stored logs only
1 parent fce06fa commit ddbfbeb

File tree

1 file changed

+24
-39
lines changed

1 file changed

+24
-39
lines changed

app/src/main/java/com/dp/logcatapp/fragments/savedlogs/SavedLogsFragment.kt

+24-39
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ import com.dp.logcatapp.db.SavedLogInfo
3838
import com.dp.logcatapp.fragments.base.BaseDialogFragment
3939
import com.dp.logcatapp.fragments.base.BaseFragment
4040
import com.dp.logcatapp.fragments.logcatlive.LogcatLiveFragment
41-
import com.dp.logcatapp.util.*
41+
import com.dp.logcatapp.util.ShareUtils
42+
import com.dp.logcatapp.util.closeQuietly
43+
import com.dp.logcatapp.util.inflateLayout
44+
import com.dp.logcatapp.util.showToast
4245
import io.reactivex.Flowable
4346
import io.reactivex.android.schedulers.AndroidSchedulers
4447
import io.reactivex.disposables.Disposable
@@ -194,9 +197,7 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick
194197
return when (item.itemId) {
195198
R.id.action_rename -> {
196199
val fileInfo = recyclerViewAdapter.getItem(viewModel.selectedItems.toIntArray()[0])
197-
val folder = File(context!!.filesDir, LogcatLiveFragment.LOGCAT_DIR)
198-
val file = File(folder, fileInfo.info.fileName)
199-
val frag = RenameDialogFragment.newInstance(file.absolutePath)
200+
val frag = RenameDialogFragment.newInstance(fileInfo.info.fileName, fileInfo.info.path)
200201
frag.setTargetFragment(this, 0)
201202
frag.show(fragmentManager, RenameDialogFragment.TAG)
202203
true
@@ -339,10 +340,16 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick
339340
val save = toolbar.menu.findItem(R.id.action_save)
340341
val rename = toolbar.menu.findItem(R.id.action_rename)
341342

342-
val visible = viewModel.selectedItems.size <= 1
343+
val visible = viewModel.selectedItems.size == 1
343344
share.isVisible = visible
344345
save.isVisible = visible
345-
rename.isVisible = visible
346+
347+
if (visible) {
348+
val info = recyclerViewAdapter.getItem(viewModel.selectedItems.toIntArray()[0])
349+
rename.isVisible = !info.info.isCustom
350+
} else {
351+
rename.isVisible = false
352+
}
346353
}
347354

348355
override fun onCabToolbarClose(toolbar: Toolbar) {
@@ -362,6 +369,8 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick
362369
.subscribe {
363370
viewModel.fileNames.reload()
364371
}
372+
373+
(activity as SavedLogsActivity).closeCabToolbar()
365374
}
366375

367376
override fun onDestroy() {
@@ -484,10 +493,12 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick
484493
companion object {
485494
val TAG = RenameDialogFragment::class.qualifiedName
486495

496+
private val KEY_FILENAME = TAG + "_key_filename"
487497
private val KEY_PATH = TAG + "_key_path"
488498

489-
fun newInstance(path: String): RenameDialogFragment {
499+
fun newInstance(fileName: String, path: String): RenameDialogFragment {
490500
val bundle = Bundle()
501+
bundle.putString(KEY_FILENAME, fileName)
491502
bundle.putString(KEY_PATH, path)
492503
val frag = RenameDialogFragment()
493504
frag.arguments = bundle
@@ -498,9 +509,7 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick
498509
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
499510
val view = inflateLayout(R.layout.rename_dialog)
500511
val editText = view.findViewById<EditText>(R.id.editText)
501-
502-
val path = arguments!!.getString(KEY_PATH)!!
503-
editText.setText(getName(path))
512+
editText.setText(arguments!!.getString(KEY_FILENAME))
504513
editText.selectAll()
505514

506515
val dialog = AlertDialog.Builder(activity!!)
@@ -509,10 +518,13 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick
509518
.setPositiveButton(android.R.string.ok) { _, _ ->
510519
val newName = editText.text.toString()
511520
if (newName.isNotEmpty()) {
512-
if (!doRename(path, newName)) {
521+
val file = arguments!!.getString(KEY_PATH)!!.toUri().toFile()
522+
val newFile = File(file.parent, newName)
523+
if (file.renameTo(newFile)) {
524+
(targetFragment as SavedLogsFragment).onRename(newName, newFile.toUri())
525+
} else {
513526
activity!!.showToast(getString(R.string.error))
514527
}
515-
(activity as SavedLogsActivity).closeCabToolbar()
516528
}
517529
dismiss()
518530
}
@@ -530,32 +542,5 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick
530542

531543
return dialog
532544
}
533-
534-
private fun getName(path: String): String {
535-
if (Utils.isUsingCustomSaveLocation(context!!) && Build.VERSION.SDK_INT >= 21) {
536-
return DocumentFile.fromSingleUri(context!!, path.toUri())!!.name!!
537-
} else {
538-
return File(path).name
539-
}
540-
}
541-
542-
private fun doRename(path: String, newName: String): Boolean {
543-
if (Utils.isUsingCustomSaveLocation(context!!) && Build.VERSION.SDK_INT >= 21) {
544-
val file = DocumentFile.fromSingleUri(context!!, path.toUri())!!
545-
if (file.renameTo(newName)) {
546-
(targetFragment as SavedLogsFragment).onRename(newName, file.uri)
547-
return true
548-
}
549-
} else {
550-
val file = File(path)
551-
val newFile = File(file.parent, newName)
552-
if (file.renameTo(newFile)) {
553-
(targetFragment as SavedLogsFragment).onRename(newName, newFile.toUri())
554-
return true
555-
}
556-
}
557-
558-
return false
559-
}
560545
}
561546
}

0 commit comments

Comments
 (0)