package com.ved.musicmapapp.providers;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.util.Log;
import com.ved.musicmapapp.Objects.Artist;
import com.ved.musicmapapp.Objects.Song;
import com.ved.musicmapapp.Objects.User;
import com.ved.musicmapapp.utils.LikeDataSort;
import com.ved.musicmapapp.utils.Statics;
public class GetLikeDataTask extends AsyncTask<Void, Void, String> {
private Activity activity;
private int totalLike = 0;
private String TAG = "";
// private ProgressDialog mLoadingDialog;
private SharedPreferences prefs;
private Editor edt;
public GetLikeDataTask(Activity activity) {
Log.i("check","In GetLikeDataTask");
this.activity = activity;
// mLoadingDialog = new ProgressDialog(activity);
// mLoadingDialog.setMessage("Getting data...");
// mLoadingDialog.setCancelable(false);
prefs = activity
.getSharedPreferences("MUSIC_MAP", Context.MODE_PRIVATE);
edt = prefs.edit();
}
@Override
protected void onPreExecute() {
if (null == Statics.likeDatas)
Statics.likeDatas = new ArrayList<Artist>();
}
@Override
protected String doInBackground(Void... params) {
try {
if (!Statics.isNetworkConnected(activity)) {
getData(prefs.getString("LAST_LIKEDATA", ""));
return null;
}
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 20000;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
int timeoutSocket = 20000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost(Statics.WS_PATH
+ "getLikeData.php");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("fbid", prefs
.getString("FB_ID", "")));
httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
// get the response content as a string
InputStream instream = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(instream, "UTF-8"));
String line;
StringBuilder sb = new StringBuilder("");
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
// if(prefs.getString("LAST_LIKEDATA", "").length()>0 &&
// sb.toString().trim().equals(prefs.getString("LAST_LIKEDATA",
// ""))) return "CANCEL";
edt.putString("LAST_LIKEDATA", sb.toString().trim());
edt.commit();
getData(sb.toString());
}
} catch (UnsupportedEncodingException e1) {
Log.e("Exception", "" + e1.getMessage());
return e1.getMessage();
// e1.printStackTrace();
} catch (ClientProtocolException e1) {
Log.e("ClientProtocolException", "" + e1.getMessage());
return e1.getMessage();
// e1.printStackTrace();
} catch (IOException e1) {
Log.e("IOException", "" + e1.getMessage());
e1.printStackTrace();
return e1.getMessage();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "OK";
}
private void getData(String json) throws JSONException {
Statics.likeDatas.clear();
JSONArray jArray = new JSONArray(json);
for (int i = 0; i < jArray.length(); i++) {
if (isCancelled()) {
Log.i("CANCELLED", TAG);
Statics.likeDatas.clear();
break;
}
JSONObject job = jArray.getJSONObject(i);
Artist art = new Artist();
art.setID(job.getInt("id"));
art.setName(job.getString("name_cleaned"));
art.setAvatar(job.getString("avatar"));
art.setLikeTime(job.getLong("likeTime"));
JSONArray sArray = job.getJSONArray("songs");
ArrayList<Song> songs = new ArrayList<Song>();
for (int j = 0; j < sArray.length(); j++) {
JSONObject sjob = sArray.getJSONObject(j);
Song s = new Song();
s.setTitle(sjob.getString("title_cleaned"));
JSONArray uArray = sjob.getJSONArray("users");
ArrayList<User> users = new ArrayList<User>();
for (int k = 0; k < uArray.length(); k++) {
JSONObject ujob = uArray.getJSONObject(k);
User u = new User();
u.setFbid(ujob.getString("fbid"));
users.add(u);
}
s.setUserLiked(users);
songs.add(s);
totalLike++;
}
art.setSongs(songs);
Statics.likeDatas.add(art);
}
if (null != Statics.curUser) {
Statics.curUser.setTotalLike(totalLike);
}
}
@Override
protected void onPostExecute(String result) {
Collections.sort(Statics.likeDatas, new LikeDataSort());
onSuccess();
}
public void onSuccess() {
}
}