rifki_cs29

StackRemoteViewsFactory

Jul 6th, 2020
910
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.content.Context
  4. import android.content.Intent
  5. import android.database.Cursor
  6. import android.graphics.Bitmap
  7.  
  8. import android.os.Binder
  9. import android.os.Bundle
  10. import android.widget.RemoteViews
  11. import android.widget.RemoteViewsService
  12. import com.bumptech.glide.Glide
  13. import com.rifki.kotlin.mygithubfinal.R
  14. import com.rifki.kotlin.mygithubfinal.db.DatabaseContract.FavouriteColumns.Companion.CONTENT_URI
  15. import com.rifki.kotlin.mygithubfinal.helper.FavouritesItem
  16. import com.squareup.picasso.Picasso
  17. import java.util.concurrent.ExecutionException
  18.  
  19. class StackRemoteViewsFactory(private val mContext: Context, intent: Intent?) : RemoteViewsService.RemoteViewsFactory{
  20.  
  21.     private var listFavouriteUsers : Cursor? = null
  22.  
  23.     override fun onDataSetChanged() {
  24.         if (listFavouriteUsers != null) {
  25.             listFavouriteUsers?.close()
  26.         }
  27.  
  28.         val identityToken = Binder.clearCallingIdentity()
  29.         listFavouriteUsers = mContext.contentResolver.query(CONTENT_URI, null, null, null, null)
  30.         Binder.restoreCallingIdentity(identityToken)
  31.     }
  32.  
  33.     override fun onCreate() {
  34.         listFavouriteUsers = mContext.contentResolver.query(CONTENT_URI, null, null, null, null)
  35.     }
  36.  
  37.     override fun getCount(): Int {
  38.         return listFavouriteUsers?.count ?: 0
  39.     }
  40.  
  41.     override fun getViewAt(position: Int): RemoteViews {
  42.         val item: FavouritesItem = getItem(position)
  43.         val rv = RemoteViews(mContext.packageName, R.layout.favorite_user_widget)
  44.         var bitmap: Bitmap? = null
  45.         try {
  46.             bitmap = Glide.with(mContext)
  47.                 .asBitmap()
  48.                 .load(item.avatar)
  49.                 .submit()
  50.                 .get()
  51.         } catch (e: InterruptedException) {
  52.             e.printStackTrace()
  53.         } catch (e: ExecutionException) {
  54.             e.printStackTrace()
  55.         }
  56.         rv.setImageViewBitmap(R.id.imageView, bitmap)
  57.         val extras = Bundle()
  58.         extras.putString(FavoriteUserWidget.EXTRA_ITEM, item.username)
  59.         val fillInIntent = Intent()
  60.         fillInIntent.putExtras(extras)
  61.         rv.setOnClickFillInIntent(R.id.imageView, fillInIntent)
  62.         return rv
  63.     }
  64.  
  65.     private fun getItem(position: Int): FavouritesItem {
  66.         listFavouriteUsers?.moveToPosition(position)?.let { check(it) { "Position invalid!" } }
  67.         return FavouritesItem(listFavouriteUsers)
  68.     }
  69.  
  70.     override fun getLoadingView(): RemoteViews? = null
  71.  
  72.     override fun getViewTypeCount(): Int = 1
  73.  
  74.     override fun getItemId(i: Int): Long = 0
  75.  
  76.     override fun hasStableIds(): Boolean = false
  77.  
  78.     override fun onDestroy() { }
  79.  
  80. }
RAW Paste Data