package net.homelinux.penecoptero.android.citybikes.app;
import java.util.List;
import java.util.Locale;
import net.homelinux.penecoptero.android.citybikes.utils.CircleHelper;
import org.json.JSONArray;
import org.json.JSONObject;
import android.content.Context;
import android.content.SharedPreferences;
import android.location.Address;
import android.location.Geocoder;
import android.util.Log;
import com.google.android.maps.GeoPoint;
public static final String NETWORKS_FEED = "http://api.citybik.es/networks.json";
private RESTHelper mRESTHelper;
private String RAWNetworks;
private JSONArray networks;
private Context mCtx;
private SharedPreferences settings;
mCtx = ctx;
this.mRESTHelper = new RESTHelper(false, null, null);
settings = this.mCtx.getSharedPreferences(CityBikes.PREFERENCES_NAME,0);
}
public JSONArray
update()
throws Exception{
RAWNetworks = mRESTHelper.restGET(NETWORKS_FEED);
networks = new JSONArray(RAWNetworks);
this.store();
return networks;
}
public JSONArray
getStored()
throws Exception{
this.load();
return networks;
}
public void load()
throws Exception{
RAWNetworks = settings.getString("networks", "[]");
networks = new JSONArray(RAWNetworks);
}
SharedPreferences.Editor editor = settings.edit();
editor.putString("networks", this.RAWNetworks);
editor.commit();
}
this.load();
Geocoder oracle = new Geocoder(mCtx,Locale.ENGLISH);
List <Address> results = oracle.getFromLocation(center.getLatitudeE6()/1E6, center.getLongitudeE6()/1E6,5);
for (int i = 0; i < results.size(); i++){
}
return null;
}
this.load();
if (method==0)
return getAutomaticNetworkMethod0(center);
else if (method==1)
return getAutomaticNetworkMethod1(center);
else
return null;
}
this.load();
for (int i = 0; i < networks.length(); i++){
JSONObject network = networks.getJSONObject(i);
GeoPoint netCenter = new GeoPoint(network.getInt("lat"),network.getInt("lng"));
if (CircleHelper.isOnCircle(center,netCenter, network.getInt("radius"))){
return network;
}else{
}
}
return null;
}
return getAutomaticNetworkMethod0(center);
}
JSONObject net = getAutomaticNetwork(center);
if (net != null){
if(settings.getInt("network_id", -1)!= net.getInt("id")){
setManualNetwork(net.getInt("id"));
}
}
}
public JSONObject
getNetworks(
int id)
throws Exception{
return networks.getJSONObject(id);
}
String url = networks.getJSONObject(id).getString("url");
String name = networks.getJSONObject(id).getString("name");
SharedPreferences.Editor editor = settings.edit();
editor.putString("network_name", name);
editor.putString("network_url", url);
editor.putBoolean("reload_network", true);
editor.putInt("network_id", id);
editor.commit();
}
int net_id = settings.getInt("network_id", -1);
return net_id!=-1;
}
}