content: /*
   * Copyright (c) 2014 Amahi
   *
   * This file is part of Amahi.
   *
   * Amahi is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * Amahi is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with Amahi. If not, see <http ://www.gnu.org/licenses/>.
   */
  
  package org.amahi.anywhere.activity;
  
  import android.content.Context;
  import android.graphics.PorterDuff;
  import android.graphics.drawable.Drawable;
  import android.os.Bundle;
  import android.view.MenuItem;
  
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.appcompat.app.AppCompatDelegate;
  import androidx.fragment.app.FragmentManager;
  import androidx.fragment.app.FragmentTransaction;
  
  import com.squareup.otto.Subscribe;
  
  import org.amahi.anywhere.AmahiApplication;
  import org.amahi.anywhere.R;
  import org.amahi.anywhere.bus.BusProvider;
  import org.amahi.anywhere.bus.UploadSettingsOpeningEvent;
  import org.amahi.anywhere.fragment.SettingsFragment;
  import org.amahi.anywhere.fragment.UploadSettingsFragment;
  import org.amahi.anywhere.util.AppTheme;
  import org.amahi.anywhere.util.LocaleHelper;
  
  /**
   * Settings activity. Shows application's settings.
   * Settings itself are provided via {@link org.amahi.anywhere.fragment.SettingsFragment}.
   */
  public class SettingsActivity extends AppCompatActivity {
      public static final int RESULT_THEME_UPDATED = 3;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
  
          AppTheme selectedTheme = AmahiApplication.getInstance().getThemeEnabled();
          switch (selectedTheme) {
              case DEFAULT:
                  getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
                  break;
              case LIGHT:
                  getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                  break;
              case DARK:
                  getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
          }
  
          super.onCreate(savedInstanceState);
  
          setUpHomeNavigation();
  
          setContentView(R.layout.activity_settings);
  
          if (savedInstanceState == null) {
              setUpSettingsFragment();
          }
      }
  
      private void setUpHomeNavigation() {
          if (getSupportActionBar() != null) {
              getSupportActionBar().setDisplayHomeAsUpEnabled(true);
          }
      }
  
      private void setUpSettingsFragment() {
          FragmentManager fragmentManager = getSupportFragmentManager();
          fragmentManager.beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
              .replace(R.id.settings_container, new SettingsFragment()).commit();
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem menuItem) {
          if (menuItem.getItemId() == android.R.id.home) {
              onBackPressed();
              return true;
          }
          return super.onOptionsItemSelected(menuItem);
      }
  
      @Subscribe
      public void onUploadSettingsOpenEvent(UploadSettingsOpeningEvent event) {
          getSupportFragmentManager().beginTransaction()
              .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
              .replace(R.id.settings_container, new UploadSettingsFragment())
              .addToBackStack(null)
              .commit();
      }
  
      @Override
      protected void onResume() {
          super.onResume();
  
          BusProvider.getBus().register(this);
      }
  
      @Override
      protected void onPause() {
          super.onPause();
  
          BusProvider.getBus().unregister(this);
      }
  
      @Override
      protected void attachBaseContext(Context newBase) {
          super.attachBaseContext(LocaleHelper.onAttach(newBase));
      }
  }
  