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.HashMap;
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.apache.http.util.EntityUtils;
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.util.Log;
import com.ved.musicmapapp.Objects.Artist;
import com.ved.musicmapapp.Objects.Song;
import com.ved.musicmapapp.Objects.User;
import com.ved.musicmapapp.adapters.DBAdapter;
import com.ved.musicmapapp.utils.LikeDataSort;
import com.ved.musicmapapp.utils.Statics;
public class GetCurrentUserIDTask extends MyTask {
private Activity activity;
private String fbid;
private String follow_fbid;
private SharedPreferences prefs;
private Editor edt;
private boolean useNetwork;
private int totalLike = 0;
private User followUser = null;
private boolean isGetting = false;
private Context mContext;
public GetCurrentUserIDTask(Activity activity, String follow_fbid, boolean useNetwork) {
Log.i("check","In GetCurrentUserIDTask");
prefs = activity
.getSharedPreferences("MUSIC_MAP", Context.MODE_PRIVATE);
edt = prefs.edit();
mContext = activity;
this.activity = activity;
this.fbid = prefs.getString("FB_ID", "");
this.useNetwork = useNetwork;
this.follow_fbid = follow_fbid;
TAG = "GetDataTask";
Log.i(TAG, "this is task get data");
}
@Override
protected void onPreExecute() {
isGetting = true;
if (null == Statics.hashUsers)
Statics.hashUsers = new HashMap<String, User>();
// if(null == Statics.youFollows) Statics.youFollows = new
// ArrayList<User>();
// if(null == Statics.followYous) Statics.followYous = new
// ArrayList<User>();
if (null == Statics.likeDatas)
Statics.likeDatas = new ArrayList<Artist>();
/*
* try {
* activity.findViewById(R.id.prog_syncing).setVisibility(View.VISIBLE);
* activity.findViewById(R.id.imv_refresh).setVisibility(View.GONE);
* activity
* .findViewById(R.id.prog_syncing_nav).setVisibility(View.VISIBLE); }
* catch (Exception e) {}
*/
}
@Override
protected String doInBackground(Object... params) {
try {
if (Statics.isNetworkConnected(activity)) {
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 20000;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost(Statics.WS_PATH
+ "getAllUsers.php");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("fbid", fbid));
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_DATA", "").length()>0
// &&
// sb.toString().trim().equals(prefs.getString("LAST_DATA",
// "")))
// return "CANCEL";
// edt.putString("LAST_DATA", sb.toString().trim());
// edt.commit();
followUser = Statics.getDataFromJSON(GetCurrentUserIDTask.this, sb
.toString().trim(), this.fbid, null, follow_fbid);
HttpClient httpclientLike = new DefaultHttpClient(
httpParameters);
HttpPost httppostLike = new HttpPost(Statics.WS_PATH
+ "getLikeData.php");
List<NameValuePair> nvpsLike = new ArrayList<NameValuePair>();
nvpsLike.add(new BasicNameValuePair("fbid", prefs
.getString("FB_ID", "")));
httppostLike.setEntity(new UrlEncodedFormEntity(nvpsLike,
HTTP.UTF_8));
HttpResponse responseLike = httpclientLike
.execute(httppostLike);
HttpEntity entityLike = responseLike.getEntity();
if (entityLike != null) {
// get the response content as a string
InputStream instreamLike = entityLike.getContent();
BufferedReader readerLike = new BufferedReader(
new InputStreamReader(instreamLike, "UTF-8"));
String lineLike;
StringBuilder sbLike = new StringBuilder("");
while ((lineLike = readerLike.readLine()) != null) {
sbLike.append(lineLike + "\n");
}
edt.putString("LAST_LIKEDATA", sbLike.toString().trim());
edt.commit();
getData(sbLike.toString());
}
}
if (follow_fbid != null && follow_fbid.length() > 0
&& Statics.hashUsers.get(follow_fbid) == null) {
httpclient = new DefaultHttpClient(httpParameters);
httppost = new HttpPost(Statics.WS_PATH
+ "getUserDetails.php");
nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("fbid", follow_fbid));
nvps.add(new BasicNameValuePair("curFbId", fbid));
httppost.setEntity(new UrlEncodedFormEntity(nvps,
HTTP.UTF_8));
response = httpclient.execute(httppost);
String data = EntityUtils.toString(response.getEntity(),
"UTF-8");
try {
followUser = Statics.getUserFromJSON(new JSONObject(data));
//Statics.hashUsers.put(follow_fbid, followUser);
User userTmp = Statics.hashUsers.get(followUser.getFbid());
if (userTmp != null) {
userTmp.compareTo(followUser);
Statics.hashUsers.put(followUser.getFbid(), userTmp);
/*DBAdapter adapter = new DBAdapter(activity);
adapter.addUser(userTmp);*/
} else {
Statics.hashUsers.put(followUser.getFbid(), followUser);
/*DBAdapter adapter = new DBAdapter(activity);
adapter.addUser(userTmp);*/
}
} catch (Exception e) {
}
}
}
} catch (UnsupportedEncodingException e1) {
Log.e("Exception", "" + e1.getMessage());
return e1.getMessage();
// e1.printStackTrace();
} catch (ClientProtocolException e3) {
Log.e("ClientProtocolException", "" + e3.getMessage());
return e3.getMessage();
// e1.printStackTrace();
} catch (IOException e2) {
Log.e("IOException", "Time out :" + e2.getMessage());
e2.printStackTrace();
doInBackground(params);
return e2.getMessage();
} catch (JSONException e) {
e.printStackTrace();
return e.getMessage();
}
return "OK";
}
private void getData(String json) throws JSONException {
Statics.likeDatas.clear();
Log.d("json", json);
JSONArray jArray = new JSONArray(json);
for (int i = 0; i < jArray.length(); i++) {
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);
if (fbid.equals(u.getFbid())) { // Current user
Statics.curUser = u;
}
}
s.setUserLiked(users);
songs.add(s);
totalLike++;
}
art.setSongs(songs);
Statics.likeDatas.add(art);
}
if (null != Statics.curUser) {
Statics.curUser.setTotalLike(totalLike);
}
try {
Collections.sort(Statics.likeDatas, new LikeDataSort());
} catch (Exception e) {
}
}
@Override
protected void onPostExecute(Object result) {
// Collections.sort(Statics.youFollows, new UserSort());
// Collections.sort(Statics.followYous, new UserSort());
/*
* try {
* activity.findViewById(R.id.prog_syncing).setVisibility(View.GONE);
* activity
* .findViewById(R.id.prog_syncing_nav).setVisibility(View.GONE);
* activity.findViewById(R.id.imv_refresh).setVisibility(View.VISIBLE);
* } catch (Exception e) {}
*/
Log.i(TAG, "on Post");
onSuccess(followUser);
isGetting = false;
if(result != null){
Log.d("Men", "saveDB nhe");
new SaveDataTask(mContext).execute();
}else{
Log.d("Men", "Not saveDB nhe");
}
}
public void onSuccess(User user) {
}
public boolean isGetting() {
return isGetting;
}
}