InstantUploadBroadcastReceiver.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.utils.FileStorageUtils;
  25. import com.owncloud.android.utils.Log_OC;
  26. import android.accounts.Account;
  27. import android.content.BroadcastReceiver;
  28. import android.content.Context;
  29. import android.content.Intent;
  30. //import android.content.IntentFilter;
  31. import android.database.Cursor;
  32. import android.net.ConnectivityManager;
  33. import android.net.NetworkInfo.State;
  34. import android.preference.PreferenceManager;
  35. import android.provider.MediaStore.*;
  36. import android.webkit.MimeTypeMap;
  37. public class InstantUploadBroadcastReceiver extends BroadcastReceiver {
  38. private static String TAG = "InstantUploadBroadcastReceiver";
  39. // private static final String[] CONTENT_PROJECTION = { Media.DATA, Media.DISPLAY_NAME, Media.MIME_TYPE, Media.SIZE };
  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. 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(android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
  50. handleConnectivityAction(context, intent);
  51. }else if (intent.getAction().equals(NEW_PHOTO_ACTION_UNOFFICIAL)) {
  52. handleNewMediaAction(context, intent); //handleNewPhotoAction(context, intent);
  53. Log_OC.d(TAG, "UNOFFICIAL processed: com.android.camera.NEW_PICTURE");
  54. } else if (intent.getAction().equals(NEW_PHOTO_ACTION)) {
  55. handleNewMediaAction(context, intent); //handleNewPhotoAction(context, intent);
  56. Log_OC.d(TAG, "OFFICIAL processed: android.hardware.action.NEW_PICTURE");
  57. } else if (intent.getAction().equals(NEW_PHOTO_ACTION) || intent.getAction().equals(NEW_VIDEO_ACTION)) {
  58. handleNewMediaAction(context, intent);
  59. } else if (intent.getAction().equals(FileUploader.getUploadFinishMessage())) {
  60. handleUploadFinished(context, intent);
  61. } else {
  62. Log_OC.e(TAG, "Incorrect intent sent: " + intent.getAction());
  63. }
  64. }
  65. private void handleUploadFinished(Context context, Intent intent) {
  66. // remove successfull uploading, ignore rest for reupload on reconnect
  67. /*
  68. if (intent.getBooleanExtra(FileUploader.EXTRA_UPLOAD_RESULT, false)) {
  69. DbHandler db = new DbHandler(context);
  70. String localPath = intent.getStringExtra(FileUploader.EXTRA_OLD_FILE_PATH);
  71. if (!db.removeIUPendingFile(localPath)) {
  72. Log_OC.w(TAG, "Tried to remove non existing instant upload file " + localPath);
  73. }
  74. db.close();
  75. }
  76. */
  77. }
  78. private void handleNewMediaAction(Context context, Intent intent) {
  79. Cursor c = null;
  80. String file_path = null;
  81. String file_name = null;
  82. String mime_type = null;
  83. if (!instantUploadEnabled(context)) {
  84. Log_OC.d(TAG, "Instant upload disabled, aborting uploading");
  85. return;
  86. }
  87. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  88. if (account == null) {
  89. Log_OC.w(TAG, "No owncloud account found for instant upload, aborting");
  90. return;
  91. }
  92. if (intent.getAction().equals(NEW_PHOTO_ACTION) || intent.getAction().equals(NEW_PHOTO_ACTION_UNOFFICIAL)) {
  93. String[] CONTENT_PROJECTION = { Images.Media.DATA, Images.Media.DISPLAY_NAME, Images.Media.MIME_TYPE, Images.Media.SIZE };
  94. c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
  95. if (!c.moveToFirst()) {
  96. Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
  97. return;
  98. }
  99. file_path = c.getString(c.getColumnIndex(Images.Media.DATA));
  100. file_name = c.getString(c.getColumnIndex(Images.Media.DISPLAY_NAME));
  101. mime_type = c.getString(c.getColumnIndex(Images.Media.MIME_TYPE));
  102. Log_OC.w(TAG, "New photo received");
  103. }
  104. else if (intent.getAction().equals(NEW_VIDEO_ACTION)) {
  105. if (!isConnectedViaWiFi(context)) {
  106. Log_OC.e(TAG, "No Wifi available .. Video instant upload only possible if WiFi is on");
  107. return;
  108. }
  109. String[] CONTENT_PROJECTION = { Video.Media.DATA, Video.Media.DISPLAY_NAME, Video.Media.MIME_TYPE, Video.Media.SIZE };
  110. c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
  111. if (!c.moveToFirst()) {
  112. Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
  113. return;
  114. }
  115. file_path = c.getString(c.getColumnIndex(Video.Media.DATA));
  116. file_name = c.getString(c.getColumnIndex(Video.Media.DISPLAY_NAME));
  117. mime_type = c.getString(c.getColumnIndex(Video.Media.MIME_TYPE));
  118. Log_OC.w(TAG, "New video received");
  119. }
  120. c.close();
  121. Log_OC.d(TAG, file_path + "");
  122. // same always temporally the picture to upload
  123. DbHandler db = new DbHandler(context);
  124. db.putFileForLater(file_path, account.name, null);
  125. db.close();
  126. if (!isOnline(context) || (instantUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))) {
  127. return;
  128. }
  129. // register for upload finishe message
  130. // there is a litte problem with android API, we can register for
  131. // particular
  132. // intent in registerReceiver but we cannot unregister from precise
  133. // intent
  134. // we can unregister from entire listenings but thats suck a bit.
  135. // On the other hand this might be only for dynamicly registered
  136. // broadcast receivers, needs investigation.
  137. /*IntentFilter filter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);
  138. context.getApplicationContext().registerReceiver(this, filter);*/
  139. Intent i = new Intent(context, FileUploader.class);
  140. i.putExtra(FileUploader.KEY_ACCOUNT, account);
  141. i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
  142. i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, file_name));
  143. i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
  144. i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
  145. i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
  146. context.startService(i);
  147. }
  148. private void handleConnectivityAction(Context context, Intent intent) {
  149. if (!instantUploadEnabled(context)) {
  150. Log_OC.d(TAG, "Instant upload disabled, abording uploading");
  151. return;
  152. }
  153. if (!intent.hasExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY)
  154. && isOnline(context)
  155. && (!instantUploadViaWiFiOnly(context) || (instantUploadViaWiFiOnly(context) == isConnectedViaWiFi(context) == true))) {
  156. DbHandler db = new DbHandler(context);
  157. Cursor c = db.getAwaitingFiles();
  158. if (c.moveToFirst()) {
  159. //IntentFilter filter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);
  160. //context.getApplicationContext().registerReceiver(this, filter);
  161. do {
  162. String account_name = c.getString(c.getColumnIndex("account"));
  163. String file_path = c.getString(c.getColumnIndex("path"));
  164. File f = new File(file_path);
  165. if (f.exists()) {
  166. Account account = new Account(account_name, MainApp.getAccountType());
  167. String mimeType = null;
  168. try {
  169. mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
  170. f.getName().substring(f.getName().lastIndexOf('.') + 1));
  171. } catch (Throwable e) {
  172. Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + f.getName());
  173. }
  174. if (mimeType == null)
  175. mimeType = "application/octet-stream";
  176. Intent i = new Intent(context, FileUploader.class);
  177. i.putExtra(FileUploader.KEY_ACCOUNT, account);
  178. i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
  179. i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, f.getName()));
  180. i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
  181. i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
  182. context.startService(i);
  183. } else {
  184. Log_OC.w(TAG, "Instant upload file " + f.getAbsolutePath() + " dont exist anymore");
  185. }
  186. } while (c.moveToNext());
  187. }
  188. c.close();
  189. db.close();
  190. }
  191. }
  192. public static boolean isOnline(Context context) {
  193. ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  194. return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
  195. }
  196. public static boolean isConnectedViaWiFi(Context context) {
  197. ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  198. return cm != null && cm.getActiveNetworkInfo() != null
  199. && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI
  200. && cm.getActiveNetworkInfo().getState() == State.CONNECTED;
  201. }
  202. public static boolean instantUploadEnabled(Context context) {
  203. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_uploading", false);
  204. }
  205. public static boolean instantUploadViaWiFiOnly(Context context) {
  206. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_upload_on_wifi", false);
  207. }
  208. }