Permalink
Browse files

Migrating to use dagger code

  • Loading branch information...
1 parent dce9635 commit 1b360b168242f81ba26be70eeb2cba62bbe8b64f @donnfelker donnfelker committed Mar 26, 2013
Showing with 1,294 additions and 131 deletions.
  1. +1 −1 android-bootstrap/.gitignore
  2. +5 −3 android-bootstrap/README.md
  3. +20 −6 android-bootstrap/app/pom.xml
  4. +92 −0 android-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/AndroidModule.java
  5. +26 −13 android-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/BootstrapApplication.java
  6. +49 −9 android-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/BootstrapModule.java
  7. +6 −5 android-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/BootstrapServiceProvider.java
  8. +16 −0 android-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/RootModule.java
  9. +3 −4 ...id-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/authenticator/ApiKeyProvider.java
  10. +13 −17 .../src/main/java/com/donnfelker/android/bootstrap/authenticator/BootstrapAuthenticatorActivity.java
  11. +15 −11 ...oid-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/authenticator/LogoutService.java
  12. +89 −0 ...ain/java/com/donnfelker/android/bootstrap/authenticator/SherlockAccountAuthenticatorActivity.java
  13. +4 −4 android-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/core/AvatarLoader.java
  14. +9 −6 android-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/core/TimerService.java
  15. +9 −8 android-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/core/UserAgentProvider.java
  16. +16 −2 android-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/ui/BootstrapActivity.java
  17. +28 −0 ...id-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/ui/BootstrapFragmentActivity.java
  18. +6 −5 android-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/ui/BootstrapTimerActivity.java
  19. +7 −5 android-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/ui/CarouselActivity.java
  20. +0 −3 android-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/ui/CheckInsListAdapter.java
  21. +30 −2 android-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/ui/CheckInsListFragment.java
  22. +8 −12 android-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/ui/ItemListFragment.java
  23. +6 −3 android-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/ui/NewsActivity.java
  24. +25 −2 android-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/ui/NewsListFragment.java
  25. +2 −1 android-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/ui/ThrowableLoader.java
  26. +10 −5 android-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/ui/UserActivity.java
  27. +24 −4 android-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/ui/UserListFragment.java
  28. +284 −0 android-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/util/Ln.java
  29. +296 −0 android-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/util/SafeAsyncTask.java
  30. +181 −0 android-bootstrap/app/src/main/java/com/donnfelker/android/bootstrap/util/Strings.java
  31. +14 −0 android-bootstrap/pom.xml
