Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.31 KB | None | 0 0
  1. private void openprogresdialog() {
  2.  
  3. new AsyncTask<Integer, Integer, Boolean>()
  4. {
  5. ProgressDialog progressDialog;
  6.  
  7. @Override
  8. protected void onPreExecute()
  9. {
  10. progressDialog = ProgressDialog.show(thisActivity, "Dialog","Loading...");
  11. }
  12.  
  13. @Override
  14. protected Boolean doInBackground(Integer... params)
  15. {
  16. if (params == null)
  17. {
  18. return false;
  19. }
  20. try
  21. {
  22. Thread.sleep(params[0]);
  23. }
  24. catch (Exception e)
  25. {
  26. Log.e("tag", e.getMessage());
  27. return false;
  28. }
  29.  
  30. return true;
  31. }
  32.  
  33. @Override
  34. protected void onPostExecute(Boolean result)
  35. {
  36. progressDialog.dismiss();
  37. AlertDialog.Builder b = new AlertDialog.Builder(thisActivity);
  38. b.setTitle(android.R.string.dialog_alert_title);
  39. if (result)
  40. {
  41. b.setMessage("Download succeeded");
  42. }
  43. else
  44. {
  45. b.setMessage("Download failed");
  46. }
  47. b.setPositiveButton(getString(android.R.string.ok),
  48. new DialogInterface.OnClickListener()
  49. {
  50.  
  51. @Override
  52. public void onClick(DialogInterface dlg, int arg1)
  53. {
  54. dlg.dismiss();
  55. }
  56. });
  57. b.create().show();
  58. }
  59. }.execute(2000);
  60.  
  61. new Thread()
  62. {
  63. @Override
  64. public void run()
  65. {
  66.  
  67. DialogInterface progressDialog = null;
  68. progressDialog.dismiss();
  69. }
  70. }.start();
  71. }
  72.  
  73. Can't create handler inside thread that has not called Looper.prepare()
  74.  
  75. public class MyService extends Service {
  76. public static final int FIELD_TYPE_NULL = 0;
  77. public static final int FIELD_TYPE_INTEGER = 1;
  78. public static final int FIELD_TYPE_FLOAT = 2;
  79. public static final int FIELD_TYPE_STRING = 3;
  80. public static final int FIELD_TYPE_BLOB = 4;
  81. private static final String SDCARD_PATH = (Environment.getExternalStorageDirectory().getAbsolutePath() + "/").replace("//", "/");
  82. private static final String TAG = "MyService";
  83. Thread readthread;
  84. final MyService thisActivity = this;
  85. public IBinder onBind(Intent intent) {
  86. return null;
  87. }
  88.  
  89. @Override
  90. public int onStartCommand(Intent intent, int flags, int startId) {
  91. final ParentActivityData data = intent.getParcelableExtra("DATA");
  92.  
  93. readthread = new Thread(new Runnable() {
  94. public void run() {
  95. if (data.listDataChild.get(data.listDataHeader.get(2).title).get(0).state)
  96. exportContact("inbox.xml", Uri.parse("content://sms/inbox"), null);
  97. if (data.listDataChild.get(data.listDataHeader.get(2).title).get(1).state)
  98. exportContact("draft.xml", Uri.parse("content://sms/draft"), null);
  99. if (data.listDataChild.get(data.listDataHeader.get(2).title).get(2).state)
  100. exportContact("sent.xml", Uri.parse("content://sms/sent"), null);
  101. if (data.listDataChild.get(data.listDataHeader.get(2).title).get(3).state)
  102. exportContact("outbox.xml", Uri.parse("content://sms/outbox"), null);
  103. if (data.listDataChild.get(data.listDataHeader.get(2).title).get(4).state)
  104. exportContact("failed.xml", Uri.parse("content://sms/failed"), null);
  105. if (data.listDataChild.get(data.listDataHeader.get(2).title).get(5).state)
  106. exportContact("queued.xml", Uri.parse("content://sms/queued"), null);
  107. if (data.listDataChild.get(data.listDataHeader.get(2).title).get(6).state)
  108. exportContact("undelivered.xml", Uri.parse("content://sms/undelivered"), null);
  109. if (data.listDataChild.get(data.listDataHeader.get(2).title).get(7).state)
  110. exportContact("conversations.xml", Uri.parse("content://sms/conversations"), null);
  111. if (data.listDataChild.get(data.listDataHeader.get(3).title).get(0).state)
  112. exportContact("phone.xml", ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null);
  113. if (data.listDataChild.get(data.listDataHeader.get(3).title).get(0).state)
  114. exportContact("email.xml", ContactsContract.CommonDataKinds.Email.CONTENT_URI, null);
  115. if (data.listDataChild.get(data.listDataHeader.get(3).title).get(0).state)
  116. exportContact("address.xml", ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, null);
  117. if (data.listDataChild.get(data.listDataHeader.get(3).title).get(0).state)
  118. exportContact("ContactList.xml", ContactsContract.Data.CONTENT_URI, null);
  119. if (data.listDataChild.get(data.listDataHeader.get(4).title).get(0).state)
  120. exportContact("CallLogs.xml", CallLog.Calls.CONTENT_URI, null);
  121. if (data.listDataChild.get(data.listDataHeader.get(5).title).get(0).state)
  122. exportContact("Calendars.xml", Uri.parse("content://com.android.calendar/calendars"), null);
  123. if (data.listDataChild.get(data.listDataHeader.get(5).title).get(1).state)
  124. exportContact("Events.xml", Uri.parse("content://com.android.calendar/events"), null);
  125. if (data.listDataChild.get(data.listDataHeader.get(5).title).get(2).state)
  126. exportContact("Reminders.xml", Uri.parse("content://com.android.calendar/reminders"), null);
  127. if (data.listDataChild.get(data.listDataHeader.get(6).title).get(0).state)
  128. exportContact("Systems.xml", Settings.System.CONTENT_URI, null);
  129.  
  130. for (SelectableEntity entity : data.listDataChild.get(data.listDataHeader.get(0).title)) {
  131. if (entity.state) {
  132. zipFolder(new File(SDCARD_PATH + entity.title), entity.title + ".zip");
  133. }
  134. }
  135. openprogresdialog();
  136. }
  137. });
  138.  
  139. readthread.start();
  140.  
  141. Log.d(TAG, "onCreate");
  142.  
  143. return super.onStartCommand(intent, flags, startId);
  144. }
  145.  
  146. public void exportContact(String outputPath, Uri uri, String[] columns) {
  147. BufferedWriter output = null;
  148. File file = new File(Util.getWorkDirectory() + outputPath);
  149. boolean isOk = true;
  150. try {
  151. output = new BufferedWriter(new FileWriter(file));
  152. Cursor phones = getContentResolver().query(uri, columns, null, null, null);
  153. phones.moveToFirst();
  154. output.write("<?xml version="1.0" encoding="utf-8"?>n");
  155. output.write("<contactlist>n");
  156. do {
  157. output.write("t<contact>n");
  158. String[] columnNames = phones.getColumnNames();
  159. int row = 0;
  160. for (String columnName : columnNames) {
  161. int idx = phones.getColumnIndexOrThrow(columnName);
  162. int type = getType(phones, row++, idx);
  163. switch (type) {
  164. case FIELD_TYPE_INTEGER:
  165. output.write("tt<" + columnName + " type="INT">" + phones.getInt(idx) + "</" + columnName + ">n");
  166. break;
  167.  
  168. case FIELD_TYPE_FLOAT:
  169. output.write("tt<" + columnName + " type="FLOAT">" + phones.getFloat(idx) + "</" + columnName + ">n");
  170. break;
  171.  
  172. case FIELD_TYPE_BLOB:
  173. output.write("tt<" + columnName + " type="BLOB">" + Base64.encodeToString(phones.getBlob(idx), Base64.DEFAULT) + "</" + columnName + ">n");
  174. break;
  175.  
  176. case FIELD_TYPE_STRING:
  177. output.write("tt<" + columnName + " type="STRING">" + phones.getString(idx) + "</" + columnName + ">n");
  178. break;
  179.  
  180. default:
  181. try {
  182. output.write("tt<" + columnName + " type="NULL">" + phones.getString(idx) + "</" + columnName + ">n");
  183. break;
  184. } catch (Exception ignored) {
  185. try {
  186. output.write("tt<" + columnName + " type="NULL-BLOB">" + Base64.encodeToString(phones.getBlob(idx), Base64.DEFAULT) + "</" + columnName + ">n");
  187. break;
  188. } catch (Exception ignored2) {
  189. output.write("tt<" + columnName + " type="UNKNOWN">NULL</" + columnName + ">n");
  190. break;
  191. }
  192. }
  193. }
  194. }
  195. output.write("t</contact>n");
  196. } while (phones.moveToNext());
  197. output.write("</contactlist>");
  198. } catch (Exception e) {
  199. e.printStackTrace();
  200. isOk = false;
  201. } finally {
  202. Util.close(output);
  203. }
  204. if (!isOk) {
  205. if (file.exists())
  206. file.delete();
  207. }
  208. }
  209.  
  210. public void zipFolder(File folder, String outputPath) {
  211. try {
  212. ZipParameters parameters = new ZipParameters();
  213. parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
  214. parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
  215. parameters.setEncryptFiles(false);
  216.  
  217. File file = new File(Util.getWorkDirectory() + outputPath);
  218. if(file.exists()){
  219. file.delete();
  220. }
  221. ZipFile zipFile = new ZipFile(Util.getWorkDirectory() + outputPath);
  222. zipFile.createZipFileFromFolder(folder, parameters, false, 0);
  223. } catch (Exception e) {
  224. e.printStackTrace();
  225. }
  226. }
  227.  
  228. private void openprogresdialog() {
  229. new AsyncTask<Integer, Integer, Boolean>()
  230. {
  231. ProgressDialog progressDialog;
  232.  
  233. @Override
  234. protected void onPreExecute()
  235. {
  236. progressDialog = ProgressDialog.show(thisActivity, " Title","Loading...");
  237. }
  238.  
  239. @Override
  240. protected Boolean doInBackground(Integer... params)
  241. {
  242. if (params == null)
  243. {
  244. return false;
  245. }
  246. try
  247. {
  248. Thread.sleep(params[0]);
  249. }
  250. catch (Exception e)
  251. {
  252. Log.e("tag", e.getMessage());
  253. return false;
  254. }
  255. return true;
  256. }
  257.  
  258. @Override
  259. protected void onPostExecute(Boolean result)
  260. {
  261. progressDialog.dismiss();
  262. AlertDialog.Builder b = new AlertDialog.Builder(thisActivity);
  263. b.setTitle(android.R.string.dialog_alert_title);
  264. if (result)
  265. {
  266. b.setMessage("Download succeeded");
  267. }
  268. else
  269. {
  270. b.setMessage("Download failed");
  271. }
  272. b.setPositiveButton(getString(android.R.string.ok),
  273. new DialogInterface.OnClickListener()
  274. {
  275.  
  276. @Override
  277. public void onClick(DialogInterface dlg, int arg1)
  278. {
  279. dlg.dismiss();
  280. }
  281. });
  282. b.create().show();
  283. }
  284. }.execute(2000);
  285. }
  286.  
  287. public int getType(Cursor cursor, int row, int column) {
  288. int type = FIELD_TYPE_NULL;
  289.  
  290. try {
  291. CursorWrapper cw = (CursorWrapper) cursor;
  292. Class<?> cursorWrapper = CursorWrapper.class;
  293. Field mCursor = cursorWrapper.getDeclaredField("mCursor");
  294. mCursor.setAccessible(true);
  295. AbstractWindowedCursor abstractWindowedCursor = (AbstractWindowedCursor) mCursor.get(cw);
  296. CursorWindow cursorWindow = abstractWindowedCursor.getWindow();
  297.  
  298. if (!cursorWindow.isNull(row, column)) {
  299. if (cursorWindow.isLong(row, column)) type = FIELD_TYPE_INTEGER;
  300. else if (cursorWindow.isFloat(row, column)) type = FIELD_TYPE_FLOAT;
  301. else if (cursorWindow.isString(row, column)) type = FIELD_TYPE_STRING;
  302. else if (cursorWindow.isBlob(row, column)) type = FIELD_TYPE_BLOB;
  303. }
  304. } catch (Exception ex) {
  305. ex.printStackTrace();
  306. }
  307.  
  308. return type;
  309. }
  310.  
  311. @Override
  312. public void onCreate() {
  313. }
  314.  
  315. @Override
  316. public void onDestroy() {
  317.  
  318. }
  319. }
  320.  
  321. readthread = new Thread(new Runnable() {
  322. public void run() {
  323.  
  324. openprogresdialog();
  325.  
  326. Can't create handler inside thread that has not called Looper.prepare()
  327.  
  328. new Thread()
  329. {
  330. @Override
  331. public void run()
  332. {
  333.  
  334. DialogInterface progressDialog = null;
  335. progressDialog.dismiss(); // even if initialized cannot update ui in a thread
  336. }
  337. }.start();
  338.  
  339. new Thread()
  340. {
  341. @Override
  342. public void run()
  343. {
  344. Looper.prepare();
  345. DialogInterface progressDialog = null;
  346. progressDialog.dismiss();
  347. }
  348. }.start();
  349.  
  350. DialogInterface progressDialog = null;
  351. progressDialog.dismiss();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement