Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void openprogresdialog() {
- new AsyncTask<Integer, Integer, Boolean>()
- {
- ProgressDialog progressDialog;
- @Override
- protected void onPreExecute()
- {
- progressDialog = ProgressDialog.show(thisActivity, "Dialog","Loading...");
- }
- @Override
- protected Boolean doInBackground(Integer... params)
- {
- if (params == null)
- {
- return false;
- }
- try
- {
- Thread.sleep(params[0]);
- }
- catch (Exception e)
- {
- Log.e("tag", e.getMessage());
- return false;
- }
- return true;
- }
- @Override
- protected void onPostExecute(Boolean result)
- {
- progressDialog.dismiss();
- AlertDialog.Builder b = new AlertDialog.Builder(thisActivity);
- b.setTitle(android.R.string.dialog_alert_title);
- if (result)
- {
- b.setMessage("Download succeeded");
- }
- else
- {
- b.setMessage("Download failed");
- }
- b.setPositiveButton(getString(android.R.string.ok),
- new DialogInterface.OnClickListener()
- {
- @Override
- public void onClick(DialogInterface dlg, int arg1)
- {
- dlg.dismiss();
- }
- });
- b.create().show();
- }
- }.execute(2000);
- new Thread()
- {
- @Override
- public void run()
- {
- DialogInterface progressDialog = null;
- progressDialog.dismiss();
- }
- }.start();
- }
- Can't create handler inside thread that has not called Looper.prepare()
- public class MyService extends Service {
- public static final int FIELD_TYPE_NULL = 0;
- public static final int FIELD_TYPE_INTEGER = 1;
- public static final int FIELD_TYPE_FLOAT = 2;
- public static final int FIELD_TYPE_STRING = 3;
- public static final int FIELD_TYPE_BLOB = 4;
- private static final String SDCARD_PATH = (Environment.getExternalStorageDirectory().getAbsolutePath() + "/").replace("//", "/");
- private static final String TAG = "MyService";
- Thread readthread;
- final MyService thisActivity = this;
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- final ParentActivityData data = intent.getParcelableExtra("DATA");
- readthread = new Thread(new Runnable() {
- public void run() {
- if (data.listDataChild.get(data.listDataHeader.get(2).title).get(0).state)
- exportContact("inbox.xml", Uri.parse("content://sms/inbox"), null);
- if (data.listDataChild.get(data.listDataHeader.get(2).title).get(1).state)
- exportContact("draft.xml", Uri.parse("content://sms/draft"), null);
- if (data.listDataChild.get(data.listDataHeader.get(2).title).get(2).state)
- exportContact("sent.xml", Uri.parse("content://sms/sent"), null);
- if (data.listDataChild.get(data.listDataHeader.get(2).title).get(3).state)
- exportContact("outbox.xml", Uri.parse("content://sms/outbox"), null);
- if (data.listDataChild.get(data.listDataHeader.get(2).title).get(4).state)
- exportContact("failed.xml", Uri.parse("content://sms/failed"), null);
- if (data.listDataChild.get(data.listDataHeader.get(2).title).get(5).state)
- exportContact("queued.xml", Uri.parse("content://sms/queued"), null);
- if (data.listDataChild.get(data.listDataHeader.get(2).title).get(6).state)
- exportContact("undelivered.xml", Uri.parse("content://sms/undelivered"), null);
- if (data.listDataChild.get(data.listDataHeader.get(2).title).get(7).state)
- exportContact("conversations.xml", Uri.parse("content://sms/conversations"), null);
- if (data.listDataChild.get(data.listDataHeader.get(3).title).get(0).state)
- exportContact("phone.xml", ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null);
- if (data.listDataChild.get(data.listDataHeader.get(3).title).get(0).state)
- exportContact("email.xml", ContactsContract.CommonDataKinds.Email.CONTENT_URI, null);
- if (data.listDataChild.get(data.listDataHeader.get(3).title).get(0).state)
- exportContact("address.xml", ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, null);
- if (data.listDataChild.get(data.listDataHeader.get(3).title).get(0).state)
- exportContact("ContactList.xml", ContactsContract.Data.CONTENT_URI, null);
- if (data.listDataChild.get(data.listDataHeader.get(4).title).get(0).state)
- exportContact("CallLogs.xml", CallLog.Calls.CONTENT_URI, null);
- if (data.listDataChild.get(data.listDataHeader.get(5).title).get(0).state)
- exportContact("Calendars.xml", Uri.parse("content://com.android.calendar/calendars"), null);
- if (data.listDataChild.get(data.listDataHeader.get(5).title).get(1).state)
- exportContact("Events.xml", Uri.parse("content://com.android.calendar/events"), null);
- if (data.listDataChild.get(data.listDataHeader.get(5).title).get(2).state)
- exportContact("Reminders.xml", Uri.parse("content://com.android.calendar/reminders"), null);
- if (data.listDataChild.get(data.listDataHeader.get(6).title).get(0).state)
- exportContact("Systems.xml", Settings.System.CONTENT_URI, null);
- for (SelectableEntity entity : data.listDataChild.get(data.listDataHeader.get(0).title)) {
- if (entity.state) {
- zipFolder(new File(SDCARD_PATH + entity.title), entity.title + ".zip");
- }
- }
- openprogresdialog();
- }
- });
- readthread.start();
- Log.d(TAG, "onCreate");
- return super.onStartCommand(intent, flags, startId);
- }
- public void exportContact(String outputPath, Uri uri, String[] columns) {
- BufferedWriter output = null;
- File file = new File(Util.getWorkDirectory() + outputPath);
- boolean isOk = true;
- try {
- output = new BufferedWriter(new FileWriter(file));
- Cursor phones = getContentResolver().query(uri, columns, null, null, null);
- phones.moveToFirst();
- output.write("<?xml version="1.0" encoding="utf-8"?>n");
- output.write("<contactlist>n");
- do {
- output.write("t<contact>n");
- String[] columnNames = phones.getColumnNames();
- int row = 0;
- for (String columnName : columnNames) {
- int idx = phones.getColumnIndexOrThrow(columnName);
- int type = getType(phones, row++, idx);
- switch (type) {
- case FIELD_TYPE_INTEGER:
- output.write("tt<" + columnName + " type="INT">" + phones.getInt(idx) + "</" + columnName + ">n");
- break;
- case FIELD_TYPE_FLOAT:
- output.write("tt<" + columnName + " type="FLOAT">" + phones.getFloat(idx) + "</" + columnName + ">n");
- break;
- case FIELD_TYPE_BLOB:
- output.write("tt<" + columnName + " type="BLOB">" + Base64.encodeToString(phones.getBlob(idx), Base64.DEFAULT) + "</" + columnName + ">n");
- break;
- case FIELD_TYPE_STRING:
- output.write("tt<" + columnName + " type="STRING">" + phones.getString(idx) + "</" + columnName + ">n");
- break;
- default:
- try {
- output.write("tt<" + columnName + " type="NULL">" + phones.getString(idx) + "</" + columnName + ">n");
- break;
- } catch (Exception ignored) {
- try {
- output.write("tt<" + columnName + " type="NULL-BLOB">" + Base64.encodeToString(phones.getBlob(idx), Base64.DEFAULT) + "</" + columnName + ">n");
- break;
- } catch (Exception ignored2) {
- output.write("tt<" + columnName + " type="UNKNOWN">NULL</" + columnName + ">n");
- break;
- }
- }
- }
- }
- output.write("t</contact>n");
- } while (phones.moveToNext());
- output.write("</contactlist>");
- } catch (Exception e) {
- e.printStackTrace();
- isOk = false;
- } finally {
- Util.close(output);
- }
- if (!isOk) {
- if (file.exists())
- file.delete();
- }
- }
- public void zipFolder(File folder, String outputPath) {
- try {
- ZipParameters parameters = new ZipParameters();
- parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
- parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
- parameters.setEncryptFiles(false);
- File file = new File(Util.getWorkDirectory() + outputPath);
- if(file.exists()){
- file.delete();
- }
- ZipFile zipFile = new ZipFile(Util.getWorkDirectory() + outputPath);
- zipFile.createZipFileFromFolder(folder, parameters, false, 0);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private void openprogresdialog() {
- new AsyncTask<Integer, Integer, Boolean>()
- {
- ProgressDialog progressDialog;
- @Override
- protected void onPreExecute()
- {
- progressDialog = ProgressDialog.show(thisActivity, " Title","Loading...");
- }
- @Override
- protected Boolean doInBackground(Integer... params)
- {
- if (params == null)
- {
- return false;
- }
- try
- {
- Thread.sleep(params[0]);
- }
- catch (Exception e)
- {
- Log.e("tag", e.getMessage());
- return false;
- }
- return true;
- }
- @Override
- protected void onPostExecute(Boolean result)
- {
- progressDialog.dismiss();
- AlertDialog.Builder b = new AlertDialog.Builder(thisActivity);
- b.setTitle(android.R.string.dialog_alert_title);
- if (result)
- {
- b.setMessage("Download succeeded");
- }
- else
- {
- b.setMessage("Download failed");
- }
- b.setPositiveButton(getString(android.R.string.ok),
- new DialogInterface.OnClickListener()
- {
- @Override
- public void onClick(DialogInterface dlg, int arg1)
- {
- dlg.dismiss();
- }
- });
- b.create().show();
- }
- }.execute(2000);
- }
- public int getType(Cursor cursor, int row, int column) {
- int type = FIELD_TYPE_NULL;
- try {
- CursorWrapper cw = (CursorWrapper) cursor;
- Class<?> cursorWrapper = CursorWrapper.class;
- Field mCursor = cursorWrapper.getDeclaredField("mCursor");
- mCursor.setAccessible(true);
- AbstractWindowedCursor abstractWindowedCursor = (AbstractWindowedCursor) mCursor.get(cw);
- CursorWindow cursorWindow = abstractWindowedCursor.getWindow();
- if (!cursorWindow.isNull(row, column)) {
- if (cursorWindow.isLong(row, column)) type = FIELD_TYPE_INTEGER;
- else if (cursorWindow.isFloat(row, column)) type = FIELD_TYPE_FLOAT;
- else if (cursorWindow.isString(row, column)) type = FIELD_TYPE_STRING;
- else if (cursorWindow.isBlob(row, column)) type = FIELD_TYPE_BLOB;
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return type;
- }
- @Override
- public void onCreate() {
- }
- @Override
- public void onDestroy() {
- }
- }
- readthread = new Thread(new Runnable() {
- public void run() {
- openprogresdialog();
- Can't create handler inside thread that has not called Looper.prepare()
- new Thread()
- {
- @Override
- public void run()
- {
- DialogInterface progressDialog = null;
- progressDialog.dismiss(); // even if initialized cannot update ui in a thread
- }
- }.start();
- new Thread()
- {
- @Override
- public void run()
- {
- Looper.prepare();
- DialogInterface progressDialog = null;
- progressDialog.dismiss();
- }
- }.start();
- DialogInterface progressDialog = null;
- progressDialog.dismiss();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement