mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-31 14:53:59 +01:00 
			
		
		
		
	Refactor usage of dagger application with fixed dagger application
This commit is contained in:
		
							parent
							
								
									1224302ccb
								
							
						
					
					
						commit
						e1afa6081e
					
				
					 32 changed files with 556 additions and 311 deletions
				
			
		|  | @ -0,0 +1,10 @@ | |||
| package fr.free.nrw.commons.di; | ||||
| 
 | ||||
| import java.lang.annotation.Retention; | ||||
| import java.lang.annotation.RetentionPolicy; | ||||
| 
 | ||||
| import javax.inject.Qualifier; | ||||
| 
 | ||||
| @Qualifier | ||||
| @Retention(RetentionPolicy.RUNTIME) | ||||
| public @interface ApplicationContext {} | ||||
|  | @ -0,0 +1,99 @@ | |||
| package fr.free.nrw.commons.di; | ||||
| 
 | ||||
| import android.app.Activity; | ||||
| import android.app.Service; | ||||
| import android.content.BroadcastReceiver; | ||||
| import android.content.ContentProvider; | ||||
| import android.content.Context; | ||||
| import android.support.v4.app.Fragment; | ||||
| 
 | ||||
| import javax.inject.Inject; | ||||
| 
 | ||||
| import dagger.android.AndroidInjector; | ||||
| import dagger.android.DispatchingAndroidInjector; | ||||
| import dagger.android.HasActivityInjector; | ||||
| import dagger.android.HasBroadcastReceiverInjector; | ||||
| import dagger.android.HasContentProviderInjector; | ||||
| import dagger.android.HasFragmentInjector; | ||||
| import dagger.android.HasServiceInjector; | ||||
| import dagger.android.support.HasSupportFragmentInjector; | ||||
| 
 | ||||
