CtrlMaintanence.java :  » Game » guessthenumber » com » saatcioglu » android » guessthenumber » Android Open Source

Android Open Source » Game » guessthenumber 
guessthenumber » com » saatcioglu » android » guessthenumber » CtrlMaintanence.java
/*
  Copyright 2009 Omer Saatcioglu
  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.

  $Author$
  $Revision$
  $LastChangedDate$
   
 */

package com.saatcioglu.android.guessthenumber;

import android.content.SharedPreferences;

public class CtrlMaintanence {

  static final public String PREF_NAME = "GuesstheNumber.pref";
  static final private String ELAPSED_TIME = "isDisplayElapsedTime";
  static final private String TOTAL_SHOTS = "isDisplayTotalShots";
  static final private String FLAG_LIST = "isDisplayFlagList";
  static final private String PLAYER_NAME = "strNameofPlayer";
  static final private String DIGIT_NUMBER = "nDigitNumber";
  static final private String MAX_SHOTS = "nMaxShots";
  static final private String MAX_TIME = "nMaxTime";

  static final public String QUICK_SETTINGS_NAME = "GuesstheNumber.QuickSettings";
  static final public String MASTER_SETTINGS_NAME = "GuesstheNumber.MasterSettings";
  static final private String GS_GAMESTARTED = "isGameStarted";
  static final private String GS_EDITORTEXT = "mEditorText";
  static final private String GS_STRINGS = "mStrings_";
  static final private String GS_COUNTER = "intCounter";
  static final private String GS_DIGITSPRESSED = "theDigitsPressed";
  static final private String GS_PREVBUTTONS = "arrintPrevButtons_";
  static final private String GS_ELAPSEDTIME = "lngElapsedTime_";
  static final private String GS_BUTTONENABLED = "arrisButtonEnabled_";
  static final private String GS_BUTTONSTATES = "arrintButtonStates";
  static final private String GS_SECRETNUMBER = "intSecretNumber";

  static final private String US_CURRENTLEVEL = "nCurrentLevel";

  private SharedPreferences.Editor editor;

  static private CtrlMaintanence ctrlMaintanence;

  static public CtrlMaintanence getInstance() {
    if (ctrlMaintanence == null) {
      ctrlMaintanence = new CtrlMaintanence();
    }
    return ctrlMaintanence;
  }

  private CtrlMaintanence() {
    // the class is singleton
  }

  public boolean updatePreferences() {
    
    if(SMBGuesstheNumber.generalSettings == null){
      return false;
    }

    final int nOldDigits;
    final int nOldMaxShots;
    final long nOldMaxTime;

    SMBGuesstheNumber.bShowElapsedTime = SMBGuesstheNumber.generalSettings
        .getBoolean(ELAPSED_TIME, true);
    SMBGuesstheNumber.bShowTotalShots = SMBGuesstheNumber.generalSettings
        .getBoolean(TOTAL_SHOTS, true);
    SMBGuesstheNumber.bDisplayFlagList = SMBGuesstheNumber.generalSettings
        .getBoolean(FLAG_LIST, true);
    SMBGuesstheNumber.strPlayerName = SMBGuesstheNumber.generalSettings
        .getString(PLAYER_NAME, "Human");

    nOldDigits = SMBGuesstheNumber.nDigits;
    nOldMaxShots = SMBGuesstheNumber.nMaxShots;
    nOldMaxTime = SMBGuesstheNumber.nMaxTime;

    SMBGuesstheNumber.nDigits = Integer
        .parseInt(SMBGuesstheNumber.generalSettings.getString(
            DIGIT_NUMBER, "3"));
    SMBGuesstheNumber.nMaxShots = Integer
        .parseInt(SMBGuesstheNumber.generalSettings.getString(
            MAX_SHOTS, "0"));
    try {
      SMBGuesstheNumber.nMaxTime = Integer
          .parseInt(SMBGuesstheNumber.generalSettings.getString(MAX_TIME,
              "0"));
    } catch (NumberFormatException e) {
      SMBGuesstheNumber.nMaxTime = 0;
    }

    if (nOldDigits == SMBGuesstheNumber.nDigits
        || nOldMaxShots == SMBGuesstheNumber.nMaxShots
        || nOldMaxTime == SMBGuesstheNumber.nMaxTime) {
      return true;
    } else {
      return false;
    }

  }

  public void updatePlayerName(String strPlayerName) {
    if(SMBGuesstheNumber.generalSettings == null){
      return;
    }
    editor = SMBGuesstheNumber.generalSettings.edit();
    editor.putString(PLAYER_NAME, strPlayerName);
    editor.commit();
    SMBGuesstheNumber.strPlayerName = strPlayerName;
  }

  public void getCurrentLevel() {
    if(SMBGuesstheNumber.generalSettings == null){
      return;
    }
    SMBGuesstheNumber.nCurrentLevel = Integer
        .parseInt(SMBGuesstheNumber.generalSettings.getString(
            US_CURRENTLEVEL, "0"));
  }

  public void setCurrentLevel(int nCurrentLevel) {
    if(SMBGuesstheNumber.generalSettings == null){
      return;
    }
    editor = SMBGuesstheNumber.generalSettings.edit();
    editor.putString(US_CURRENTLEVEL, Integer.toString(nCurrentLevel));
    editor.commit();
  }

