Android Open Source - BerryTube Settings Fragment






From Project

Back to project page BerryTube.

License

The source code is released under:

GNU General Public License

If you think the Android project BerryTube listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * BerryTubeChat android client//  w w w .  j  a v  a2s.  co  m
 * Copyright (C) 2015 Daniel Triendl <trellmor@trellmor.com>
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.trellmor.berrytubechat;

import android.content.SharedPreferences;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.RingtonePreference;
import android.text.TextUtils;

import com.trellmor.berrymotes.EmoteSettings;

public class SettingsFragment extends PreferenceFragment
    implements SharedPreferences.OnSharedPreferenceChangeListener {

  private EditTextPreference mPrefScrollback;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
    addPreferencesFromResource(R.xml.pref_notification);
    EmoteSettings.addEmoteSettings(this);

    mPrefScrollback = (EditTextPreference) findPreference(MainActivity.KEY_SCROLLBACK);

    bindPreferenceSummaryToValue(findPreference(MainActivity.KEY_FLAIR));
    bindPreferenceSummaryToValue(findPreference(MainActivity.KEY_SQUEE_RINGTONE));
  }

  @Override
  public void onPause() {
    super.onPause();

    getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
  }

  @Override
  public void onResume() {
    super.onResume();

    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    onSharedPreferenceChanged(null, MainActivity.KEY_SCROLLBACK);
  }

  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    if (key.equals(MainActivity.KEY_SCROLLBACK)) {
      Preference pref = findPreference(key);
      if (pref instanceof EditTextPreference) {
        int scrollback;
        try {
          scrollback = Integer.parseInt(mPrefScrollback.getText());
        } catch (NumberFormatException e) {
          scrollback = 100;
        }

        if (scrollback <= 0)
          scrollback = 100;

        String scrollbackSummary = getString(R.string.pref_scrollback_summary);
        mPrefScrollback.setSummary(String.format(scrollbackSummary, scrollback));
      }
    }
  }

  /**
   * A preference value change listener that updates the preference's summary
   * to reflect its new value.
   */
  private static final Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
    @Override
    public boolean onPreferenceChange(Preference preference, Object value) {
      String stringValue = value.toString();

      if (preference instanceof ListPreference) {
        // For list preferences, look up the correct display value in
        // the preference's 'entries' list.
        ListPreference listPreference = (ListPreference) preference;
        int index = listPreference.findIndexOfValue(stringValue);

        // Set the summary to reflect the new value.
        preference
            .setSummary(index >= 0 ? listPreference.getEntries()[index]
                : null);

      } else if (preference instanceof RingtonePreference) {
        // For ringtone preferences, look up the correct display value
        // using RingtoneManager.
        if (TextUtils.isEmpty(stringValue)) {
          // Empty values correspond to 'silent' (no ringtone).
          preference.setSummary(R.string.pref_squee_ringtone_silent);

        } else {
          Ringtone ringtone = RingtoneManager.getRingtone(
              preference.getContext(), Uri.parse(stringValue));

          if (ringtone == null) {
            // Clear the summary if there was a lookup error.
            preference.setSummary(null);
          } else {
            // Set the summary to reflect the new ringtone display
            // name.
            String name = ringtone
                .getTitle(preference.getContext());
            preference.setSummary(name);
          }
        }

      } else {
        // For all other preferences, set the summary to the value's
        // simple string representation.
        preference.setSummary(stringValue);
      }
      return true;
    }
  };

  /**
   * Binds a preference's summary to its value. More specifically, when the
   * preference's value is changed, its summary (line of text below the
   * preference title) is updated to reflect the value. The summary is also
   * immediately updated upon calling this method. The exact display format is
   * dependent on the type of preference.
   *
   * @see #sBindPreferenceSummaryToValueListener
   */
  private static void bindPreferenceSummaryToValue(Preference preference) {
    // Set the listener to watch for value changes.
    preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);

    // Trigger the listener immediately with the preference's
    // current value.
    sBindPreferenceSummaryToValueListener.onPreferenceChange(
        preference,
        PreferenceManager.getDefaultSharedPreferences(
            preference.getContext()).getString(preference.getKey(),
            ""));
  }
}




Java Source Code List

com.trellmor.berrytube.BerryTubeBinder.java
com.trellmor.berrytube.BerryTubeCallback.java
com.trellmor.berrytube.BerryTubeIOCallback.java
com.trellmor.berrytube.BerryTube.java
com.trellmor.berrytube.ChatMessage.java
com.trellmor.berrytube.ChatUser.java
com.trellmor.berrytube.Poll.java
com.trellmor.berrytubechat.BerryTubeUtils.java
com.trellmor.berrytubechat.ChatActivity.java
com.trellmor.berrytubechat.ChatMessageAdapter.java
com.trellmor.berrytubechat.ChatMessageFormatter.java
com.trellmor.berrytubechat.FlairGetter.java
com.trellmor.berrytubechat.MainActivity.java
com.trellmor.berrytubechat.SettingsActivity.java
com.trellmor.berrytubechat.SettingsFragment.java
com.trellmor.berrytubechat.SimpleCrypto.java