InstantUploadBroadcastReceiver.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.authentication.AccountUtils;
  33. import com.owncloud.android.db.PreferenceManager;
  34. import com.owncloud.android.files.services.FileUploader;
  35. import com.owncloud.android.lib.common.utils.Log_OC;
  36. import com.owncloud.android.operations.UploadFileOperation;
  37. import com.owncloud.android.utils.FileStorageUtils;
  38. public class InstantUploadBroadcastReceiver extends BroadcastReceiver {
  39. private static String TAG = InstantUploadBroadcastReceiver.class.getName();
  40. // Image action
  41. // Unofficial action, works for most devices but not HTC. See: https://github.com/owncloud/android/issues/6
  42. private static String NEW_PHOTO_ACTION_UNOFFICIAL = "com.android.camera.NEW_PICTURE";
  43. // Officially supported action since SDK 14:
  44. // http://developer.android.com/reference/android/hardware/Camera.html#ACTION_NEW_PICTURE
  45. private static String NEW_PHOTO_ACTION = "android.hardware.action.NEW_PICTURE";
  46. // Video action
  47. // Officially supported action since SDK 14:
  48. // http://developer.android.com/reference/android/hardware/Camera.html#ACTION_NEW_VIDEO
  49. private static String NEW_VIDEO_ACTION = "android.hardware.action.NEW_VIDEO";
  50. @Override
  51. public void onReceive(Context context, Intent intent) {
  52. Log_OC.d(TAG, "Received: " + intent.getAction());
  53. if (intent.getAction().equals(NEW_PHOTO_ACTION_UNOFFICIAL)) {
  54. handleNewPictureAction(context, intent);
  55. Log_OC.d(TAG, "UNOFFICIAL processed: com.android.camera.NEW_PICTURE");
  56. } else if (intent.getAction().equals(NEW_PHOTO_ACTION)) {
  57. handleNewPictureAction(context, intent);
  58. Log_OC.d(TAG, "OFFICIAL processed: android.hardware.action.NEW_PICTURE");
  59. } else if (intent.getAction().equals(NEW_VIDEO_ACTION)) {
  60. handleNewVideoAction(context, intent);
  61. Log_OC.d(TAG, "OFFICIAL processed: android.hardware.action.NEW_VIDEO");
  62. } else {
  63. Log_OC.e(TAG, "Incorrect intent received: " + intent.getAction());
  64. }
  65. }
  66. /**
  67. * Because we support NEW_PHOTO_ACTION and NEW_PHOTO_ACTION_UNOFFICIAL it can happen that
  68. * handleNewPictureAction is called twice for the same photo. Use this simple static variable to
  69. * remember last uploaded photo to filter duplicates. Must not be null!
  70. */
  71. static String lastUploadedPhotoPath = "";
  72. private void handleNewPictureAction(Context context, Intent intent) {
  73. Cursor c = null;
  74. String file_path = null;
  75. String file_name = null;
  76. String mime_type = null;
  77. long date_taken = 0;
  78. Log_OC.i(TAG, "New photo received");
  79. if (!PreferenceManager.instantPictureUploadEnabled(context)) {
  80. Log_OC.d(TAG, "Instant picture upload disabled, ignoring new picture");
  81. return;
  82. }
  83. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  84. if (account == null) {
  85. Log_OC.w(TAG, "No account found for instant upload, aborting");
  86. return;
  87. }
  88. String[] CONTENT_PROJECTION = {
  89. Images.Media.DATA, Images.Media.DISPLAY_NAME, Images.Media.MIME_TYPE, Images.Media.SIZE };
  90. int permissionCheck = ContextCompat.checkSelfPermission(context,
  91. Manifest.permission.READ_EXTERNAL_STORAGE);
  92. if (android.content.pm.PackageManager.PERMISSION_GRANTED != permissionCheck) {
  93. Log_OC.w(TAG, "Read external storage permission isn't granted, aborting");
  94. return;
  95. }
  96. c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
  97. if (!c.moveToFirst()) {
  98. Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
  99. return;
  100. }
  101. file_path = c.getString(c.getColumnIndex(Images.Media.DATA));
  102. file_name = c.getString(c.getColumnIndex(Images.Media.DISPLAY_NAME));
  103. mime_type = c.getString(c.getColumnIndex(Images.Media.MIME_TYPE));
  104. date_taken = System.currentTimeMillis();
  105. c.close();
  106. if (file_path.equals(lastUploadedPhotoPath)) {
  107. Log_OC.d(TAG, "Duplicate detected: " + file_path + ". Ignore.");
  108. return;
  109. }
  110. lastUploadedPhotoPath = file_path;
  111. Log_OC.d(TAG, "Path: " + file_path + "");
  112. new FileUploader.UploadRequester();
  113. int behaviour = getUploadBehaviour(context);
  114. FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
  115. requester.uploadNewFile(
  116. context,
  117. account,
  118. file_path,
  119. FileStorageUtils.getInstantUploadFilePath(context, file_name, date_taken),
  120. behaviour,
  121. mime_type,
  122. true, // create parent folder if not existent
  123. UploadFileOperation.CREATED_AS_INSTANT_PICTURE
  124. );
  125. }
  126. private Integer getUploadBehaviour(Context context) {
  127. SharedPreferences appPreferences = android.preference.PreferenceManager.getDefaultSharedPreferences(context);
  128. String behaviour = appPreferences.getString("prefs_instant_behaviour", "NOTHING");
  129. if (behaviour.equalsIgnoreCase("NOTHING")) {
  130. Log_OC.d(TAG, "upload file and do nothing");
  131. return FileUploader.LOCAL_BEHAVIOUR_FORGET;
  132. } else if (behaviour.equalsIgnoreCase("MOVE")) {
  133. Log_OC.d(TAG, "upload file and move file to oc folder");
  134. return FileUploader.LOCAL_BEHAVIOUR_MOVE;
  135. }
  136. return null;
  137. }
  138. private void handleNewVideoAction(Context context, Intent intent) {
  139. Cursor c = null;
  140. String file_path = null;
  141. String file_name = null;
  142. String mime_type = null;
  143. Log_OC.i(TAG, "New video received");
  144. if (!PreferenceManager.instantVideoUploadEnabled(context)) {
  145. Log_OC.d(TAG, "Instant video upload disabled, ignoring new video");
  146. return;
  147. }
  148. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  149. if (account == null) {
  150. Log_OC.w(TAG, "No account found for instant upload, aborting");
  151. return;
  152. }
  153. String[] CONTENT_PROJECTION = {Video.Media.DATA, Video.Media.DISPLAY_NAME, Video.Media.MIME_TYPE,
  154. Video.Media.SIZE};
  155. c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
  156. if (!c.moveToFirst()) {
  157. Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
  158. return;
  159. }
  160. file_path = c.getString(c.getColumnIndex(Video.Media.DATA));
  161. file_name = c.getString(c.getColumnIndex(Video.Media.DISPLAY_NAME));
  162. mime_type = c.getString(c.getColumnIndex(Video.Media.MIME_TYPE));
  163. c.close();
  164. Log_OC.d(TAG, file_path + "");
  165. int behaviour = getUploadBehaviour(context);
  166. FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
  167. requester.uploadNewFile(
  168. context,
  169. account,
  170. file_path,
  171. FileStorageUtils.getInstantVideoUploadFilePath(context, file_name),
  172. behaviour,
  173. mime_type,
  174. true, // create parent folder if not existent
  175. UploadFileOperation.CREATED_AS_INSTANT_VIDEO
  176. );
  177. }
  178. }