Android Open Source - HapiPodcastJ Pref






From Project

Back to project page HapiPodcastJ.

License

The source code is released under:

GNU General Public License

If you think the Android project HapiPodcastJ 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

/*
 * Copyright (C) 2007 The Android Open Source Project
 *// w  w w  . j a  va 2 s .  c o m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package info.xuluan.podcast;

import info.xuluan.podcast.service.PodcastService;
import info.xuluan.podcastj.R;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.Preference;

public class Pref extends HapiPreferenceActivity {

  public static final String HAPI_PREFS_FILE_NAME = "info.xuluan.podcastj_preferences";
    //Default filename is our package name (see manifest) with _preferences appended
  private static final int DAYS_PER_WEEK = 7;
  private static final int DAYS_PER_MONTH = 30;
  private static final int DAYS_PER_YEAR = 365;
  private static final int HOURS_PER_DAY = 24;
  private static final int HOURS_PER_WEEK = HOURS_PER_DAY * DAYS_PER_WEEK;
  private static final int HOURS_PER_MONTH = HOURS_PER_DAY * DAYS_PER_MONTH;
  private static final int HOURS_PER_YEAR = HOURS_PER_DAY * DAYS_PER_YEAR;
  private static final int MINUTES_PER_HOUR = 60;
  private static final int MINUTES_PER_DAY = MINUTES_PER_HOUR * HOURS_PER_DAY;
  private static final int MINUTES_PER_WEEK = MINUTES_PER_HOUR * HOURS_PER_WEEK;
  
  private PodcastService serviceBinder = null;
  ComponentName service = null;
  private SummaryUpdater summaryUpdater = new SummaryUpdater();

  private ServiceConnection serviceConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
      serviceBinder = ((PodcastService.PodcastBinder) service)
          .getService();
    }

    public void onServiceDisconnected(ComponentName className) {
      serviceBinder = null;
    }
  };

  @Override
  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences);

    service = startService(new Intent(this, PodcastService.class));

    Intent bindIntent = new Intent(this, PodcastService.class);
    bindService(bindIntent, serviceConnection, Context.BIND_AUTO_CREATE);
  }

  @Override
  protected void onResume() {
    super.onResume();
    SharedPreferences prefs = getPreferenceScreen().getSharedPreferences();
    prefs.registerOnSharedPreferenceChangeListener(summaryUpdater);
    summaryUpdater.updateAllSummaries(prefs);
  }

  @Override
  protected void onPause() {
    super.onPause();
    if(serviceBinder!=null)
      serviceBinder.updateSetting();
      getPreferenceScreen().getSharedPreferences()
            .unregisterOnSharedPreferenceChangeListener(summaryUpdater);
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    unbindService(serviceConnection);
    // stopService(new Intent(this, service.getClass()));
  }

  class SummaryUpdater implements OnSharedPreferenceChangeListener {
    public void updateAllSummaries(SharedPreferences prefs) {
      String[] keys = {
          "pref_update_wifi", "pref_update_mobile", "pref_download_only_wifi",
          "pref_max_new_items",
          "pref_item_expire", "pref_download_file_expire", "pref_played_file_expire",
          "pref_rewind_interval", "pref_fast_forward_interval"
      };
      for (String key : keys) {
        onSharedPreferenceChanged(prefs, key);
      }
    }
    
      public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
        if (key.equals("pref_download_only_wifi"))
          updateDisableMobile(prefs,key);
          if (key.equals("pref_update_wifi"))
            updateMinuteIntervalSummary(prefs, key);
          else if (key.equals("pref_update_mobile"))
            updateMinuteIntervalSummary(prefs, key);
          else if (key.equals("pref_max_new_items"))
            updateMaxItems(prefs, key);
          else if (key.equals("pref_item_expire"))
            updateExpireDaysSummary(prefs, key);
          else if (key.equals("pref_download_file_expire"))
            updateExpireDaysSummary(prefs, key);
          else if (key.equals("pref_played_file_expire"))
            updateExpireHoursSummary(prefs, key);
          else if (key.equals("pref_rewind_interval"))
            updatePlayerIntervalSummary(prefs, key, "Rewind");
          else if (key.equals("pref_fast_forward_interval"))
            updatePlayerIntervalSummary(prefs, key, "Fast-forward");
      }

      private void updateDisableMobile(SharedPreferences prefs, String key) {
        Boolean disabled = prefs.getBoolean(key, false);
        Preference mobileIntervalPref = findPreference("pref_update_mobile");
        mobileIntervalPref.setEnabled(!disabled);
        if (disabled)
          mobileIntervalPref.setSummary("To set, enable Mobile update");
        else
          onSharedPreferenceChanged(prefs, "pref_update_mobile");
      }
      
      private void updateMaxItems(SharedPreferences prefs, String key) {
        Preference pref = findPreference(key);
        String value = prefs.getString(key, "");
        pref.setSummary("Download up to "+value+" new items");
      }
      
      private void updateExpireDaysSummary(SharedPreferences prefs, String key) {
            Preference pref = findPreference(key);
            String value = prefs.getString(key,"");
            int days = Integer.parseInt(value);
            String s;
            if ((days%DAYS_PER_YEAR)==0)
              s = maybePlural(days/DAYS_PER_YEAR,"year","years");
            else if ((days%DAYS_PER_MONTH)==0)
              s = maybePlural(days/DAYS_PER_MONTH,"month","months");
            else if ((days%DAYS_PER_WEEK)==0)
              s = maybePlural(days/DAYS_PER_WEEK,"week","weeks");
            else
              s = maybePlural(days,"day","days");
            pref.setSummary("Expire after "+s);        
      }
      
      private void updateExpireHoursSummary(SharedPreferences prefs, String key) {
            Preference pref = findPreference(key);
            String value = prefs.getString(key,"");
            int hours = Integer.parseInt(value);
            String s;
            if ((hours%HOURS_PER_YEAR)==0)
              s = maybePlural(hours/HOURS_PER_YEAR,"year","years");
            else if ((hours%HOURS_PER_MONTH)==0)
              s = maybePlural(hours/HOURS_PER_MONTH,"month","months");
            else if ((hours%HOURS_PER_WEEK)==0)
              s = maybePlural(hours/HOURS_PER_WEEK,"week","weeks");
            else if ((hours%HOURS_PER_DAY)==0)
              s = maybePlural(hours/HOURS_PER_DAY,"day","days");
            else
              s = maybePlural(hours,"hour","hours");
            pref.setSummary("Expire after "+s);        
      }
      
      private void updateMinuteIntervalSummary(SharedPreferences prefs, String key) {
            Preference pref = findPreference(key);
            String value = prefs.getString(key,"");
            int minutes = Integer.parseInt(value);
            String s;
            if ((minutes%MINUTES_PER_WEEK)==0)
              s = maybePlural(minutes/MINUTES_PER_WEEK,"week","weeks");
            else if ((minutes%MINUTES_PER_DAY)==0)
              s = maybePlural(minutes/MINUTES_PER_DAY,"day","days");
            else if ((minutes%MINUTES_PER_HOUR)==0)
              s = maybePlural(minutes/MINUTES_PER_HOUR,"hour","hours");
            else
              s = maybePlural(minutes,"minute","minutes");
            pref.setSummary("Every "+s);        
      }
      
      private String maybePlural(int n, String singular, String plural) {
        return ""+n+" "+((n==1)?singular:plural);
      }

      private void updatePlayerIntervalSummary(SharedPreferences prefs, String key, String direction) {
            Preference pref = findPreference(key);
            String value = prefs.getString(key,"");
            pref.setSummary(direction+" "+value+" seconds");        
      }

  }

}




