Preferences.java :  » App » mycollectionpro » com » app » my_collection » Android Open Source

Android Open Source » App » mycollectionpro 
mycollectionpro » com » app » my_collection » Preferences.java
/*****************************************************************************
 * Name..........: Preferences.java
 * 
 * Description...: Manages application preferences
 *
 * Author........: Timothy A. DeWees
 * 
 * Version.......: 1.0
 * 
 * Change History: 1.0 (Timothy A. DeWees - 01/21/2009)
 *                     Initial writing
 *****************************************************************************/
package com.app.my_collection;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.Spinner;

public class Preferences extends Activity {

  private CheckBox mShowBook = null;
  private CheckBox mShowCD = null;
  private CheckBox mShowGame = null;
  private CheckBox mShowMovie = null;
  private CheckBox mShowLoan = null;
  private CheckBox mAutoSave = null;
  private Spinner mDefaultTab = null;
  String _book = "";
  String _cd = "";
  String _game = "";
  String _movie = "";
  String _loan = "";
  String _defaultTab = "";
  String _region = "";
  String _autoSave = "";
  public static final String KEY_SHOW_BOOK_TAB = "KEY_SHOW_BOOK_TAB";
  public static final String KEY_SHOW_CD_TAB = "KEY_SHOW_CD_TAB";
  public static final String KEY_SHOW_GAME_TAB = "KEY_SHOW_GAME_TAB";
  public static final String KEY_SHOW_MOVIE_TAB = "KEY_SHOW_MOVIE_TAB";
  public static final String KEY_SHOW_LOAN_TAB = "KEY_SHOW_LOAN_TAB";
  public static final String KEY_DEFAULT_TAB = "KEY_DEFAULT_TAB";
  public static final String KEY_REGION = "KEY_REGION";
  public static final String KEY_AUTO_SAVE_AFTER_SEARCH = "KEY_AUTO_SAVE_AFTER_SEARCH";
  public static final String PREFS_NAME = "MyCollectionPrefsFile";
  private static final int MENU_SAVE_ID = Menu.FIRST;
  private MenuItem saveMenu = null;
  private static final int MENU_CANCEL_ID = Menu.FIRST + 1;
  private MenuItem cancelMenu = null;
    
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Setup activity
    setContentView(R.layout.preferences);
    setTitle(R.string.title_preferences);
    
    //Setup CheckBox widgets
    mShowBook = (CheckBox)findViewById(R.id.show_book);
    mShowCD = (CheckBox)findViewById(R.id.show_cd);
    mShowGame = (CheckBox)findViewById(R.id.show_game);
    mShowMovie = (CheckBox)findViewById(R.id.show_movie);
    mShowLoan = (CheckBox)findViewById(R.id.show_rental);
    mAutoSave = (CheckBox)findViewById(R.id.auto_save);
    
    //Setup default tab Spinner
    mDefaultTab = (Spinner)findViewById(R.id.default_tab);
    ArrayAdapter<CharSequence> defaultTabAdapter = ArrayAdapter
      .createFromResource(this, R.array.tabs, android.R.layout.simple_spinner_item);
    defaultTabAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mDefaultTab.setAdapter(defaultTabAdapter);
    
    //Pull preference extras (used for screen flip out)
    Bundle extras = getIntent().getExtras();
    if(extras != null) {
      _book = (extras.containsKey(KEY_SHOW_BOOK_TAB))?extras.getString(KEY_SHOW_BOOK_TAB):"";
      _cd = (extras.containsKey(KEY_SHOW_CD_TAB))?extras.getString(KEY_SHOW_CD_TAB):"";
      _game = (extras.containsKey(KEY_SHOW_GAME_TAB))?extras.getString(KEY_SHOW_GAME_TAB):"";
      _movie = (extras.containsKey(KEY_SHOW_MOVIE_TAB))?extras.getString(KEY_SHOW_MOVIE_TAB):"";
      _loan = (extras.containsKey(KEY_SHOW_LOAN_TAB))?extras.getString(KEY_SHOW_LOAN_TAB):"";
      _defaultTab = (extras.containsKey(KEY_DEFAULT_TAB))?extras.getString(KEY_DEFAULT_TAB):"";
      _autoSave = (extras.containsKey(KEY_AUTO_SAVE_AFTER_SEARCH))?extras.getString(KEY_AUTO_SAVE_AFTER_SEARCH):"";
    }
    
