InstantUploadBroadcastReceiver.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author Bartek Przybylski
  5. * @author David A. Velasco
  6. * Copyright (C) 2012 Bartek Przybylski
  7. * Copyright (C) 2016 ownCloud Inc.
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2,
  11. * as published by the Free Software Foundation.
  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 General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.files;
  22. import android.Manifest;
  23. import android.accounts.Account;
  24. import android.content.BroadcastReceiver;
  25. import android.content.Context;
  26. import android.content.Intent;
  27. import android.content.SharedPreferences;
  28. import android.database.Cursor;
  29. import android.provider.MediaStore.Images;
  30. import android.provider.MediaStore.Video;
  31. import android.support.v4.content.ContextCompat;
  32. import com.owncloud.android.R;
  33. import com.owncloud.android.authentication.AccountUtils;
  34. import com.owncloud.android.db.PreferenceManager;
  35. import com.owncloud.android.files.services.FileUploader;
  36. import com.owncloud.android.lib.common.utils.Log_OC;
  37. import com.owncloud.android.operations.UploadFileOperation;
  38. import com.owncloud.android.utils.FileStorageUtils;
  39. public class InstantUploadBroadcastReceiver extends BroadcastReceiver {
  40. private static String TAG = InstantUploadBroadcastReceiver.class.getName();
  41. // Image action
  42. // Unofficial action, works for most devices but not HTC. See: https://github.com/owncloud/android/issues/6
  43. private static String NEW_PHOTO_ACTION_UNOFFICIAL = "com.android.camera.NEW_PICTURE";
  44. // Officially supported action since SDK 14:
  45. // http://developer.android.com/reference/android/hardware/Camera.html#ACTION_NEW_PICTURE
  46. private static String NEW_PHOTO_ACTION = "android.hardware.action.NEW_PICTURE";
  47. // Video action
  48. // Officially supported action since SDK 14:
  49. // http://developer.android.com/reference/android/hardware/Camera.html#ACTION_NEW_VIDEO
  50. private static String NEW_VIDEO_ACTION = "android.hardware.action.NEW_VIDEO";
  51. @Override
  52. public void onReceive(Context context, Intent intent) {
  53. Log_OC.d(TAG, "Received: " + intent.getAction());
  54. if (intent.getAction().equals(NEW_PHOTO_ACTION_UNOFFICIAL)) {
  55. handleNewPictureAction(context, intent);
  56. Log_OC.d(TAG, "UNOFFICIAL processed: com.android.camera.NEW_PICTURE");
  57. } else if (intent.getAction().equals(NEW_PHOTO_ACTION)) {
  58. handleNewPictureAction(context, intent);
  59. Log_OC.d(TAG, "OFFICIAL processed: android.hardware.action.NEW_PICTURE");
  60. } else if (intent.getAction().equals(NEW_VIDEO_ACTION)) {
  61. handleNewVideoAction(context, intent);
  62. Log_OC.d(TAG, "OFFICIAL processed: android.hardware.action.NEW_VIDEO");
  63. } else {
  64. Log_OC.e(TAG, "Incorrect intent received: " + intent.getAction());
  65. }
  66. }
  67. /**
  68. * Because we support NEW_PHOTO_ACTION and NEW_PHOTO_ACTION_UNOFFICIAL it can happen that
  69. * handleNewPictureAction is called twice for the same photo. Use this simple static variable to
  70. * remember last uploaded photo to filter duplicates. Must not be null!
  71. */
  72. static String lastUploadedPhotoPath = "";
  73. private void handleNewPictureAction(Context context, Intent intent) {
  74. Cursor c = null;
  75. String file_path = null;
  76. String file_name = null;
  77. String mime_type = null;
  78. long date_taken = 0;
  79. Log_OC.i(TAG, "New photo received");
  80. if (!PreferenceManager.instantPictureUploadEnabled(context)) {
  81. Log_OC.d(TAG, "Instant picture upload disabled, ignoring new picture");
  82. return;
  83. }
  84. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  85. if (account == null) {
  86. Log_OC.w(TAG, "No account found for instant upload, aborting");
  87. return;
  88. }
  89. String[] CONTENT_PROJECTION = {
  90. Images.Media.DATA, Images.Media.DISPLAY_NAME, Images.Media.MIME_TYPE, Images.Media.SIZE };
  91. int permissionCheck = ContextCompat.checkSelfPermission(context,
  92. Manifest.permission.READ_EXTERNAL_STORAGE);
  93. if (android.content.pm.PackageManager.PERMISSION_GRANTED != permissionCheck) {
  94. Log_OC.w(TAG, "Read external storage permission isn't granted, aborting");
  95. return;
  96. }
  97. c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
  98. if (!c.moveToFirst()) {
  99. Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
  100. return;
  101. }
  102. file_path = c.getString(c.getColumnIndex(Images.Media.DATA));
  103. file_name = c.getString(c.getColumnIndex(Images.Media.DISPLAY_NAME));
  104. mime_type = c.getString(c.getColumnIndex(Images.Media.MIME_TYPE));
  105. date_taken = System.currentTimeMillis();
  106. c.close();
  107. if (file_path.equals(lastUploadedPhotoPath)) {
  108. Log_OC.d(TAG, "Duplicate detected: " + file_path + ". Ignore.");
  109. return;
  110. }
  111. lastUploadedPhotoPath = file_path;
  112. Log_OC.d(TAG, "Path: " + file_path + "");
  113. new FileUploader.UploadRequester();
  114. int behaviour = getUploadBehaviour(context);
  115. Boolean subfolderByDate = com.owncloud.android.db.PreferenceManager.instantPictureUploadPathUseSubfolders(context);
  116. SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
  117. String uploadPathdef = context.getString(R.string.instant_upload_path);
  118. String uploadPath = pref.getString("instant_upload_path", uploadPathdef);
  119. FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
  120. requester.uploadNewFile(
  121. context,
  122. account,
  123. file_path,
  124. FileStorageUtils.getInstantUploadFilePath(uploadPath, file_name, date_taken, subfolderByDate),
  125. behaviour,
  126. mime_type,
  127. true, // create parent folder if not existent
  128. UploadFileOperation.CREATED_AS_INSTANT_PICTURE
  129. );
  130. }
  131. private Integer getUploadBehaviour(Context context) {
  132. SharedPreferences appPreferences = android.preference.PreferenceManager.getDefaultSharedPreferences(context);
  133. String behaviour = appPreferences.getString("prefs_instant_behaviour", "NOTHING");
  134. if (behaviour.equalsIgnoreCase("NOTHING")) {
  135. Log_OC.d(TAG, "upload file and do nothing");
  136. return FileUploader.LOCAL_BEHAVIOUR_FORGET;
  137. } else if (behaviour.equalsIgnoreCase("MOVE")) {
  138. Log_OC.d(TAG, "upload file and move file to oc folder");
  139. return FileUploader.LOCAL_BEHAVIOUR_MOVE;
  140. } else if (behaviour.equalsIgnoreCase("DELETE")) {
  141. Log_OC.d(TAG, "upload file and delete original file");
  142. return FileUploader.LOCAL_BEHAVIOUR_DELETE;
  143. }
  144. return null;
  145. }
  146. private void handleNewVideoAction(Context context, Intent intent) {
  147. Cursor c = null;
  148. String file_path = null;
  149. String file_name = null;
  150. String mime_type = null;
  151. long date_taken = 0;
  152. Log_OC.i(TAG, "New video received");
  153. if (!PreferenceManager.instantVideoUploadEnabled(context)) {
  154. Log_OC.d(TAG, "Instant video upload disabled, ignoring new video");
  155. return;
  156. }
  157. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  158. if (account == null) {
  159. Log_OC.w(TAG, "No account found for instant upload, aborting");
  160. return;
  161. }
  162. String[] CONTENT_PROJECTION = {Video.Media.DATA, Video.Media.DISPLAY_NAME, Video.Media.MIME_TYPE,
  163. Video.Media.SIZE};
  164. c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
  165. if (!c.moveToFirst()) {
  166. Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
  167. return;
  168. }
  169. file_path = c.getString(c.getColumnIndex(Video.Media.DATA));
  170. file_name = c.getString(c.getColumnIndex(Video.Media.DISPLAY_NAME));
  171. mime_type = c.getString(c.getColumnIndex(Video.Media.MIME_TYPE));
  172. c.close();
  173. date_taken = System.currentTimeMillis();
  174. Log_OC.d(TAG, file_path + "");
  175. int behaviour = getUploadBehaviour(context);
  176. FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
  177. requester.uploadNewFile(
  178. context,
  179. account,
  180. file_path,
  181. FileStorageUtils.getInstantVideoUploadFilePath(context, file_name, date_taken),
  182. behaviour,
  183. mime_type,
  184. true, // create parent folder if not existent
  185. UploadFileOperation.CREATED_AS_INSTANT_VIDEO
  186. );
  187. }
  188. }