Created
February 22, 2014 06:30
-
-
Save vinc3m1/9149599 to your computer and use it in GitHub Desktop.
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
package com.example; | |
import android.app.Activity; | |
import android.app.Application; | |
import android.content.Context; | |
import com.crashlytics.android.Crashlytics; | |
import dagger.ObjectGraph; | |
import java.util.concurrent.TimeUnit; | |
import com.example.util.CrashlyticsTree; | |
import timber.log.Timber; | |
public class MyApp extends Application { | |
private ObjectGraph objectGraph; | |
@Override public void onCreate() { | |
super.onCreate(); | |
if (BuildConfig.DEBUG) { | |
Timber.plant(new Timber.DebugTree()); | |
} else { | |
Crashlytics.start(this); | |
Timber.plant(new CrashlyticsTree()); | |
} | |
buildObjectGraphAndInject(); | |
} | |
public void buildObjectGraphAndInject() { | |
long start = System.nanoTime(); | |
objectGraph = ObjectGraph.create(Modules.list(this)); | |
long diff = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start); | |
Timber.i("Global object graph creation took %sms", diff); | |
} | |
public static void inject(Object o, Context context) { | |
get(context).injectInternal(o); | |
} | |
public static void inject(Activity activity) { | |
get(activity).injectInternal(activity); | |
} | |
public void injectInternal(Object o) { | |
objectGraph.inject(o); | |
} | |
public static MyApp get(Context context) { | |
return (MyApp) context.getApplicationContext(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment