Skip to content

Instantly share code, notes, and snippets.

View Ayush783's full-sized avatar

Aayush sharma Ayush783

View GitHub Profile
class ListViewAppWidgetProvider : HomeWidgetProvider() {
...
/// Capture the intent, replace current view with a loader and trigger WorkRequest
override fun onReceive(context: Context?, intent: Intent?) {
super.onReceive(context, intent)
val action = intent?.action
if (action == "com.example.listview_app_widget.refresh") {
val appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID)
val loader = RemoteViews(context!!.packageName, R.layout.listview_widget_loader)
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M17.65,6.35C16.2,4.9 14.21,4 12,4c-4.42,0 -7.99,3.58 -7.99,8s3.57,8 7.99,8c3.73,0 6.84,-2.55 7.73,-6h-2.08c-0.82,2.33 -3.04,4 -5.65,4 -3.31,0 -6,-2.69 -6,-6s2.69,-6 6,-6c1.66,0 3.14,0.69 4.22,1.78L13,11h7V4l-2.35,2.35z"/>
</vector>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp"
android:layout_centerVertical="true"
android:background="@android:color/white">
class ListViewRemoteViewsFactory(private val context: Context, private val intent: Intent?) : RemoteViewsService.RemoteViewsFactory{
private var articles: List<NewsArticle>? = null
private var articleImages: List<Bitmap?> = ArrayList()
override fun onCreate() {
val articlesJson: String? = intent?.extras?.getString("ARTICLES_JSON","")
if(articlesJson != null) {
...
// Load images
LoadImagesTask().execute(articles)
class ListViewRemoteViewsFactory(private val context: Context, private val intent: Intent?) : RemoteViewsService.RemoteViewsFactory{
private var articleImages: List<Bitmap?> = ArrayList()
...
private inner class LoadImagesTask : AsyncTask<List<NewsArticle>, Void, List<Bitmap?>>() {
override fun doInBackground(vararg params: List<NewsArticle>): List<Bitmap?> {
return params[0].map { article ->
try {
Glide.with(context)
class ListViewRemoteViewsFactory(private val context: Context, private val intent: Intent?) : RemoteViewsService.RemoteViewsFactory{
private var articles: List<NewsArticle>? = null
override fun onCreate() {
val articlesJson: String? = intent?.extras?.getString("ARTICLES_JSON","")
if(articlesJson != null) {
val articleListType = object : TypeToken<List<NewsArticle>>() {}.type
val data: List<NewsArticle> = Gson().fromJson(articlesJson, articleListType)
articles = data
}
class ListViewAppWidgetProvider : HomeWidgetProvider() {
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray,
widgetData: SharedPreferences
) {
// Attempts to get the data from shared preferences
val data = HomeWidgetPlugin.getData(context).getString("NEWS_ARTICLES_DATA","")
appWidgetIds.forEach { id ->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
tools:context=".MainActivity">
<!-- Circular Loading Indicator -->
<ProgressBar
class UpdateWidgetWorker(context: Context, workerParams: WorkerParameters) : CoroutineWorker(context, workerParams) {
private val client = OkHttpClient()
override suspend fun doWork(): Result {
return withContext(Dispatchers.IO) {
fetchData()
}
}
private suspend fun fetchData(): Result {
class ListViewAppWidgetProvider : HomeWidgetProvider() {
...
companion object {
fun createListView(context: Context, widgetId: Int, data: String): RemoteViews {
val intent = Intent(
context,
ListViewWidgetService::class.java
)
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId)