//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.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.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.database.Cursor;
//import android.net.Uri;
//import android.os.AsyncTask;
//import android.provider.MediaStore;
//import android.util.Log;
//
//import com.ved.musicmapapp.Objects.LocalSong;
//import com.ved.musicmapapp.utils.Statics;
//
//public class CheckingDataTask extends AsyncTask<Void, Object, Void> {
// private Activity activity;
// private String fbjson, fbid, device_id;
// private String TAG = "MusicMapr";
// private ArrayList<LocalSong> exists, sPhones, adds, dels;
//// private ProgressDialog pd;
// private int user_id = 0;
// private SharedPreferences prefs;
// private Editor edt;
// private StringBuilder data;
//
// public CheckingDataTask(String fbjson, String fbid, String device_id, Activity activity) {
// super();
// this.activity = activity;
// this.fbjson = fbjson;
// this.fbid = fbid;
// this.device_id = device_id;
//
// prefs = activity.getSharedPreferences("MUSIC_MAP", Context.MODE_PRIVATE);
// edt = prefs.edit();
// }
//
// @Override
// protected void onPreExecute() {
// /*Log.v(TAG,"finding mp3");
// pd.setMessage("Loading...");
// if(!pd.isShowing()) pd.show();*/
// }
//
// protected Void doInBackground(Void... params) {
// exists = new ArrayList<LocalSong>();
// adds = new ArrayList<LocalSong>();
// dels = new ArrayList<LocalSong>();
// try{
// if(!Statics.isNetworkConnected(activity)) return null;
//
// HttpClient httpclient = new DefaultHttpClient();
//
// HttpPost httppost = new HttpPost(Statics.WS_PATH + "getSongExist.php");
// List <NameValuePair> nvps = new ArrayList <NameValuePair>();
// nvps.add(new BasicNameValuePair("fbid", fbid));
// nvps.add(new BasicNameValuePair("fbusername", prefs.getString("FB_USERNAME", "")));
// nvps.add(new BasicNameValuePair("device_id", device_id));
// nvps.add(new BasicNameValuePair("fbjson", fbjson));
//
// String data = prefs.getString("DATA_UPLOAD", "");
// if(data.length()>0){
// nvps.add(new BasicNameValuePair("data", data));
// }
//
// httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
// HttpResponse response = httpclient.execute(httppost);
//
// edt.putString("DATA_UPLOAD", "");
// edt.commit();
//
//
// 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");
// }
//
//// publishProgress(0, sb);
//
// try {
// user_id = Integer.parseInt(sb.toString().trim());
// } catch (Exception e) {
// JSONArray songarray = new JSONArray(sb.toString().trim());
// for (int i = 0; i < songarray.length(); i++){
// LocalSong s = new LocalSong();
// JSONObject song = songarray.getJSONObject(i);
// s.setTitle(song.getString("title"));
// s.setTitleEdited(song.getString("title_cleaned"));
// s.setArtist(song.getString("artist"));
// s.setArtistEdited(song.getString("artist_cleaned"));
// s.setDeleted(song.getBoolean("deleted"));
// user_id = song.getInt("user_id");
// exists.add(s);
// }
// }
// }
// } catch (UnsupportedEncodingException e1) {
// Log.e("Exception", "" + e1.getMessage());
// //e1.printStackTrace();
// } catch (ClientProtocolException e1) {
// Log.e("ClientProtocolException", "" + e1.getMessage());
// //e1.printStackTrace();
// } catch (IOException e1) {
// Log.e("IOException", "" + e1.getMessage());
// //e1.printStackTrace();
// } catch (JSONException e) {
// e.printStackTrace();
// }
//
// sPhones = getAllSongFromPhone();
//
//
// data = new StringBuilder();
// data.append("{\"data\" : { " +
// "\"user\": {" +
// "\"id\": \""+user_id+"\"," +
// "\"email\": \"nvngoc@sdc.ud.edu.vn\"" +
// "}");
// boolean first = true, hasbreak = false;
// for(LocalSong s1 : sPhones){
// if(this.isCancelled()) return null;
// if(isFirst && adds.size()>=20) break;
//
// boolean exist = false;
// for (LocalSong s2 :exists) {
// if(s1.getTitle().trim().equals(s2.getTitle().trim())){
// if(s2.isDeleted()){ // Exist but deleted
// s1.setDeleted(true);
// s1.setTitleEdited(s2.getTitleEdited());
// s1.setArtistEdited(s2.getArtistEdited());
//
// }else{ // Exist and using
// exist = true;
// }
// break;
// }
// }
// if(!exist){
// adds.add(s1);
// }
// }
//
// /**
// * DEL
// */
// for(LocalSong s3 : exists){
// boolean exist = false;
// if(s3.isDeleted()) continue;
// for (LocalSong s4 : sPhones) {
// if(s3.getTitle().trim().equals(s4.getTitle().trim())){
// exist = true;
// break;
// }
// }
// if(!exist){
// dels.add(s3);
// }
// }
//
// return null;
// }
//
// @Override
// protected void onProgressUpdate(Object... values) {
//// Statics.showToast(activity.getBaseContext(), values[0]+" added, "+values[1]+" deleted");
// int type = Integer.parseInt(values[0].toString());
// if(type == 0){ // Notification
// Statics.showToast(activity.getBaseContext(), values[1].toString());
// }else if(type == 1){ // Progress update
// /*pd.setMessage("Loading "+values[1].toString()+"%...");*/
// }
//
// }
//
// protected void onPostExecute(Void result) {
// onSuccess(adds, dels);
// }
//
// public void onSuccess(ArrayList<LocalSong> adds, ArrayList<LocalSong> dels){}
//
// public ArrayList<LocalSong> getAllSongFromPhone(){
// ArrayList<LocalSong> songs = new ArrayList<LocalSong>();
// String where = MediaStore.Audio.Media.IS_MUSIC + "=1";
// Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
// String[] cursor_cols = {
// MediaStore.Audio.Media.TITLE,
// MediaStore.Audio.Media.ARTIST,
// MediaStore.Audio.Media.ALBUM
// };
// Cursor cursor = activity.getContentResolver().query(uri, cursor_cols, where, null, null);
//
// while (cursor.moveToNext()) {
// String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
//
//
// boolean exist = false;
// for(LocalSong s:songs){
// if(s.getTitle().equals(title)){
// exist = true;
// break;
// }
// }
// if(exist) continue;
//
// String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
// artist = artist.replace('\'', ' ');
// artist = artist.replace(',', '&');
// artist = artist.replaceAll("[^a-zA-Z 0-9],_", " ");
// if(artist.length() == 0) artist = "<unknown>";
//
// String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
// album = album.replace('\'', ' ');
// album = album.replaceAll("[^a-zA-Z 0-9],_", " ");
//
//
//
// String artist_cleaned = artist;
//
// if(!artist_cleaned.equals("<unknown>")){
// artist_cleaned = cleanSongArtist(artist_cleaned);
// }
// String title_cleaned = cleanSongTrack(title, artist_cleaned);
//
// if(artist_cleaned.length()>0 && title_cleaned.length()>0){
// songs.add(new LocalSong(-1,
// title,
// title_cleaned,
// artist,
// artist_cleaned,
// album,
// 0,
// 0));
// }
// }
//
// return songs;
// }
//
//
// private String cleanSongArtist(String str){
// while(str.contains("(") && str.contains(")")){
// int start = -1, end = 0;
// for (int i = 0; i < str.length(); i++) {
// if(str.charAt(i) == '(' && start == -1) start = i;
// if(str.charAt(i) == ')'){
// end = i;
// break;
// }
// }
// if(end>start) str = str.replace(str.substring(start, end+1), "");
// }
// while(str.contains("[") && str.contains("]")){
// int start = -1, end = 0;
// for (int i = 0; i < str.length(); i++) {
// if(str.charAt(i) == '[' && start == -1) start = i;
// if(str.charAt(i) == ']'){
// end = i;
// break;
// }
// }
// if(end>start) str = str.replace(str.substring(start, end+1), "");
// }
// while(str.contains("{") && str.contains("}")){
// int start = -1, end = 0;
// for (int i = 0; i < str.length(); i++) {
// if(str.charAt(i) == '{' && start == -1) start = i;
// if(str.charAt(i) == '}'){
// end = i;
// break;
// }
// }
// if(end>start) str = str.replace(str.substring(start, end+1), "");
// }
//
//
//
// String tmp = str.toLowerCase();
// if(tmp.contains("www.")){
// int start = tmp.indexOf("www."), end = tmp.length()-1;
// for (int i = start; i < tmp.length(); i++) {
// if(tmp.charAt(i) == ' '){
// end = i;
// break;
// }
// }
// if(end>start){
// str = str.replace(str.substring(start, end+1), "");
// tmp = str.toLowerCase();
// }
// }
//
//
//// str = AccentRemover.toUrlFriendly(str);
//
//
// str = str.replaceAll("[~!@#$%^&*()_+:\'\"?><,./=`]", " ");
//
// // trim()
// while (str.contains(" ")) {
// str = str.replace(" ", " ");
// }
//
// int i=0, start=-1;
// for (; i < str.length(); i++) {
// if(str.charAt(i)>='0' && str.charAt(i)<='9' ) {
// if(start == -1) start = i;
// }else if(start != -1 && i > start && (str.charAt(i)==' ' || str.charAt(i)=='-')){
//
// String s = str.substring(start, i);
// str = str.replace(s, "");
// i -= s.length();
// start = -1;
// }else{
// start = -1;
// }
// }
//
// if(start>0 && i==str.length()){
// str = str.replace(str.substring(start), "");
// }
//
// str = str.replaceAll(" - ", " ");
//
// while (str.contains("--")) {
// str = str.replace("--", " ");
// }
//
// // trim()
// while (str.contains(" ")) {
// str = str.replace(" ", " ");
// }
//
// return str.trim();
// }
//
// private String cleanSongTrack(String str, String artist){
// while(str.contains("(") && str.contains(")")){
// int start = -1, end = 0;
// for (int i = 0; i < str.length(); i++) {
// if(str.charAt(i) == '(' && start == -1) start = i;
// if(str.charAt(i) == ')'){
// end = i;
// break;
// }
// }
// if(end>start) str = str.replace(str.substring(start, end+1), "");
// }
// while(str.contains("[") && str.contains("]")){
// int start = -1, end = 0;
// for (int i = 0; i < str.length(); i++) {
// if(str.charAt(i) == '[' && start == -1) start = i;
// if(str.charAt(i) == ']'){
// end = i;
// break;
// }
// }
// if(end>start) str = str.replace(str.substring(start, end+1), "");
// }
// while(str.contains("{") && str.contains("}")){
// int start = -1, end = 0;
// for (int i = 0; i < str.length(); i++) {
// if(str.charAt(i) == '{' && start == -1) start = i;
// if(str.charAt(i) == '}'){
// end = i;
// break;
// }
// }
// if(end>start) str = str.replace(str.substring(start, end+1), "");
// }
//
// //str = AccentRemover.toUrlFriendly(str);
// //artist = AccentRemover.toUrlFriendly(artist);
// Log.i("ARTIST", str + " " + artist);
// if (str.contains(artist)) {
// str = str.replaceAll(artist, " ");
// }
//
//
// String tmp = str.toLowerCase();
// while(tmp.contains("www.")){
// int start = tmp.indexOf("www."), end = tmp.length()-1;
// for (int i = start; i < tmp.length(); i++) {
// if(tmp.charAt(i) == ' '){
// end = i;
// break;
// }
// }
// if(end>start){
// str = str.replace(str.substring(start, end+1), "");
// tmp = str.toLowerCase();
// }
// }
//
// str = str.replaceAll("[~!@#$%^&*()_+:\'\"?><,./=`]", " ");
//
// // trim()
// while (str.contains(" ")) {
// str = str.replace(" ", " ");
// }
//
// int i=0, start=-1;
// for (; i < str.length(); i++) {
// if(str.charAt(i)>='0' && str.charAt(i)<='9' ) {
// if(start == -1) start = i;
// }else if(start != -1 && i > start && (str.charAt(i)==' ' || str.charAt(i)=='-')){
// try {
// Character a = str.charAt(i+1);
// Character b = str.charAt(i+2);
// if((a=='a'||a=='A'||a=='p'||a=='P') && (b=='m'||b=='M')){
// start = -1;
// continue;
// }
// } catch (Exception e) {}
//
// String s = str.substring(start, i);
// str = str.replace(s, "");
// i -= s.length();
// start = -1;
// }else{
// start = -1;
// }
// }
//
// if(start>0 && i==str.length()){
// str = str.replace(str.substring(start), "");
// }
//
// str = str.replaceAll(" - ", " ");
//
// while (str.contains("--")) {
// str = str.replace("--", " ");
// }
//
// // trim()
// while (str.contains(" ")) {
// str = str.replace(" ", " ");
// }
//
// return str.trim();
// }
//
//}
//
//
//