diff --git a/.travis.yml b/.travis.yml index d6c3e3d9908..bbfcc54fd5a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ language: java -install: mvn install clean --fail-never --quiet -DskipTests=true -Dinvoker.skip=true -script: mvn verify + +install: mvn install clean -P!android --fail-never --quiet -DskipTests=true -Dinvoker.skip=true +script: mvn verify -P!android notifications: email: false diff --git a/examples/android-activity-graphs/AndroidManifest.xml b/examples/android-activity-graphs/AndroidManifest.xml new file mode 100644 index 00000000000..adac7a87907 --- /dev/null +++ b/examples/android-activity-graphs/AndroidManifest.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + diff --git a/examples/android-activity-graphs/README.md b/examples/android-activity-graphs/README.md new file mode 100644 index 00000000000..533d40a973e --- /dev/null +++ b/examples/android-activity-graphs/README.md @@ -0,0 +1,17 @@ +Example: Android Activity Graphs +================================ + +Building on top of the simple Android example, this example demonstrates how it is possible to +create child graphs for each activity which extend from the global graph. + +Some of the advantages of the activity scope: + + * Provides the ability to inject objects which require the activity to be constructed. + * Allows for the use of singletons on a per-activity basis. This is a great way to manage a + resource that is shared by a bunch of fragments in an activity. + * Keeps the global object graph clear of things that can be used only be activities. + +While this example only shows the presence of an activity scope, you should be able to see the +potential for other useful scopes that can be used. For example, having a dedicated object graph +for the current user sessions is a great way to manage data that is tied to the currently logged-in +user. diff --git a/examples/android-activity-graphs/pom.xml b/examples/android-activity-graphs/pom.xml new file mode 100644 index 00000000000..be3b39fed07 --- /dev/null +++ b/examples/android-activity-graphs/pom.xml @@ -0,0 +1,63 @@ + + + + 4.0.0 + + + com.squareup.dagger.example + dagger-example-parent + 1.0-SNAPSHOT + + + android-activity-graphs + Examples: Android - Activity Graphs + apk + + + + com.squareup.dagger + dagger + ${project.version} + + + com.squareup.dagger + dagger-compiler + ${project.version} + true + + + + com.google.android + android + provided + + + com.google.android + support-v4 + + + + + + + com.jayway.maven.plugins.android.generation2 + android-maven-plugin + true + + + + diff --git a/examples/android-activity-graphs/res/values/strings.xml b/examples/android-activity-graphs/res/values/strings.xml new file mode 100644 index 00000000000..4247e0349b6 --- /dev/null +++ b/examples/android-activity-graphs/res/values/strings.xml @@ -0,0 +1,4 @@ + + + Dagger Activity Graph + diff --git a/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/ActivityModule.java b/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/ActivityModule.java new file mode 100644 index 00000000000..465d66d5d07 --- /dev/null +++ b/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/ActivityModule.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2013 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.dagger.activitygraphs; + +import android.content.Context; +import com.example.dagger.activitygraphs.ui.ActivityTitleController; +import com.example.dagger.activitygraphs.ui.HomeActivity; +import com.example.dagger.activitygraphs.ui.HomeFragment; +import dagger.Module; +import dagger.Provides; +import javax.inject.Singleton; + +/** + * This module represents objects which exist only for the scope of a single activity. We can + * safely create singletons using the activity instance because ths entire object graph will only + * ever exist inside of that activity. + */ +@Module( + injects = { + HomeActivity.class, + HomeFragment.class + }, + complete = false, + library = true +) +public class ActivityModule { + private final DemoBaseActivity activity; + + public ActivityModule(DemoBaseActivity activity) { + this.activity = activity; + } + + /** + * Allow the activity context to be injected but require that it be annotated with + * {@link ForActivity @ForActivity} to explicitly differentiate it from application context. + */ + @Provides @Singleton @ForActivity Context provideActivityContext() { + return activity; + } + + @Provides @Singleton ActivityTitleController provideTitleController() { + return new ActivityTitleController(activity); + } +} diff --git a/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/AndroidModule.java b/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/AndroidModule.java new file mode 100644 index 00000000000..78a02a4f19a --- /dev/null +++ b/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/AndroidModule.java @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2013 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.dagger.activitygraphs; + +import android.content.Context; +import android.location.LocationManager; +import dagger.Module; +import dagger.Provides; +import javax.inject.Singleton; + +import static android.content.Context.LOCATION_SERVICE; + +/** + * A module for Android-specific dependencies which require a {@link Context} or + * {@link android.app.Application} to create. + */ +@Module(library = true) +public class AndroidModule { + private final DemoApplication application; + + public AndroidModule(DemoApplication application) { + this.application = application; + } + + /** + * Allow the application context to be injected but require that it be annotated with + * {@link ForApplication @Annotation} to explicitly differentiate it from an activity context. + */ + @Provides @Singleton @ForApplication Context provideApplicationContext() { + return application; + } + + @Provides @Singleton LocationManager provideLocationManager() { + return (LocationManager) application.getSystemService(LOCATION_SERVICE); + } +} diff --git a/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/DemoApplication.java b/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/DemoApplication.java new file mode 100644 index 00000000000..56e1ab44058 --- /dev/null +++ b/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/DemoApplication.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2013 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.dagger.activitygraphs; + +import android.app.Application; +import dagger.ObjectGraph; +import java.util.Arrays; +import java.util.List; + +public class DemoApplication extends Application { + private ObjectGraph applicationGraph; + + @Override public void onCreate() { + super.onCreate(); + + applicationGraph = ObjectGraph.create(getModules().toArray()); + } + + /** + * A list of modules to use for the application graph. Subclasses can override this method to + * provide additional modules provided they call {@code super.getModules()}. + */ + protected List getModules() { + return Arrays.asList(new AndroidModule(this)); + } + + ObjectGraph getApplicationGraph() { + return applicationGraph; + } +} diff --git a/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/DemoBaseActivity.java b/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/DemoBaseActivity.java new file mode 100644 index 00000000000..44693dcc644 --- /dev/null +++ b/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/DemoBaseActivity.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2013 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.dagger.activitygraphs; + +import android.os.Bundle; +import android.support.v4.app.FragmentActivity; +import dagger.ObjectGraph; +import java.util.Arrays; +import java.util.List; + +/** Base activity which sets up a per-activity object graph and performs injection. */ +public abstract class DemoBaseActivity extends FragmentActivity { + private ObjectGraph activityGraph; + + @Override protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // Create the activity graph by .plus-ing our modules onto the application graph. + DemoApplication application = (DemoApplication) getApplication(); + activityGraph = application.getApplicationGraph().plus(getModules().toArray()); + + // Inject ourselves so subclasses will have dependencies fulfilled when this method returns. + activityGraph.inject(this); + } + + @Override protected void onDestroy() { + // Eagerly clear the reference to the activity graph to allow it to be garbage collected as + // soon as possible. + activityGraph = null; + + super.onDestroy(); + } + + /** + * A list of modules to use for the individual activity graph. Subclasses can override this + * method to provide additional modules provided they call and include the modules returned by + * calling {@code super.getModules()}. + */ + protected List getModules() { + return Arrays.asList(new ActivityModule(this)); + } + + /** Inject the supplied {@code object} using the activity-specific graph. */ + public void inject(Object object) { + activityGraph.inject(object); + } +} diff --git a/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/DemoBaseFragment.java b/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/DemoBaseFragment.java new file mode 100644 index 00000000000..ef7960ced65 --- /dev/null +++ b/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/DemoBaseFragment.java @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2013 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.dagger.activitygraphs; + +import android.app.Activity; +import android.support.v4.app.Fragment; + +/** Base fragment which performs injection using the activity object graph of its parent. */ +public class DemoBaseFragment extends Fragment { + @Override public void onAttach(Activity activity) { + ((DemoBaseActivity) activity).inject(this); + } +} diff --git a/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/ForActivity.java b/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/ForActivity.java new file mode 100644 index 00000000000..abd423b0397 --- /dev/null +++ b/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/ForActivity.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2013 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.dagger.activitygraphs; + +import java.lang.annotation.Retention; +import javax.inject.Qualifier; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Qualifier @Retention(RUNTIME) +public @interface ForActivity { +} diff --git a/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/ForApplication.java b/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/ForApplication.java new file mode 100644 index 00000000000..585d46b1c77 --- /dev/null +++ b/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/ForApplication.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2013 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.dagger.activitygraphs; + +import java.lang.annotation.Retention; +import javax.inject.Qualifier; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Qualifier @Retention(RUNTIME) +public @interface ForApplication { +} diff --git a/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/ui/ActivityTitleController.java b/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/ui/ActivityTitleController.java new file mode 100644 index 00000000000..f42dbee70d3 --- /dev/null +++ b/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/ui/ActivityTitleController.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2013 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.dagger.activitygraphs.ui; + +import android.app.Activity; + +/** + * A simple abstraction which provides the ability to set the title on an activity. + *

+ * Fragments should not directly modify any part of an activity outside of the view or dialog that + * it creates. This class provides a way for fragments to inject a controller that will allow for + * control of the activity title. While not exceedingly useful in practice, this concept could be + * expanded to things like facilitating control over the action bar, dialogs, notifications, etc. + */ +public class ActivityTitleController { + private final Activity activity; + + public ActivityTitleController(Activity activity) { + this.activity = activity; + } + + public void setTitle(CharSequence title) { + activity.setTitle(title); + } +} diff --git a/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/ui/HomeActivity.java b/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/ui/HomeActivity.java new file mode 100644 index 00000000000..36125cf66ae --- /dev/null +++ b/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/ui/HomeActivity.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2013 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.dagger.activitygraphs.ui; + +import android.location.LocationManager; +import android.os.Bundle; +import com.example.dagger.activitygraphs.DemoBaseActivity; +import javax.inject.Inject; + +public class HomeActivity extends DemoBaseActivity { + @Inject LocationManager locationManager; + + @Override protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + // After the super.onCreate call returns we are guaranteed our injections are available. + + if (savedInstanceState == null) { + getSupportFragmentManager().beginTransaction() + .add(android.R.id.content, HomeFragment.newInstance()) + .commit(); + } + + // TODO do something with the injected dependencies here! + } +} diff --git a/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/ui/HomeFragment.java b/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/ui/HomeFragment.java new file mode 100644 index 00000000000..56bbd583ce1 --- /dev/null +++ b/examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/ui/HomeFragment.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2013 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.dagger.activitygraphs.ui; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; +import com.example.dagger.activitygraphs.DemoBaseFragment; +import javax.inject.Inject; + +import static android.view.Gravity.CENTER; + +public class HomeFragment extends DemoBaseFragment { + public static HomeFragment newInstance() { + return new HomeFragment(); + } + + @Inject ActivityTitleController titleController; + + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + TextView tv = new TextView(getActivity()); + tv.setGravity(CENTER); + tv.setText("Hello, World"); + return tv; + } + + @Override public void onResume() { + super.onResume(); + + // Fragments should not modify things outside of their own view. Use an external controller to + // ask the activity to change its title. + titleController.setTitle("Home Fragment"); + } +} diff --git a/examples/android-simple/AndroidManifest.xml b/examples/android-simple/AndroidManifest.xml new file mode 100644 index 00000000000..8f0fce3a803 --- /dev/null +++ b/examples/android-simple/AndroidManifest.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + diff --git a/examples/android-simple/README.md b/examples/android-simple/README.md new file mode 100644 index 00000000000..2a9c1fe2a96 --- /dev/null +++ b/examples/android-simple/README.md @@ -0,0 +1,13 @@ +Example: Android Simple +======================= + +This example demonstrates how to structure an Android application with Dagger. + +A custom `Application` class is used to manage a global object graph of objects. Modules are +assembled with a `getModules` method on the application that can be overridden to add additional +modules in development versions of your applications or in tests. + +Injection of activities is done automatically in a base activity. + +_Note: The app does not actually do anything when it is run. It is only to show how you can + structure Dagger within an Android app_ diff --git a/examples/android-simple/pom.xml b/examples/android-simple/pom.xml new file mode 100644 index 00000000000..3650700caf6 --- /dev/null +++ b/examples/android-simple/pom.xml @@ -0,0 +1,59 @@ + + + + 4.0.0 + + + com.squareup.dagger.example + dagger-example-parent + 1.0-SNAPSHOT + + + android-simple + Examples: Android - Simple + apk + + + + com.squareup.dagger + dagger + ${project.version} + + + com.squareup.dagger + dagger-compiler + ${project.version} + true + + + + com.google.android + android + provided + + + + + + + com.jayway.maven.plugins.android.generation2 + android-maven-plugin + true + + + + diff --git a/examples/android-simple/res/values/strings.xml b/examples/android-simple/res/values/strings.xml new file mode 100644 index 00000000000..7c398f540d7 --- /dev/null +++ b/examples/android-simple/res/values/strings.xml @@ -0,0 +1,4 @@ + + + Dagger Simple + diff --git a/examples/android-simple/src/main/java/com/example/dagger/simple/AndroidModule.java b/examples/android-simple/src/main/java/com/example/dagger/simple/AndroidModule.java new file mode 100644 index 00000000000..e6b8aa57cd3 --- /dev/null +++ b/examples/android-simple/src/main/java/com/example/dagger/simple/AndroidModule.java @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2013 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.dagger.simple; + +import android.content.Context; +import android.location.LocationManager; +import dagger.Module; +import dagger.Provides; +import javax.inject.Singleton; + +import static android.content.Context.LOCATION_SERVICE; + +/** + * A module for Android-specific dependencies which require a {@link Context} or + * {@link android.app.Application} to create. + */ +@Module(library = true) +public class AndroidModule { + private final DemoApplication application; + + public AndroidModule(DemoApplication application) { + this.application = application; + } + + /** + * Allow the application context to be injected but require that it be annotated with + * {@link ForApplication @Annotation} to explicitly differentiate it from an activity context. + */ + @Provides @Singleton @ForApplication Context provideApplicationContext() { + return application; + } + + @Provides @Singleton LocationManager provideLocationManager() { + return (LocationManager) application.getSystemService(LOCATION_SERVICE); + } +} diff --git a/examples/android-simple/src/main/java/com/example/dagger/simple/DemoApplication.java b/examples/android-simple/src/main/java/com/example/dagger/simple/DemoApplication.java new file mode 100644 index 00000000000..8bc75bf7917 --- /dev/null +++ b/examples/android-simple/src/main/java/com/example/dagger/simple/DemoApplication.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2013 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.dagger.simple; + +import android.app.Application; +import dagger.ObjectGraph; +import java.util.Arrays; +import java.util.List; + +public class DemoApplication extends Application { + private ObjectGraph graph; + + @Override public void onCreate() { + super.onCreate(); + + graph = ObjectGraph.create(getModules().toArray()); + } + + protected List getModules() { + return Arrays.asList( + new AndroidModule(this) + ); + } + + public void inject(Object object) { + graph.inject(object); + } +} diff --git a/examples/android-simple/src/main/java/com/example/dagger/simple/DemoBaseActivity.java b/examples/android-simple/src/main/java/com/example/dagger/simple/DemoBaseActivity.java new file mode 100644 index 00000000000..c037ac02bd7 --- /dev/null +++ b/examples/android-simple/src/main/java/com/example/dagger/simple/DemoBaseActivity.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2013 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.dagger.simple; + +import android.app.Activity; +import android.os.Bundle; + +public abstract class DemoBaseActivity extends Activity { + @Override protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // Perform injection so that when this call returns all dependencies will be available for use. + ((DemoApplication) getApplication()).inject(this); + } +} diff --git a/examples/android-simple/src/main/java/com/example/dagger/simple/DemoModule.java b/examples/android-simple/src/main/java/com/example/dagger/simple/DemoModule.java new file mode 100644 index 00000000000..33d7278ac10 --- /dev/null +++ b/examples/android-simple/src/main/java/com/example/dagger/simple/DemoModule.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2013 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.dagger.simple; + +import com.example.dagger.simple.ui.HomeActivity; +import dagger.Module; + +@Module( + injects = HomeActivity.class, + complete = false +) +public class DemoModule { + // TODO put your application-specific providers here! +} diff --git a/examples/android-simple/src/main/java/com/example/dagger/simple/ForApplication.java b/examples/android-simple/src/main/java/com/example/dagger/simple/ForApplication.java new file mode 100644 index 00000000000..84d224740d1 --- /dev/null +++ b/examples/android-simple/src/main/java/com/example/dagger/simple/ForApplication.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2013 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.dagger.simple; + +import java.lang.annotation.Retention; +import javax.inject.Qualifier; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Qualifier @Retention(RUNTIME) +public @interface ForApplication { +} diff --git a/examples/android-simple/src/main/java/com/example/dagger/simple/ui/HomeActivity.java b/examples/android-simple/src/main/java/com/example/dagger/simple/ui/HomeActivity.java new file mode 100644 index 00000000000..5c22127256e --- /dev/null +++ b/examples/android-simple/src/main/java/com/example/dagger/simple/ui/HomeActivity.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2013 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.dagger.simple.ui; + +import android.location.LocationManager; +import android.os.Bundle; +import com.example.dagger.simple.DemoBaseActivity; +import javax.inject.Inject; + +public class HomeActivity extends DemoBaseActivity { + @Inject LocationManager locationManager; + + @Override protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + // After the super.onCreate call returns we are guaranteed our injections are available. + + // TODO do something with the injected dependencies here! + } +} diff --git a/examples/pom.xml b/examples/pom.xml index fe0346fef5b..875aab55ac9 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -27,9 +27,58 @@ com.squareup.dagger.example dagger-example-parent pom - Dagger Example (Parent) + Examples simple + + + + + android + + true + + + simple + android-simple + android-activity-graphs + + + + + + + + + com.google.android + android + 4.1.1.4 + + + com.google.android + support-v4 + r7 + + + + + + + + + + com.jayway.maven.plugins.android.generation2 + android-maven-plugin + 3.5.3 + + + 16 + + + + + + diff --git a/examples/simple/pom.xml b/examples/simple/pom.xml index 0a25a1a48f5..5e11afa1fac 100644 --- a/examples/simple/pom.xml +++ b/examples/simple/pom.xml @@ -23,8 +23,8 @@ 1.0-SNAPSHOT - simple-dagger-example - Dagger Example - Simple + simple + Examples: Simple @@ -39,16 +39,4 @@ true - - - - maven-compiler-plugin - - -Xlint:all - true - true - - - - diff --git a/pom.xml b/pom.xml index 67689a0bf19..49bd024eecb 100644 --- a/pom.xml +++ b/pom.xml @@ -100,11 +100,26 @@ + + + + org.apache.maven.plugins + maven-invoker-plugin + 1.7 + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + + + org.apache.maven.plugins maven-compiler-plugin - 2.5 ${java.version} ${java.version} @@ -142,15 +157,5 @@ - - - - - org.apache.maven.plugins - maven-invoker-plugin - 1.7 - - -