content: package com.ulicae.cinelog.android.settings;
  
  import android.Manifest;
  import android.content.pm.PackageManager;
  import android.os.Bundle;
  import android.preference.CheckBoxPreference;
  import android.preference.Preference;
  import android.preference.PreferenceFragment;
  import androidx.core.app.ActivityCompat;
  import android.widget.Toast;
  
  import com.ulicae.cinelog.R;
  
  /**
   * CineLog Copyright 2018 Pierre Rognon
   *
   *
   * This file is part of CineLog.
   * CineLog 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.
   *
   * CineLog 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 CineLog. If not, see <https://www.gnu.org/licenses/>.
   *
   */
  public class SettingsFragment extends PreferenceFragment {
  
      private boolean writeStoragePermission;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          // Load the preferences from an XML resource
          addPreferencesFromResource(R.xml.app_preferences);
  
          configureThemePreference();
          configureAutomaticExportPreference();
      }
  
      private void configureThemePreference() {
          CheckBoxPreference listcheckBoxPreference = (CheckBoxPreference) findPreference("theme");
          listcheckBoxPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
              @Override
              public boolean onPreferenceChange(Preference preference, Object newValue) {
                  Toast.makeText(getActivity().getBaseContext(), getResources().getString(R.string.change_theme), Toast.LENGTH_LONG).show();
                  return true;
              }
          });
      }
  
      private void configureAutomaticExportPreference() {
          CheckBoxPreference listcheckBoxPreference = (CheckBoxPreference) findPreference("automatic_save");
          listcheckBoxPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
              @Override
              public boolean onPreferenceChange(Preference preference, Object newValue) {
                  if((Boolean) newValue && !writeStoragePermission) {
                      ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                  }
                  return true;
              }
          });
      }
  
      @Override
      public void onRequestPermissionsResult(int requestCode,
                                             @SuppressWarnings("NullableProblems") String permissions[],
                                             @SuppressWarnings("NullableProblems") int[] grantResults) {
          super.onRequestPermissionsResult(requestCode, permissions, grantResults);
          switch (requestCode) {
              case 1: {
                  writeStoragePermission = grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED;
              }
          }
      }
  
  }
  
  