Home | History | Annotate | Download | only in simple
      1 /*
      2  * Copyright (C) 2013 Square, Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.example.dagger.simple;
     17 
     18 import android.content.Context;
     19 import android.location.LocationManager;
     20 import dagger.Module;
     21 import dagger.Provides;
     22 import javax.inject.Singleton;
     23 
     24 import static android.content.Context.LOCATION_SERVICE;
     25 
     26 /**
     27  * A module for Android-specific dependencies which require a {@link Context} or
     28  * {@link android.app.Application} to create.
     29  */
     30 @Module
     31 public class AndroidModule {
     32   private final DemoApplication application;
     33 
     34   public AndroidModule(DemoApplication application) {
     35     this.application = application;
     36   }
     37 
     38   /**
     39    * Allow the application context to be injected but require that it be annotated with
     40    * {@link ForApplication @Annotation} to explicitly differentiate it from an activity context.
     41    */
     42   @Provides @Singleton @ForApplication Context provideApplicationContext() {
     43     return application;
     44   }
     45 
     46   @Provides @Singleton LocationManager provideLocationManager() {
     47     return (LocationManager) application.getSystemService(LOCATION_SERVICE);
     48   }
     49 }
     50