Skip to content

Commit f4fd41e

Browse files
committed
Adding some dagger module stuff.
1 parent 42940bd commit f4fd41e

File tree

2 files changed

+80
-7
lines changed

2 files changed

+80
-7
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.donnfelker.android.bootstrap;
2+
3+
import android.content.Context;
4+
import android.content.SharedPreferences;
5+
import android.content.pm.PackageInfo;
6+
import android.content.pm.PackageManager;
7+
import android.preference.PreferenceManager;
8+
import android.telephony.TelephonyManager;
9+
import android.view.inputmethod.InputMethodManager;
10+
11+
import dagger.Module;
12+
import dagger.Provides;
13+
14+
@Module(
15+
complete = false
16+
)
17+
public class AndroidModule {
18+
19+
@Provides
20+
SharedPreferences provideDefaultSharedPreferences(final Context context)
21+
{
22+
return PreferenceManager.getDefaultSharedPreferences(context);
23+
}
24+
25+
@Provides
26+
PackageInfo providePackageInfo(Context context)
27+
{
28+
try
29+
{
30+
return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
31+
}
32+
catch (PackageManager.NameNotFoundException e)
33+
{
34+
throw new RuntimeException(e);
35+
}
36+
}
37+
38+
@Provides
39+
TelephonyManager provideTelephonyManager(Context context)
40+
{
41+
return getSystemService(context, Context.TELEPHONY_SERVICE);
42+
}
43+
44+
@SuppressWarnings("unchecked")
45+
public <T> T getSystemService(Context context, String serviceConstant)
46+
{
47+
return (T) context.getSystemService(serviceConstant);
48+
}
49+
50+
@Provides
51+
InputMethodManager provideInputMethodManager(final Context context)
52+
{
53+
return (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
54+
}
55+
56+
57+
}

app/src/main/java/com/donnfelker/android/bootstrap/BootstrapModule.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,33 @@
44
import com.google.inject.Singleton;
55
import com.squareup.otto.Bus;
66

7+
import javax.inject.Singleton;
8+
9+
import dagger.Module;
10+
import dagger.Provides;
11+
712
/**
8-
* Module for setting up custom bindings in RoboGuice.
13+
* Dagger module for setting up provides statements.
914
*/
10-
public class BootstrapModule extends AbstractModule {
15+
@Module
16+
(
17+
complete = false,
18+
19+
entryPoints = {
20+
BootstrapApplication.class
21+
},
22+
23+
includes = {
24+
AndroidModule.class
25+
}
26+
)
27+
public class BootstrapModule {
1128

12-
@Override
13-
protected void configure() {
29+
@Singleton
30+
@Provides
31+
Bus provideOttoBus() {
1432

15-
// We want Otto to be bound as a singleton as one instance only needs
16-
// to be present in this app
17-
bind(Bus.class).in(Singleton.class);
33+
return new Bus();
1834

1935
}
2036

0 commit comments

Comments
 (0)