Warning, /network/kdeconnect-android/src/org/kde/kdeconnect/Plugins/RunCommandPlugin/RunCommandWidgetConfigActivity.kt is written in an unsupported language. File is not indexed.
0001 /* 0002 * SPDX-FileCopyrightText: 2023 Albert Vaca Cintora <albertvaka@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.RunCommandPlugin 0008 0009 import android.appwidget.AppWidgetManager 0010 import android.appwidget.AppWidgetManager.EXTRA_APPWIDGET_ID 0011 import android.content.Context 0012 import android.content.Intent 0013 import android.os.Bundle 0014 import android.view.View 0015 import android.view.ViewGroup 0016 import android.view.Window 0017 import android.widget.ArrayAdapter 0018 import android.widget.ImageView 0019 import android.widget.TextView 0020 import androidx.appcompat.app.AppCompatActivity 0021 import org.kde.kdeconnect.Device 0022 import org.kde.kdeconnect.KdeConnect 0023 import org.kde.kdeconnect_tp.R 0024 import org.kde.kdeconnect_tp.databinding.WidgetRemoteCommandPluginDialogBinding 0025 import java.util.stream.Collectors 0026 import kotlin.streams.toList 0027 0028 class RunCommandWidgetConfigActivity : AppCompatActivity() { 0029 0030 private var appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID 0031 0032 override fun onCreate(bundle: Bundle?) { 0033 super.onCreate(bundle) 0034 0035 setResult(RESULT_CANCELED) // Default result 0036 0037 appWidgetId = intent.extras?.getInt(EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID) ?: AppWidgetManager.INVALID_APPWIDGET_ID 0038 if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) { 0039 finish() 0040 return 0041 } 0042 0043 supportRequestWindowFeature(Window.FEATURE_NO_TITLE) 0044 0045 val binding = WidgetRemoteCommandPluginDialogBinding.inflate(layoutInflater) 0046 setContentView(binding.root) 0047 0048 val pairedDevices = KdeConnect.getInstance().devices.values.stream().filter(Device::isPaired).collect(Collectors.toList()); 0049 0050 binding.runCommandsDeviceList.adapter = object : ArrayAdapter<Device>(this, 0, pairedDevices) { 0051 override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { 0052 val view : View = convertView ?: layoutInflater.inflate(R.layout.list_item_with_icon_entry, parent, false) 0053 val device = pairedDevices[position] 0054 view.findViewById<TextView>(R.id.list_item_entry_title).text = device.name 0055 view.findViewById<ImageView>(R.id.list_item_entry_icon).setImageDrawable(device.icon) 0056 return view 0057 } 0058 } 0059 binding.runCommandsDeviceList.setOnItemClickListener { _, _, position, _ -> 0060 val deviceId = pairedDevices[position].deviceId 0061 saveWidgetDeviceIdPref(this, appWidgetId, deviceId) 0062 0063 val appWidgetManager = AppWidgetManager.getInstance(this) 0064 updateAppWidget(this, appWidgetManager, appWidgetId) 0065 0066 val resultValue = Intent() 0067 resultValue.putExtra(EXTRA_APPWIDGET_ID, appWidgetId) 0068 setResult(RESULT_OK, resultValue) 0069 finish() 0070 } 0071 binding.runCommandsDeviceList.emptyView = binding.noDevices 0072 } 0073 } 0074 0075 private const val PREFS_NAME = "org.kde.kdeconnect_tp.WidgetProvider" 0076 private const val PREF_PREFIX_KEY = "appwidget_" 0077 0078 internal fun saveWidgetDeviceIdPref(context: Context, appWidgetId: Int, deviceName: String) { 0079 val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit() 0080 prefs.putString(PREF_PREFIX_KEY + appWidgetId, deviceName) 0081 prefs.apply() 0082 } 0083 0084 internal fun loadWidgetDeviceIdPref(context: Context, appWidgetId: Int): String? { 0085 val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) 0086 return prefs.getString(PREF_PREFIX_KEY + appWidgetId, null) 0087 } 0088 0089 internal fun deleteWidgetDeviceIdPref(context: Context, appWidgetId: Int) { 0090 val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit() 0091 prefs.remove(PREF_PREFIX_KEY + appWidgetId) 0092 prefs.apply() 0093 }