    //Populate the activity with data from preferences
    fillData();
    
  }//End of method protected void onCreate(Bundle)
  
  /**
   * Populates the widgets with their values from the preferences
   * system.
   */
  private void fillData() {
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    //Book
    if(_book.equals("")) {
      _book = (settings.getBoolean(KEY_SHOW_BOOK_TAB, true))?"TRUE":"FALSE";
    }
    mShowBook.setChecked((_book.toUpperCase().equals("TRUE")?true:false));
    //CD
    if(_cd.equals("")) {
      _cd = (settings.getBoolean(KEY_SHOW_CD_TAB, true))?"TRUE":"FALSE";
    }
    mShowCD.setChecked((_cd.toUpperCase().equals("TRUE")?true:false));
    //Game
    if(_game.equals("")) {
      _game = (settings.getBoolean(KEY_SHOW_GAME_TAB, true))?"TRUE":"FALSE";
    }
    mShowGame.setChecked((_game.toUpperCase().equals("TRUE")?true:false));
    //Movie
    if(_movie.equals("")) {
      _movie = (settings.getBoolean(KEY_SHOW_MOVIE_TAB, true))?"TRUE":"FALSE";
    }
    mShowMovie.setChecked((_movie.toUpperCase().equals("TRUE")?true:false));
    //Rental
    if(_loan.equals("")) {
      _loan = (settings.getBoolean(KEY_SHOW_LOAN_TAB, true))?"TRUE":"FALSE";
    }
    mShowLoan.setChecked((_loan.toUpperCase().equals("TRUE")?true:false));
    //AutoSave
    if(_autoSave.equals("")) {
      _autoSave = (settings.getBoolean(KEY_AUTO_SAVE_AFTER_SEARCH, true))?"TRUE":"FALSE";
    }
    mAutoSave.setChecked((_autoSave.toUpperCase().equals("TRUE")?true:false));
    //Default tab
    if(_defaultTab.equals("")) {
      _defaultTab = settings.getString(KEY_DEFAULT_TAB, "BOOK").toUpperCase();
    }
    int defaultIndex = 0;
    for(int i = 0; i < mDefaultTab.getCount(); i++) {
      if(mDefaultTab.getItemAtPosition(i).toString().toUpperCase().equals(_defaultTab)) {
        defaultIndex = i;
        break;
      }
    }
    mDefaultTab.setSelection(defaultIndex);
    
  }//End of method private void fillData()
  
  @Override
  protected void onPause() {
    super.onPause();
    
    Intent editor = getIntent();
    
    _book = (mShowBook.isChecked())?"TRUE":"FALSE";
    _cd = (mShowCD.isChecked())?"TRUE":"FALSE";
    _game = (mShowGame.isChecked())?"TRUE":"FALSE";
    _movie = (mShowMovie.isChecked())?"TRUE":"FALSE";
    _loan = (mShowLoan.isChecked())?"TRUE":"FALSE";
    _autoSave = (mAutoSave.isChecked())?"TRUE":"FALSE";
    _defaultTab = mDefaultTab.getSelectedItem().toString().toUpperCase();
    editor.putExtra(KEY_SHOW_BOOK_TAB, _book);
    editor.putExtra(KEY_SHOW_CD_TAB, _cd);
    editor.putExtra(KEY_SHOW_GAME_TAB, _game);
    editor.putExtra(KEY_SHOW_MOVIE_TAB, _movie);
    editor.putExtra(KEY_SHOW_LOAN_TAB, _loan);
    editor.putExtra(KEY_DEFAULT_TAB, _defaultTab);
    editor.putExtra(KEY_AUTO_SAVE_AFTER_SEARCH, _autoSave);
    SavePreferences();
    
  }//End of method protected void onPause()
  
  /**
   * Saves the preferences then exits the activity
   */
  private void SavePreferences() {
    if(validate()) {
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putBoolean(KEY_SHOW_BOOK_TAB, mShowBook.isChecked());
      editor.putBoolean(KEY_SHOW_CD_TAB, mShowCD.isChecked());
      editor.putBoolean(KEY_SHOW_GAME_TAB, mShowGame.isChecked());
      editor.putBoolean(KEY_SHOW_MOVIE_TAB, mShowMovie.isChecked());
      editor.putBoolean(KEY_SHOW_LOAN_TAB, mShowLoan.isChecked());
      editor.putBoolean(KEY_AUTO_SAVE_AFTER_SEARCH, mAutoSave.isChecked());
      editor.putString(KEY_DEFAULT_TAB, mDefaultTab.getSelectedItem().toString());
      editor.commit();
    }
  }//End of method private void SavePreferences
  
  /**
   * Validates that the user does not disable a tab and set it to be the default
   * @return
   */
  private boolean validate() {
    boolean valid = true;
    _book = (mShowBook.isChecked())?"TRUE":"FALSE";
    _cd = (mShowCD.isChecked())?"TRUE":"FALSE";
    _game = (mShowGame.isChecked())?"TRUE":"FALSE";
    _movie = (mShowMovie.isChecked())?"TRUE":"FALSE";
    _loan = (mShowLoan.isChecked())?"TRUE":"FALSE";
    _defaultTab = mDefaultTab.getSelectedItem().toString().toUpperCase();
    if(_defaultTab.equals("BOOK") && _book.equals("FALSE")) {
      valid = false;
      NotificationHelper.showOkAlert(this, "INVALID PREFERENCE"
        ,"You cannot hide the Book tab and select it as your default tab"
        , new OnClickListener() {
          
          @Override
          public void onClick(DialogInterface dialog, int which) {
            //We don't need to do anything
          }
        
        } );
    } else if(_defaultTab.equals("CD") && _cd.equals("FALSE")) {
      valid = false;
      NotificationHelper.showOkAlert(this, "INVALID PREFERENCE"
          ,"You cannot hide the CD tab and select it as your default tab"
          , new OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
              //We don't need to do anything
            }
          
          } );
    } else if(_defaultTab.equals("GAME") && _game.equals("FALSE")) {
      valid = false;
      NotificationHelper.showOkAlert(this, "INVALID PREFERENCE"
          ,"You cannot hide the Game tab and select it as your default tab"
          , new OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
              //We don't need to do anything
            }
          
          } );
    } else if(_defaultTab.equals("MOVIE") && _movie.equals("FALSE")) {
      valid = false;
      NotificationHelper.showOkAlert(this, "INVALID PREFERENCE"
          ,"You cannot hide the Movie tab and select it as your default tab"
          , new OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
              //We don't need to do anything
            }
          
          } );
    } else if(_defaultTab.equals("LOAN") && _loan.equals("FALSE")) {
      valid = false;
      NotificationHelper.showOkAlert(this, "INVALID PREFERENCE"
          ,"You cannot hide the Rental tab and select it as your default tab"
          , new OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
              //We don't need to do anything
            }
          
          } );
    }
    return valid;
  }
}//End of class Preferences
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.