rifki_cs29

FavouriteUserWidget

Jul 6th, 2020
741
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.rifki.kotlin.mygithubfinal.widget
  2.  
  3. import android.app.PendingIntent
  4. import android.appwidget.AppWidgetManager
  5. import android.appwidget.AppWidgetProvider
  6. import android.content.Context
  7. import android.content.Intent
  8. import android.widget.RemoteViews
  9. import android.widget.Toast
  10. import androidx.core.net.toUri
  11. import com.rifki.kotlin.mygithubfinal.R
  12.  
  13. /**
  14.  * Implementation of App Widget functionality.
  15.  */
  16. class FavoriteUserWidget : AppWidgetProvider() {
  17.     companion object {
  18.         private const val TOAST_ACTION = "com.rifki.kotlin.mygithubfinal.TOAST_ACTION"
  19.         const val EXTRA_ITEM = "com.rifki.kotlin.mygithubfinal.EXTRA_ITEM"
  20.  
  21.         //pindahkan fungsi ini ke companion object, karena kita akan memanggil fungsi ini dari luar kelas
  22.         //Kita memasang RemoteAdapter ke dalam widget dengan menggunakan obyek Intent dan nilai id dari RemoteView yaitu stack_view
  23.         private fun updateAppWidget(context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int) {
  24.             val intent = Intent(context, StackWidgetService::class.java)
  25.             intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
  26.             intent.data = intent.toUri(Intent.URI_INTENT_SCHEME).toUri()
  27.  
  28.             val views = RemoteViews(context.packageName, R.layout.favorite_user_widget)
  29.             views.setRemoteAdapter(R.id.stack_view, intent)
  30.             views.setEmptyView(R.id.stack_view, R.id.empty_view)
  31.  
  32.             val toastIntent = Intent(context, FavoriteUserWidget::class.java)
  33.             toastIntent.action = TOAST_ACTION
  34.             toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
  35.             intent.data = intent.toUri(Intent.URI_INTENT_SCHEME).toUri()
  36.             val toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent, PendingIntent.FLAG_UPDATE_CURRENT)
  37.             views.setPendingIntentTemplate(R.id.stack_view, toastPendingIntent)
  38.  
  39.             appWidgetManager.updateAppWidget(appWidgetId, views)
  40.         }
  41.     }
  42.  
  43.     override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
  44.         // There may be multiple widgets active, so update all of them
  45.         for (appWidgetId in appWidgetIds) {
  46.             updateAppWidget(context, appWidgetManager, appWidgetId)
  47.         }
  48.     }
  49.  
  50.     override fun onReceive(context: Context, intent: Intent) {
  51.         super.onReceive(context, intent)
  52.         if (intent.action != null) {
  53.             if (intent.action == TOAST_ACTION) {
  54.                 val viewIndex = intent.getStringExtra(EXTRA_ITEM)
  55.                 Toast.makeText(context, viewIndex, Toast.LENGTH_SHORT).show()
  56.             }
  57.         }
  58.     }
  59.  
  60.     override fun onEnabled(context: Context) {
  61.         // Enter relevant functionality for when the first widget is created
  62.     }
  63.  
  64.     override fun onDisabled(context: Context) {
  65.         // Enter relevant functionality for when the last widget is disabled
  66.     }
  67. }
RAW Paste Data