InstantUploadBroadcastReceiver.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012 Bartek Przybylski
  3. * Copyright (C) 2012-2013 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.Images.Media;
  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. @Override
  45. public void onReceive(Context context, Intent intent) {
  46. Log_OC.d(TAG, "Received: " + intent.getAction());
  47. if (intent.getAction().equals(android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
  48. handleConnectivityAction(context, intent);
  49. }else if (intent.getAction().equals(NEW_PHOTO_ACTION_UNOFFICIAL)) {
  50. handleNewPhotoAction(context, intent);
  51. Log_OC.d(TAG, "UNOFFICIAL processed: com.android.camera.NEW_PICTURE");
  52. } else if (intent.getAction().equals(NEW_PHOTO_ACTION)) {
  53. handleNewPhotoAction(context, intent);
  54. Log_OC.d(TAG, "OFFICIAL processed: android.hardware.action.NEW_PICTURE");
  55. } else if (intent.getAction().equals(FileUploader.getUploadFinishMessage())) {
  56. handleUploadFinished(context, intent);
  57. } else {
  58. Log_OC.e(TAG, "Incorrect intent sent: " + intent.getAction());
  59. }
  60. }
  61. private void handleUploadFinished(Context context, Intent intent) {
  62. // remove successfull uploading, ignore rest for reupload on reconnect
  63. /*
  64. if (intent.getBooleanExtra(FileUploader.EXTRA_UPLOAD_RESULT, false)) {
  65. DbHandler db = new DbHandler(context);
  66. String localPath = intent.getStringExtra(FileUploader.EXTRA_OLD_FILE_PATH);
  67. if (!db.removeIUPendingFile(localPath)) {
  68. Log_OC.w(TAG, "Tried to remove non existing instant upload file " + localPath);
  69. }
  70. db.close();
  71. }
  72. */
  73. }
  74. private void handleNewPhotoAction(Context context, Intent intent) {
  75. if (!instantUploadEnabled(context)) {
  76. Log_OC.d(TAG, "Instant upload disabled, aborting uploading");
  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. Cursor c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
  85. if (!c.moveToFirst()) {
  86. Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
  87. return;
  88. }
  89. String file_path = c.getString(c.getColumnIndex(Media.DATA));
  90. String file_name = c.getString(c.getColumnIndex(Media.DISPLAY_NAME));
  91. String mime_type = c.getString(c.getColumnIndex(Media.MIME_TYPE));
  92. c.close();
  93. Log_OC.e(TAG, file_path + "");
  94. // same always temporally the picture to upload
  95. DbHandler db = new DbHandler(context);
  96. db.putFileForLater(file_path, account.name, null);
  97. db.close();
  98. if (!isOnline(context) || (instantUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))) {
  99. return;
  100. }
  101. // register for upload finishe message
  102. // there is a litte problem with android API, we can register for
  103. // particular
  104. // intent in registerReceiver but we cannot unregister from precise
  105. // intent
  106. // we can unregister from entire listenings but thats suck a bit.
  107. // On the other hand this might be only for dynamicly registered
  108. // broadcast receivers, needs investigation.
  109. /*IntentFilter filter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);
  110. context.getApplicationContext().registerReceiver(this, filter);*/
  111. Intent i = new Intent(context, FileUploader.class);
  112. i.putExtra(FileUploader.KEY_ACCOUNT, account);
  113. i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
  114. i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, file_name));
  115. i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
  116. i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
  117. i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
  118. context.startService(i);
  119. }
  120. private void handleConnectivityAction(Context context, Intent intent) {
  121. if (!instantUploadEnabled(context)) {
  122. Log_OC.d(TAG, "Instant upload disabled, abording uploading");
  123. return;
  124. }
  125. if (!intent.hasExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY)
  126. && isOnline(context)
  127. && (!instantUploadViaWiFiOnly(context) || (instantUploadViaWiFiOnly(context) == isConnectedViaWiFi(context) == true))) {
  128. DbHandler db = new DbHandler(context);
  129. Cursor c = db.getAwaitingFiles();
  130. if (c.moveToFirst()) {
  131. //IntentFilter filter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);
  132. //context.getApplicationContext().registerReceiver(this, filter);
  133. do {
  134. String account_name = c.getString(c.getColumnIndex("account"));
  135. String file_path = c.getString(c.getColumnIndex("path"));
  136. File f = new File(file_path);
  137. if (f.exists()) {
  138. Account account = new Account(account_name, MainApp.getAccountType());
  139. String mimeType = null;
  140. try {
  141. mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
  142. f.getName().substring(f.getName().lastIndexOf('.') + 1));
  143. } catch (Throwable e) {
  144. Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + f.getName());
  145. }
  146. if (mimeType == null)
  147. mimeType = "application/octet-stream";
  148. Intent i = new Intent(context, FileUploader.class);
  149. i.putExtra(FileUploader.KEY_ACCOUNT, account);
  150. i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
  151. i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, f.getName()));
  152. i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
  153. i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
  154. context.startService(i);
  155. } else {
  156. Log_OC.w(TAG, "Instant upload file " + f.getAbsolutePath() + " dont exist anymore");
  157. }
  158. } while (c.moveToNext());
  159. }
  160. c.close();
  161. db.close();
  162. }
  163. }
  164. public static boolean isOnline(Context context) {
  165. ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  166. return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
  167. }
  168. public static boolean isConnectedViaWiFi(Context context) {
  169. ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  170. return cm != null && cm.getActiveNetworkInfo() != null
  171. && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI
  172. && cm.getActiveNetworkInfo().getState() == State.CONNECTED;
  173. }
  174. public static boolean instantUploadEnabled(Context context) {
  175. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_uploading", false);
  176. }
  177. public static boolean instantUploadViaWiFiOnly(Context context) {
  178. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_upload_on_wifi", false);
  179. }
  180. }