InstantUploadBroadcastReceiver.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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.lib.common.utils.Log_OC;
  33. import com.owncloud.android.utils.FileStorageUtils;
  34. public class InstantUploadBroadcastReceiver extends BroadcastReceiver {
  35. private static String TAG = InstantUploadBroadcastReceiver.class.getName();
  36. // Image action
  37. // Unofficial action, works for most devices but not HTC. See: https://github.com/owncloud/android/issues/6
  38. private static String NEW_PHOTO_ACTION_UNOFFICIAL = "com.android.camera.NEW_PICTURE";
  39. // Officially supported action since SDK 14:
  40. // http://developer.android.com/reference/android/hardware/Camera.html#ACTION_NEW_PICTURE
  41. private static String NEW_PHOTO_ACTION = "android.hardware.action.NEW_PICTURE";
  42. // Video action
  43. // Officially supported action since SDK 14:
  44. // http://developer.android.com/reference/android/hardware/Camera.html#ACTION_NEW_VIDEO
  45. private static String NEW_VIDEO_ACTION = "android.hardware.action.NEW_VIDEO";
  46. @Override
  47. public void onReceive(Context context, Intent intent) {
  48. Log_OC.d(TAG, "Received: " + intent.getAction());
  49. if (intent.getAction().equals(NEW_PHOTO_ACTION_UNOFFICIAL)) {
  50. handleNewPictureAction(context, intent);
  51. Log_OC.d(TAG, "UNOFFICIAL processed: com.android.camera.NEW_PICTURE");
  52. } else if (intent.getAction().equals(NEW_PHOTO_ACTION)) {
  53. handleNewPictureAction(context, intent);
  54. Log_OC.d(TAG, "OFFICIAL processed: android.hardware.action.NEW_PICTURE");
  55. } else if (intent.getAction().equals(NEW_VIDEO_ACTION)) {
  56. handleNewVideoAction(context, intent);
  57. Log_OC.d(TAG, "OFFICIAL processed: android.hardware.action.NEW_VIDEO");
  58. } else {
  59. Log_OC.e(TAG, "Incorrect intent sent: " + intent.getAction());
  60. }
  61. }
  62. /**
  63. * Because we support NEW_PHOTO_ACTION and NEW_PHOTO_ACTION_UNOFFICIAL it can happen that
  64. * handleNewPictureAction is called twice for the same photo. Use this simple static variable to
  65. * remember last uploaded photo to filter duplicates. Must not be null!
  66. */
  67. static String lastUploadedPhotoPath = "";
  68. private void handleNewPictureAction(Context context, Intent intent) {
  69. Cursor c = null;
  70. String file_path = null;
  71. String file_name = null;
  72. String mime_type = null;
  73. Log_OC.w(TAG, "New photo received");
  74. if (!instantPictureUploadEnabled(context)) {
  75. Log_OC.d(TAG, "Instant picture upload disabled, ignoring new picture");
  76. return;
  77. }
  78. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  79. if (account == null) {
  80. Log_OC.w(TAG, "No ownCloud account found for instant upload, aborting");
  81. return;
  82. }
  83. String[] CONTENT_PROJECTION = { Images.Media.DATA, Images.Media.DISPLAY_NAME, Images.Media.MIME_TYPE,
  84. Images.Media.SIZE };
  85. c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
  86. if (!c.moveToFirst()) {
  87. Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
  88. return;
  89. }
  90. file_path = c.getString(c.getColumnIndex(Images.Media.DATA));
  91. file_name = c.getString(c.getColumnIndex(Images.Media.DISPLAY_NAME));
  92. mime_type = c.getString(c.getColumnIndex(Images.Media.MIME_TYPE));
  93. c.close();
  94. if(file_path.equals(lastUploadedPhotoPath)) {
  95. Log_OC.d(TAG, "Duplicate detected: " + file_path + ". Ignore.");
  96. return;
  97. }
  98. lastUploadedPhotoPath = file_path;
  99. Log_OC.d(TAG, "Path: " + file_path + "");
  100. Intent i = new Intent(context, FileUploadService.class);
  101. i.putExtra(FileUploadService.KEY_ACCOUNT, account);
  102. i.putExtra(FileUploadService.KEY_LOCAL_FILE, file_path);
  103. i.putExtra(FileUploadService.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, file_name));
  104. i.putExtra(FileUploadService.KEY_UPLOAD_TYPE, FileUploadService.UPLOAD_SINGLE_FILE);
  105. i.putExtra(FileUploadService.KEY_MIME_TYPE, mime_type);
  106. i.putExtra(FileUploadService.KEY_CREATE_REMOTE_FOLDER, true);
  107. i.putExtra(FileUploadService.KEY_WIFI_ONLY, instantPictureUploadViaWiFiOnly(context));
  108. // On master
  109. // Intent i = new Intent(context, FileUploader.class);
  110. // i.putExtra(FileUploader.KEY_ACCOUNT, account);
  111. // i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
  112. // i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, file_name));
  113. // i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
  114. // i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
  115. // i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
  116. // instant upload behaviour
  117. i = addInstantUploadBehaviour(i, context);
  118. context.startService(i);
  119. }
  120. private Intent addInstantUploadBehaviour(Intent i, Context context){
  121. SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(context);
  122. String behaviour = appPreferences.getString("prefs_instant_behaviour", "NOTHING");
  123. if (behaviour.equalsIgnoreCase("NOTHING")) {
  124. Log_OC.d(TAG, "upload file and do nothing");
  125. i.putExtra(FileUploadService.KEY_LOCAL_BEHAVIOUR, FileUploadService.LOCAL_BEHAVIOUR_FORGET);
  126. } else if (behaviour.equalsIgnoreCase("MOVE")) {
  127. i.putExtra(FileUploadService.KEY_LOCAL_BEHAVIOUR, FileUploadService.LOCAL_BEHAVIOUR_MOVE);
  128. Log_OC.d(TAG, "upload file and move file to oc folder");
  129. }
  130. return i;
  131. }
  132. private void handleNewVideoAction(Context context, Intent intent) {
  133. Cursor c = null;
  134. String file_path = null;
  135. String file_name = null;
  136. String mime_type = null;
  137. Log_OC.w(TAG, "New video received");
  138. if (!instantVideoUploadEnabled(context)) {
  139. Log_OC.d(TAG, "Instant video upload disabled, ignoring new video");
  140. return;
  141. }
  142. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  143. if (account == null) {
  144. Log_OC.w(TAG, "No owncloud account found for instant upload, aborting");
  145. return;
  146. }
  147. String[] CONTENT_PROJECTION = { Video.Media.DATA, Video.Media.DISPLAY_NAME, Video.Media.MIME_TYPE,
  148. Video.Media.SIZE };
  149. c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
  150. if (!c.moveToFirst()) {
  151. Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
  152. return;
  153. }
  154. file_path = c.getString(c.getColumnIndex(Video.Media.DATA));
  155. file_name = c.getString(c.getColumnIndex(Video.Media.DISPLAY_NAME));
  156. mime_type = c.getString(c.getColumnIndex(Video.Media.MIME_TYPE));
  157. c.close();
  158. Log_OC.d(TAG, file_path + "");
  159. Intent i = new Intent(context, FileUploadService.class);
  160. i.putExtra(FileUploadService.KEY_ACCOUNT, account);
  161. i.putExtra(FileUploadService.KEY_LOCAL_FILE, file_path);
  162. i.putExtra(FileUploadService.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, file_name));
  163. i.putExtra(FileUploadService.KEY_UPLOAD_TYPE, FileUploadService.UPLOAD_SINGLE_FILE);
  164. i.putExtra(FileUploadService.KEY_MIME_TYPE, mime_type);
  165. i.putExtra(FileUploadService.KEY_CREATE_REMOTE_FOLDER, true);
  166. i.putExtra(FileUploadService.KEY_WIFI_ONLY, instantVideoUploadViaWiFiOnly(context));
  167. context.startService(i);
  168. // On master
  169. // if (!isOnline(context) || (instantVideoUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))) {
  170. // return;
  171. // }
  172. //
  173. // Intent i = new Intent(context, FileUploader.class);
  174. // i.putExtra(FileUploader.KEY_ACCOUNT, account);
  175. // i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
  176. // i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantVideoUploadFilePath(context, file_name));
  177. // i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
  178. // i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
  179. // i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
  180. // instant upload behaviour
  181. i = addInstantUploadBehaviour(i, context);
  182. context.startService(i);
  183. }
  184. public static boolean instantPictureUploadEnabled(Context context) {
  185. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_uploading", false);
  186. }
  187. public static boolean instantVideoUploadEnabled(Context context) {
  188. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_uploading", false);
  189. }
  190. public static boolean instantPictureUploadViaWiFiOnly(Context context) {
  191. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_upload_on_wifi", false);
  192. }
  193. public static boolean instantVideoUploadViaWiFiOnly(Context context) {
  194. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_upload_on_wifi", false);
  195. }
  196. }