Java Source Code List

info.xuluan.podcast.AddChannelActivity.java
info.xuluan.podcast.BackupChannelsActivity.java
info.xuluan.podcast.ChannelActivity.java
info.xuluan.podcast.ChannelDetailsActivity.java
info.xuluan.podcast.ChannelsActivity.java
info.xuluan.podcast.DownloadActivity.java
info.xuluan.podcast.EpisodeDetailsActivity.java
info.xuluan.podcast.EpisodeIcons.java
info.xuluan.podcast.EpisodesActivity.java
info.xuluan.podcast.FlingGestureDetector.java
info.xuluan.podcast.Flingable.java
info.xuluan.podcast.HapiActivity.java
info.xuluan.podcast.HapiListActivity.java
info.xuluan.podcast.HapiPreferenceActivity.java
info.xuluan.podcast.HomeActivity.java
info.xuluan.podcast.MainActivity.java
info.xuluan.podcast.PlayerActivity.java
info.xuluan.podcast.PodcastBaseActivity.java
info.xuluan.podcast.PodcastTab.java
info.xuluan.podcast.Pref.java
info.xuluan.podcast.SearchActivity.java
info.xuluan.podcast.StartupActivity.java
info.xuluan.podcast.TabsHelper.java
info.xuluan.podcast.actionbar.ActionBarHelperBase.java
info.xuluan.podcast.actionbar.ActionBarHelperHoneycomb.java
info.xuluan.podcast.actionbar.ActionBarHelperICS.java
info.xuluan.podcast.actionbar.ActionBarHelper.java
info.xuluan.podcast.actionbar.SimpleMenuItem.java
info.xuluan.podcast.actionbar.SimpleMenu.java
info.xuluan.podcast.fetcher.FeedFetcher.java
info.xuluan.podcast.fetcher.Response.java
info.xuluan.podcast.parser.FeedHandler.java
info.xuluan.podcast.parser.FeedParserHandler.java
info.xuluan.podcast.parser.FeedParserListenerInterface.java
info.xuluan.podcast.parser.FeedParserListener.java
info.xuluan.podcast.parser.FeedParser.java
info.xuluan.podcast.parser.OPMLParserHandler.java
info.xuluan.podcast.parser.SearchItem.java
info.xuluan.podcast.provider.FeedItem.java
info.xuluan.podcast.provider.ItemColumns.java
info.xuluan.podcast.provider.PodcastOpenHelper.java
info.xuluan.podcast.provider.PodcastProvider.java
info.xuluan.podcast.provider.SubscriptionColumns.java
info.xuluan.podcast.provider.Subscription.java
info.xuluan.podcast.service.PlayerService.java
info.xuluan.podcast.service.PodcastService.java
info.xuluan.podcast.utils.DialogMenu.java
info.xuluan.podcast.utils.FileUtils.java
info.xuluan.podcast.utils.IconCursorAdapter.java
info.xuluan.podcast.utils.LabeledFrame.java
info.xuluan.podcast.utils.LockHandler.java
info.xuluan.podcast.utils.Log.java
info.xuluan.podcast.utils.SDCardMgr.java
info.xuluan.podcast.utils.StrUtils.java
info.xuluan.podcast.utils.ZipExporter.java
info.xuluan.podcast.utils.ZipImporter.java