Warning, /network/kdeconnect-android/src/org/kde/kdeconnect/Plugins/RunCommandPlugin/RunCommandWidgetDataProvider.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.util.Log 0014 import android.view.View 0015 import android.widget.RemoteViews 0016 import android.widget.RemoteViewsService 0017 import android.widget.RemoteViewsService.RemoteViewsFactory 0018 import org.kde.kdeconnect.KdeConnect 0019 import org.kde.kdeconnect_tp.R 0020 0021 internal class RunCommandWidgetDataProvider(private val context: Context, val intent: Intent?) : RemoteViewsFactory { 0022 0023 private var deviceId : String? = null 0024 private var widgetId : Int = AppWidgetManager.INVALID_APPWIDGET_ID 0025 0026 override fun onCreate() { 0027 widgetId = intent?.getIntExtra(EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID) ?: AppWidgetManager.INVALID_APPWIDGET_ID 0028 if (widgetId == AppWidgetManager.INVALID_APPWIDGET_ID) { 0029 Log.e("KDEConnect/Widget", "RunCommandWidgetDataProvider: No widget id extra was set") 0030 return 0031 } 0032 deviceId = loadWidgetDeviceIdPref(context, widgetId) 0033 } 0034 0035 override fun onDataSetChanged() { 0036 deviceId = loadWidgetDeviceIdPref(context, widgetId) 0037 } 0038 0039 override fun onDestroy() {} 0040 0041 private fun getPlugin(): RunCommandPlugin? { 0042 return KdeConnect.getInstance().getDevicePlugin(deviceId, RunCommandPlugin::class.java) 0043 } 0044 0045 override fun getCount(): Int { 0046 return getPlugin()?.commandItems?.size ?: 0 0047 } 0048 0049 override fun getViewAt(i: Int): RemoteViews { 0050 val remoteView = RemoteViews(context.packageName, R.layout.list_item_entry) 0051 0052 val plugin : RunCommandPlugin? = getPlugin() 0053 if (plugin == null) { 0054 // Either the deviceId was null, or the plugin is not available. 0055 if (deviceId != null) { 0056 Log.e("getViewAt", "RunCommandWidgetDataProvider: Plugin not found") 0057 } 0058 // Return a new, not-configured layout as a fallback 0059 return remoteView 0060 } 0061 0062 val listItem = plugin.commandItems[i] 0063 0064 remoteView.setTextViewText(R.id.list_item_entry_title, listItem.name) 0065 remoteView.setTextViewText(R.id.list_item_entry_summary, listItem.command) 0066 remoteView.setViewVisibility(R.id.list_item_entry_summary, View.VISIBLE) 0067 0068 val runCommandIntent = Intent(context, RunCommandWidgetProvider::class.java) 0069 runCommandIntent.action = RUN_COMMAND_ACTION 0070 runCommandIntent.putExtra(EXTRA_APPWIDGET_ID, widgetId) 0071 runCommandIntent.putExtra(TARGET_COMMAND, listItem.key) 0072 runCommandIntent.putExtra(TARGET_DEVICE, deviceId) 0073 remoteView.setOnClickFillInIntent(R.id.list_item_entry, runCommandIntent) 0074 0075 return remoteView 0076 } 0077 0078 override fun getLoadingView(): RemoteViews? { 0079 return null 0080 } 0081 0082 override fun getViewTypeCount(): Int { 0083 return 1 0084 } 0085 0086 override fun getItemId(i: Int): Long { 0087 return getPlugin()?.commandItems?.get(i)?.key?.hashCode()?.toLong() ?: 0 0088 } 0089 0090 override fun hasStableIds(): Boolean { 0091 return false 0092 } 0093 } 0094 0095 class CommandsRemoteViewsService : RemoteViewsService() { 0096 override fun onGetViewFactory(intent: Intent): RemoteViewsFactory { 0097 return RunCommandWidgetDataProvider(this.applicationContext, intent) 0098 } 0099 } 0100