View
2 android-bootstrap/.gitignore 100644 → 100755
@@ -19,4 +19,4 @@ gen-external-apklibs
.idea
out
.DS_Store
-
+classes
@@ -51,10 +51,10 @@ The application
* [Apache Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.html)
-Copyright 2012 Donn Felker
+Copyright 2013 Donn Felker
-Copyright 2012 GitHub Inc.
+Copyright 2013 GitHub Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -107,7 +107,9 @@ and uses many great open-source libraries from the Android dev community:
for swiping between fragments and
[NineOldAndroids](https://github.com/JakeWharton/NineOldAndroids) for
view animations - all from [Jake Wharton](http://jakewharton.com/).
-* [RoboGuice](http://code.google.com/p/roboguice/) for dependency-injection.
+* [Dagger](https://github.com/square/dagger) for dependency-injection.
+* [ButterKnife](https://github.com/JakeWharton/butterknife) for view injection
+* [Otto](https://github.com/square/otto) as the event bus
* [Robotium](http://code.google.com/p/robotium/)
for driving our app during integration tests.
* [android-maven-plugin](https://github.com/jayway/maven-android-plugin)
@@ -17,6 +17,7 @@
<properties>
<abs.version>4.2.0</abs.version>
+ <dagger.version>1.0-SNAPSHOT</dagger.version>
</properties>
<dependencies>
<dependency>
@@ -31,14 +32,20 @@
<version>2.2.2</version>
</dependency>
<dependency>
- <groupId>com.github.rtyley</groupId>
- <artifactId>roboguice-sherlock</artifactId>
- <version>1.5</version>
+ <groupId>com.squareup.dagger</groupId>
+ <artifactId>dagger</artifactId>
+ <version>${dagger.version}</version>
</dependency>
<dependency>
- <groupId>org.roboguice</groupId>
- <artifactId>roboguice</artifactId>
- <version>2.0</version>
+ <groupId>com.squareup.dagger</groupId>
+ <artifactId>dagger-compiler</artifactId>
+ <version>${dagger.version}</version>
+ <optional>true</optional>
+ </dependency>
+ <dependency>
+ <groupId>com.jakewharton</groupId>
+ <artifactId>butterknife</artifactId>
+ <version>1.2.2</version>
</dependency>
<dependency>
<groupId>com.actionbarsherlock</groupId>
@@ -109,6 +116,13 @@
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
+ <dependencies>
+ <dependency>
+ <groupId>com.squareup.dagger</groupId>
+ <artifactId>dagger-compiler</artifactId>
+ <version>${dagger.version}</version>
+ </dependency>
+ </dependencies>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
@@ -0,0 +1,92 @@
+package com.donnfelker.android.bootstrap;
+
+import android.accounts.AccountManager;
+import android.app.NotificationManager;
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.preference.PreferenceManager;
+import android.telephony.TelephonyManager;
+import android.view.inputmethod.InputMethodManager;
+
+import javax.inject.Singleton;
+
+import dagger.Module;
+import dagger.Provides;
+
+/**
+ * Module for all Android related provisions
+ */
+@Module
+(
+ complete = false
+)
+public class AndroidModule {
+
+ @Provides
+ @Singleton
+ Context provideAppContext()
+ {
+ return BootstrapApplication.getInstance().getApplicationContext();
+ }
+
+ @Provides
+ SharedPreferences provideDefaultSharedPreferences(final Context context)
+ {
+ return PreferenceManager.getDefaultSharedPreferences(context);
+ }
+
+ @Provides
+ PackageInfo providePackageInfo(Context context)
+ {
+ try
+ {
+ return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
+ }
+ catch (PackageManager.NameNotFoundException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Provides
+ TelephonyManager provideTelephonyManager(Context context)
+ {
+ return getSystemService(context, Context.TELEPHONY_SERVICE);
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T> T getSystemService(Context context, String serviceConstant)
+ {
+ return (T) context.getSystemService(serviceConstant);
+ }
+
+ @Provides
+ InputMethodManager provideInputMethodManager(final Context context)
+ {
+ return (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
+ }
+
+ @Provides
+ ApplicationInfo provideApplicationInfo(final Context context) {
+ return context.getApplicationInfo();
+ }
+
+ @Provides
+ AccountManager provideAccountManager(final Context context) {
+ return AccountManager.get(context);
+ }
+
+ @Provides
+ ClassLoader provideClassLoader(final Context context) {
+ return context.getClassLoader();
+ }
+
+ @Provides
+ NotificationManager provideNotificationManager(final Context context) {
+ return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
+ }
+
+}
@@ -9,20 +9,22 @@
import android.content.Context;
import com.github.kevinsawicki.http.HttpRequest;
-import com.google.inject.Injector;
-import com.google.inject.Stage;
-import roboguice.RoboGuice;
+import dagger.ObjectGraph;
/**
* Android Bootstrap application
*/
public class BootstrapApplication extends Application {
+ private static BootstrapApplication instance;
+ ObjectGraph objectGraph;
+
/**
* Create main application
*/
public BootstrapApplication() {
+
// Disable http.keepAlive on Froyo and below
if (SDK_INT <= FROYO)
HttpRequest.keepAlive(false);
@@ -36,15 +38,26 @@ public BootstrapApplication() {
public BootstrapApplication(final Context context) {
this();
attachBaseContext(context);
+
}
@Override
public void onCreate() {
super.onCreate();
- setApplicationInjector(this);
+ instance = this;
+ // Perform Injection
+ objectGraph = ObjectGraph.create(getRootModule());
+ objectGraph.inject(this);
+ objectGraph.injectStatics();
+
}
+ private Object getRootModule() {
+ return new RootModule();
+ }
+
+
/**
* Create main application
*
@@ -55,14 +68,14 @@ public BootstrapApplication(final Instrumentation instrumentation) {
attachBaseContext(instrumentation.getTargetContext());
}
- /**
- * Sets the application injector. Using the {@link RoboGuice#newDefaultRoboModule} as well as a
- * custom binding module {@link BootstrapModule} to set up your application module
- * @param application
- * @return
- */
- public static Injector setApplicationInjector(Application application) {
- return RoboGuice.setBaseApplicationInjector(application, Stage.DEVELOPMENT, RoboGuice.newDefaultRoboModule
- (application), new BootstrapModule());
+ public void inject(Object object)
+ {
+ objectGraph.inject(object);
+ }
+
+
+
+ public static BootstrapApplication getInstance() {
+ return instance;
}
}
@@ -1,21 +1,61 @@
package com.donnfelker.android.bootstrap;
-import com.google.inject.AbstractModule;
-import com.google.inject.Singleton;
+import android.accounts.AccountManager;
+import android.content.Context;
+
+import com.donnfelker.android.bootstrap.authenticator.BootstrapAuthenticatorActivity;
+import com.donnfelker.android.bootstrap.authenticator.LogoutService;
+import com.donnfelker.android.bootstrap.core.CheckIn;
+import com.donnfelker.android.bootstrap.core.TimerService;
+import com.donnfelker.android.bootstrap.ui.BootstrapTimerActivity;
+import com.donnfelker.android.bootstrap.ui.CarouselActivity;
+import com.donnfelker.android.bootstrap.ui.CheckInsListFragment;
+import com.donnfelker.android.bootstrap.ui.ItemListFragment;
+import com.donnfelker.android.bootstrap.ui.NewsActivity;
+import com.donnfelker.android.bootstrap.ui.NewsListFragment;
+import com.donnfelker.android.bootstrap.ui.UserActivity;
+import com.donnfelker.android.bootstrap.ui.UserListFragment;
import com.squareup.otto.Bus;
+import javax.inject.Singleton;
+
+import dagger.Module;
+import dagger.Provides;
+
/**
- * Module for setting up custom bindings in RoboGuice.
+ * Dagger module for setting up provides statements.
+ * Register all of your entry points below.
*/
-public class BootstrapModule extends AbstractModule {
+@Module
+(
+ complete = false,
- @Override
- protected void configure() {
+ entryPoints = {
+ BootstrapApplication.class,
+ BootstrapAuthenticatorActivity.class,
+ CarouselActivity.class,
+ BootstrapTimerActivity.class,
+ CheckInsListFragment.class,
+ NewsActivity.class,
+ NewsListFragment.class,
+ UserActivity.class,
+ UserListFragment.class,
+ TimerService.class
+ }
- // We want Otto to be bound as a singleton as one instance only needs
- // to be present in this app
- bind(Bus.class).in(Singleton.class);
+)
+public class BootstrapModule {
+
+ @Singleton
+ @Provides
+ Bus provideOttoBus() {
+ return new Bus();
+ }
+ @Provides
+ @Singleton
+ LogoutService provideLogoutService(final Context context, final AccountManager accountManager) {
+ return new LogoutService(context, accountManager);
}
}
@@ -2,11 +2,12 @@
package com.donnfelker.android.bootstrap;
import android.accounts.AccountsException;
+import android.app.Activity;
import com.donnfelker.android.bootstrap.authenticator.ApiKeyProvider;
import com.donnfelker.android.bootstrap.core.BootstrapService;
import com.donnfelker.android.bootstrap.core.UserAgentProvider;
-import com.google.inject.Inject;
+import javax.inject.Inject;
import java.io.IOException;
@@ -15,8 +16,8 @@
*/
public class BootstrapServiceProvider {
- @Inject private ApiKeyProvider keyProvider;
- @Inject private UserAgentProvider userAgentProvider;
+ @Inject ApiKeyProvider keyProvider;
+ @Inject UserAgentProvider userAgentProvider;
/**
* Get service for configured key provider
@@ -27,7 +28,7 @@
* @throws IOException
* @throws AccountsException
*/
- public BootstrapService getService() throws IOException, AccountsException {
- return new BootstrapService(keyProvider.getAuthKey(), userAgentProvider);
+ public BootstrapService getService(Activity activity) throws IOException, AccountsException {
+ return new BootstrapService(keyProvider.getAuthKey(activity), userAgentProvider);
}
}
@@ -0,0 +1,16 @@
+package com.donnfelker.android.bootstrap;
+
+import dagger.Module;
+
+/**
+ * Add all the other modules to this one.
+ */
+@Module
+(
+ includes = {
+ AndroidModule.class,
+ BootstrapModule.class
+ }
+)
+public class RootModule {
+}
Oops, something went wrong.

0 comments on commit 1b360b1

Please sign in to comment.