use of com.mareksebera.simpledilbert.utilities.GetStripUrlInterface in project Simple-Dilbert by smarek.
the class WidgetProvider method updateAppWidget.
private static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId) {
final DilbertPreferences prefs = new DilbertPreferences(context);
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
views.setOnClickPendingIntent(R.id.widget_previous, getPendingIntent(INTENT_PREVIOUS, context, appWidgetId));
views.setOnClickPendingIntent(R.id.widget_next, getPendingIntent(INTENT_NEXT, context, appWidgetId));
views.setOnClickPendingIntent(R.id.widget_latest, getPendingIntent(INTENT_LATEST, context, appWidgetId));
views.setOnClickPendingIntent(R.id.widget_random, getPendingIntent(INTENT_RANDOM, context, appWidgetId));
views.setOnClickPendingIntent(R.id.widget_image, getPendingIntent(INTENT_DISPLAY, context, appWidgetId));
views.setOnClickPendingIntent(R.id.widget_refresh, getPendingIntent(INTENT_REFRESH, context, appWidgetId));
views.setOnClickPendingIntent(R.id.widget_full_layout, getPendingIntent(INTENT_DISPLAY, context, appWidgetId));
final LocalDate currentDate = prefs.getDateForWidgetId(appWidgetId);
views.setViewVisibility(R.id.widget_next, prefs.isWidgetAlwaysShowLatest() ? View.GONE : View.VISIBLE);
views.setViewVisibility(R.id.widget_previous, prefs.isWidgetAlwaysShowLatest() ? View.GONE : View.VISIBLE);
views.setViewVisibility(R.id.widget_random, prefs.isWidgetAlwaysShowLatest() ? View.GONE : View.VISIBLE);
views.setViewVisibility(R.id.widget_latest, prefs.isWidgetAlwaysShowLatest() ? View.GONE : View.VISIBLE);
final String cachedUrl = prefs.getCachedUrl(currentDate);
final String cachedTitle = prefs.getCachedTitle(currentDate);
views.setViewVisibility(R.id.widget_progress, View.VISIBLE);
views.setTextViewText(R.id.widget_title, prefs.getDateForWidgetId(appWidgetId).toString(DilbertPreferences.NICE_DATE_FORMATTER));
if (prefs.isWidgetShowTitle() && cachedTitle != null && !cachedTitle.isEmpty()) {
views.setViewVisibility(R.id.widget_strip_title, View.VISIBLE);
views.setTextViewText(R.id.widget_strip_title, cachedTitle);
} else {
views.setViewVisibility(R.id.widget_strip_title, View.GONE);
}
appWidgetManager.updateAppWidget(appWidgetId, views);
if (cachedUrl == null) {
new GetStripUrl(new GetStripUrlInterface() {
@Override
public void imageLoadFailed(String url, Throwable reason) {
currentToast = Toast.makeText(context, "Image Loading failed", Toast.LENGTH_SHORT);
currentToast.show();
views.setImageViewResource(R.id.widget_image, R.drawable.cancel);
views.setViewVisibility(R.id.widget_progress, View.GONE);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void displayImage(String url, String title) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}, prefs, currentDate).execute();
} else {
Glide.with(context).asBitmap().load(cachedUrl).apply(new RequestOptions().dontAnimate()).listener(new RequestListener<Bitmap>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
updateAppWidget(context, appWidgetManager, appWidgetId);
return false;
}
@Override
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
views.setViewVisibility(R.id.widget_progress, View.GONE);
appWidgetManager.updateAppWidget(appWidgetId, views);
Glide.with(context).asBitmap().load(cachedUrl).apply(new RequestOptions().dontAnimate()).into(new AppWidgetTarget(context, R.id.widget_image, views, appWidgetId));
return false;
}
}).into(new AppWidgetTarget(context, R.id.widget_image, views, appWidgetId));
}
}
Aggregations