/* * Copyright 2013 Prateek Srivastava (@f2prateek) * * 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.f2prateek.xkcd; import android.content.Context; import android.content.SharedPreferences; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.res.Resources; import android.preference.PreferenceManager; import dagger.Module; import dagger.Provides; import javax.inject.Singleton; /** * A module for Android-specific dependencies which require a {@link android.content.Context} or * {@link android.app.Application} to create. */ @Module(library = true) public class AndroidModule { private final XKCDApplication application; public AndroidModule(XKCDApplication application) { this.application = application; } @Provides @Singleton Context provideApplicationContext() { return application; } @Provides @Singleton Resources provideResources(final Context context) { return context.getResources(); } @Provides @Singleton SharedPreferences provideSharedPreferences(final Context context) { return PreferenceManager.getDefaultSharedPreferences(context); } @Provides @Singleton PackageInfo providePackageInfo(final Context context) { try { return context.getPackageManager().getPackageInfo(context.getPackageName(), 0); } catch (PackageManager.NameNotFoundException e) { throw new RuntimeException(e); } } }