Search in sources :

Example 1 with DilbertPreferences

use of com.mareksebera.simpledilbert.preferences.DilbertPreferences 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 DilbertPreferences

use of com.mareksebera.simpledilbert.preferences.DilbertPreferences in project Simple-Dilbert by smarek.

the class WidgetProvider method onReceive.

@Override
public void onReceive(@NotNull Context context, @NotNull Intent intent) {
    String action = intent.getAction();
    if (intent.getExtras() == null)
        return;
    final int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
    final DilbertPreferences preferences = new DilbertPreferences(context);
    if (action == null || appWidgetId == -1) {
        super.onReceive(context, intent);
        return;
    }
    if (currentToast != null)
        currentToast.cancel();
    switch(action) {
        case INTENT_PREVIOUS:
            preferences.saveDateForWidgetId(appWidgetId, preferences.getDateForWidgetId(appWidgetId).minusDays(1));
            break;
        case INTENT_NEXT:
            preferences.saveDateForWidgetId(appWidgetId, preferences.getDateForWidgetId(appWidgetId).plusDays(1));
            break;
        case INTENT_LATEST:
            preferences.saveDateForWidgetId(appWidgetId, LocalDate.now());
            break;
        case INTENT_RANDOM:
            preferences.saveDateForWidgetId(appWidgetId, DilbertPreferences.getRandomDate());
            break;
        case INTENT_REFRESH:
            preferences.removeCache(preferences.getDateForWidgetId(appWidgetId));
            break;
        case INTENT_DISPLAY:
            preferences.saveCurrentDate(preferences.getDateForWidgetId(appWidgetId));
            Intent display = new Intent(context, DilbertFragmentActivity.class);
            display.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(display);
            break;
        case AppWidgetManager.ACTION_APPWIDGET_UPDATE:
            LocalDate current = preferences.getDateForWidgetId(appWidgetId);
            if (current.equals(LocalDate.now().minusDays(1))) {
                preferences.saveDateForWidgetId(appWidgetId, LocalDate.now());
            }
            break;
    }
    updateAppWidget(context, AppWidgetManager.getInstance(context), appWidgetId);
    if (currentToast != null)
        currentToast.show();
    super.onReceive(context, intent);
}
Also used : Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) DilbertPreferences(com.mareksebera.simpledilbert.preferences.DilbertPreferences) LocalDate(org.joda.time.LocalDate)

Example 3 with DilbertPreferences

use of com.mareksebera.simpledilbert.preferences.DilbertPreferences in project Simple-Dilbert by smarek.

the class DilbertFragmentActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstance) {
    preferences = new DilbertPreferences(this);
    setTheme(preferences.isDarkLayoutEnabled() ? R.style.AppThemeDark : R.style.AppThemeLight);
    super.onCreate(savedInstance);
    if (preferences.isForceLandscape())
        setRequestedOrientation(preferences.getLandscapeOrientation());
    setContentView(R.layout.activity_dilbert_fragments);
    viewPager = findViewById(R.id.view_pager);
    adapter = new DilbertFragmentAdapter(getSupportFragmentManager(), preferences);
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(pageChangedListener);
    if (preferences.isToolbarsHidden())
        ActionBarUtility.toggleActionBar(this, viewPager);
    tryHandleUrlIntent();
}
Also used : DilbertPreferences(com.mareksebera.simpledilbert.preferences.DilbertPreferences)

Example 4 with DilbertPreferences

use of com.mareksebera.simpledilbert.preferences.DilbertPreferences in project Simple-Dilbert by smarek.

the class DilbertFragment method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.preferences = new DilbertPreferences(getActivity());
    this.zoomLevel = preferences.getDefaultZoomLevel();
    setHasOptionsMenu(true);
}
Also used : DilbertPreferences(com.mareksebera.simpledilbert.preferences.DilbertPreferences)

Example 5 with DilbertPreferences

use of com.mareksebera.simpledilbert.preferences.DilbertPreferences in project Simple-Dilbert by smarek.

the class DownloadManagerBroadcastReceiver method onReceive.

@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void onReceive(Context context, Intent intent) {
    try {
        DilbertPreferences preferences = new DilbertPreferences(context);
        DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        Uri downloadedFilePath = dm.getUriForDownloadedFile(intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0));
        if (downloadedFilePath == null) {
            Log.d("DlManagerBReceiver", "No Uri for downloaded file from DownloadManager");
            return;
        }
        File downloadedFile = new File(downloadedFilePath.getPath());
        if (downloadedFilePath.getLastPathSegment() == null) {
            return;
        }
        String scheduledTargetPath = preferences.getScheduledTargetPath(downloadedFilePath.getLastPathSegment().substring(0, 10));
        if (scheduledTargetPath != null) {
            if (!downloadedFile.renameTo(new File(scheduledTargetPath))) {
                Toast.makeText(context, "Couldn't move picture to selected Folder", Toast.LENGTH_SHORT).show();
            }
        }
    } catch (Throwable t) {
        Log.e("DlManagerBReceiver", "Error while moving downloaded file to desired target folder", t);
    }
}
Also used : DilbertPreferences(com.mareksebera.simpledilbert.preferences.DilbertPreferences) DownloadManager(android.app.DownloadManager) Uri(android.net.Uri) File(java.io.File) TargetApi(android.annotation.TargetApi)

Aggregations

DilbertPreferences (com.mareksebera.simpledilbert.preferences.DilbertPreferences)6 LocalDate (org.joda.time.LocalDate)2 TargetApi (android.annotation.TargetApi)1 DownloadManager (android.app.DownloadManager)1 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 Bitmap (android.graphics.Bitmap)1 Uri (android.net.Uri)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 GetStripUrl (com.mareksebera.simpledilbert.utilities.GetStripUrl)1 GetStripUrlInterface (com.mareksebera.simpledilbert.utilities.GetStripUrlInterface)1 File (java.io.File)1