Search in sources :

Example 1 with AppWidgetTarget

use of com.bumptech.glide.request.target.AppWidgetTarget 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)

Example 2 with AppWidgetTarget

use of com.bumptech.glide.request.target.AppWidgetTarget in project reark by reark.

the class WidgetService method updateWidget.

private void updateWidget(final int widgetId) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
    RemoteViews remoteViews = new RemoteViews(getApplication().getPackageName(), R.layout.widget_layout);
    remoteViews.setTextViewText(R.id.widget_layout_title, "Loading repository..");
    appWidgetManager.updateAppWidget(widgetId, remoteViews);
    clearDisposable();
    compositeDisposable.add(getUserSettings.call().map(UserSettings::getSelectedRepositoryId).doOnNext(repositoryId -> Log.d(TAG, "Changed repository to " + repositoryId)).switchMap(fetchAndGetGitHubRepository::call).filter(DataStreamNotification::isOnNext).map(DataStreamNotification::getValue).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(repository -> {
        remoteViews.setTextViewText(R.id.widget_layout_title, repository.getName());
        remoteViews.setTextViewText(R.id.widget_layout_stargazers, "stars: " + repository.getStargazersCount());
        remoteViews.setTextViewText(R.id.widget_layout_forks, "forks: " + repository.getForksCount());
        AppWidgetTarget widgetTarget = new AppWidgetTarget(WidgetService.this, remoteViews, R.id.widget_avatar_image_view, widgetId);
        Glide.with(WidgetService.this).load(repository.getOwner().getAvatarUrl()).asBitmap().fitCenter().into(widgetTarget);
        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }));
}
Also used : RemoteViews(android.widget.RemoteViews) R(io.reark.rxgithubapp.R) UserSettings(io.reark.rxgithubapp.shared.pojo.UserSettings) Log(io.reark.reark.utils.Log) Service(android.app.Service) Intent(android.content.Intent) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) IBinder(android.os.IBinder) AppWidgetManager(android.appwidget.AppWidgetManager) Inject(javax.inject.Inject) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Glide(com.bumptech.glide.Glide) AppWidgetTarget(com.bumptech.glide.request.target.AppWidgetTarget) RxGitHubApp(io.reark.rxgithubapp.advanced.RxGitHubApp) Schedulers(io.reactivex.schedulers.Schedulers) DataStreamNotification(io.reark.reark.data.DataStreamNotification) DataFunctions(io.reark.rxgithubapp.shared.data.DataFunctions) RemoteViews(android.widget.RemoteViews) AppWidgetTarget(com.bumptech.glide.request.target.AppWidgetTarget) UserSettings(io.reark.rxgithubapp.shared.pojo.UserSettings) DataStreamNotification(io.reark.reark.data.DataStreamNotification) AppWidgetManager(android.appwidget.AppWidgetManager)

Aggregations

RemoteViews (android.widget.RemoteViews)2 AppWidgetTarget (com.bumptech.glide.request.target.AppWidgetTarget)2 Service (android.app.Service)1 AppWidgetManager (android.appwidget.AppWidgetManager)1 Intent (android.content.Intent)1 Bitmap (android.graphics.Bitmap)1 IBinder (android.os.IBinder)1 Nullable (android.support.annotation.Nullable)1 Glide (com.bumptech.glide.Glide)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 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 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)1 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)1 Schedulers (io.reactivex.schedulers.Schedulers)1