package com.ad.cow.library;
import java.util.Date;
import java.util.HashMap;
import java.util.Map.Entry;
import org.json.JSONException;
import org.json.JSONObject;
import com.ad.cow.HomeActivity;
import com.ad.cow.LoginActivity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
private int _level = 0;
private float _percent = 0.0f;
private long _time = new Date().getTime();
private long _expTime = new Date().getTime();
private float _exp = 0.0f;
return _level;
}
return _percent;
}
return _time;
}
return _expTime;
}
return _exp;
}
this._level = level;
}
this._percent = percent;
}
this._time = time;
}
this._expTime = expTime;
}
public void setExp(
float exp) {
this._exp = exp;
}
private DatabaseHandler db;
private static GlobalVar instance;
static {
instance = new GlobalVar();
}
}
GlobalVar gv = GlobalVar.instance;
gv.LoadPreferences(context);
return gv;
}
db = new DatabaseHandler(context);
UserFunctions userFunctions = new UserFunctions();
if (userFunctions.isUserLoggedIn(context)) {
JSONObject json = userFunctions.getUserData(context);
try {
String KEY_SUCCESS = "success";
String KEY_ERROR = "error";
if (json.has(KEY_SUCCESS)) {
String res = json.getString(KEY_SUCCESS);
if (Integer.parseInt(res) == 1) {
JSONObject json_data = json.getJSONObject("data");
this._level = json_data.getInt("level");
this._percent = (float)json_data.getDouble("percent");
this._time = json_data.getLong("feed_time");
this._expTime = json_data.getLong("exp_time");
this._exp = (float)json_data.getDouble("exp");
save();
}
} else if (json.has(KEY_ERROR)) {
String res = json.getString(KEY_ERROR);
if (Integer.parseInt(res) == 1) {
userFunctions.logoutUser(context);
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
} else {
HashMap<String, String> data = db.getUserData();
if(!data.isEmpty()) {
this._level = Integer.parseInt(data.get("level"));
this._percent = Float.parseFloat(data.get("percent"));
this._time = Long.parseLong(data.get("feed_time"));
this._expTime = Long.parseLong(data.get("exp_time"));
this._exp = Float.parseFloat(data.get("exp"));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
return GlobalVar.instance;
}
HashMap<String, String> data = new HashMap<String, String>();
data.put("level", Integer.toString(_level));
data.put("percent", Float.toString(_percent));
data.put("feed_time", Long.toString(_time));
data.put("exp_time", Long.toString(_expTime));
data.put("exp", Float.toString(_exp));
db.saveUserData(data);
}
}