Search in sources :

Example 1 with ArtworkModelLoader

use of com.simplecity.amp_library.glide.loader.ArtworkModelLoader in project Shuttle by timusus.

the class ArtworkDownloadService method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    final ComponentName serviceName = new ComponentName(this, ArtworkDownloadService.class);
    Intent intent = new Intent(ACTION_CANCEL);
    intent.setComponent(serviceName);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
    notificationBuilder = new NotificationCompat.Builder(this).setContentTitle(getResources().getString(R.string.notif_downloading_art)).setSmallIcon(android.R.drawable.stat_sys_download).setOngoing(true).setProgress(100, 0, true).addAction(new NotificationCompat.Action(R.drawable.ic_action_navigation_close, getString(R.string.cancel), pendingIntent));
    if (!ShuttleUtils.isOnline(false)) {
        Toast toast = Toast.makeText(this, getResources().getString(R.string.connection_unavailable), Toast.LENGTH_SHORT);
        toast.show();
        stopSelf();
        return;
    }
    if (notificationBuilder != null) {
        notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
    }
    subscription = new CompositeSubscription();
    Observable<List<ArtworkProvider>> sharedItemsObservable = DataManager.getInstance().getAlbumArtistsRelay().first().<ArtworkProvider>flatMap(Observable::from).mergeWith(DataManager.getInstance().getAlbumsRelay().first().flatMap(Observable::from)).toList().share();
    subscription.add(sharedItemsObservable.observeOn(AndroidSchedulers.mainThread()).subscribe(list -> {
        max = list.size();
        updateProgress();
    }));
    subscription.add(sharedItemsObservable.flatMap(Observable::from).flatMap(artworkProvider -> Observable.just(artworkProvider).subscribeOn(Schedulers.computation()).map(artwork -> {
        FutureTarget<File> futureTarget = Glide.with(ArtworkDownloadService.this).using(new ArtworkModelLoader(true), InputStream.class).load(artwork).as(InputStream.class).downloadOnly(SimpleTarget.SIZE_ORIGINAL, SimpleTarget.SIZE_ORIGINAL);
        try {
            futureTarget.get(30, TimeUnit.SECONDS);
        } catch (InterruptedException | ExecutionException | TimeoutException e) {
            Log.e(TAG, "Error downloading artworkProvider: " + e);
        }
        Glide.clear(futureTarget);
        return null;
    })).observeOn(AndroidSchedulers.mainThread()).subscribe(item -> {
        updateProgress();
    }));
}
Also used : R(com.simplecity.amp_library.R) Context(android.content.Context) TimeoutException(java.util.concurrent.TimeoutException) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) IBinder(android.os.IBinder) Observable(rx.Observable) FutureTarget(com.bumptech.glide.request.FutureTarget) Toast(android.widget.Toast) Schedulers(rx.schedulers.Schedulers) ShuttleUtils(com.simplecity.amp_library.utils.ShuttleUtils) Log(android.util.Log) ArtworkModelLoader(com.simplecity.amp_library.glide.loader.ArtworkModelLoader) SimpleTarget(com.bumptech.glide.request.target.SimpleTarget) NotificationManager(android.app.NotificationManager) ComponentName(android.content.ComponentName) Service(android.app.Service) ArtworkProvider(com.simplecity.amp_library.model.ArtworkProvider) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CompositeSubscription(rx.subscriptions.CompositeSubscription) List(java.util.List) Glide(com.bumptech.glide.Glide) NotificationCompat(android.support.v4.app.NotificationCompat) DataManager(com.simplecity.amp_library.utils.DataManager) Nullable(android.support.annotation.Nullable) InputStream(java.io.InputStream) InputStream(java.io.InputStream) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Observable(rx.Observable) ArtworkModelLoader(com.simplecity.amp_library.glide.loader.ArtworkModelLoader) Toast(android.widget.Toast) CompositeSubscription(rx.subscriptions.CompositeSubscription) ComponentName(android.content.ComponentName) List(java.util.List) PendingIntent(android.app.PendingIntent) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

NotificationManager (android.app.NotificationManager)1 PendingIntent (android.app.PendingIntent)1 Service (android.app.Service)1 ComponentName (android.content.ComponentName)1 Context (android.content.Context)1 Intent (android.content.Intent)1 IBinder (android.os.IBinder)1 Nullable (android.support.annotation.Nullable)1 NotificationCompat (android.support.v4.app.NotificationCompat)1 Log (android.util.Log)1 Toast (android.widget.Toast)1 Glide (com.bumptech.glide.Glide)1 FutureTarget (com.bumptech.glide.request.FutureTarget)1 SimpleTarget (com.bumptech.glide.request.target.SimpleTarget)1 R (com.simplecity.amp_library.R)1 ArtworkModelLoader (com.simplecity.amp_library.glide.loader.ArtworkModelLoader)1 ArtworkProvider (com.simplecity.amp_library.model.ArtworkProvider)1 DataManager (com.simplecity.amp_library.utils.DataManager)1 ShuttleUtils (com.simplecity.amp_library.utils.ShuttleUtils)1 File (java.io.File)1