InstantUploadBroadcastReceiver.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * Copyright (C) 2012 Bartek Przybylski
  5. * Copyright (C) 2015 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.files;
  21. import android.accounts.Account;
  22. import android.content.BroadcastReceiver;
  23. import android.content.Context;
  24. import android.content.Intent;
  25. import android.content.SharedPreferences;
  26. import android.database.Cursor;
  27. import android.preference.PreferenceManager;
  28. import android.provider.MediaStore.Images;
  29. import android.provider.MediaStore.Video;
  30. import com.owncloud.android.authentication.AccountUtils;
  31. import com.owncloud.android.files.services.FileUploadService;
  32. import com.owncloud.android.files.services.FileUploader;
  33. import com.owncloud.android.lib.common.utils.Log_OC;
  34. import com.owncloud.android.utils.FileStorageUtils;
  35. public class InstantUploadBroadcastReceiver extends BroadcastReceiver {
  36. private static String TAG = InstantUploadBroadcastReceiver.class.getName();
  37. // Image action
  38. // Unofficial action, works for most devices but not HTC. See: https://github.com/owncloud/android/issues/6
  39. private static String NEW_PHOTO_ACTION_UNOFFICIAL = "com.android.camera.NEW_PICTURE";
  40. // Officially supported action since SDK 14:
  41. // http://developer.android.com/reference/android/hardware/Camera.html#ACTION_NEW_PICTURE
  42. private static String NEW_PHOTO_ACTION = "android.hardware.action.NEW_PICTURE";
  43. // Video action
  44. // Officially supported action since SDK 14:
  45. // http://developer.android.com/reference/android/hardware/Camera.html#ACTION_NEW_VIDEO
  46. private static String NEW_VIDEO_ACTION = "android.hardware.action.NEW_VIDEO";
  47. @Override
  48. public void onReceive(Context context, Intent intent) {
  49. Log_OC.d(TAG, "Received: " + intent.getAction());
  50. if (intent.getAction().equals(NEW_PHOTO_ACTION_UNOFFICIAL)) {
  51. handleNewPictureAction(context, intent);
  52. Log_OC.d(TAG, "UNOFFICIAL processed: com.android.camera.NEW_PICTURE");
  53. } else if (intent.getAction().equals(NEW_PHOTO_ACTION)) {
  54. handleNewPictureAction(context, intent);
  55. Log_OC.d(TAG, "OFFICIAL processed: android.hardware.action.NEW_PICTURE");
  56. } else if (intent.getAction().equals(NEW_VIDEO_ACTION)) {
  57. handleNewVideoAction(context, intent);
  58. Log_OC.d(TAG, "OFFICIAL processed: android.hardware.action.NEW_VIDEO");
  59. } else {
  60. Log_OC.e(TAG, "Incorrect intent sent: " + intent.getAction());
  61. }
  62. }
  63. /**
  64. * Because we support NEW_PHOTO_ACTION and NEW_PHOTO_ACTION_UNOFFICIAL it can happen that
  65. * handleNewPictureAction is called twice for the same photo. Use this simple static variable to
  66. * remember last uploaded photo to filter duplicates. Must not be null!
  67. */
  68. static String lastUploadedPhotoPath = "";
  69. private void handleNewPictureAction(Context context, Intent intent) {
  70. Cursor c = null;
  71. String file_path = null;
  72. String file_name = null;
  73. String mime_type = null;
  74. Log_OC.w(TAG, "New photo received");
  75. if (!instantPictureUploadEnabled(context)) {
  76. Log_OC.d(TAG, "Instant picture upload disabled, ignoring new picture");
  77. return;
  78. }
  79. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  80. if (account == null) {
  81. Log_OC.w(TAG, "No ownCloud account found for instant upload, aborting");
  82. return;
  83. }
  84. String[] CONTENT_PROJECTION = { Images.Media.DATA, Images.Media.DISPLAY_NAME, Images.Media.MIME_TYPE,
  85. Images.Media.SIZE };
  86. c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
  87. if (!c.moveToFirst()) {
  88. Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
  89. return;
  90. }
  91. file_path = c.getString(c.getColumnIndex(Images.Media.DATA));
  92. file_name = c.getString(c.getColumnIndex(Images.Media.DISPLAY_NAME));
  93. mime_type = c.getString(c.getColumnIndex(Images.Media.MIME_TYPE));
  94. c.close();
  95. if(file_path.equals(lastUploadedPhotoPath)) {
  96. Log_OC.d(TAG, "Duplicate detected: " + file_path + ". Ignore.");
  97. return;
  98. }
  99. lastUploadedPhotoPath = file_path;
  100. Log_OC.d(TAG, "Path: " + file_path + "");
  101. Intent i = new Intent(context, FileUploadService.class);
  102. i.putExtra(FileUploadService.KEY_ACCOUNT, account);
  103. i.putExtra(FileUploadService.KEY_LOCAL_FILE, file_path);
  104. i.putExtra(FileUploadService.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, file_name));
  105. i.putExtra(FileUploadService.KEY_UPLOAD_TYPE, FileUploadService.UploadQuantity.UPLOAD_SINGLE_FILE);
  106. i.putExtra(FileUploadService.KEY_MIME_TYPE, mime_type);
  107. i.putExtra(FileUploadService.KEY_CREATE_REMOTE_FOLDER, true);
  108. i.putExtra(FileUploadService.KEY_WIFI_ONLY, instantPictureUploadViaWiFiOnly(context));
  109. // On master
  110. // Intent i = new Intent(context, FileUploader.class);
  111. // i.putExtra(FileUploader.KEY_ACCOUNT, account);
  112. // i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
  113. // i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, file_name));
  114. // i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
  115. // i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
  116. // i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
  117. // instant upload behaviour
  118. i = addInstantUploadBehaviour(i, context);
  119. context.startService(i);
  120. }
  121. private Intent addInstantUploadBehaviour(Intent i, Context context){
  122. SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(context);
  123. String behaviour = appPreferences.getString("prefs_instant_behaviour", "NOTHING");
  124. if (behaviour.equalsIgnoreCase("NOTHING")) {
  125. Log_OC.d(TAG, "upload file and do nothing");
  126. i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_FORGET);
  127. } else if (behaviour.equalsIgnoreCase("MOVE")) {
  128. i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE);
  129. Log_OC.d(TAG, "upload file and move file to oc folder");
  130. }
  131. return i;
  132. }
  133. private void handleNewVideoAction(Context context, Intent intent) {
  134. Cursor c = null;
  135. String file_path = null;
  136. String file_name = null;
  137. String mime_type = null;
  138. Log_OC.w(TAG, "New video received");
  139. if (!instantVideoUploadEnabled(context)) {
  140. Log_OC.d(TAG, "Instant video upload disabled, ignoring new video");
  141. return;
  142. }
  143. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  144. if (account == null) {
  145. Log_OC.w(TAG, "No owncloud account found for instant upload, aborting");
  146. return;
  147. }
  148. String[] CONTENT_PROJECTION = { Video.Media.DATA, Video.Media.DISPLAY_NAME, Video.Media.MIME_TYPE,
  149. Video.Media.SIZE };
  150. c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
  151. if (!c.moveToFirst()) {
  152. Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
  153. return;
  154. }
  155. file_path = c.getString(c.getColumnIndex(Video.Media.DATA));
  156. file_name = c.getString(c.getColumnIndex(Video.Media.DISPLAY_NAME));
  157. mime_type = c.getString(c.getColumnIndex(Video.Media.MIME_TYPE));
  158. c.close();
  159. Log_OC.d(TAG, file_path + "");
  160. Intent i = new Intent(context, FileUploadService.class);
  161. i.putExtra(FileUploadService.KEY_ACCOUNT, account);
  162. i.putExtra(FileUploadService.KEY_LOCAL_FILE, file_path);
  163. i.putExtra(FileUploadService.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, file_name));
  164. i.putExtra(FileUploadService.KEY_UPLOAD_TYPE, FileUploadService.UploadQuantity.UPLOAD_SINGLE_FILE);
  165. i.putExtra(FileUploadService.KEY_MIME_TYPE, mime_type);
  166. i.putExtra(FileUploadService.KEY_CREATE_REMOTE_FOLDER, true);
  167. i.putExtra(FileUploadService.KEY_WIFI_ONLY, instantVideoUploadViaWiFiOnly(context));
  168. context.startService(i);
  169. // On master
  170. // if (!isOnline(context) || (instantVideoUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))) {
  171. // return;
  172. // }
  173. //
  174. // Intent i = new Intent(context, FileUploader.class);
  175. // i.putExtra(FileUploader.KEY_ACCOUNT, account);
  176. // i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
  177. // i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantVideoUploadFilePath(context, file_name));
  178. // i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
  179. // i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
  180. // i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
  181. // instant upload behaviour
  182. i = addInstantUploadBehaviour(i, context);
  183. context.startService(i);
  184. }
  185. public static boolean instantPictureUploadEnabled(Context context) {
  186. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_uploading", false);
  187. }
  188. public static boolean instantVideoUploadEnabled(Context context) {
  189. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_uploading", false);
  190. }
  191. public static boolean instantPictureUploadViaWiFiOnly(Context context) {
  192. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_upload_on_wifi", false);
  193. }
  194. public static boolean instantVideoUploadViaWiFiOnly(Context context) {
  195. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_upload_on_wifi", false);
  196. }
  197. }