Skip to content

Commit

Permalink
Use Dagger's AndroidInjectionModule.
Browse files Browse the repository at this point in the history
  • Loading branch information
NightlyNexus committed Aug 1, 2017
1 parent 72c1b15 commit bd06fe4
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 57 deletions.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-rc-3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-rc-2-all.zip
8 changes: 5 additions & 3 deletions telecine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def versionBuild = 0 // bump for dogfood builds, public betas, etc.

buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-alpha3'
classpath 'com.android.tools.build:gradle:3.0.0-alpha9'
}
}

Expand Down Expand Up @@ -83,8 +83,10 @@ dependencies {
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
implementation 'com.jakewharton.timber:timber:4.3.1'
implementation 'com.bugsnag:bugsnag-android:3.7.0'
implementation 'com.google.dagger:dagger:2.8'
annotationProcessor 'com.google.dagger:dagger-compiler:2.8'
implementation 'com.google.dagger:dagger:2.11'
annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
implementation 'com.google.dagger:dagger-android:2.11'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
testImplementation 'junit:junit:4.12'
testImplementation 'com.google.truth:truth:0.30'
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import butterknife.OnClick;
import butterknife.OnItemSelected;
import com.google.android.gms.analytics.HitBuilders;
import dagger.android.AndroidInjection;
import javax.inject.Inject;
import timber.log.Timber;

Expand Down Expand Up @@ -53,14 +54,13 @@ public final class TelecineActivity extends AppCompatActivity {
private DemoModeHelper.ShowDemoModeSetting showDemoModeSetting;

@Override protected void onCreate(Bundle savedInstanceState) {
AndroidInjection.inject(this);
super.onCreate(savedInstanceState);

if ("true".equals(getIntent().getStringExtra("crash"))) {
throw new RuntimeException("Crash! Bang! Pow! This is only a test...");
}

((TelecineApplication) getApplication()).injector().inject(this);

setContentView(R.layout.activity_main);
ButterKnife.bind(this);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package com.jakewharton.telecine;

import android.app.Activity;
import android.app.Application;
import android.app.Service;
import com.bugsnag.android.BeforeNotify;
import com.bugsnag.android.Bugsnag;
import com.bugsnag.android.Error;
import dagger.android.AndroidInjector;
import dagger.android.DispatchingAndroidInjector;
import dagger.android.HasActivityInjector;
import dagger.android.HasServiceInjector;
import javax.inject.Inject;
import timber.log.Timber;

public final class TelecineApplication extends Application {

private TelecineComponent telecineComponent;
public final class TelecineApplication extends Application
implements HasActivityInjector, HasServiceInjector {
@Inject DispatchingAndroidInjector<Activity> dispatchingActivityInjector;
@Inject DispatchingAndroidInjector<Service> dispatchingServiceInjector;

@Override public void onCreate() {
DaggerTelecineComponent.builder().application(this).build().inject(this);
super.onCreate();

if (BuildConfig.DEBUG) {
Expand All @@ -30,13 +39,13 @@ public final class TelecineApplication extends Application {

Timber.plant(tree);
}
}

telecineComponent = DaggerTelecineComponent.builder()
.telecineModule(new TelecineModule(this))
.build();
@Override public AndroidInjector<Activity> activityInjector() {
return dispatchingActivityInjector;
}

final TelecineComponent injector() {
return telecineComponent;
@Override public AndroidInjector<Service> serviceInjector() {
return dispatchingServiceInjector;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.jakewharton.telecine;

import android.app.Application;
import dagger.BindsInstance;
import dagger.Component;
import dagger.android.AndroidInjectionModule;
import javax.inject.Singleton;

@Singleton
@Component(modules = TelecineModule.class)
@Singleton @Component(modules = { AndroidInjectionModule.class, TelecineModule.class })
interface TelecineComponent {
void inject(TelecineActivity activity);
void inject(TelecineShortcutConfigureActivity activity);
void inject(TelecineShortcutLaunchActivity activity);
void inject(TelecineApplication app);

void inject(TelecineService service);
void inject(TelecineTileService service);
@Component.Builder interface Builder {
@BindsInstance Builder application(Application application);

TelecineComponent build();
}
}
65 changes: 36 additions & 29 deletions telecine/src/main/java/com/jakewharton/telecine/TelecineModule.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package com.jakewharton.telecine;

import android.app.Application;
import android.content.ContentResolver;
import android.content.SharedPreferences;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
import dagger.Module;
import dagger.Provides;
import timber.log.Timber;

import javax.inject.Singleton;
import dagger.android.ContributesAndroidInjector;
import java.util.Map;
import javax.inject.Singleton;
import timber.log.Timber;

import static android.content.Context.MODE_PRIVATE;

@Module
final class TelecineModule {
@Module abstract class TelecineModule {
private static final String PREFERENCES_NAME = "telecine";
private static final boolean DEFAULT_SHOW_COUNTDOWN = true;
private static final boolean DEFAULT_HIDE_FROM_RECENTS = false;
Expand All @@ -23,13 +23,7 @@ final class TelecineModule {
private static final boolean DEFAULT_RECORDING_NOTIFICATION = false;
private static final int DEFAULT_VIDEO_SIZE_PERCENTAGE = 100;

private final TelecineApplication app;

TelecineModule(TelecineApplication app) {
this.app = app;
}

@Provides @Singleton Analytics provideAnalytics() {
@Provides @Singleton static Analytics provideAnalytics(Application app) {
if (BuildConfig.DEBUG) {
return new Analytics() {
@Override public void send(Map<String, String> params) {
Expand All @@ -44,20 +38,21 @@ final class TelecineModule {
return new Analytics.GoogleAnalytics(tracker);
}

@Provides @Singleton ContentResolver provideContentResolver() {
@Provides @Singleton static ContentResolver provideContentResolver(Application app) {
return app.getContentResolver();
}

@Provides @Singleton SharedPreferences provideSharedPreferences() {
@Provides @Singleton static SharedPreferences provideSharedPreferences(Application app) {
return app.getSharedPreferences(PREFERENCES_NAME, MODE_PRIVATE);
}

@Provides @Singleton @ShowCountdown static BooleanPreference provideShowCountdownPreference(
SharedPreferences prefs) {
@Provides @Singleton @ShowCountdown
static BooleanPreference provideShowCountdownPreference(SharedPreferences prefs) {
return new BooleanPreference(prefs, "show-countdown", DEFAULT_SHOW_COUNTDOWN);
}

@Provides @ShowCountdown static Boolean provideShowCountdown(@ShowCountdown BooleanPreference pref) {
@Provides @ShowCountdown
static Boolean provideShowCountdown(@ShowCountdown BooleanPreference pref) {
return pref.get();
}

Expand All @@ -66,41 +61,53 @@ static BooleanPreference provideRecordingNotificationPreference(SharedPreference
return new BooleanPreference(prefs, "recording-notification", DEFAULT_RECORDING_NOTIFICATION);
}

@Provides @RecordingNotification static Boolean provideRecordingNotification(
@RecordingNotification BooleanPreference pref) {
@Provides @RecordingNotification
static Boolean provideRecordingNotification(@RecordingNotification BooleanPreference pref) {
return pref.get();
}

@Provides @Singleton @HideFromRecents static BooleanPreference provideHideFromRecentsPreference(
SharedPreferences prefs) {
@Provides @Singleton @HideFromRecents
static BooleanPreference provideHideFromRecentsPreference(SharedPreferences prefs) {
return new BooleanPreference(prefs, "hide-from-recents", DEFAULT_HIDE_FROM_RECENTS);
}

@Provides @Singleton @ShowTouches static BooleanPreference provideShowTouchesPreference(
SharedPreferences prefs) {
@Provides @Singleton @ShowTouches
static BooleanPreference provideShowTouchesPreference(SharedPreferences prefs) {
return new BooleanPreference(prefs, "show-touches", DEFAULT_SHOW_TOUCHES);
}

@Provides @ShowTouches static Boolean provideShowTouches(@ShowTouches BooleanPreference pref) {
return pref.get();
}

@Provides @Singleton @UseDemoMode static BooleanPreference provideUseDemoModePreference(
SharedPreferences prefs) {
@Provides @Singleton @UseDemoMode
static BooleanPreference provideUseDemoModePreference(SharedPreferences prefs) {
return new BooleanPreference(prefs, "use-demo-mode", DEFAULT_USE_DEMO_MODE);
}

@Provides @UseDemoMode static Boolean provideUseDemoMode(@UseDemoMode BooleanPreference pref) {
return pref.get();
}

@Provides @Singleton @VideoSizePercentage static IntPreference provideVideoSizePercentagePreference(
SharedPreferences prefs) {
@Provides @Singleton @VideoSizePercentage
static IntPreference provideVideoSizePercentagePreference(SharedPreferences prefs) {
return new IntPreference(prefs, "video-size", DEFAULT_VIDEO_SIZE_PERCENTAGE);
}

@Provides @VideoSizePercentage static Integer provideVideoSizePercentage(
@VideoSizePercentage IntPreference pref) {
@Provides @VideoSizePercentage
static Integer provideVideoSizePercentage(@VideoSizePercentage IntPreference pref) {
return pref.get();
}

@ContributesAndroidInjector abstract TelecineActivity contributeTelecineActivity();

@ContributesAndroidInjector
abstract TelecineShortcutConfigureActivity contributeTelecineShortcutConfigureActivity();

@ContributesAndroidInjector
abstract TelecineShortcutLaunchActivity contributeTelecineShortcutLaunchActivity();

@ContributesAndroidInjector abstract TelecineService contributeTelecineService();

@ContributesAndroidInjector abstract TelecineTileService contributeTelecineTileService();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.nightlynexus.demomode.NotificationsBuilder;
import com.nightlynexus.demomode.SystemIconsBuilder;
import com.nightlynexus.demomode.WifiBuilder;
import dagger.android.AndroidInjection;
import javax.inject.Inject;
import javax.inject.Provider;
import timber.log.Timber;
Expand Down Expand Up @@ -122,6 +123,11 @@ static Intent newIntent(Context context, int resultCode, Intent data) {
}
};

@Override public void onCreate() {
AndroidInjection.inject(this);
super.onCreate();
}

@Override public int onStartCommand(@NonNull Intent intent, int flags, int startId) {
if (running) {
Timber.d("Already running! Ignoring...");
Expand All @@ -136,8 +142,6 @@ static Intent newIntent(Context context, int resultCode, Intent data) {
throw new IllegalStateException("Result code or data missing.");
}

((TelecineApplication) getApplication()).injector().inject(this);

recordingSession =
new RecordingSession(this, listener, resultCode, data, analytics, showCountdownProvider,
videoSizePercentageProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Intent;
import android.os.Bundle;
import com.google.android.gms.analytics.HitBuilders;
import dagger.android.AndroidInjection;
import javax.inject.Inject;

import static android.content.Intent.ShortcutIconResource;
Expand All @@ -12,9 +13,9 @@ public final class TelecineShortcutConfigureActivity extends Activity {
@Inject Analytics analytics;

@Override protected void onCreate(Bundle savedInstanceState) {
AndroidInjection.inject(this);
super.onCreate(savedInstanceState);

((TelecineApplication) getApplication()).injector().inject(this);
analytics.send(new HitBuilders.EventBuilder() //
.setCategory(Analytics.CATEGORY_SHORTCUT) //
.setAction(Analytics.ACTION_SHORTCUT_ADDED) //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.Intent;
import android.os.Bundle;
import com.google.android.gms.analytics.HitBuilders;
import dagger.android.AndroidInjection;
import javax.inject.Inject;

public final class TelecineShortcutLaunchActivity extends Activity {
Expand All @@ -19,8 +20,8 @@ static Intent createQuickTileIntent(Context context) {
@Inject Analytics analytics;

@Override protected void onCreate(Bundle savedInstanceState) {
AndroidInjection.inject(this);
super.onCreate(savedInstanceState);
((TelecineApplication) getApplication()).injector().inject(this);

String launchAction = getIntent().getStringExtra(KEY_ACTION);
if (launchAction == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package com.jakewharton.telecine;

import android.annotation.TargetApi;
import android.os.Build;
import android.service.quicksettings.Tile;
import android.service.quicksettings.TileService;
import com.google.android.gms.analytics.HitBuilders;
import dagger.android.AndroidInjection;
import javax.inject.Inject;
import timber.log.Timber;

@TargetApi(Build.VERSION_CODES.N) // Only created on N+
import static android.os.Build.VERSION_CODES.N;

@TargetApi(N) // Only created on N+
public final class TelecineTileService extends TileService {
@Inject Analytics analytics;

@Override public void onCreate() {
AndroidInjection.inject(this);
super.onCreate();
((TelecineApplication) getApplication()).injector().inject(this);
}

@Override public void onClick() {
Expand Down

0 comments on commit bd06fe4

Please sign in to comment.