app/src/main/java/com/geekorum/ttrss/Application.java
changeset 0 14443efede32
child 5 130497abe90a
equal deleted inserted replaced
-1:000000000000 0:14443efede32
       
     1 /**
       
     2  * Geekttrss is a RSS feed reader application on the Android Platform.
       
     3  *
       
     4  * Copyright (C) 2017-2018 by Frederic-Charles Barthelery.
       
     5  *
       
     6  * This file is part of Geekttrss.
       
     7  *
       
     8  * Geekttrss is free software: you can redistribute it and/or modify
       
     9  * it under the terms of the GNU General Public License as published by
       
    10  * the Free Software Foundation, either version 3 of the License, or
       
    11  * (at your option) any later version.
       
    12  *
       
    13  * Geekttrss is distributed in the hope that it will be useful,
       
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    16  * GNU General Public License for more details.
       
    17  *
       
    18  * You should have received a copy of the GNU General Public License
       
    19  * along with Geekttrss.  If not, see <http://www.gnu.org/licenses/>.
       
    20  */
       
    21 package com.geekorum.ttrss;
       
    22 
       
    23 import android.app.Activity;
       
    24 import android.app.Service;
       
    25 import android.content.SharedPreferences;
       
    26 import android.preference.PreferenceManager;
       
    27 import androidx.appcompat.app.AppCompatDelegate;
       
    28 import com.geekorum.ttrss.di.ApplicationComponent;
       
    29 import com.geekorum.ttrss.di.DaggerApplicationComponent;
       
    30 import com.squareup.picasso.Picasso;
       
    31 import dagger.android.AndroidInjector;
       
    32 import dagger.android.DispatchingAndroidInjector;
       
    33 import dagger.android.HasActivityInjector;
       
    34 import dagger.android.HasServiceInjector;
       
    35 import timber.log.Timber;
       
    36 
       
    37 import javax.inject.Inject;
       
    38 
       
    39 import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_AUTO;
       
    40 
       
    41 /**
       
    42  * Initialize global component for the TTRSS application.
       
    43  */
       
    44 public class Application extends android.app.Application implements HasActivityInjector, HasServiceInjector {
       
    45     @Inject
       
    46     ApplicationComponent applicationComponent;
       
    47 
       
    48     @Inject
       
    49     DispatchingAndroidInjector<Activity> dispatchinActivityInjector;
       
    50     @Inject
       
    51     DispatchingAndroidInjector<Service> dispatchingServiceInjector;
       
    52 
       
    53     @Inject
       
    54     Timber.Tree timberTree;
       
    55 
       
    56     @Inject
       
    57     Picasso picasso;
       
    58 
       
    59     private boolean needToInject = true;
       
    60 
       
    61     @Override
       
    62     public void onCreate() {
       
    63         injectIfNecessary();
       
    64         super.onCreate();
       
    65         Timber.plant(timberTree);
       
    66         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
       
    67         String nighModeStr = sharedPreferences.getString(SettingsActivity.KEY_THEME, Integer.toString(MODE_NIGHT_AUTO));
       
    68         int nighMode = Integer.valueOf(nighModeStr);
       
    69         AppCompatDelegate.setDefaultNightMode(nighMode);
       
    70     }
       
    71 
       
    72     public void injectIfNecessary() {
       
    73         if (needToInject) {
       
    74             synchronized (this) {
       
    75                 if (needToInject) {
       
    76                     DaggerApplicationComponent.builder()
       
    77                             .bindApplication(this)
       
    78                             .build().inject(this);
       
    79                     setupPicasso();
       
    80                     needToInject = false;
       
    81                 }
       
    82             }
       
    83         }
       
    84     }
       
    85 
       
    86     public ApplicationComponent getApplicationComponent() {
       
    87         // Content providers can be created before Application.onCreate() is called
       
    88         injectIfNecessary();
       
    89         return applicationComponent;
       
    90     }
       
    91 
       
    92     @Override
       
    93     public AndroidInjector<Activity> activityInjector() {
       
    94         return dispatchinActivityInjector;
       
    95     }
       
    96 
       
    97     private void setupPicasso() {
       
    98         Picasso.setSingletonInstance(picasso);
       
    99     }
       
   100 
       
   101     @Override
       
   102     public AndroidInjector<Service> serviceInjector() {
       
   103         return dispatchingServiceInjector;
       
   104     }
       
   105 }