  public void resumetheGame() {
    int i;
    if(SMBGuesstheNumber.gameSettings == null){
      return;
    }

    SMBGuesstheNumber.isGameStarted = SMBGuesstheNumber.gameSettings.getBoolean(
        GS_GAMESTARTED, false);
    if (SMBGuesstheNumber.isGameStarted) {
      SMBGuesstheNumber.mEditorText = SMBGuesstheNumber.gameSettings.getString(
          GS_EDITORTEXT, "");
      SMBGuesstheNumber.mStrings.clear();
      for (i = 0; SMBGuesstheNumber.gameSettings.contains(GS_STRINGS + Integer.toString(i)); i++) {
        SMBGuesstheNumber.mStrings.add(SMBGuesstheNumber.gameSettings.getString(
            GS_STRINGS + Integer.toString(i), ""));
      }
      SMBGuesstheNumber.intCounter = SMBGuesstheNumber.gameSettings.getInt(GS_COUNTER, 0);
      SMBGuesstheNumber.theDigitsPressed = SMBGuesstheNumber.gameSettings.getInt(
          GS_DIGITSPRESSED, 0);
      if (SMBGuesstheNumber.arrintPrevButtons == null) {
        SMBGuesstheNumber.arrintPrevButtons = new int[SMBGuesstheNumber.nActiveDigits];
      }
      for (i = 0; i < SMBGuesstheNumber.nActiveDigits; i++) {
        SMBGuesstheNumber.arrintPrevButtons[i] = SMBGuesstheNumber.gameSettings.getInt(
            GS_PREVBUTTONS + Integer.toString(i), 0);
      }
      SMBGuesstheNumber.lngElapsedTime = SMBGuesstheNumber.gameSettings.getLong(
          GS_ELAPSEDTIME, 0);
      for (i = 0; SMBGuesstheNumber.gameSettings.contains(GS_BUTTONENABLED
          + Integer.toString(i)); i++) {
        SMBGuesstheNumber.arrisBtnNumberEnabled[i] = SMBGuesstheNumber.gameSettings
            .getBoolean(GS_BUTTONENABLED + Integer.toString(i),
                true);
      }

      for (i = 0; SMBGuesstheNumber.gameSettings.contains(GS_BUTTONSTATES
          + Integer.toString(i)); i++) {
        SMBGuesstheNumber.arrintBtnNumberStates[i] = SMBGuesstheNumber.gameSettings
            .getInt(GS_BUTTONSTATES + Integer.toString(i), 0);
      }
      SMBGuesstheNumber.intSecretNumber = SMBGuesstheNumber.gameSettings.getInt(GS_SECRETNUMBER, 0);
    }
  }

  public void savetheGame() {

    if(SMBGuesstheNumber.gameSettings == null){
      return;
    }
    editor = SMBGuesstheNumber.gameSettings.edit();
    editor.clear();
    if (SMBGuesstheNumber.isGameStarted) {
      int i;
      editor.putBoolean(GS_GAMESTARTED, SMBGuesstheNumber.isGameStarted);
      editor.putString(GS_EDITORTEXT, SMBGuesstheNumber.mEditorText);
      i = 0;
      for (String tmpString : SMBGuesstheNumber.mStrings) {
        editor.putString(GS_STRINGS + Integer.toString(i), tmpString);
        i++;
      }
      editor.putInt(GS_COUNTER, SMBGuesstheNumber.intCounter);
      editor.putInt(GS_DIGITSPRESSED, SMBGuesstheNumber.theDigitsPressed);
      for (i = 0; i < SMBGuesstheNumber.nActiveDigits; i++) {
        editor.putInt(GS_PREVBUTTONS + Integer.toString(i),
            SMBGuesstheNumber.arrintPrevButtons[i]);
      }
      editor.putLong(GS_ELAPSEDTIME, SMBGuesstheNumber.lngElapsedTime);
      for (i = 0; i < SMBGuesstheNumber.NBUTTON_ENABLED; i++) {
        editor.putBoolean(GS_BUTTONENABLED + Integer.toString(i),
            SMBGuesstheNumber.arrisBtnNumberEnabled[i]);
      }
      for (i = 0; i < SMBGuesstheNumber.NBUTTON_STATE; i++) {
        editor.putInt(GS_BUTTONSTATES + Integer.toString(i),
            SMBGuesstheNumber.arrintBtnNumberStates[i]);
      }
      editor.putInt(GS_SECRETNUMBER, SMBGuesstheNumber.intSecretNumber);
    }
    editor.commit();
  }

  public String formatShots(int nShots) {
    String strShots;
    if (nShots == 0) {
      strShots = "Unlimited";
    } else {
      strShots = Integer.toString(nShots);
    }
    return strShots;
  }

  public String formatTime(long nTime) {
    String strTime;
    if (nTime == 0) {
      strTime = "None";
    } else {
      if (nTime / 60 > 0) {
        strTime = Long.toString(nTime / 60);
      } else {
        strTime = "00";
      }
      strTime += ":";
      if (nTime % 60 < 10) {
        strTime += "0";
      }
      strTime += Long.toString(nTime % 60);
    }
    return strTime;
  }
  
  public String formatLevels(int nLevel){
    String strLevel = new String();
    strLevel = Integer.toString(nLevel/25 +1);
    if(nLevel % 25 != 0){
      strLevel = strLevel
      + "."
      + Integer.toString((nLevel%25 -1)/6 + 1)
      + "."
      + Integer.toString((nLevel%25 -1)%6 + 1);
    }
    else{
      strLevel = strLevel
      + " - Warm Up";
    }
    return strLevel;
  }
}
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.