Advertisement
Guest User

v2 punnya

a guest
Apr 23rd, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.82 KB | None | 0 0
  1. package com.sourcecanyon.whatsClone.helpers;
  2.  
  3. import android.Manifest;
  4. import android.annotation.SuppressLint;
  5. import android.app.Activity;
  6. import android.content.ContentResolver;
  7. import android.content.Context;
  8. import android.database.Cursor;
  9. import android.net.Uri;
  10. import android.os.AsyncTask;
  11. import android.provider.ContactsContract;
  12.  
  13. import com.google.i18n.phonenumbers.NumberParseException;
  14. import com.google.i18n.phonenumbers.PhoneNumberUtil;
  15. import com.google.i18n.phonenumbers.Phonenumber;
  16. import com.sourcecanyon.whatsClone.app.WhatsCloneApplication;
  17. import com.sourcecanyon.whatsClone.interfaces.ContactMobileNumbQuery;
  18. import com.sourcecanyon.whatsClone.models.users.contacts.ContactsModel;
  19.  
  20. import java.util.ArrayList;
  21. import java.util.Locale;
  22. import java.util.concurrent.ExecutionException;
  23.  
  24. /**
  25. * Created by Abderrahim El imame on 03/03/2016.
  26. * Email : abderrahim.elimame@gmail.com
  27. */
  28. public class UtilsPhone {
  29.  
  30.  
  31. private static ArrayList<ContactsModel> mListContacts = new ArrayList<ContactsModel>();
  32. private static PhoneNumberUtil mPhoneUtil = PhoneNumberUtil.getInstance();
  33. private static String name = null;
  34.  
  35. /**
  36. * method to retrieve all contacts from the book
  37. *
  38. * @return return value
  39. */
  40. public static ArrayList<ContactsModel> GetPhoneContacts() {
  41. ContentResolver contentResolver = WhatsCloneApplication.getInstance().getApplicationContext().getContentResolver();
  42. Cursor cur = contentResolver.query(ContactMobileNumbQuery.CONTENT_URI, ContactMobileNumbQuery.PROJECTION, ContactMobileNumbQuery.SELECTION, null, ContactMobileNumbQuery.SORT_ORDER);
  43. if (cur != null) {
  44. if (cur.getCount() > 0) {
  45. while (cur.moveToNext()) {
  46. ContactsModel contactsModel = new ContactsModel();
  47. String name = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
  48. String phoneNumber = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
  49. String id = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
  50. String image_uri = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
  51.  
  52.  
  53. // AppHelper.LogCat("number phone --> " + phoneNumber);
  54. if (name.contains("\\s+")) {
  55. String[] nameArr = name.split("\\s+");
  56. contactsModel.setUsername(nameArr[0] + nameArr[1]);
  57. // AppHelper.LogCat("Fname --> " + nameArr[0]);
  58. // AppHelper.LogCat("Lname --> " + nameArr[1]);
  59. } else {
  60. contactsModel.setUsername(name);
  61. //AppHelper.LogCat("name" + name);
  62. }
  63. if (phoneNumber != null) {
  64.  
  65. String Regex = "[^\\d]";
  66. String PhoneDigits = phoneNumber.replaceAll(Regex, "");
  67. boolean isValid = !(PhoneDigits.length() < 6 || PhoneDigits.length() > 13);
  68. String phNumberProto = PhoneDigits.replaceAll("-", "");
  69. String PhoneNo;
  70. if (PhoneDigits.length() != 10) {
  71. PhoneNo = "+";
  72. PhoneNo = PhoneNo.concat(phNumberProto);
  73. } else {
  74. PhoneNo = phNumberProto;
  75. }
  76. // AppHelper.LogCat("phoneNumber --> " + phoneNumber);
  77. String phoneNumberTmpFinal;
  78. Phonenumber.PhoneNumber phoneNumberInter = getPhoneNumber(phoneNumber);
  79. if (phoneNumberInter != null) {
  80. // AppHelper.LogCat("phoneNumberInter --> " + phoneNumberInter.getNationalNumber());
  81. phoneNumberTmpFinal = String.valueOf(phoneNumberInter.getNationalNumber());
  82.  
  83. // AppHelper.LogCat("phoneNumberTmpFinal --> " + phoneNumberTmpFinal);
  84. if (isValid) {
  85. // AppHelper.LogCat("PhoneNo --> " + PhoneNo);
  86. contactsModel.setPhoneTmp(phoneNumberTmpFinal);
  87. contactsModel.setPhone(PhoneNo.trim());
  88. contactsModel.setContactID(Integer.parseInt(id));
  89. contactsModel.setImage(image_uri);
  90.  
  91. int flag = 0;
  92. int arraySize = mListContacts.size();
  93. if (arraySize == 0) {
  94. mListContacts.add(contactsModel);
  95. }
  96. //remove duplicate numbers
  97. for (int i = 0; i < arraySize; i++) {
  98.  
  99. if (!mListContacts.get(i).getPhone().trim().equals(PhoneNo.trim())) {
  100. flag = 1;
  101.  
  102. } else {
  103. flag = 0;
  104. break;
  105. }
  106. }
  107.  
  108. if (flag == 1) {
  109. mListContacts.add(contactsModel);
  110. }
  111.  
  112.  
  113. } else {
  114. // AppHelper.LogCat("invalid phone --> ");
  115. }
  116. }
  117.  
  118.  
  119. }
  120. }
  121. cur.close();
  122. }
  123. }
  124. return mListContacts;
  125. }
  126.  
  127. /**
  128. * Check if number is valid
  129. *
  130. * @return boolean
  131. */
  132. @SuppressWarnings("unused")
  133. public static boolean isValid(String phone) {
  134. Phonenumber.PhoneNumber phoneNumber = getPhoneNumber(phone);
  135. return phoneNumber != null && mPhoneUtil.isValidNumber(phoneNumber);
  136. }
  137.  
  138. /**
  139. * Get PhoneNumber object
  140. *
  141. * @return PhoneNumber | null on error
  142. */
  143. @SuppressWarnings("unused")
  144. public static Phonenumber.PhoneNumber getPhoneNumber(String phone) {
  145. final String DEFAULT_COUNTRY = Locale.getDefault().getCountry();
  146. try {
  147. return mPhoneUtil.parse(phone, DEFAULT_COUNTRY);
  148. } catch (NumberParseException ignored) {
  149. return null;
  150. }
  151. }
  152.  
  153. /**
  154. * method to get contact ID
  155. *
  156. * @param mContext this is the first parameter for getContactID method
  157. * @param phone this is the second parameter for getContactID method
  158. * @return return value
  159. */
  160. public static long getContactID(Activity mContext, String phone) {
  161. if (PermissionHandler.checkPermission(mContext, Manifest.permission.READ_EXTERNAL_STORAGE)) {
  162. AppHelper.LogCat("Read contact data permission already granted.");
  163. // CONTENT_FILTER_URI allow to search contact by phone number
  164. Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));
  165. // This query will return NAME and ID of contact, associated with phone //number.
  166. Cursor mcursor = mContext.getContentResolver().query(lookupUri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID}, null, null, null);
  167. //Now retrieve _ID from query result
  168. long idPhone = 0;
  169. try {
  170. if (mcursor != null) {
  171. if (mcursor.moveToFirst()) {
  172. idPhone = Long.valueOf(mcursor.getString(mcursor.getColumnIndex(ContactsContract.PhoneLookup._ID)));
  173. }
  174. }
  175. } finally {
  176. mcursor.close();
  177. }
  178. return idPhone;
  179. } else {
  180. AppHelper.LogCat("Please request Read contact data permission.");
  181. PermissionHandler.requestPermission(mContext, Manifest.permission.READ_EXTERNAL_STORAGE);
  182. return 0;
  183. }
  184.  
  185. }
  186.  
  187.  
  188. /**
  189. * method to check for contact name
  190. *
  191. * @param phone this is the second parameter for getContactName method
  192. * @return return value
  193. */
  194. @SuppressLint("StaticFieldLeak")
  195. public static String getContactName(String phone) {
  196. try {
  197. return new AsyncTask<String, Void, String>() {
  198. @Override
  199. protected String doInBackground(String... params) {
  200. try {
  201.  
  202. // CONTENT_FILTER_URI allow to search contact by phone number
  203. Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(params[0]));
  204. // This query will return NAME and ID of contact, associated with phone //number.
  205. Cursor mcursor = WhatsCloneApplication.getInstance().getApplicationContext().getContentResolver().query(lookupUri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID}, null, null, null);
  206. //Now retrieve _ID from query result
  207. String name = null;
  208. try {
  209. if (mcursor != null) {
  210. if (mcursor.moveToFirst()) {
  211. name = mcursor.getString(mcursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
  212. }
  213. }
  214. } finally {
  215. mcursor.close();
  216. }
  217. return name;
  218. } catch (Exception e) {
  219. return e.getMessage();
  220. }
  221. }
  222.  
  223. @Override
  224. protected void onPostExecute(String username) {
  225. super.onPostExecute(username);
  226. // String name = UtilsPhone.getContactName(mActivity, conversationsModel.getRecipientPhone());
  227. // AppHelper.LogCat("name " + username);
  228. name = username;
  229. }
  230. }.execute(phone).get();
  231. } catch (InterruptedException e) {
  232. return null;
  233. } catch (ExecutionException e) {
  234. return null;
  235. }
  236.  
  237. }
  238.  
  239. /**
  240. * method to check if user contact exist
  241. *
  242. * @param phone this is the second parameter for checkIfContactExist method
  243. * @return return value
  244. */
  245. public static boolean checkIfContactExist(Context mContext, String phone) {
  246. try {
  247. // CONTENT_FILTER_URI allow to search contact by phone number
  248. Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));
  249. // This query will return NAME and ID of contact, associated with phone //number.
  250. Cursor mcursor = mContext.getApplicationContext().getContentResolver().query(lookupUri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID}, null, null, null);
  251. //Now retrieve _ID from query result
  252. String name = null;
  253. try {
  254. if (mcursor != null) {
  255. if (mcursor.moveToFirst()) {
  256. name = mcursor.getString(mcursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
  257. }
  258. }
  259. } finally {
  260. mcursor.close();
  261. }
  262.  
  263. return name != null;
  264. } catch (Exception e) {
  265. AppHelper.LogCat(e);
  266. return false;
  267. }
  268. }
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement