InstantUploadBroadcastReceiver.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012 Bartek Przybylski
  3. * Copyright (C) 2012-2014 ownCloud Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2,
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. package com.owncloud.android.files;
  19. import java.io.File;
  20. import com.owncloud.android.MainApp;
  21. import com.owncloud.android.authentication.AccountUtils;
  22. import com.owncloud.android.db.DbHandler;
  23. import com.owncloud.android.files.services.FileUploader;
  24. import com.owncloud.android.lib.common.utils.Log_OC;
  25. import com.owncloud.android.utils.FileStorageUtils;
  26. import android.accounts.Account;
  27. import android.content.BroadcastReceiver;
  28. import android.content.Context;
  29. import android.content.Intent;
  30. import android.database.Cursor;
  31. import android.net.ConnectivityManager;
  32. import android.net.NetworkInfo.State;
  33. import android.preference.PreferenceManager;
  34. import android.provider.MediaStore.Images;
  35. import android.provider.MediaStore.Video;
  36. import android.webkit.MimeTypeMap;
  37. public class InstantUploadBroadcastReceiver extends BroadcastReceiver {
  38. private static String TAG = InstantUploadBroadcastReceiver.class.getName();
  39. // Image action
  40. // Unofficial action, works for most devices but not HTC. See: https://github.com/owncloud/android/issues/6
  41. private static String NEW_PHOTO_ACTION_UNOFFICIAL = "com.android.camera.NEW_PICTURE";
  42. // Officially supported action since SDK 14: http://developer.android.com/reference/android/hardware/Camera.html#ACTION_NEW_PICTURE
  43. private static String NEW_PHOTO_ACTION = "android.hardware.action.NEW_PICTURE";
  44. // Video action
  45. // Officially supported action since SDK 14: 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(android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
  51. handleConnectivityAction(context, intent);
  52. }else if (intent.getAction().equals(NEW_PHOTO_ACTION_UNOFFICIAL)) {
  53. handleNewPictureAction(context, intent);
  54. Log_OC.d(TAG, "UNOFFICIAL processed: com.android.camera.NEW_PICTURE");
  55. } else if (intent.getAction().equals(NEW_PHOTO_ACTION)) {
  56. handleNewPictureAction(context, intent);
  57. Log_OC.d(TAG, "OFFICIAL processed: android.hardware.action.NEW_PICTURE");
  58. } else if (intent.getAction().equals(NEW_VIDEO_ACTION)) {
  59. Log_OC.d(TAG, "OFFICIAL processed: android.hardware.action.NEW_VIDEO");
  60. handleNewVideoAction(context, intent);
  61. } else {
  62. Log_OC.e(TAG, "Incorrect intent sent: " + intent.getAction());
  63. }
  64. }
  65. private void handleNewPictureAction(Context context, Intent intent) {
  66. Cursor c = null;
  67. String file_path = null;
  68. String file_name = null;
  69. String mime_type = null;
  70. Log_OC.w(TAG, "New photo received");
  71. if (!instantPictureUploadEnabled(context)) {
  72. Log_OC.d(TAG, "Instant picture upload disabled, ignoring new picture");
  73. return;
  74. }
  75. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  76. if (account == null) {
  77. Log_OC.w(TAG, "No owncloud account found for instant upload, aborting");
  78. return;
  79. }
  80. String[] CONTENT_PROJECTION = { Images.Media.DATA, Images.Media.DISPLAY_NAME, Images.Media.MIME_TYPE, Images.Media.SIZE };
  81. c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
  82. if (!c.moveToFirst()) {
  83. Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
  84. return;
  85. }
  86. file_path = c.getString(c.getColumnIndex(Images.Media.DATA));
  87. file_name = c.getString(c.getColumnIndex(Images.Media.DISPLAY_NAME));
  88. mime_type = c.getString(c.getColumnIndex(Images.Media.MIME_TYPE));
  89. c.close();
  90. Log_OC.d(TAG, file_path + "");
  91. // save always temporally the picture to upload
  92. DbHandler db = new DbHandler(context);
  93. db.putFileForLater(file_path, account.name, null);
  94. db.close();
  95. if (!isOnline(context) || (instantPictureUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))) {
  96. return;
  97. }
  98. Intent i = new Intent(context, FileUploader.class);
  99. i.putExtra(FileUploader.KEY_ACCOUNT, account);
  100. i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
  101. i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, file_name));
  102. i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
  103. i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
  104. i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
  105. context.startService(i);
  106. }
  107. private void handleNewVideoAction(Context context, Intent intent) {
  108. Cursor c = null;
  109. String file_path = null;
  110. String file_name = null;
  111. String mime_type = null;
  112. Log_OC.w(TAG, "New video received");
  113. if (!instantVideoUploadEnabled(context)) {
  114. Log_OC.d(TAG, "Instant video upload disabled, ignoring new video");
  115. return;
  116. }
  117. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  118. if (account == null) {
  119. Log_OC.w(TAG, "No owncloud account found for instant upload, aborting");
  120. return;
  121. }
  122. String[] CONTENT_PROJECTION = { Video.Media.DATA, Video.Media.DISPLAY_NAME, Video.Media.MIME_TYPE, Video.Media.SIZE };
  123. c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
  124. if (!c.moveToFirst()) {
  125. Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
  126. return;
  127. }
  128. file_path = c.getString(c.getColumnIndex(Video.Media.DATA));
  129. file_name = c.getString(c.getColumnIndex(Video.Media.DISPLAY_NAME));
  130. mime_type = c.getString(c.getColumnIndex(Video.Media.MIME_TYPE));
  131. c.close();
  132. Log_OC.d(TAG, file_path + "");
  133. if (!isOnline(context) || (instantVideoUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))) {
  134. return;
  135. }
  136. Intent i = new Intent(context, FileUploader.class);
  137. i.putExtra(FileUploader.KEY_ACCOUNT, account);
  138. i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
  139. i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantVideoUploadFilePath(context, file_name));
  140. i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
  141. i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
  142. i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
  143. context.startService(i);
  144. }
  145. private void handleConnectivityAction(Context context, Intent intent) {
  146. if (!instantPictureUploadEnabled(context)) {
  147. Log_OC.d(TAG, "Instant upload disabled, don't upload anything");
  148. return;
  149. }
  150. if (!intent.hasExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY)
  151. && isOnline(context)
  152. && (!instantPictureUploadViaWiFiOnly(context) || (instantPictureUploadViaWiFiOnly(context) == isConnectedViaWiFi(context) == true))) {
  153. DbHandler db = new DbHandler(context);
  154. Cursor c = db.getAwaitingFiles();
  155. if (c.moveToFirst()) {
  156. do {
  157. String account_name = c.getString(c.getColumnIndex("account"));
  158. String file_path = c.getString(c.getColumnIndex("path"));
  159. File f = new File(file_path);
  160. if (f.exists()) {
  161. Account account = new Account(account_name, MainApp.getAccountType());
  162. String mimeType = null;
  163. try {
  164. mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
  165. f.getName().substring(f.getName().lastIndexOf('.') + 1));
  166. } catch (Throwable e) {
  167. Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + f.getName());
  168. }
  169. if (mimeType == null)
  170. mimeType = "application/octet-stream";
  171. Intent i = new Intent(context, FileUploader.class);
  172. i.putExtra(FileUploader.KEY_ACCOUNT, account);
  173. i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
  174. i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, f.getName()));
  175. i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
  176. i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
  177. context.startService(i);
  178. } else {
  179. Log_OC.w(TAG, "Instant upload file " + f.getAbsolutePath() + " dont exist anymore");
  180. }
  181. } while (c.moveToNext());
  182. }
  183. c.close();
  184. db.close();
  185. }
  186. }
  187. public static boolean isOnline(Context context) {
  188. ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  189. return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
  190. }
  191. public static boolean isConnectedViaWiFi(Context context) {
  192. ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  193. return cm != null && cm.getActiveNetworkInfo() != null
  194. && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI
  195. && cm.getActiveNetworkInfo().getState() == State.CONNECTED;
  196. }
  197. public static boolean instantPictureUploadEnabled(Context context) {
  198. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_uploading", false);
  199. }
  200. public static boolean instantVideoUploadEnabled(Context context) {
  201. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_uploading", false);
  202. }
  203. public static boolean instantPictureUploadViaWiFiOnly(Context context) {
  204. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_upload_on_wifi", false);
  205. }
  206. public static boolean instantVideoUploadViaWiFiOnly(Context context) {
  207. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_upload_on_wifi", false);
  208. }
  209. }