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

Unverified Commit 73e00e21 authored by Luca Stefani's avatar Luca Stefani Committed by Sebastiano Barezzi
Browse files

Recorder: Rework edit name dialog

Change-Id: I13a9e87b0c0ca804f6125515189337e673db1994
parent 029fc802
Loading
Loading
Loading
Loading
+48 −18
Original line number Diff line number Diff line
@@ -12,9 +12,12 @@ import android.view.ActionMode
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.ProgressBar
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.view.ViewCompat
@@ -26,6 +29,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.AdapterDataObserver
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import org.lineageos.recorder.ext.scheduleShowSoftInput
import org.lineageos.recorder.list.ListActionModeCallback
import org.lineageos.recorder.list.RecordingData
import org.lineageos.recorder.list.RecordingListCallbacks
@@ -48,6 +52,9 @@ class ListActivity : AppCompatActivity(), RecordingListCallbacks {
    private val listRecyclerView by lazy { findViewById<RecyclerView>(R.id.listRecyclerView) }
    private val toolbar by lazy { findViewById<Toolbar>(R.id.toolbar) }

    // System services
    private val inputMethodManager by lazy { getSystemService(InputMethodManager::class.java) }

    // Adapters
    private val adapter by lazy {
        RecordingsAdapter(this)
@@ -127,31 +134,54 @@ class ListActivity : AppCompatActivity(), RecordingListCallbacks {
    }

    override fun onRename(index: Int, uri: Uri, currentName: String) {
        val view = layoutInflater.inflate(R.layout.dialog_content_rename, null)

        val editText = view.findViewById<EditText>(R.id.nameEditText)
        editText.setText(currentName)
        editText.requestFocus()
        Utils.showKeyboard(this)
        lateinit var alertDialog: AlertDialog
        lateinit var editText: EditText

        MaterialAlertDialogBuilder(this)
            .setTitle(R.string.list_edit_title)
            .setView(view)
            .setPositiveButton(R.string.list_edit_confirm) { _: DialogInterface?, _: Int ->
                val editable = editText.text ?: return@setPositiveButton
                if (editable.isEmpty()) {
                    return@setPositiveButton
                }
        val onConfirm = {
            editText.text?.takeIf { it.isNotEmpty() }?.let { editable ->
                val newTitle = editable.toString()
                if (newTitle != currentName) {
                    renameRecording(uri, newTitle, index)
                }
                Utils.closeKeyboard(this)

                true
            } ?: false
        }

        val view = layoutInflater.inflate(
            R.layout.dialog_content_rename,
            null,
            false
        )
        editText = view.findViewById<EditText>(R.id.nameEditText).apply {
            setText(currentName)
            setSelection(0, currentName.length)
            setOnEditorActionListener { _, actionId, _ ->
                when (actionId) {
                    EditorInfo.IME_ACTION_UNSPECIFIED,
                    EditorInfo.IME_ACTION_DONE -> {
                        onConfirm().also {
                            if (it) {
                                alertDialog.dismiss()
                            }
                        }
                    }

                    else -> false
                }
            }
            .setNegativeButton(R.string.cancel) { _: DialogInterface?, _: Int ->
                Utils.closeKeyboard(this)
        }

        alertDialog = MaterialAlertDialogBuilder(this)
            .setTitle(R.string.list_edit_title)
            .setView(view)
            .setPositiveButton(R.string.list_edit_confirm) { _, _ -> onConfirm() }
            .setNegativeButton(R.string.cancel, null)
            .show()
            .also {
                editText.requestFocus()
                inputMethodManager.scheduleShowSoftInput(editText, 0)
            }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
+44 −0
Original line number Diff line number Diff line
/*
 * SPDX-FileCopyrightText: 2024 The LineageOS Project
 * SPDX-License-Identifier: Apache-2.0
 */

package org.lineageos.recorder.ext

import android.view.View
import android.view.inputmethod.InputMethodManager

private const val SHOW_REQUEST_TIMEOUT = 1000

private fun InputMethodManager.scheduleShowSoftInput(
    view: View,
    flags: Int,
    runnable: Runnable,
    showRequestTime: Long,
) {
    if (!view.hasFocus()
        || (showRequestTime + SHOW_REQUEST_TIMEOUT) <= System.currentTimeMillis()
    ) {
        return
    }

    if (showSoftInput(view, flags)) {
        return
    } else {
        view.removeCallbacks(runnable)
        view.postDelayed(runnable, 50)
    }
}

/**
 * @see InputMethodManager.showSoftInput
 */
fun InputMethodManager.scheduleShowSoftInput(view: View, flags: Int) {
    val runnable = object : Runnable {
        override fun run() {
            scheduleShowSoftInput(view, flags, this, System.currentTimeMillis())
        }
    }

    runnable.run()
}
+0 −15
Original line number Diff line number Diff line
@@ -7,24 +7,9 @@ package org.lineageos.recorder.utils

import android.app.NotificationManager
import android.content.Context
import android.view.inputmethod.InputMethodManager
import org.lineageos.recorder.service.SoundRecorderService

object Utils {
    fun showKeyboard(context: Context) {
        val inputMethodManager = context.getSystemService(
            InputMethodManager::class.java
        )
        inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
    }

    fun closeKeyboard(context: Context) {
        val inputMethodManager = context.getSystemService(
            InputMethodManager::class.java
        )
        inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
    }

    fun cancelShareNotification(context: Context) {
        val nm = context.getSystemService(
            NotificationManager::class.java
+4 −5
Original line number Diff line number Diff line
@@ -4,17 +4,16 @@
     SPDX-License-Identifier: Apache-2.0
-->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingHorizontal="?dialogPreferredPadding">
    android:paddingHorizontal="?attr/dialogPreferredPadding">

    <androidx.appcompat.widget.AppCompatEditText
    <EditText
        android:id="@+id/nameEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:importantForAutofill="no"
        android:maxLines="1"
        android:singleLine="true"
        tools:text="Name" />
        android:singleLine="true" />

</FrameLayout>
+8 −0
Original line number Diff line number Diff line
@@ -10,6 +10,8 @@
        <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
        <item name="android:windowLightNavigationBar">?attr/isLightTheme</item>
        <item name="android:windowLightStatusBar">?attr/isLightTheme</item>
        <item name="alertDialogTheme">@style/Theme.Recorder.AlertDialog</item>
        <item name="materialAlertDialogTheme">@style/Theme.Recorder.AlertDialog</item>
        <item name="windowActionModeOverlay">true</item>
    </style>

@@ -52,4 +54,10 @@
        <item name="android:iconTint">?attr/colorOnSurface</item>
        <item name="iconTint">?attr/colorOnSurface</item>
    </style>

    <!-- Alert dialog theme. -->
    <style name="Theme.Recorder.AlertDialog" parent="ThemeOverlay.Material3.MaterialAlertDialog">
        <item name="android:colorBackground">?attr/colorSurface</item>
        <item name="dialogCornerRadius">16dp</item>
    </style>
</resources>