package net.homelinux.penecoptero.android.citybikes.app;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
private SharedPreferences settings;
private String netnick;
private JSONObject bookmarks;
private JSONArray stations;
settings = ctx.getSharedPreferences(CityBikes.PREFERENCES_NAME,0);
netnick = settings.getString("network_name", "error");
if (netnick == "error"){
Log.i("CityBikes","FATAL: Guessing network name, this sucks");
NetworksDBAdapter nDbAdapter = new NetworksDBAdapter(ctx);
try{
nDbAdapter.load();
if (nDbAdapter.getStored().equals(new JSONArray("[]"))){
nDbAdapter.update();
}
JSONObject net = nDbAdapter.getNetworks(settings.getInt("network_id", -1));
netnick = net.getString("name");
Editor editor = settings.edit();
editor.putString("network_name", netnick);
editor.commit();
editor = null;
} catch (Exception e){
Log.i("CityBikes","Unable to load networks");
}
}
Log.i("CityBikes",netnick);
load();
}
public void setBookmarked(Station station,
boolean bookmarked)
throws Exception{
if (bookmarked){
try{
JSONArray tmp = new JSONArray("[]");
for (int i = 0; i < stations.length(); i++){
if (stations.getInt(i) != station.getHash()){
tmp.put(stations.getInt(i));
}
}
stations = tmp;
store();
}catch (Exception e){
}
}else{
if (!isBookmarked(station)){
stations.put(station.getHash());
store();
}
}
}
public int getIndex(Station station)
throws Exception{
for (int i = 0; i < stations.length(); i++){
if (stations.getInt(i) == station.getHash())
return i;
}
return -1;
}
try{
if (getIndex(station) < 0)
return false;
else
return true;
} catch (Exception e){
Log.i("CityBikes", "Error checking if bookmarked");
e.printStackTrace();
}
return false;
}
private void load()
throws Exception{
bookmarks = new JSONObject(settings.getString("bookmarks", "{}"));
try{
stations = bookmarks.getJSONArray(netnick);
} catch (Exception e){
stations = new JSONArray("[]");
}
}
Editor editor = settings.edit();
try {
bookmarks.put(netnick, stations);
editor.putString("bookmarks", bookmarks.toString());
editor.commit();
} catch (JSONException e) {
Log.i("CityBikes","Unable to store bookmarks");
e.printStackTrace();
}
editor = null;
}
}