-
-
Save pishguy/7ce8d3cb9db41828a5a017b6499d4fad to your computer and use it in GitHub Desktop.
Implementing JobManager for Dagger2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Scope | |
public @interface ActivitiesScope { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.content.Context; | |
import javax.inject.Named; | |
import dagger.Module; | |
import dagger.Provides; | |
import ir.pishguy.cafealachiqpro.Dagger.Scope.GithubApplicationScope; | |
@Module | |
public class ActivityModule { | |
private final Activity context; | |
public ActivityModule(Activity context) { | |
this.context = context; | |
} | |
@Provides | |
@GithubApplicationScope | |
@Named("activity_context") | |
public Context context() { | |
return context; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ActivityRegister extends BaseActivities | |
implements ActivityRegisterContract.View, BaseActivities.connections, | |
PermissionCallback, ErrorCallback, SlidingLayer.OnInteractListener, | |
LoaderManager.LoaderCallbacks<Cursor> { | |
@Inject | |
GithubService githubService; | |
@Inject | |
JobManager jobManager; | |
private GetLatestRepositories getLatestRepositories; | |
private ApplicationComponent component; | |
private Call<List<GithubRepo>> repositoryCall; | |
private UserInformation userInfo; | |
private SocketServiceProvider mBoundService; | |
private ServiceConnection socketConnection = new ServiceConnection() { | |
@Override | |
public void onServiceConnected(ComponentName name, IBinder service) { | |
mBoundService = ((SocketServiceProvider.LocalBinder) service).getService(); | |
} | |
@Override | |
public void onServiceDisconnected(ComponentName name) { | |
mBoundService = null; | |
} | |
}; | |
private ActivityRegisterBinding binding; | |
private OnGetPermission onGetPermission; | |
private StringLoader loader; | |
private List<RobotViewModel> model = new ArrayList<>(); | |
private MediaPlayer mediaPlayer; | |
private RobotMessagesAdapter adapter; | |
private boolean socketAvailable; | |
private final String TAG = getClass().getSimpleName(); | |
private static final int READ_CONTACTS = 1; | |
private long userPhoneContacts; | |
private boolean mIsBound; | |
@Override | |
public void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
ActivityRegisterPresenter presenter = new ActivityRegisterPresenter(this); | |
ActivityRegisterViewModel viewModel = new ActivityRegisterViewModel(); | |
binding.setViewModel(viewModel); | |
binding.setPresenter(presenter); | |
component = DaggerApplicationComponent.builder() | |
.githubApplicationComponent(Alachiq.get(this).getComponent()) | |
.build(); | |
component.inject(this); | |
getRepositories(); | |
} | |
private void getRepositories() { | |
jobManager.addJobInBackground(getLatestRepositories); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.content.Context; | |
import javax.inject.Named; | |
import dagger.Module; | |
import dagger.Provides; | |
import ir.pishguy.cafealachiqpro.Dagger.Scope.GithubApplicationScope; | |
@Module | |
public class ActivityModule { | |
private final Activity context; | |
public ActivityModule(Activity context) { | |
this.context = context; | |
} | |
@Provides | |
@GithubApplicationScope | |
@Named("activity_context") | |
public Context context() { | |
return context; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Alachiq extends MultiDexApplication { | |
... | |
private static Context context; | |
public static SlidrConfig config; | |
private GithubService githubService; | |
private Picasso picasso; | |
private static GithubApplicationComponent component; | |
private JobManager jobManager; | |
private static Alachiq instance; | |
@Override | |
protected void attachBaseContext(Context base) { | |
super.attachBaseContext(base); | |
} | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
... | |
component = DaggerGithubApplicationComponent.builder() | |
.contextModule(new ContextModule(this)) | |
.jobManagerModule(new JobManagerModule()) | |
.build(); | |
githubService = component.getGithubService(); | |
picasso = component.getPicasso(); | |
jobManager = component.getJobManager(); | |
} | |
public static GithubApplicationComponent getComponent() { | |
return component; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.inject.Qualifier; | |
@Qualifier | |
public @interface ApplicationContext { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import dagger.Module; | |
import dagger.Provides; | |
import ir.pishguy.cafealachiqpro.Dagger.ApplicationContext; | |
import ir.pishguy.cafealachiqpro.Dagger.Scope.GithubApplicationScope; | |
@Module | |
public class ContextModule { | |
private final Context context; | |
public ContextModule(Context context) { | |
this.context = context.getApplicationContext(); | |
} | |
@Provides | |
@GithubApplicationScope | |
@ApplicationContext | |
public Context context() { | |
return context; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.util.Log; | |
import com.birbit.android.jobqueue.Job; | |
import com.birbit.android.jobqueue.Params; | |
import com.birbit.android.jobqueue.RetryConstraint; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.inject.Inject; | |
import ir.pishguy.cafealachiqpro.Dagger.Components.ApplicationComponent; | |
import ir.pishguy.cafealachiqpro.Dagger.Components.JobManagerInjectable; | |
import ir.pishguy.cafealachiqpro.NetWork.GithubRepo; | |
import ir.pishguy.cafealachiqpro.NetWork.GithubService; | |
import retrofit2.Call; | |
import retrofit2.Callback; | |
import retrofit2.Response; | |
/** | |
* Created by mahdi pishguy on 4/9/2017 AD. | |
*/ | |
public class GetLatestRepositories extends Job implements JobManagerInjectable { | |
@Inject | |
transient GithubService githubService; | |
private Call<List<GithubRepo>> repositoryCall; | |
public GetLatestRepositories() { | |
super(new Params(Priority.MID).requireNetwork().persist()); | |
} | |
@Override | |
public void onAdded() { | |
} | |
@Override | |
public void onRun() throws Throwable { | |
if (githubService != null) { | |
repositoryCall = githubService.getAllRepositories(); | |
repositoryCall.enqueue(new Callback<List<GithubRepo>>() { | |
@Override | |
public void onResponse(Call<List<GithubRepo>> call, Response<List<GithubRepo>> response) { | |
List<GithubRepo> repoList = new ArrayList<>(); | |
repoList.addAll(response.body()); | |
Log.e("JOB ", "OK"); | |
} | |
@Override | |
public void onFailure(Call<List<GithubRepo>> call, Throwable t) { | |
Log.e("JOB ", "NO!!"); | |
} | |
}); | |
} else { | |
Log.e("JOB ", "githubService is null"); | |
} | |
// EventBus.getDefault().post(); | |
} | |
@Override | |
protected void onCancel(int cancelReason, @Nullable Throwable throwable) { | |
} | |
@Override | |
protected RetryConstraint shouldReRunOnThrowable(@NonNull Throwable throwable, int runCount, int maxRunCount) { | |
return null; | |
} | |
@Override | |
public void inject(ApplicationComponent component) { | |
component.inject(this); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dagger.Module; | |
import dagger.Provides; | |
import ir.pishguy.cafealachiqpro.Dagger.Scope.ActivitiesScope; | |
import ir.pishguy.cafealachiqpro.Service.GetLatestRepositories; | |
/** | |
* Created by mahdi on 4/9/2017 AD. | |
*/ | |
@Module | |
public class GetLatestRepositoriesModule { | |
private final GetLatestRepositories githubService; | |
public GetLatestRepositoriesModule(GetLatestRepositories mGithubService) { | |
githubService = mGithubService; | |
} | |
@Provides | |
@ActivitiesScope | |
public GetLatestRepositories getLatestRepositories() { | |
return githubService; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.birbit.android.jobqueue.JobManager; | |
import com.squareup.picasso.Picasso; | |
import dagger.Component; | |
import ir.pishguy.cafealachiqpro.Dagger.Modules.ActivityModule; | |
import ir.pishguy.cafealachiqpro.Dagger.Modules.GithubServiceModule; | |
import ir.pishguy.cafealachiqpro.Dagger.Modules.JobManagerModule; | |
import ir.pishguy.cafealachiqpro.Dagger.Modules.PicassoModule; | |
import ir.pishguy.cafealachiqpro.Dagger.SchemaModules.UserInformationModule; | |
import ir.pishguy.cafealachiqpro.Dagger.Scope.GithubApplicationScope; | |
import ir.pishguy.cafealachiqpro.NetWork.GithubService; | |
/** | |
* Created by mahdi on 4/9/2017 AD. | |
*/ | |
@GithubApplicationScope | |
@Component( | |
modules = { | |
UserInformationModule.class, | |
GithubServiceModule.class, | |
PicassoModule.class, | |
JobManagerModule.class, | |
ActivityModule.class | |
} | |
) | |
public interface GithubApplicationComponent { | |
Picasso getPicasso(); | |
GithubService getGithubService(); | |
JobManager getJobManager(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.inject.Scope; | |
/** | |
* Created by mahdi on 4/9/2017 AD. | |
*/ | |
@Scope | |
public @interface ActivitiesScope { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
import org.joda.time.DateTime; | |
import dagger.Module; | |
import dagger.Provides; | |
import ir.pishguy.cafealachiqpro.Dagger.Scope.GithubApplicationScope; | |
import ir.pishguy.cafealachiqpro.NetWork.DateTimeConverter; | |
import ir.pishguy.cafealachiqpro.NetWork.GithubService; | |
import okhttp3.OkHttpClient; | |
import retrofit2.Retrofit; | |
import retrofit2.converter.gson.GsonConverterFactory; | |
@Module(includes = NetworkModule.class) | |
public class GithubServiceModule { | |
@Provides | |
@GithubApplicationScope | |
public GithubService githubService(Retrofit gitHubRetrofit) { | |
return gitHubRetrofit.create(GithubService.class); | |
} | |
@Provides | |
@GithubApplicationScope | |
public Gson gson() { | |
GsonBuilder gsonBuilder = new GsonBuilder(); | |
gsonBuilder.registerTypeAdapter(DateTime.class, new DateTimeConverter()); | |
return gsonBuilder.create(); | |
} | |
@Provides | |
@GithubApplicationScope | |
public Retrofit retrofit(OkHttpClient okHttpClient, Gson gson) { | |
return new Retrofit.Builder() | |
.addConverterFactory(GsonConverterFactory.create(gson)) | |
.client(okHttpClient) | |
.baseUrl("https://api.github.com/") | |
.build(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface JobManagerInjectable { | |
void inject(ApplicationComponent component); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.os.Build; | |
import android.util.Log; | |
import com.birbit.android.jobqueue.Job; | |
import com.birbit.android.jobqueue.JobManager; | |
import com.birbit.android.jobqueue.config.Configuration; | |
import com.birbit.android.jobqueue.di.DependencyInjector; | |
import com.birbit.android.jobqueue.log.CustomLogger; | |
import com.birbit.android.jobqueue.scheduling.FrameworkJobSchedulerService; | |
import com.birbit.android.jobqueue.scheduling.GcmJobSchedulerService; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.GoogleApiAvailability; | |
import dagger.Module; | |
import dagger.Provides; | |
import ir.pishguy.cafealachiqpro.CoreAlachiq.Alachiq; | |
import ir.pishguy.cafealachiqpro.Dagger.ApplicationContext; | |
import ir.pishguy.cafealachiqpro.Dagger.Components.ApplicationComponent; | |
import ir.pishguy.cafealachiqpro.Dagger.Components.DaggerApplicationComponent; | |
import ir.pishguy.cafealachiqpro.Dagger.Components.JobManagerInjectable; | |
import ir.pishguy.cafealachiqpro.Dagger.Scope.GithubApplicationScope; | |
import ir.pishguy.cafealachiqpro.Service.GcmJobService; | |
import ir.pishguy.cafealachiqpro.Service.MyJobService; | |
/** | |
* Created by mahdi on 4/9/2017 AD. | |
*/ | |
@Module(includes = ContextModule.class) | |
public class JobManagerModule { | |
private ApplicationComponent component; | |
@Provides | |
@GithubApplicationScope | |
public JobManager jobManager(@ApplicationContext Context context) { | |
component = DaggerApplicationComponent.builder() | |
.githubApplicationComponent(Alachiq.getComponent()) | |
.build(); | |
Configuration.Builder builder = new Configuration.Builder(context) | |
.customLogger(new CustomLogger() { | |
private static final String TAG = "JOBS"; | |
@Override | |
public boolean isDebugEnabled() { | |
return true; | |
} | |
@Override | |
public void d(String text, Object... args) { | |
Log.e(TAG, String.format(text, args)); | |
} | |
@Override | |
public void e(Throwable t, String text, Object... args) { | |
Log.e(TAG, String.format(text, args), t); | |
} | |
@Override | |
public void e(String text, Object... args) { | |
Log.e(TAG, String.format(text, args)); | |
} | |
@Override | |
public void v(String text, Object... args) { | |
} | |
}) | |
.minConsumerCount(1)//always keep at least one consumer alive | |
.maxConsumerCount(3)//up to 3 consumers at a time | |
.loadFactor(3)//3 jobs per consumer | |
.consumerKeepAlive(30)//wait 1 minute | |
.injector(new DependencyInjector() { | |
@Override | |
public void inject(Job job) { | |
if (job instanceof JobManagerInjectable) { | |
((JobManagerInjectable) job).inject(component); | |
} | |
} | |
}); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
builder.scheduler(FrameworkJobSchedulerService.createSchedulerFor(context, MyJobService.class), true); | |
} else { | |
int enableGcm = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context); | |
if (enableGcm == ConnectionResult.SUCCESS) { | |
builder.scheduler(GcmJobSchedulerService.createSchedulerFor(context, GcmJobService.class), true); | |
} | |
} | |
return new JobManager(builder.build()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by mahdi on 4/9/2017 AD. | |
*/ | |
import android.support.annotation.NonNull; | |
import com.birbit.android.jobqueue.JobManager; | |
import com.birbit.android.jobqueue.scheduling.FrameworkJobSchedulerService; | |
import javax.inject.Inject; | |
public class MyJobService extends FrameworkJobSchedulerService { | |
@Inject | |
JobManager jobManager; | |
@NonNull | |
@Override | |
protected JobManager getJobManager() { | |
return jobManager; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dagger.Module; | |
import dagger.Provides; | |
import ir.pishguy.cafealachiqpro.Dagger.ApplicationContext; | |
import ir.pishguy.cafealachiqpro.Dagger.Scope.GithubApplicationScope; | |
import okhttp3.Cache; | |
import okhttp3.OkHttpClient; | |
import okhttp3.logging.HttpLoggingInterceptor; | |
import timber.log.Timber; | |
@Module(includes = ContextModule.class) | |
public class NetworkModule { | |
@Provides | |
@GithubApplicationScope | |
public HttpLoggingInterceptor loggingInterceptor() { | |
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() { | |
@Override | |
public void log(String message) { | |
Timber.i(message); | |
} | |
}); | |
interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC); | |
return interceptor; | |
} | |
@Provides | |
@GithubApplicationScope | |
public Cache cache(File cacheFile) { | |
return new Cache(cacheFile, 10 * 1000 * 1000); //10MB Cahe | |
} | |
@Provides | |
@GithubApplicationScope | |
public File cacheFile(@ApplicationContext Context context) { | |
return new File(context.getCacheDir(), "okhttp_cache"); | |
} | |
@Provides | |
@GithubApplicationScope | |
public OkHttpClient okHttpClient(HttpLoggingInterceptor loggingInterceptor, Cache cache) { | |
return new OkHttpClient.Builder() | |
.addInterceptor(loggingInterceptor) | |
.cache(cache) | |
.build(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import com.jakewharton.picasso.OkHttp3Downloader; | |
import com.squareup.picasso.Picasso; | |
import dagger.Module; | |
import dagger.Provides; | |
import ir.pishguy.cafealachiqpro.Dagger.ApplicationContext; | |
import ir.pishguy.cafealachiqpro.Dagger.Scope.GithubApplicationScope; | |
import okhttp3.OkHttpClient; | |
@Module(includes = {ContextModule.class, NetworkModule.class}) | |
public class PicassoModule { | |
@Provides | |
@GithubApplicationScope | |
public Picasso picasso(@ApplicationContext Context context, OkHttp3Downloader okHttp3Downloader) { | |
return new Picasso.Builder(context) | |
.downloader(okHttp3Downloader) | |
.build(); | |
} | |
@Provides | |
@GithubApplicationScope | |
public OkHttp3Downloader okHttp3Downloader(OkHttpClient okHttpClient) { | |
return new OkHttp3Downloader(okHttpClient); | |
} | |
} |
@LadwaAditya yes, that work fine without any problem. i'm using that on my projects
Does this actually inject the MyJobService.class correctly? You're doing field injection but no where are you using the DaggerComponent to inject that object. This might work, but I'm wondering what are the side effects to having a null jobManager.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The injector?