Warning, /network/kdeconnect-android/src/org/kde/kdeconnect/Plugins/MousePadPlugin/ComposeSendActivity.kt is written in an unsupported language. File is not indexed.
0001 /* 0002 * SPDX-FileCopyrightText: 2023 Dmitry Yudin <dgyudin@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 package org.kde.kdeconnect.Plugins.MousePadPlugin 0008 0009 import android.os.Bundle 0010 import android.util.Log 0011 import androidx.activity.compose.setContent 0012 import androidx.appcompat.app.AppCompatActivity 0013 import androidx.compose.foundation.layout.Box 0014 import androidx.compose.foundation.layout.fillMaxSize 0015 import androidx.compose.foundation.layout.fillMaxWidth 0016 import androidx.compose.foundation.layout.padding 0017 import androidx.compose.material.icons.Icons 0018 import androidx.compose.material.icons.filled.Send 0019 import androidx.compose.material3.Scaffold 0020 import androidx.compose.runtime.Composable 0021 import androidx.compose.runtime.mutableStateOf 0022 import androidx.compose.ui.Alignment 0023 import androidx.compose.ui.Modifier 0024 import androidx.compose.ui.res.stringResource 0025 import androidx.compose.ui.unit.dp 0026 import androidx.preference.PreferenceManager 0027 import com.google.accompanist.themeadapter.material3.Mdc3Theme 0028 import org.kde.kdeconnect.KdeConnect 0029 import org.kde.kdeconnect.NetworkPacket 0030 import org.kde.kdeconnect.UserInterface.compose.KdeTextButton 0031 import org.kde.kdeconnect.UserInterface.compose.KdeTextField 0032 import org.kde.kdeconnect.UserInterface.compose.KdeTopAppBar 0033 import org.kde.kdeconnect_tp.R 0034 0035 private const val INPUT_CACHE_KEY = "compose_send_input_cache" 0036 0037 class ComposeSendActivity : AppCompatActivity() { 0038 private var deviceId: String? = null 0039 private val userInput = mutableStateOf(String()) 0040 private val prefs by lazy { PreferenceManager.getDefaultSharedPreferences(this) } 0041 0042 override fun onCreate(savedInstanceState: Bundle?) { 0043 0044 super.onCreate(savedInstanceState) 0045 0046 prefs.getString(INPUT_CACHE_KEY, null)?.let { userInput.value = it } 0047 0048 setContent { ComposeSendScreen() } 0049 0050 deviceId = intent.getStringExtra("org.kde.kdeconnect.Plugins.MousePadPlugin.deviceId") 0051 } 0052 0053 override fun onStop() { 0054 super.onStop() 0055 with(prefs.edit()) { 0056 if (userInput.value.isNotBlank()) putString(INPUT_CACHE_KEY, userInput.value) else remove(INPUT_CACHE_KEY) 0057 apply() 0058 } 0059 } 0060 0061 private fun sendChars(chars: String) { 0062 val np = NetworkPacket(MousePadPlugin.PACKET_TYPE_MOUSEPAD_REQUEST) 0063 np["key"] = chars 0064 sendKeyPressPacket(np) 0065 } 0066 0067 private fun sendKeyPressPacket(np: NetworkPacket) { 0068 try { 0069 Log.d("packed", np.serialize()) 0070 } catch (e: Exception) { 0071 Log.e("KDE/ComposeSend", "Exception", e) 0072 } 0073 val plugin = KdeConnect.getInstance().getDevicePlugin(deviceId, MousePadPlugin::class.java) 0074 if (plugin == null) { 0075 finish() 0076 return 0077 } 0078 plugin.sendKeyboardPacket(np) 0079 } 0080 0081 private fun sendComposed() { 0082 sendChars(userInput.value) 0083 clearComposeInput() 0084 } 0085 0086 private fun clearComposeInput() { 0087 userInput.value = String() 0088 } 0089 0090 @Composable 0091 private fun ComposeSendScreen() { 0092 Mdc3Theme { 0093 Scaffold( 0094 topBar = { 0095 KdeTopAppBar( 0096 title = stringResource(R.string.compose_send_title), 0097 navIconOnClick = { onBackPressedDispatcher.onBackPressed() }, 0098 actions = { 0099 KdeTextButton( 0100 modifier = Modifier.padding(horizontal = 8.dp), 0101 onClick = { clearComposeInput() }, 0102 text = stringResource(R.string.clear_compose), 0103 ) 0104 } 0105 ) 0106 }, 0107 ) { scaffoldPadding -> 0108 Box(modifier = Modifier.padding(scaffoldPadding).fillMaxSize()) { 0109 KdeTextField( 0110 modifier = Modifier 0111 .padding(horizontal = 16.dp) 0112 .padding(bottom = 80.dp) 0113 .align(Alignment.BottomStart) 0114 .fillMaxWidth(), 0115 input = userInput, 0116 label = stringResource(R.string.click_here_to_type), 0117 ) 0118 KdeTextButton( 0119 onClick = { sendComposed() }, 0120 modifier = Modifier.padding(all = 16.dp).align(Alignment.BottomEnd), 0121 enabled = userInput.value.isNotEmpty(), 0122 text = stringResource(R.string.send_compose), 0123 iconLeft = Icons.Default.Send, 0124 ) 0125 } 0126 } 0127 } 0128 } 0129 }