| public class ApplicationlessInjection | ||||
|         implements | ||||
|         HasActivityInjector, | ||||
|         HasFragmentInjector, | ||||
|         HasSupportFragmentInjector, | ||||
|         HasServiceInjector, | ||||
|         HasBroadcastReceiverInjector, | ||||
|         HasContentProviderInjector { | ||||
| 
 | ||||
|     private static ApplicationlessInjection instance = null; | ||||
| 
 | ||||
|     @Inject | ||||
|     DispatchingAndroidInjector<Activity> activityInjector; | ||||
|     @Inject | ||||
|     DispatchingAndroidInjector<BroadcastReceiver> broadcastReceiverInjector; | ||||
|     @Inject | ||||
|     DispatchingAndroidInjector<android.app.Fragment> fragmentInjector; | ||||
|     @Inject | ||||
|     DispatchingAndroidInjector<Fragment> supportFragmentInjector; | ||||
|     @Inject | ||||
|     DispatchingAndroidInjector<Service> serviceInjector; | ||||
|     @Inject | ||||
|     DispatchingAndroidInjector<ContentProvider> contentProviderInjector; | ||||
| 
 | ||||
|     private CommonsApplicationComponent commonsApplicationComponent; | ||||
| 
 | ||||
|     public ApplicationlessInjection(Context applicationContext) { | ||||
|         commonsApplicationComponent = DaggerCommonsApplicationComponent.builder() | ||||
|                 .appModule(new CommonsApplicationModule(applicationContext)).build(); | ||||
|         commonsApplicationComponent.inject(this); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public DispatchingAndroidInjector<Activity> activityInjector() { | ||||
|         return activityInjector; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public DispatchingAndroidInjector<android.app.Fragment> fragmentInjector() { | ||||
|         return fragmentInjector; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public DispatchingAndroidInjector<Fragment> supportFragmentInjector() { | ||||
|         return supportFragmentInjector; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public DispatchingAndroidInjector<BroadcastReceiver> broadcastReceiverInjector() { | ||||
|         return broadcastReceiverInjector; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public DispatchingAndroidInjector<Service> serviceInjector() { | ||||
|         return serviceInjector; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public AndroidInjector<ContentProvider> contentProviderInjector() { | ||||
|         return contentProviderInjector; | ||||
|     } | ||||
| 
 | ||||
|     public CommonsApplicationComponent getCommonsApplicationComponent() { | ||||
|         return commonsApplicationComponent; | ||||
|     } | ||||
| 
 | ||||
|     public static ApplicationlessInjection getInstance(Context applicationContext) { | ||||
|         if (instance == null) { | ||||
|             synchronized (ApplicationlessInjection.class) { | ||||
|                 if (instance == null) { | ||||
|                     instance = new ApplicationlessInjection(applicationContext); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         return instance; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | @ -1,5 +1,7 @@ | |||
| package fr.free.nrw.commons.di; | ||||
| 
 | ||||
| import android.content.Context; | ||||
| 
 | ||||
| import javax.inject.Singleton; | ||||
| 
 | ||||
| import dagger.Component; | ||||
|  | @ -8,8 +10,13 @@ import dagger.android.AndroidInjector; | |||
| import dagger.android.support.AndroidSupportInjectionModule; | ||||
| import fr.free.nrw.commons.CommonsApplication; | ||||
| import fr.free.nrw.commons.MediaWikiImageView; | ||||
| import fr.free.nrw.commons.auth.LoginActivity; | ||||
| import fr.free.nrw.commons.category.CategoryContentProvider; | ||||
| import fr.free.nrw.commons.contributions.ContributionsContentProvider; | ||||
| import fr.free.nrw.commons.contributions.ContributionsSyncAdapter; | ||||
| import fr.free.nrw.commons.modifications.ModificationsContentProvider; | ||||
| import fr.free.nrw.commons.modifications.ModificationsSyncAdapter; | ||||
| import fr.free.nrw.commons.settings.SettingsFragment; | ||||
| 
 | ||||
| @Singleton | ||||
| @Component(modules = { | ||||
|  | @ -21,7 +28,7 @@ import fr.free.nrw.commons.modifications.ModificationsSyncAdapter; | |||
|         ServiceBuilderModule.class, | ||||
|         ContentProviderBuilderModule.class | ||||
| }) | ||||
| public interface CommonsApplicationComponent extends AndroidInjector<CommonsApplication> { | ||||
| public interface CommonsApplicationComponent extends AndroidInjector<ApplicationlessInjection> { | ||||
|     void inject(CommonsApplication application); | ||||
| 
 | ||||
|     void inject(ContributionsSyncAdapter syncAdapter); | ||||
|  | @ -30,6 +37,13 @@ public interface CommonsApplicationComponent extends AndroidInjector<CommonsAppl | |||
| 
 | ||||
|     void inject(MediaWikiImageView mediaWikiImageView); | ||||
| 
 | ||||
|     void inject(LoginActivity activity); | ||||
| 
 | ||||
|     void inject(SettingsFragment fragment); | ||||
| 
 | ||||
|     @Override | ||||
|     void inject(ApplicationlessInjection instance); | ||||
| 
 | ||||
|     @Component.Builder | ||||
|     @SuppressWarnings({"WeakerAccess", "unused"}) | ||||
|     interface Builder { | ||||
|  |  | |||
|  | @ -1,6 +1,7 @@ | |||
| package fr.free.nrw.commons.di; | ||||
| 
 | ||||
| import android.content.ContentProviderClient; | ||||
| import android.content.Context; | ||||
| import android.content.SharedPreferences; | ||||
| import android.preference.PreferenceManager; | ||||
| import android.support.v4.util.LruCache; | ||||
|  | @ -8,10 +9,10 @@ import android.support.v4.util.LruCache; | |||
| import javax.inject.Named; | ||||
| import javax.inject.Singleton; | ||||
| 
 | ||||
| import dagger.Binds; | ||||
| import dagger.Module; | ||||
| import dagger.Provides; | ||||
| import fr.free.nrw.commons.BuildConfig; | ||||
| import fr.free.nrw.commons.CommonsApplication; | ||||
| import fr.free.nrw.commons.auth.AccountUtil; | ||||
| import fr.free.nrw.commons.auth.SessionManager; | ||||
| import fr.free.nrw.commons.caching.CacheController; | ||||
|  | @ -31,62 +32,70 @@ import static fr.free.nrw.commons.modifications.ModificationsContentProvider.MOD | |||
| public class CommonsApplicationModule { | ||||
|     public static final String CATEGORY_AUTHORITY = "fr.free.nrw.commons.categories.contentprovider"; | ||||
| 
 | ||||
|     private CommonsApplication application; | ||||
|     private Context applicationContext; | ||||
| 
 | ||||
|     public CommonsApplicationModule(CommonsApplication application) { | ||||
|         this.application = application; | ||||
|     public CommonsApplicationModule(Context applicationContext) { | ||||
|         this.applicationContext = applicationContext; | ||||
|     } | ||||
| 
 | ||||
|     @Provides | ||||
|     public AccountUtil providesAccountUtil() { | ||||
|         return new AccountUtil(application); | ||||
|     public Context providesApplicationContext() { | ||||
|         return this.applicationContext; | ||||
|     } | ||||
| 
 | ||||
|     @Provides | ||||
|     public AccountUtil providesAccountUtil(Context context) { | ||||
|         return new AccountUtil(context); | ||||
|     } | ||||
| 
 | ||||
|     @Provides | ||||
|     @Named("category") | ||||
|     public ContentProviderClient provideCategoryContentProviderClient() { | ||||
|         return application.getContentResolver().acquireContentProviderClient(CATEGORY_AUTHORITY); | ||||
|     public ContentProviderClient provideCategoryContentProviderClient(Context context) { | ||||
|         return context.getContentResolver().acquireContentProviderClient(CATEGORY_AUTHORITY); | ||||
|     } | ||||
| 
 | ||||
|     @Provides | ||||
|     @Named("contribution") | ||||
|     public ContentProviderClient provideContributionContentProviderClient() { | ||||
|         return application.getContentResolver().acquireContentProviderClient(CONTRIBUTION_AUTHORITY); | ||||
|     public ContentProviderClient provideContributionContentProviderClient(Context context) { | ||||
|         return context.getContentResolver().acquireContentProviderClient(CONTRIBUTION_AUTHORITY); | ||||
|     } | ||||
| 
 | ||||
|     @Provides | ||||
|     @Named("modification") | ||||
|     public ContentProviderClient provideModificationContentProviderClient() { | ||||
|         return application.getContentResolver().acquireContentProviderClient(MODIFICATIONS_AUTHORITY); | ||||
|     public ContentProviderClient provideModificationContentProviderClient(Context context) { | ||||
|         return context.getContentResolver().acquireContentProviderClient(MODIFICATIONS_AUTHORITY); | ||||
|     } | ||||
| 
 | ||||
|     @Provides | ||||
|     @Named("application_preferences") | ||||
|     public SharedPreferences providesApplicationSharedPreferences() { | ||||
|         return application.getSharedPreferences("fr.free.nrw.commons", MODE_PRIVATE); | ||||
|     public SharedPreferences providesApplicationSharedPreferences(Context context) { | ||||
|         return context.getSharedPreferences("fr.free.nrw.commons", MODE_PRIVATE); | ||||
|     } | ||||
| 
 | ||||
|     @Provides | ||||
|     @Named("default_preferences") | ||||
|     public SharedPreferences providesDefaultSharedPreferences() { | ||||
|         return PreferenceManager.getDefaultSharedPreferences(application); | ||||
|     public SharedPreferences providesDefaultSharedPreferences(Context context) { | ||||
|         return PreferenceManager.getDefaultSharedPreferences(context); | ||||
|     } | ||||
| 
 | ||||
|     @Provides | ||||
|     @Named("prefs") | ||||
|     public SharedPreferences providesOtherSharedPreferences() { | ||||
|         return application.getSharedPreferences("prefs", MODE_PRIVATE); | ||||
|     public SharedPreferences providesOtherSharedPreferences(Context context) { | ||||
|         return context.getSharedPreferences("prefs", MODE_PRIVATE); | ||||
|     } | ||||
| 
 | ||||
|     @Provides | ||||
|     public UploadController providesUploadController(SessionManager sessionManager, @Named("default_preferences") SharedPreferences sharedPreferences) { | ||||
|         return new UploadController(sessionManager, application, sharedPreferences); | ||||
|     public UploadController providesUploadController(Context context, | ||||
|                                                      SessionManager sessionManager, | ||||
|                                                      @Named("default_preferences") SharedPreferences sharedPreferences) { | ||||
|         return new UploadController(sessionManager, context, sharedPreferences); | ||||
|     } | ||||
| 
 | ||||
|     @Provides | ||||
|     @Singleton | ||||
|     public SessionManager providesSessionManager(MediaWikiApi mediaWikiApi) { | ||||
|         return new SessionManager(application, mediaWikiApi); | ||||
|     public SessionManager providesSessionManager(Context context, | ||||
|                                                  MediaWikiApi mediaWikiApi) { | ||||
|         return new SessionManager(context, mediaWikiApi); | ||||
|     } | ||||
| 
 | ||||
|     @Provides | ||||
|  | @ -97,8 +106,8 @@ public class CommonsApplicationModule { | |||
| 
 | ||||
|     @Provides | ||||
|     @Singleton | ||||
|     public LocationServiceManager provideLocationServiceManager() { | ||||
|         return new LocationServiceManager(application); | ||||
|     public LocationServiceManager provideLocationServiceManager(Context context) { | ||||
|         return new LocationServiceManager(context); | ||||
|     } | ||||
| 
 | ||||
|     @Provides | ||||
|  | @ -109,8 +118,8 @@ public class CommonsApplicationModule { | |||
| 
 | ||||
|     @Provides | ||||
|     @Singleton | ||||
|     public DBOpenHelper provideDBOpenHelper() { | ||||
|         return new DBOpenHelper(application); | ||||
|     public DBOpenHelper provideDBOpenHelper(Context context) { | ||||
|         return new DBOpenHelper(context); | ||||
|     } | ||||
| 
 | ||||
|     @Provides | ||||
|  |  | |||
|  | @ -0,0 +1,43 @@ | |||
| package fr.free.nrw.commons.di; | ||||
| 
 | ||||
| import android.app.Activity; | ||||
| import android.os.Bundle; | ||||
| import android.support.annotation.Nullable; | ||||
| import android.support.v4.app.Fragment; | ||||
| import android.support.v7.app.AppCompatActivity; | ||||
| 
 | ||||
| import javax.inject.Inject; | ||||
| 
 | ||||
| import dagger.android.AndroidInjector; | ||||
| import dagger.android.DispatchingAndroidInjector; | ||||
| import dagger.android.support.HasSupportFragmentInjector; | ||||
| 
 | ||||
| public abstract class FixedDaggerAppCompatActivity extends AppCompatActivity implements HasSupportFragmentInjector { | ||||
| 
 | ||||
|     @Inject | ||||
|     DispatchingAndroidInjector<Fragment> supportFragmentInjector; | ||||
| 
 | ||||
|     @Override | ||||
|     protected void onCreate(@Nullable Bundle savedInstanceState) { | ||||
|         inject(); | ||||
|         super.onCreate(savedInstanceState); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public AndroidInjector<Fragment> supportFragmentInjector() { | ||||
|         return supportFragmentInjector; | ||||
|     } | ||||
| 
 | ||||
|     private void inject() { | ||||
|         ApplicationlessInjection injection = ApplicationlessInjection.getInstance(getApplicationContext()); | ||||
| 
 | ||||
|         AndroidInjector<Activity> activityInjector = injection.activityInjector(); | ||||
| 
 | ||||
|         if (activityInjector == null) { | ||||
|             throw new NullPointerException("ApplicationlessInjection.activityInjector() returned null"); | ||||
|         } | ||||
| 
 | ||||
|         activityInjector.inject(this); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | @ -0,0 +1,31 @@ | |||
| package fr.free.nrw.commons.di; | ||||
| 
 | ||||
| import android.content.BroadcastReceiver; | ||||
| import android.content.Context; | ||||
| import android.content.Intent; | ||||
| 
 | ||||
| import dagger.android.AndroidInjector; | ||||
| 
 | ||||
| public abstract class FixedDaggerBroadcastReceiver extends BroadcastReceiver { | ||||
| 
 | ||||
|     public FixedDaggerBroadcastReceiver() { | ||||
|         super(); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void onReceive(Context context, Intent intent) { | ||||
|         inject(context); | ||||
|     } | ||||
| 
 | ||||
|     private void inject(Context context) { | ||||
|         ApplicationlessInjection injection = ApplicationlessInjection.getInstance(context.getApplicationContext()); | ||||
| 
 | ||||
|         AndroidInjector<BroadcastReceiver> serviceInjector = injection.broadcastReceiverInjector(); | ||||
| 
 | ||||
|         if (serviceInjector == null) { | ||||
|             throw new NullPointerException("ApplicationlessInjection.broadcastReceiverInjector() returned null"); | ||||
|         } | ||||
|         serviceInjector.inject(this); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | @ -0,0 +1,32 @@ | |||
| package fr.free.nrw.commons.di; | ||||
| 
 | ||||
| import android.content.ContentProvider; | ||||
| 
 | ||||
| import dagger.android.AndroidInjector; | ||||
| 
 | ||||
| 
 | ||||
| public abstract class FixedDaggerContentProvider extends ContentProvider { | ||||
| 
 | ||||
|     public FixedDaggerContentProvider() { | ||||
|         super(); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public boolean onCreate() { | ||||
|         inject(); | ||||
|         return true; | ||||
|     } | ||||
| 
 | ||||
|     private void inject() { | ||||
|         ApplicationlessInjection injection = ApplicationlessInjection.getInstance(getContext()); | ||||
| 
 | ||||
|         AndroidInjector<ContentProvider> serviceInjector = injection.contentProviderInjector(); | ||||
| 
 | ||||
|         if (serviceInjector == null) { | ||||
|             throw new NullPointerException("ApplicationlessInjection.contentProviderInjector() returned null"); | ||||
|         } | ||||
| 
 | ||||
|         serviceInjector.inject(this); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | @ -0,0 +1,65 @@ | |||
| package fr.free.nrw.commons.di; | ||||
| 
 | ||||
| import android.app.Activity; | ||||
| import android.content.Context; | ||||
| import android.support.v4.app.Fragment; | ||||
| 
 | ||||
| import javax.inject.Inject; | ||||
| 
 | ||||
| import dagger.android.AndroidInjector; | ||||
| import dagger.android.DispatchingAndroidInjector; | ||||
| import dagger.android.support.HasSupportFragmentInjector; | ||||
| 
 | ||||
| public abstract class FixedDaggerFragment extends Fragment implements HasSupportFragmentInjector { | ||||
| 
 | ||||
|     @Inject | ||||
|     DispatchingAndroidInjector<Fragment> childFragmentInjector; | ||||
| 
 | ||||
|     @Override | ||||
|     public void onAttach(Context context) { | ||||
|         inject(); | ||||
|         super.onAttach(context); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public AndroidInjector<Fragment> supportFragmentInjector() { | ||||
|         return childFragmentInjector; | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     public void inject() { | ||||
|         HasSupportFragmentInjector hasSupportFragmentInjector = findHasFragmentInjector(); | ||||
| 
 | ||||
|         AndroidInjector<Fragment> fragmentInjector = hasSupportFragmentInjector.supportFragmentInjector(); | ||||
| 
 | ||||
|         if (fragmentInjector == null) { | ||||
|             throw new NullPointerException(String.format("%s.supportFragmentInjector() returned null", hasSupportFragmentInjector.getClass().getCanonicalName())); | ||||
|         } | ||||
| 
 | ||||
|         fragmentInjector.inject(this); | ||||
|     } | ||||
| 
 | ||||
|     private HasSupportFragmentInjector findHasFragmentInjector() { | ||||
|         Fragment parentFragment = this; | ||||
| 
 | ||||
|         while ((parentFragment = parentFragment.getParentFragment()) != null) { | ||||
|             if (parentFragment instanceof HasSupportFragmentInjector) { | ||||
|                 return (HasSupportFragmentInjector) parentFragment; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         Activity activity = getActivity(); | ||||
| 
 | ||||
|         if (activity instanceof HasSupportFragmentInjector) { | ||||
|             return (HasSupportFragmentInjector) activity; | ||||
|         } | ||||
| 
 | ||||
|         ApplicationlessInjection injection = ApplicationlessInjection.getInstance(activity.getApplicationContext()); | ||||
|         if (injection != null) { | ||||
|             return injection; | ||||
|         } | ||||
| 
 | ||||
|         throw new IllegalArgumentException(String.format("No injector was found for %s", getClass().getCanonicalName())); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | @ -0,0 +1,32 @@ | |||
| package fr.free.nrw.commons.di; | ||||
| 
 | ||||
| import android.app.IntentService; | ||||
| import android.app.Service; | ||||
| 
 | ||||
| import dagger.android.AndroidInjector; | ||||
| 
 | ||||
| public abstract class FixedDaggerIntentService extends IntentService { | ||||
| 
 | ||||
|     public FixedDaggerIntentService(String name) { | ||||
|         super(name); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void onCreate() { | ||||
|         inject(); | ||||
|         super.onCreate(); | ||||
|     } | ||||
| 
 | ||||
|     private void inject() { | ||||
|         ApplicationlessInjection injection = ApplicationlessInjection.getInstance(getApplicationContext()); | ||||
| 
 | ||||
|         AndroidInjector<Service> serviceInjector = injection.serviceInjector(); | ||||
| 
 | ||||
|         if (serviceInjector == null) { | ||||
|             throw new NullPointerException("ApplicationlessInjection.serviceInjector() returned null"); | ||||
|         } | ||||
| 
 | ||||
|         serviceInjector.inject(this); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | @ -0,0 +1,31 @@ | |||
| package fr.free.nrw.commons.di; | ||||
| 
 | ||||
| import android.app.Service; | ||||
| 
 | ||||
| import dagger.android.AndroidInjector; | ||||
| 
 | ||||
| public abstract class FixedDaggerService extends Service { | ||||
| 
 | ||||
|     public FixedDaggerService() { | ||||
|         super(); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void onCreate() { | ||||
|         inject(); | ||||
|         super.onCreate(); | ||||
|     } | ||||
| 
 | ||||
|     private void inject() { | ||||
|         ApplicationlessInjection injection = ApplicationlessInjection.getInstance(getApplicationContext()); | ||||
| 
 | ||||
|         AndroidInjector<Service> serviceInjector = injection.serviceInjector(); | ||||
| 
 | ||||
|         if (serviceInjector == null) { | ||||
|             throw new NullPointerException("ApplicationlessInjection.serviceInjector() returned null"); | ||||
|         } | ||||
| 
 | ||||
|         serviceInjector.inject(this); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 maskara
						maskara