From 9fd0b434fe58d99de532a113c301ff7bdff8c763 Mon Sep 17 00:00:00 2001 From: danijoo Date: Fri, 8 Jan 2016 02:24:23 +0100 Subject: [PATCH] export data --- app/src/main/AndroidManifest.xml | 2 + .../preferences/PreferenceActivity.java | 11 ++ .../preferences/PreferenceFragment.java | 123 ++++++++++++++++-- app/src/main/res/values/strings.xml | 5 + 4 files changed, 133 insertions(+), 8 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6ade294..cdf084a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,6 +1,8 @@ + + () { + + @Override + protected String doInBackground(Void... params) { + Cursor cursor = getContext().getContentResolver().query( + Logged_notificationTable.CONTENT_URI, + null, null, null, Logged_notificationTable.FIELD_DATE + " ASC" + ); + + JsonArray json = new JsonArray(); + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + if (cursor != null && cursor.moveToFirst()) { + do { + LoggedNotification notification = Logged_notificationTable.getRow(cursor, false); + json.add(gson.toJson(notification, LoggedNotification.class)); + } while (cursor.moveToNext()); + } + if (cursor != null) + cursor.close(); + + File sdCard = Environment.getExternalStorageDirectory(); + File file = new File(sdCard, getContext().getString(R.string.notification_export_filename)); + FileOutputStream fos = null; + try { + if (!file.exists()) + file.createNewFile(); + fos = new FileOutputStream(file, false); + fos.write(gson.toJson(json).getBytes()); + } catch (IOException e) { + Timber.e(e, "Failed to export data"); + return ""; + } finally { + if (fos != null) + try { + fos.close(); + } catch (IOException ignored) { + } + } + return file.getAbsolutePath(); + } + + @Override + protected void onPostExecute(String path) { + if (progressDialog != null && progressDialog.isShowing()) + progressDialog.dismiss(); + if (getActivity() != null) { + String message; + if (path.isEmpty()) + message = getContext().getString(R.string.pref_export_snack_failed); + else + message = String.format(getContext().getString(R.string.pref_export_snack_success), path); + Snackbar.make(getActivity().findViewById(android.R.id.content), message, Snackbar.LENGTH_SHORT).show(); + } + } + }.execute(); } private void askClearDatabase() { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 632d3bc..5cf9526 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -72,6 +72,11 @@ Remove %s from blacklist? OK Cancel + notification_export.json + Cannot export without file access + Exporting ... + Failed to export data. + Exported to %s. Alarm