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.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 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.User;
import com.ved.musicmapapp.utils.Statics;
public class GetUsersDataTask extends MyTask {
private Activity activity;
private String fbid;
private String follow_fbid;
private SharedPreferences prefs;
private Editor edt;
private int totalLike = 0;
private User followUser = null;
private Context mContext;
public GetUsersDataTask(Activity activity, String follow_fbid) {
Log.i("check","In GetUsersDataTask");
prefs = activity.getSharedPreferences("MUSIC_MAP", Context.MODE_PRIVATE);
edt = prefs.edit();
mContext = activity;
this.activity = activity;
this.fbid = prefs.getString("FB_ID", "");
this.follow_fbid = follow_fbid;
TAG = "GetDataTask";
Log.i(TAG, "this is task get data");
}
@Override
protected void onPreExecute() {
if (null == Statics.hashUsers)
Statics.hashUsers = new HashMap<String, User>();
if (null == Statics.likeDatas)
Statics.likeDatas = new ArrayList<Artist>();
}
@Override
protected String doInBackground(Object... params) {
try {
Log.d("Men", "saveDB nhe2");
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);
Log.i("check","In Get user's Data task,,,,");
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");
}
// Log.i("check","/////////// " +sb.toString());
Log.i("check","/////////// GOT RESPONSE FROM SERV /////////////////");
followUser = Statics.getCurrentUserAndSaveRestInDB (sb.toString().trim(),
this.fbid, follow_fbid, activity);
Log.i("check","/////////// Saved in DB..../////////////");
}
}
// 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) {
//
// }
// }
//Post playlists which are not yet posted to server
new AddUserPlaylistsListTask(mContext, Statics.curUser) {
protected void onPostExecute(User result) {
super.onPostExecute(result);
if(result != null) {
//Post playlist songs which are not yet posted to server
new AddAllPendingSongToUserPlaylistsTask(mContext, Statics.curUser) {
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}.execute();
}
}
}.execute();
} catch (UnsupportedEncodingException e1) {
Log.e("Exception", "" + e1.getMessage());
return e1.getMessage();
} catch (ClientProtocolException e3) {
Log.e("ClientProtocolException", "" + e3.getMessage());
return e3.getMessage();
} catch (IOException e2) {
Log.e("IOException", "Time out :" + e2.getMessage());
e2.printStackTrace();
return e2.getMessage();
}
return "OK";
}
@Override
protected void onPostExecute(Object result) {
Log.i(TAG, "on Post");
onSuccess(followUser);
}
public void onSuccess(User user) {
}
}