Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.dicoding.example;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;
import com.alfianyusufabdullah.infofilm.R;
import com.alfianyusufabdullah.infofilm.database.DatabaseContract;
import com.alfianyusufabdullah.infofilm.database.DatabaseHelper;
import com.alfianyusufabdullah.infofilm.model.ModelFavoriteMovie;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
public class FavoriteWidgetRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
private List<ModelFavoriteMovie> favorites = new ArrayList<>();
private Context context;
private int appWidgetId;
public FavoriteWidgetRemoteViewsFactory(Context context, Intent intent) {
this.context = context;
appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
@Override
public void onCreate() {
loadWidgetData();
}
@Override
public void onDataSetChanged() {
loadWidgetData();
}
private void loadWidgetData() {
favorites.clear();
DatabaseHelper helper = new DatabaseHelper(context);
SQLiteDatabase database = helper.getWritableDatabase();
Cursor cursors = database.query(DatabaseContract.MOVIE_TABLE, null, null, null, null, null, null);
if (cursors != null && cursors.moveToFirst()) {
do {
ModelFavoriteMovie fav = new ModelFavoriteMovie(cursors);
favorites.add(fav);
Log.i("WIDGET_DATA", "loadWidgetData: " + fav.getTitle());
} while (cursors.moveToNext());
cursors.close();
}
}
@Override
public void onDestroy() {
}
@Override
public int getCount() {
return favorites.size();
}
@Override
public RemoteViews getViewAt(final int position) {
final RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.row_favorite_movie);
try {
ModelFavoriteMovie favoriteMovie = favorites.get(position);
Bundle bind = new Bundle();
bind.putInt(FavoriteMovieWidget.EXTRA_ID_MOVIE, favoriteMovie.getId());
Intent i = new Intent();
i.putExtras(bind);
remoteViews.setImageViewResource(R.id.widgetBanner, R.drawable.header_background);
remoteViews.setTextViewText(R.id.widgetTitle, favoriteMovie.getTitle());
remoteViews.setOnClickFillInIntent(R.id.widgetBanner, i);
String posterUrl = favoriteMovie.getPoster().replace("/w92/", "/w500/");
try {
Bitmap preview = Glide.with(context)
.asBitmap()
.load(posterUrl)
.apply(new RequestOptions().fitCenter())
.submit()
.get();
remoteViews.setImageViewBitmap(R.id.widgetBanner , preview);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
return remoteViews;
}
@Override
public RemoteViews getLoadingView() {
return null;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.