public boolean onPreferenceChange(Preference preference, Object newValue) {
   ContentResolver resolver = mContext.getContentResolver();
   if (preference == mQuietHoursTimeRange) {
     Settings.System.putInt(
         resolver, Settings.System.QUIET_HOURS_START, mQuietHoursTimeRange.getStartTime());
     Settings.System.putInt(
         resolver, Settings.System.QUIET_HOURS_END, mQuietHoursTimeRange.getEndTime());
     SmsCallHelper.scheduleService(mContext);
     return true;
   } else if (preference == mQuietHoursEnabled) {
     Settings.System.putInt(
         resolver, Settings.System.QUIET_HOURS_ENABLED, (Boolean) newValue ? 1 : 0);
     SmsCallHelper.scheduleService(mContext);
     return true;
   } else if (preference == mQuietHoursMute) {
     Settings.System.putInt(
         resolver, Settings.System.QUIET_HOURS_MUTE, (Boolean) newValue ? 1 : 0);
     return true;
   } else if (preference == mQuietHoursStill) {
     Settings.System.putInt(
         resolver, Settings.System.QUIET_HOURS_STILL, (Boolean) newValue ? 1 : 0);
     return true;
   } else if (preference == mQuietHoursDim) {
     Settings.System.putInt(resolver, Settings.System.QUIET_HOURS_DIM, (Boolean) newValue ? 1 : 0);
     return true;
   } else if (preference == mQuietHoursHaptic) {
     Settings.System.putInt(
         resolver, Settings.System.QUIET_HOURS_HAPTIC, (Boolean) newValue ? 1 : 0);
     return true;
   } else if (preference == mRingtoneLoop) {
     mRingtoneLoop.setSummary(
         (Boolean) newValue
             ? R.string.quiet_hours_bypass_ringtone_loop_summary_on
             : R.string.quiet_hours_bypass_ringtone_loop_summary_off);
     return true;
   } else if (preference == mAutoSms) {
     mSmsPref = Integer.parseInt((String) newValue);
     mAutoSms.setSummary(mAutoSms.getEntries()[mSmsPref]);
     shouldDisplayTextPref();
     return true;
   } else if (preference == mAutoSmsCall) {
     mCallPref = Integer.parseInt((String) newValue);
     mAutoSmsCall.setSummary(mAutoSmsCall.getEntries()[mCallPref]);
     shouldDisplayTextPref();
     return true;
   } else if (preference == mSmsBypass) {
     mSmsBypassPref = Integer.parseInt((String) newValue);
     mSmsBypass.setSummary(mSmsBypass.getEntries()[mSmsBypassPref]);
     mSmsBypassCode.setEnabled(mSmsBypassPref != 0);
     shouldDisplayRingerPrefs();
     return true;
   } else if (preference == mCallBypass) {
     mCallBypassPref = Integer.parseInt((String) newValue);
     mCallBypass.setSummary(mCallBypass.getEntries()[mCallBypassPref]);
     mCallBypassNumber.setEnabled(mCallBypassPref != 0);
     shouldDisplayRingerPrefs();
     return true;
   } else if (preference == mCallBypassNumber) {
     int val = Integer.parseInt((String) newValue);
     mCallBypassNumber.setSummary(
         mCallBypassNumber.getEntries()[val - 2]
             + getResources().getString(R.string.quiet_hours_calls_required_summary));
     return true;
   } else if (preference == mBypassRingtone) {
     Uri val = Uri.parse((String) newValue);
     SharedPreferences.Editor editor = mPrefs.edit();
     Ringtone ringtone = RingtoneManager.getRingtone(mContext, val);
     if (ringtone != null) {
       editor.putString(KEY_BYPASS_RINGTONE, val.toString()).apply();
       mBypassRingtone.setSummary(ringtone.getTitle(mContext));
     } else {
       // No silent option, won't reach here
       editor.putString(KEY_BYPASS_RINGTONE, null).apply();
     }
     return true;
   }
   return false;
 }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getPreferenceManager() != null) {
      addPreferencesFromResource(R.xml.quiet_hours_settings);

      mContext = getActivity().getApplicationContext();

      ContentResolver resolver = mContext.getContentResolver();

      PreferenceScreen prefSet = getPreferenceScreen();

      mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);

      // Load the preferences
      mQuietHoursNote = prefSet.findPreference(KEY_QUIET_HOURS_NOTE);
      mQuietHoursEnabled = (CheckBoxPreference) prefSet.findPreference(KEY_QUIET_HOURS_ENABLED);
      mQuietHoursTimeRange =
          (TimeRangePreference) prefSet.findPreference(KEY_QUIET_HOURS_TIMERANGE);
      mQuietHoursMute = (CheckBoxPreference) prefSet.findPreference(KEY_QUIET_HOURS_MUTE);
      mQuietHoursStill = (CheckBoxPreference) prefSet.findPreference(KEY_QUIET_HOURS_STILL);
      mQuietHoursHaptic = (CheckBoxPreference) prefSet.findPreference(KEY_QUIET_HOURS_HAPTIC);
      mQuietHoursDim = (CheckBoxPreference) findPreference(KEY_QUIET_HOURS_DIM);
      mRingtoneLoop = (CheckBoxPreference) findPreference(KEY_LOOP_BYPASS_RINGTONE);
      mAutoSms = (ListPreference) findPreference(KEY_AUTO_SMS);
      mAutoSmsCall = (ListPreference) findPreference(KEY_AUTO_SMS_CALL);
      mAutoSmsMessage = (Preference) findPreference(KEY_AUTO_SMS_MESSAGE);
      mSmsBypass = (ListPreference) findPreference(KEY_SMS_BYPASS);
      mCallBypass = (ListPreference) findPreference(KEY_CALL_BYPASS);
      mCallBypassNumber = (ListPreference) findPreference(KEY_REQUIRED_CALLS);
      mSmsBypassCode = (Preference) findPreference(KEY_SMS_BYPASS_CODE);
      mBypassRingtone = (RingtonePreference) findPreference(KEY_BYPASS_RINGTONE);

      // Remove the "Incoming calls behaviour" note if the device does not support phone calls
      if (mQuietHoursNote != null
          && getResources().getBoolean(com.android.internal.R.bool.config_voice_capable) == false) {
        getPreferenceScreen().removePreference(mQuietHoursNote);
      }

      // Set the preference state and listeners where applicable
      mQuietHoursEnabled.setChecked(
          Settings.System.getInt(resolver, Settings.System.QUIET_HOURS_ENABLED, 0) == 1);
      mQuietHoursEnabled.setOnPreferenceChangeListener(this);
      mQuietHoursTimeRange.setTimeRange(
          Settings.System.getInt(resolver, Settings.System.QUIET_HOURS_START, 0),
          Settings.System.getInt(resolver, Settings.System.QUIET_HOURS_END, 0));
      mQuietHoursTimeRange.setOnPreferenceChangeListener(this);
      mQuietHoursMute.setChecked(
          Settings.System.getInt(resolver, Settings.System.QUIET_HOURS_MUTE, 0) == 1);
      mQuietHoursMute.setOnPreferenceChangeListener(this);
      mQuietHoursStill.setChecked(
          Settings.System.getInt(resolver, Settings.System.QUIET_HOURS_STILL, 0) == 1);
      mQuietHoursStill.setOnPreferenceChangeListener(this);
      mQuietHoursHaptic.setChecked(
          Settings.System.getInt(resolver, Settings.System.QUIET_HOURS_HAPTIC, 0) == 1);
      mQuietHoursHaptic.setOnPreferenceChangeListener(this);
      mRingtoneLoop.setOnPreferenceChangeListener(this);
      mAutoSms.setValue(mPrefs.getString(KEY_AUTO_SMS, "0"));
      mAutoSms.setOnPreferenceChangeListener(this);
      mAutoSmsCall.setValue(mPrefs.getString(KEY_AUTO_SMS_CALL, "0"));
      mAutoSmsCall.setOnPreferenceChangeListener(this);
      mSmsBypass.setValue(mPrefs.getString(KEY_SMS_BYPASS, "0"));
      mSmsBypass.setOnPreferenceChangeListener(this);
      mCallBypass.setValue(mPrefs.getString(KEY_CALL_BYPASS, "0"));
      mCallBypass.setOnPreferenceChangeListener(this);
      mCallBypassNumber.setValue(mPrefs.getString(KEY_REQUIRED_CALLS, "2"));
      mCallBypassNumber.setOnPreferenceChangeListener(this);
      mBypassRingtone.setOnPreferenceChangeListener(this);

      TelephonyManager telephonyManager =
          (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
      if (telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE) {
        prefSet.removePreference((PreferenceGroup) findPreference("sms_respond"));
        prefSet.removePreference((PreferenceGroup) findPreference("quiethours_bypass"));
      } else {
        int callBypassNumber = Integer.parseInt(mPrefs.getString(KEY_REQUIRED_CALLS, "2"));
        boolean loopRingtone = mPrefs.getBoolean(KEY_LOOP_BYPASS_RINGTONE, true);
        mSmsBypassPref = Integer.parseInt(mPrefs.getString(KEY_SMS_BYPASS, "0"));
        mSmsPref = Integer.parseInt(mPrefs.getString(KEY_AUTO_SMS, "0"));
        mCallPref = Integer.parseInt(mPrefs.getString(KEY_AUTO_SMS_CALL, "0"));
        mCallBypassPref = Integer.parseInt(mPrefs.getString(KEY_CALL_BYPASS, "0"));
        Uri alertSoundUri = SmsCallHelper.returnUserRingtone(mContext);
        Ringtone ringtoneAlarm = RingtoneManager.getRingtone(mContext, alertSoundUri);
        mBypassRingtone.setSummary(ringtoneAlarm.getTitle(mContext));
        mRingtoneLoop.setChecked(loopRingtone);
        mRingtoneLoop.setSummary(
            loopRingtone
                ? R.string.quiet_hours_bypass_ringtone_loop_summary_on
                : R.string.quiet_hours_bypass_ringtone_loop_summary_off);
        mSmsBypass.setSummary(mSmsBypass.getEntries()[mSmsBypassPref]);
        mCallBypass.setSummary(mCallBypass.getEntries()[mCallBypassPref]);
        mCallBypassNumber.setSummary(
            mCallBypassNumber.getEntries()[callBypassNumber - 2]
                + getResources().getString(R.string.quiet_hours_calls_required_summary));
        mAutoSms.setSummary(mAutoSms.getEntries()[mSmsPref]);
        mAutoSmsCall.setSummary(mAutoSmsCall.getEntries()[mCallPref]);
        mCallBypassNumber.setEnabled(mCallBypassPref != 0);
        mSmsBypassCode.setEnabled(mSmsBypassPref != 0);
        shouldDisplayRingerPrefs();
        shouldDisplayTextPref();
        setSmsBypassCodeSummary();
      }

      // Remove the notification light setting if the device does not support it
      if (mQuietHoursDim != null
          && getResources().getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed)
              == false) {
        getPreferenceScreen().removePreference(mQuietHoursDim);
      } else {
        mQuietHoursDim.setChecked(
            Settings.System.getInt(resolver, Settings.System.QUIET_HOURS_DIM, 0) == 1);
        mQuietHoursDim.setOnPreferenceChangeListener(this);
      }

      mPreferencesChangeListener =
          new OnSharedPreferenceChangeListener() {
            public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
              if (key.equals(KEY_AUTO_SMS_CALL)
                  || key.equals(KEY_AUTO_SMS)
                  || key.equals(KEY_CALL_BYPASS)
                  || key.equals(KEY_SMS_BYPASS)) {
                SmsCallHelper.scheduleService(mContext);
              }
              if (key.equals(KEY_SMS_BYPASS_CODE)) {
                setSmsBypassCodeSummary();
              }
            }
          };
    }
  }