ContactsBackupJob.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2017 Tobias Kaminsky
  6. * Copyright (C) 2017 Nextcloud GmbH.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.services;
  22. import android.accounts.Account;
  23. import android.content.ComponentName;
  24. import android.content.Context;
  25. import android.content.Intent;
  26. import android.content.ServiceConnection;
  27. import android.database.Cursor;
  28. import android.net.Uri;
  29. import android.os.IBinder;
  30. import android.provider.ContactsContract;
  31. import android.support.annotation.NonNull;
  32. import android.text.format.DateFormat;
  33. import com.evernote.android.job.Job;
  34. import com.evernote.android.job.util.support.PersistableBundleCompat;
  35. import com.owncloud.android.MainApp;
  36. import com.owncloud.android.R;
  37. import com.owncloud.android.authentication.AccountUtils;
  38. import com.owncloud.android.datamodel.ArbitraryDataProvider;
  39. import com.owncloud.android.datamodel.FileDataStorageManager;
  40. import com.owncloud.android.datamodel.OCFile;
  41. import com.owncloud.android.files.services.FileUploader;
  42. import com.owncloud.android.lib.common.utils.Log_OC;
  43. import com.owncloud.android.operations.UploadFileOperation;
  44. import com.owncloud.android.ui.activity.ContactsPreferenceActivity;
  45. import java.io.File;
  46. import java.io.FileWriter;
  47. import java.io.IOException;
  48. import java.io.InputStream;
  49. import java.io.InputStreamReader;
  50. import java.util.ArrayList;
  51. import java.util.Calendar;
  52. import java.util.Vector;
  53. /**
  54. * Job that backup contacts to /Contacts-Backup and deletes files older than x days
  55. */
  56. public class ContactsBackupJob extends Job {
  57. public static final String TAG = "ContactsBackupJob";
  58. public static final String ACCOUNT = "account";
  59. public static final String FORCE = "force";
  60. private OperationsServiceConnection operationsServiceConnection;
  61. private OperationsService.OperationsServiceBinder operationsServiceBinder;
  62. @NonNull
  63. @Override
  64. protected Result onRunJob(Params params) {
  65. final Context context = MainApp.getAppContext();
  66. PersistableBundleCompat bundle = params.getExtras();
  67. boolean force = bundle.getBoolean(FORCE, false);
  68. final Account account = AccountUtils.getOwnCloudAccountByName(context, bundle.getString(ACCOUNT, ""));
  69. ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(getContext().getContentResolver());
  70. Long lastExecution = arbitraryDataProvider.getLongValue(account,
  71. ContactsPreferenceActivity.PREFERENCE_CONTACTS_LAST_BACKUP);
  72. if (force || (lastExecution + 24 * 60 * 60 * 1000) < Calendar.getInstance().getTimeInMillis()) {
  73. Log_OC.d(TAG, "start contacts backup job");
  74. String backupFolder = getContext().getResources().getString(R.string.contacts_backup_folder) +
  75. OCFile.PATH_SEPARATOR;
  76. Integer daysToExpire = getContext().getResources().getInteger(R.integer.contacts_backup_expire);
  77. backupContact(account, backupFolder);
  78. // bind to Operations Service
  79. operationsServiceConnection = new OperationsServiceConnection(daysToExpire, backupFolder, account);
  80. getContext().bindService(new Intent(getContext(), OperationsService.class), operationsServiceConnection,
  81. OperationsService.BIND_AUTO_CREATE);
  82. // store execution date
  83. arbitraryDataProvider.storeOrUpdateKeyValue(account,
  84. ContactsPreferenceActivity.PREFERENCE_CONTACTS_LAST_BACKUP,
  85. String.valueOf(Calendar.getInstance().getTimeInMillis()));
  86. } else {
  87. Log_OC.d(TAG, "last execution less than 24h ago");
  88. }
  89. return Result.SUCCESS;
  90. }
  91. private void backupContact(Account account, String backupFolder) {
  92. ArrayList<String> vCard = new ArrayList<>();
  93. try {
  94. Cursor cursor = getContext().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,
  95. null, null, null);
  96. if (cursor != null && cursor.getCount() > 0) {
  97. cursor.moveToFirst();
  98. for (int i = 0; i < cursor.getCount(); i++) {
  99. vCard.add(getContactFromCursor(cursor));
  100. cursor.moveToNext();
  101. }
  102. }
  103. String filename = DateFormat.format("yyyy-MM-dd_HH-mm-ss", Calendar.getInstance()).toString() + ".vcf";
  104. Log_OC.d(TAG, "Storing: " + filename);
  105. File file = new File(getContext().getCacheDir(), filename);
  106. FileWriter fw = null;
  107. try {
  108. fw = new FileWriter(file);
  109. for (String card : vCard) {
  110. fw.write(card);
  111. }
  112. } catch (IOException e) {
  113. Log_OC.d(TAG, "Error ", e);
  114. } finally {
  115. if (fw != null) {
  116. try {
  117. fw.close();
  118. } catch (IOException e) {
  119. Log_OC.d(TAG, "Error closing file writer ", e);
  120. }
  121. }
  122. }
  123. FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
  124. requester.uploadNewFile(
  125. getContext(),
  126. account,
  127. file.getAbsolutePath(),
  128. backupFolder + filename,
  129. FileUploader.LOCAL_BEHAVIOUR_MOVE,
  130. null,
  131. true,
  132. UploadFileOperation.CREATED_BY_USER
  133. );
  134. } catch (Exception e) {
  135. Log_OC.d(TAG, e.getMessage());
  136. }
  137. }
  138. private void expireFiles(Integer daysToExpire, String backupFolderString, Account account) {
  139. // -1 disables expiration
  140. if (daysToExpire > -1) {
  141. FileDataStorageManager storageManager = new FileDataStorageManager(account, getContext().getContentResolver());
  142. OCFile backupFolder = storageManager.getFileByPath(backupFolderString);
  143. Calendar cal = Calendar.getInstance();
  144. cal.add(Calendar.DAY_OF_YEAR, -daysToExpire);
  145. Long timestampToExpire = cal.getTimeInMillis();
  146. if (backupFolder != null) {
  147. Log_OC.d(TAG, "expire: " + daysToExpire + " " + backupFolder.getFileName());
  148. }
  149. Vector<OCFile> backups = storageManager.getFolderContent(backupFolder, false);
  150. for (OCFile backup : backups) {
  151. if (timestampToExpire > backup.getModificationTimestamp()) {
  152. Log_OC.d(TAG, "delete " + backup.getRemotePath());
  153. // delete backups
  154. Intent service = new Intent(getContext(), OperationsService.class);
  155. service.setAction(OperationsService.ACTION_REMOVE);
  156. service.putExtra(OperationsService.EXTRA_ACCOUNT, account);
  157. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, backup.getRemotePath());
  158. service.putExtra(OperationsService.EXTRA_REMOVE_ONLY_LOCAL, false);
  159. operationsServiceBinder.queueNewOperation(service);
  160. }
  161. }
  162. }
  163. getContext().unbindService(operationsServiceConnection);
  164. }
  165. private String getContactFromCursor(Cursor cursor) {
  166. String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
  167. Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
  168. String vCard = "";
  169. try {
  170. InputStream inputStream = getContext().getContentResolver().openInputStream(uri);
  171. InputStreamReader inputStreamReader;
  172. char[] buffer = new char[1024];
  173. StringBuilder stringBuilder = new StringBuilder();
  174. if (inputStream != null) {
  175. inputStreamReader = new InputStreamReader(inputStream);
  176. while (true) {
  177. int byteCount = inputStreamReader.read(buffer, 0, buffer.length);
  178. if (byteCount > 0) {
  179. stringBuilder.append(buffer, 0, byteCount);
  180. } else {
  181. break;
  182. }
  183. }
  184. }
  185. vCard = stringBuilder.toString();
  186. return vCard;
  187. } catch (IOException e) {
  188. Log_OC.d(TAG, e.getMessage());
  189. }
  190. return vCard;
  191. }
  192. /**
  193. * Implements callback methods for service binding.
  194. */
  195. private class OperationsServiceConnection implements ServiceConnection {
  196. private Integer daysToExpire;
  197. private String backupFolder;
  198. private Account account;
  199. OperationsServiceConnection(Integer daysToExpire, String backupFolder, Account account) {
  200. this.daysToExpire = daysToExpire;
  201. this.backupFolder = backupFolder;
  202. this.account = account;
  203. }
  204. @Override
  205. public void onServiceConnected(ComponentName component, IBinder service) {
  206. Log_OC.d(TAG, "service connected");
  207. if (component.equals(new ComponentName(getContext(), OperationsService.class))) {
  208. operationsServiceBinder = (OperationsService.OperationsServiceBinder) service;
  209. expireFiles(daysToExpire, backupFolder, account);
  210. }
  211. }
  212. @Override
  213. public void onServiceDisconnected(ComponentName component) {
  214. Log_OC.d(TAG, "service disconnected");
  215. if (component.equals(new ComponentName(getContext(), OperationsService.class))) {
  216. operationsServiceBinder = null;
  217. }
  218. }
  219. }
  220. }