Search in sources :

Example 1 with GetStripUrlInterface

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));
    }
}
Also used : RequestListener(com.bumptech.glide.request.RequestListener) RequestOptions(com.bumptech.glide.request.RequestOptions) LocalDate(org.joda.time.LocalDate) GetStripUrl(com.mareksebera.simpledilbert.utilities.GetStripUrl) DataSource(com.bumptech.glide.load.DataSource) RemoteViews(android.widget.RemoteViews) Target(com.bumptech.glide.request.target.Target) AppWidgetTarget(com.bumptech.glide.request.target.AppWidgetTarget) Bitmap(android.graphics.Bitmap) AppWidgetTarget(com.bumptech.glide.request.target.AppWidgetTarget) GetStripUrlInterface(com.mareksebera.simpledilbert.utilities.GetStripUrlInterface) DilbertPreferences(com.mareksebera.simpledilbert.preferences.DilbertPreferences) GlideException(com.bumptech.glide.load.engine.GlideException) Nullable(android.support.annotation.Nullable)

Aggregations

Bitmap (android.graphics.Bitmap)1 Nullable (android.support.annotation.Nullable)1 RemoteViews (android.widget.RemoteViews)1 DataSource (com.bumptech.glide.load.DataSource)1 GlideException (com.bumptech.glide.load.engine.GlideException)1 RequestListener (com.bumptech.glide.request.RequestListener)1 RequestOptions (com.bumptech.glide.request.RequestOptions)1 AppWidgetTarget (com.bumptech.glide.request.target.AppWidgetTarget)1 Target (com.bumptech.glide.request.target.Target)1 DilbertPreferences (com.mareksebera.simpledilbert.preferences.DilbertPreferences)1 GetStripUrl (com.mareksebera.simpledilbert.utilities.GetStripUrl)1 GetStripUrlInterface (com.mareksebera.simpledilbert.utilities.GetStripUrlInterface)1 LocalDate (org.joda.time.LocalDate)1