Skip to content

Instantly share code, notes, and snippets.

@pfaffenrodt
Created February 5, 2016 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfaffenrodt/3675c3a16132f9e1ee28 to your computer and use it in GitHub Desktop.
Save pfaffenrodt/3675c3a16132f9e1ee28 to your computer and use it in GitHub Desktop.
Example of prefetching images with evernotes JobScheduler https://github.com/evernote/android-job and Picasso http://square.github.io/picasso/
dependencies {
compile 'com.evernote:android-job:1.0.2'
compile 'com.squareup.picasso:picasso:2.5.2'
}
package de.pfaffenrodt.job;
/**
* Copyright (C) 2016 Dimitri Pfaffenrodt
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.support.annotation.NonNull;
import android.util.Log;
import com.evernote.android.job.Job;
import com.evernote.android.job.JobRequest;
import com.evernote.android.job.util.support.PersistableBundleCompat;
import com.squareup.picasso.Callback;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;
import net.vrallev.android.cat.Cat;
import java.util.concurrent.CountDownLatch;
public class PrefetchImageJob extends Job {
public static final String TAG = "PrefetchImageJob";
public static final String EXTRAS_IMAGES = "images";
/**
* @param images
* @return The unique ID for this job.
*/
public static int createJob(String[] images){
PersistableBundleCompat extras = new PersistableBundleCompat();
extras.putStringArray(PrefetchImageJob.EXTRAS_IMAGES, images);
return new JobRequest.Builder(PrefetchImageJob.TAG)
.setExecutionWindow(30_000L, 40_000L)
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
.setExtras(extras)
.build()
.schedule();
}
@NonNull
@Override
protected Result onRunJob(Params params) {
final String[] images = params.getExtras().getStringArray(EXTRAS_IMAGES);
if(images == null){
return Result.FAILURE;
}
final CountDownLatch latch = new CountDownLatch(images.length);
for (int i = 0; i < images.length; i++) {
final String image = images[i];
if(!isValidImage(image)){
latch.countDown();
continue;
}
Picasso.with(getContext())
.load(image)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.fetch(new Callback() {
@Override
public void onSuccess() {
Log.d(TAG, "onSuccess: prefetched image:"+image);
latch.countDown();
}
@Override
public void onError() {
cancel();
Log.d(TAG, "onError: not prefetched image:" + image);
latch.countDown();
}
});
}
try {
latch.await();
} catch (InterruptedException e) {
Cat.e(e);
}
if (isCanceled()) {
return params.isPeriodic() ? Result.FAILURE : Result.RESCHEDULE;
} else {
return Result.SUCCESS;
}
}
private boolean isValidImage(String imageUrl) {
return imageUrl!=null && !imageUrl.trim().isEmpty();
}
}
package de.pfaffenrodt.job;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.evernote.android.job.Job;
import com.evernote.android.job.JobCreator;
import com.evernote.android.job.JobManager;
import com.evernote.android.job.JobRequest;
import com.evernote.android.job.util.support.PersistableBundleCompat;
import de.pfaffenrodt.job.PrefetchImageJob;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JobManager.create(this).addJobCreator(new JobCreator() {
@Override
public Job create(String tag) {
return new PrefetchImageJob();
}
});
PrefetchImageJob.createJob(new String[]{
"http://lorempixel.com/400/200/",
"http://lorempixel.com/200/300/",
"http://lorempixel.com/400/500/"
});
}
@Override
protected void onDestroy() {
super.onDestroy();
JobManager.instance().cancelAll();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment