Android Open Source - it.alcacoop.fourinaline App Data Manager






From Project

Back to project page it.alcacoop.fourinaline.

License

The source code is released under:

GNU General Public License

If you think the Android project it.alcacoop.fourinaline 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 it.alcacoop.fourinaline.utils;
/*from   w  w  w . j  av  a2s . c  o  m*/
import it.alcacoop.fourinaline.FourInALine;

import java.util.ArrayList;
import java.util.HashMap;

import com.badlogic.gdx.utils.Json;

public class AppDataManager {

  private HashMap<String, String> app_data;
  private static AppDataManager instance;

  public static AppDataManager getInstance() {
    if (instance == null)
      instance = new AppDataManager();
    return instance;
  }


  private AppDataManager() {
    app_data = new HashMap<String, String>();
  }

  public byte[] getBytes() {
    app_data.put("singleboard", FourInALine.Instance.gameOptionPrefs.getString("SINGLEBOARD", "0"));
    app_data.put("multiboard", FourInALine.Instance.gameOptionPrefs.getString("MULTIBOARD", "0"));

    app_data.put("sound", FourInALine.Instance.optionPrefs.getString("SOUND", "Yes"));
    app_data.put("music", FourInALine.Instance.optionPrefs.getString("MUSIC", "Yes"));
    app_data.put("vibration", FourInALine.Instance.optionPrefs.getString("VIBRATION", "Yes"));

    app_data.put("level", FourInALine.Instance.matchOptionPrefs.getString("LEVEL", "Yes"));
    app_data.put("matchTo", FourInALine.Instance.matchOptionPrefs.getString("MATCHTO", "Yes"));
    app_data.put("variant", FourInALine.Instance.matchOptionPrefs.getString("VARIANT", "Yes"));

    app_data.put("opponents", AchievementsManager.getInstance().prefs.getString("OPPONENTS", "{}"));
    Json json = new Json();
    System.out.println("SAVING DATA: " + json.toJson(app_data));
    return json.toJson(app_data).getBytes();
  }


  @SuppressWarnings("unchecked")
  public void loadState(byte[] bytes) {
    Json json = new Json();
    if (bytes.length == 0)
      return;
    app_data = json.fromJson(HashMap.class, new String(bytes));
    if (app_data == null)
      return;
    savePrefs();
  }


  @SuppressWarnings("unchecked")
  public byte[] resolveConflict(byte[] local, byte[] remote) {
    System.out.println("DATA: CONFLICT!!");
    Json jLocal = new Json();
    HashMap<String, String> hLocal = jLocal.fromJson(HashMap.class, new String(local));
    Json jRemote = new Json();
    HashMap<String, String> hRemote = jRemote.fromJson(HashMap.class, new String(remote));

    double single = Math.max(Double.parseDouble(hLocal.get("singleboard")), Double.parseDouble(hRemote.get("singleboard")));
    double multi = Math.max(Double.parseDouble(hLocal.get("multiboard")), Double.parseDouble(hRemote.get("multiboard")));

    app_data.put("singleboard", single + "");
    app_data.put("multiboard", multi + "");

    app_data.put("sound", hRemote.get("sound"));
    app_data.put("music", hRemote.get("music"));
    app_data.put("vibration", hRemote.get("vibration"));

    app_data.put("level", hRemote.get("level"));
    app_data.put("matchTo", hRemote.get("matchTo"));
    app_data.put("variant", hRemote.get("variant"));

    ArrayList<String> local_played_list = jLocal.fromJson(ArrayList.class, hLocal.get("opponents"));
    ArrayList<String> remote_played_list = jRemote.fromJson(ArrayList.class, hRemote.get("opponents"));
    if (local_played_list.size() >= remote_played_list.size())
      app_data.put("opponents", hLocal.get("opponents"));
    else
      app_data.put("opponents", hRemote.get("opponents"));

    savePrefs();
    return new Json().toJson(app_data).getBytes();
  }


