Android Open Source - DroidChineseCheckers Checkers Db Helper






From Project

Back to project page DroidChineseCheckers.

License

The source code is released under:

Apache License

If you think the Android project DroidChineseCheckers 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 2011 Federico Paolinelli//from www .j a v  a 2 s  .  c om
 * 
 * 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 com.whiterabbit.checkers.boards;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;


public class CheckersDbHelper extends CheckersStorage {
  public class ScoreRes{
    public int maxScore;
    public int minTime;
    public ScoreRes(int maxScore, int minTime) {
      super();
      this.maxScore = maxScore;
      this.minTime = minTime;
    }
  }
  
  
    public static final String PREF_NAME = "Preferences";
    public static final String LAST_BOARD_USED = "LastBoard";
    
    public CheckersDbHelper(Context context) {
        super(context);
    }
    
    public Cursor getSavedBoard(String name){
       Cursor res = mDb.query(BOARD_TABLE, new String[] {BOARD_ROW_ID,
                    BOARD_NAME_KEY,
                    BOARD_DUMP_KEY,
                    BOARD_SAVEDDATE_KEY,
                    BOARD_WIDTH_KEY,
                    BOARD_HEIGTH_KEY,
                    BOARD_SCORE_KEY,
                    BOARD_TIME_KEY}, BOARD_NAME_KEY + " = '" + name + "'", null, null, null, null);
        if(res != null){
            res.moveToFirst();
        }
        
        return res;
    }
    
    
    public boolean removeBoard(String name)
    {
        return mDb.delete(BOARD_TABLE, BOARD_NAME_KEY+ "= '" + name + "'", null) > 0;
    }
    
    public static void setLastBoardUsed(String boardName, Context ctx){
        int mode = Context.MODE_PRIVATE;
        SharedPreferences mySharedPreferences = ctx.getSharedPreferences(PREF_NAME, mode);        
        SharedPreferences.Editor editor = mySharedPreferences.edit();   
        editor.putString(LAST_BOARD_USED, boardName);
        editor.commit();
    }
    
    public static void resetLastBoard(Context ctx){
        setLastBoardUsed("", ctx);
    }
    
    /**
     * Returns the max score of the given board
     */
    public ScoreRes getBoardMaxScore(String name){
        Cursor res = mDb.query(BOARDSCORE_TABLE, new String[] {
                    BOARDSCORE_ROW_ID,
                    BOARDSCORE_NAME_KEY,
                    BOARDSCORE_MAXSCORE_KEY,
                    BOARDSCORE_MINTIME_KEY}, BOARDSCORE_NAME_KEY + " = '" + name + "'", null, null, null, null);
        
        if(res == null)
            return new ScoreRes(0,0);
        
        res.moveToFirst();
        if(res.getCount() <= 0){
            res.close();
            return new ScoreRes(0,0);
        }
        
        int result = res.getInt(BOARDSCORE_MAXSCORE_COLUMN);
        int minTime = res.getInt(BOARDSCORE_MINTIME_COLUMN);
        res.close();
        return new ScoreRes(result, minTime);
    }
    
    public void setBoardMaxScore(String name, long maxScore, long minTime){
        String where = BOARDSCORE_NAME_KEY + " = '" + name + "'";
        ContentValues contentValues = new ContentValues();
        contentValues.put(BOARDSCORE_NAME_KEY, name);
        contentValues.put(BOARDSCORE_MAXSCORE_KEY, maxScore);
        contentValues.put(BOARDSCORE_MINTIME_KEY, minTime);
        int updated = mDb.update(BOARDSCORE_TABLE, contentValues, where, null);
        
        if(updated <= 1){
            addBoardScore(name, Long.valueOf(maxScore), Long.valueOf(minTime));
        }
        
        
        
        
    }

}




Java Source Code List

com.whiterabbit.checkers.Constants.java
com.whiterabbit.checkers.PegDroidApplication.java
com.whiterabbit.checkers.PreferencesStore.java
com.whiterabbit.checkers.board.AndEngineBoard.java
com.whiterabbit.checkers.board.BoardCell.java
com.whiterabbit.checkers.board.FillableBoardCell.java
com.whiterabbit.checkers.board.FilledBoardCell.java
com.whiterabbit.checkers.board.LastMove.java
com.whiterabbit.checkers.boards.Board32Diamond.java
com.whiterabbit.checkers.boards.BoardAsymmetrical.java
com.whiterabbit.checkers.boards.BoardClassicEng.java
com.whiterabbit.checkers.boards.BoardClassicExtended.java
com.whiterabbit.checkers.boards.BoardClassic.java
com.whiterabbit.checkers.boards.BoardKind.java
com.whiterabbit.checkers.boards.BoardS.java
com.whiterabbit.checkers.boards.BoardStar.java
com.whiterabbit.checkers.boards.CheckersDbHelper.java
com.whiterabbit.checkers.boards.CheckersStorage.java
com.whiterabbit.checkers.boards.HoleMushroom.java
com.whiterabbit.checkers.boards.NineByNineBoard.java
com.whiterabbit.checkers.boards.SimpleCross1.java
com.whiterabbit.checkers.boards.SimpleCross2.java
com.whiterabbit.checkers.boards.SimpleCross3.java
com.whiterabbit.checkers.boards.SixBySixBoard.java
com.whiterabbit.checkers.boards.WieglebBoard.java
com.whiterabbit.checkers.exceptions.CantFillException.java
com.whiterabbit.checkers.ui.BackArrowSprite.java
com.whiterabbit.checkers.ui.BallSprite.java
com.whiterabbit.checkers.ui.BoardListElem.java
com.whiterabbit.checkers.ui.BoardSprite.java
com.whiterabbit.checkers.ui.BoardsListActivity.java
com.whiterabbit.checkers.ui.CheckersGameActivity.java
com.whiterabbit.checkers.ui.CheckersMainMenu.java
com.whiterabbit.checkers.ui.CheckersSpriteFactory.java
com.whiterabbit.checkers.ui.CheckersStallActivity.java
com.whiterabbit.checkers.ui.PegDroidPrefs.java
com.whiterabbit.checkers.util.Utils.java