Android Open Source - TechDissected Settings Fragment






From Project

Back to project page TechDissected.

License

The source code is released under:

Apache License

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

package com.pkmmte.techdissected.fragment;
//ww  w . ja v a2s.  co m
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;
import com.pkmmte.techdissected.R;
import com.pkmmte.techdissected.adapter.SettingsAdapter;
import com.pkmmte.techdissected.model.SettingsItem;
import com.pkmmte.techdissected.util.Constants;
import com.pkmmte.techdissected.util.Dialogs;
import com.pkmmte.techdissected.util.Utils;

public class SettingsFragment extends ListFragment
{
  // ID Keys
  private final int STORAGE_CACHE = 0;
  private final int READ_GRAYSCALE = 1;
  private final int CREDITS_LIBRARIES = 2;
  private final int CREDITS_DEV = 3;

  // App Preferences
  private SharedPreferences mPrefs;

  // List Adapter
  private SettingsAdapter mAdapter;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  {
    mPrefs = getActivity().getSharedPreferences(Constants.PREFS_NAME, 0);
    addSettings();
    setListAdapter(mAdapter);

    return super.onCreateView(inflater, container, savedInstanceState);
  }

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

    // Reinitialize our preferences if they somehow became null
    if(mPrefs == null)
      mPrefs = getActivity().getSharedPreferences(Constants.PREFS_NAME, 0);

    // We have a custom divider so let's disable this
    getListView().setDivider(null);
    getListView().setDividerHeight(0);

    // Apply custom selector
    getListView().setSelector(R.drawable.selector_transparent_lgray);
    getListView().setDrawSelectorOnTop(false);

    // Calculate overall cache size asynchronously
    calculateCacheAsync();
  }

  private void addSettings()
  {
    mAdapter = new SettingsAdapter(getActivity());

    mAdapter.addHeader("Storage");
    mAdapter.addItem(new SettingsItem.Builder()
                       .type(SettingsAdapter.TYPE_TEXT)
                       .title(getString(R.string.clear_cache))
                       .description(getString(R.string.clear_cache_description))
                       .id(STORAGE_CACHE)
                       .build());

    mAdapter.addHeader("Reading");
    mAdapter.addItem(new SettingsItem.Builder()
                       .type(SettingsAdapter.TYPE_CHECKBOX)
                       .title("Grayscale Read")
                       .description("Mark articles as read by grayscaling their preview images")
                       .selected(mPrefs.getBoolean(Constants.PREF_READ, false))
                       .id(READ_GRAYSCALE)
                       .build());

    mAdapter.addHeader("Credits");
    mAdapter.addItem(new SettingsItem.Builder()
                       .type(SettingsAdapter.TYPE_TEXT)
                       .title(getString(R.string.libraries))
                       .description("Libraries used in this app")
                       .id(CREDITS_LIBRARIES)
                       .build());
    mAdapter.addDivider();
    mAdapter.addItem(new SettingsItem.Builder()
                       .type(SettingsAdapter.TYPE_TEXT)
                       .title("About Dev")
                       .description("The guy responsible for making this app")
                       .id(CREDITS_DEV)
                       .build());
  }

  @Override
  public void onListItemClick(ListView list, View view, int position, long id)
  {
    SettingsItem mSetting = mAdapter.getItem(position);

    switch(mSetting.getID()) {
      case STORAGE_CACHE:
        boolean success = Utils.clearCache(getActivity());
        Toast.makeText(getActivity(), success ? getString(R.string.cache_success) : getString(R.string.cache_fail), Toast.LENGTH_SHORT).show();
        calculateCacheAsync();
        break;
      case READ_GRAYSCALE:
        // Update preference
        boolean grayscaleRead = mPrefs.getBoolean(Constants.PREF_READ, false);
        Editor mEditor = mPrefs.edit();
        mEditor.putBoolean(Constants.PREF_READ, !grayscaleRead);
        mEditor.commit();

        // Update setting in list
        mAdapter.getSetting(READ_GRAYSCALE).setSelected(!grayscaleRead);
        mAdapter.notifyDataSetChanged();
        break;
      case CREDITS_DEV:
        Dialogs.getAboutDialog(getActivity()).show();
        break;
      case CREDITS_LIBRARIES:
        Dialogs.getCreditsLibraryDialog(getActivity()).show();
        break;
    }
  }

  private void calculateCacheAsync()
  {
    new AsyncTask<Void, Void, Void>() {
      private String cacheSize = "Unknown";

      @Override
      protected void onPreExecute() {
        mAdapter.getSetting(STORAGE_CACHE).setDescription(getString(R.string.clear_cache_description) + "\n" + getString(R.string.calculating));
        mAdapter.notifyDataSetChanged();
      }

      @Override
      protected Void doInBackground(Void... params) {
        cacheSize = Utils.humanReadableByteCount(Utils.getTotalCacheSize(getActivity()), true);

        return null;
      }

      @Override
      protected void onPostExecute(Void result) {
        try {
          mAdapter.getSetting(STORAGE_CACHE).setDescription(getString(R.string.clear_cache_description) + "\n" + cacheSize);
          mAdapter.notifyDataSetChanged();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
  }
}




Java Source Code List

com.pkmmte.techdissected.ApplicationTest.java
com.pkmmte.techdissected.activity.ArticleActivity.java
com.pkmmte.techdissected.activity.MainActivity.java
com.pkmmte.techdissected.activity.SearchActivity.java
com.pkmmte.techdissected.adapter.AuthorAdapter.java
com.pkmmte.techdissected.adapter.CreditsLibraryAdapter.java
com.pkmmte.techdissected.adapter.FeedAdapter.java
com.pkmmte.techdissected.adapter.NavDrawerAdapter.java
com.pkmmte.techdissected.adapter.SettingsAdapter.java
com.pkmmte.techdissected.fragment.AboutFragment.java
com.pkmmte.techdissected.fragment.ArticleFragment.java
com.pkmmte.techdissected.fragment.FavoritesFragment.java
com.pkmmte.techdissected.fragment.FeedFragment.java
com.pkmmte.techdissected.fragment.SettingsFragment.java
com.pkmmte.techdissected.model.Author.java
com.pkmmte.techdissected.model.CreditsLibraryItem.java
com.pkmmte.techdissected.model.ListBuilder.java
com.pkmmte.techdissected.model.SettingsItem.java
com.pkmmte.techdissected.util.Base64DecoderException.java
com.pkmmte.techdissected.util.Base64.java
com.pkmmte.techdissected.util.Constants.java
com.pkmmte.techdissected.util.Dialogs.java
com.pkmmte.techdissected.util.IabException.java
com.pkmmte.techdissected.util.IabHelper.java
com.pkmmte.techdissected.util.IabResult.java
com.pkmmte.techdissected.util.Inventory.java
com.pkmmte.techdissected.util.Purchase.java
com.pkmmte.techdissected.util.RoundTransform.java
com.pkmmte.techdissected.util.Security.java
com.pkmmte.techdissected.util.SkuDetails.java
com.pkmmte.techdissected.util.Utils.java
com.pkmmte.techdissected.view.BakedBezierInterpolator.java
com.pkmmte.techdissected.view.CustomShareActionProvider.java
com.pkmmte.techdissected.view.FlowLayout.java
com.pkmmte.techdissected.view.HeaderGridView.java
com.pkmmte.techdissected.view.PkDrawerLayout.java
com.pkmmte.techdissected.view.PkScrollView.java
com.pkmmte.techdissected.view.PkSwipeRefreshLayout.java
com.pkmmte.techdissected.view.StickyScrollView.java
com.pkmmte.techdissected.view.SwipeProgressBar.java