  private void savePrefs() {
    System.out.println("APPSTATE DATA: savePrefs");
    System.out.println("DATA RESTORING: " + app_data.toString());
    FourInALine.Instance.gameOptionPrefs.putString("SINGLEBOARD", app_data.get("singleboard"));
    FourInALine.Instance.gameOptionPrefs.putString("MULTIBOARD", app_data.get("multiboard"));

    FourInALine.Instance.optionPrefs.putString("SOUND", app_data.get("sound"));
    FourInALine.Instance.optionPrefs.putString("MUSIC", app_data.get("music"));
    FourInALine.Instance.optionPrefs.putString("VIBRATION", app_data.get("vibration"));

    FourInALine.Instance.matchOptionPrefs.putString("LEVEL", app_data.get("level"));
    FourInALine.Instance.matchOptionPrefs.putString("MATCHTO", app_data.get("matchTo"));
    FourInALine.Instance.matchOptionPrefs.putString("VARIANT", app_data.get("variant"));

    AchievementsManager.getInstance().prefs.putString("OPPONENTS", app_data.get("opponents"));

    FourInALine.Instance.gameOptionPrefs.flush();
    FourInALine.Instance.matchOptionPrefs.flush();
    FourInALine.Instance.optionPrefs.flush();
    AchievementsManager.getInstance().prefs.flush();

    if (FourInALine.Instance.snd != null)
      FourInALine.Instance.snd.playBGMusic(); // WA: SHOULD SYNC BGMUSIC STATE
  }
}




Java Source Code List

android.UnusedStub.java
it.alcacoop.fourinaline.BaseGServiceApplication.java
it.alcacoop.fourinaline.FourInALineDesktop.java
it.alcacoop.fourinaline.FourInALine.java
it.alcacoop.fourinaline.GServiceApplication.java
it.alcacoop.fourinaline.GServiceInterface.java
it.alcacoop.fourinaline.MainActivity.java
it.alcacoop.fourinaline.NativeFunctions.java
it.alcacoop.fourinaline.PrivateDataManager.java
it.alcacoop.fourinaline.PurchaseActivity.java
it.alcacoop.fourinaline.SoundManager.java
it.alcacoop.fourinaline.actors.BoardImage.java
it.alcacoop.fourinaline.actors.Board.java
it.alcacoop.fourinaline.actors.ChatBox.java
it.alcacoop.fourinaline.actors.Checker.java
it.alcacoop.fourinaline.actors.FixedButtonGroup.java
it.alcacoop.fourinaline.actors.IconButton.java
it.alcacoop.fourinaline.actors.ParticleEffectActor.java
it.alcacoop.fourinaline.actors.PlayerBlock.java
it.alcacoop.fourinaline.actors.UIDialog.java
it.alcacoop.fourinaline.billingutils.Base64DecoderException.java
it.alcacoop.fourinaline.billingutils.Base64.java
it.alcacoop.fourinaline.billingutils.IabException.java
it.alcacoop.fourinaline.billingutils.IabHelper.java
it.alcacoop.fourinaline.billingutils.IabResult.java
it.alcacoop.fourinaline.billingutils.Inventory.java
it.alcacoop.fourinaline.billingutils.Purchase.java
it.alcacoop.fourinaline.billingutils.Security.java
it.alcacoop.fourinaline.billingutils.SkuDetails.java
it.alcacoop.fourinaline.client.GwtLauncher.java
it.alcacoop.fourinaline.fsm.FSM.java
it.alcacoop.fourinaline.gservice.GServiceClient.java
it.alcacoop.fourinaline.gservice.GServiceCookieMonster.java
it.alcacoop.fourinaline.gservice.GServiceMessages.java
it.alcacoop.fourinaline.gservice.GServiceNetHandler.java
it.alcacoop.fourinaline.layers.BaseScreen.java
it.alcacoop.fourinaline.layers.GameScreen.java
it.alcacoop.fourinaline.layers.MatchOptionsScreen.java
it.alcacoop.fourinaline.layers.MenuScreen.java
it.alcacoop.fourinaline.layers.OptionsScreen.java
it.alcacoop.fourinaline.layers.SplashScreen.java
it.alcacoop.fourinaline.logic.AIExecutor.java
it.alcacoop.fourinaline.logic.MatchState.java
it.alcacoop.fourinaline.util.Base64DecoderException.java
it.alcacoop.fourinaline.util.Base64.java
it.alcacoop.fourinaline.util.GServiceGameHelper.java
it.alcacoop.fourinaline.util.GameHelperUtils.java
it.alcacoop.fourinaline.utils.AchievementsManager.java
it.alcacoop.fourinaline.utils.AppDataManager.java
it.alcacoop.fourinaline.utils.ELORatingManager.java
org.gojul.fourinaline.model.AlphaBeta.java
org.gojul.fourinaline.model.DefaultEvalScore.java
org.gojul.fourinaline.model.EvalScore.java
org.gojul.fourinaline.model.GameModel.java