InstantUploadBroadcastReceiver.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.authentication.AccountAuthenticator;
  21. import com.owncloud.android.authentication.AccountUtils;
  22. import com.owncloud.android.db.DbHandler;
  23. import com.owncloud.android.files.services.FileUploader;
  24. import android.accounts.Account;
  25. import android.content.BroadcastReceiver;
  26. import android.content.Context;
  27. import android.content.Intent;
  28. import android.content.IntentFilter;
  29. import android.database.Cursor;
  30. import android.net.ConnectivityManager;
  31. import android.net.NetworkInfo.State;
  32. import android.preference.PreferenceManager;
  33. import android.provider.MediaStore.Images.Media;
  34. import android.webkit.MimeTypeMap;
  35. import com.owncloud.android.Log_OC;
  36. import com.owncloud.android.utils.FileStorageUtils;
  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 (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH &&
  50. intent.getAction().equals(NEW_PHOTO_ACTION_UNOFFICIAL)) {
  51. handleNewPhotoAction(context, intent);
  52. Log_OC.d(TAG, "UNOFFICIAL processed: com.android.camera.NEW_PICTURE");
  53. } else if (intent.getAction().equals(NEW_PHOTO_ACTION)) {
  54. handleNewPhotoAction(context, intent);
  55. Log_OC.d(TAG, "OFFICIAL processed: android.hardware.action.NEW_PICTURE");
  56. } else if (intent.getAction().equals(FileUploader.UPLOAD_FINISH_MESSAGE)) {
  57. handleUploadFinished(context, intent);
  58. } else {
  59. Log_OC.e(TAG, "Incorrect intent sent: " + intent.getAction());
  60. }
  61. }
  62. private void handleUploadFinished(Context context, Intent intent) {
  63. // remove successfull uploading, ignore rest for reupload on reconnect
  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. private void handleNewPhotoAction(Context context, Intent intent) {
  74. if (!instantUploadEnabled(context)) {
  75. Log_OC.d(TAG, "Instant upload disabled, aborting uploading");
  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. Cursor c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
  84. if (!c.moveToFirst()) {
  85. Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
  86. return;
  87. }
  88. String file_path = c.getString(c.getColumnIndex(Media.DATA));
  89. String file_name = c.getString(c.getColumnIndex(Media.DISPLAY_NAME));
  90. String mime_type = c.getString(c.getColumnIndex(Media.MIME_TYPE));
  91. c.close();
  92. Log_OC.e(TAG, file_path + "");
  93. // same always temporally the picture to upload
  94. DbHandler db = new DbHandler(context);
  95. db.putFileForLater(file_path, account.name, null);
  96. db.close();
  97. if (!isOnline(context) || (instantUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))) {
  98. return;
  99. }
  100. // register for upload finishe message
  101. // there is a litte problem with android API, we can register for
  102. // particular
  103. // intent in registerReceiver but we cannot unregister from precise
  104. // intent
  105. // we can unregister from entire listenings but thats suck a bit.
  106. // On the other hand this might be only for dynamicly registered
  107. // broadcast receivers, needs investigation.
  108. IntentFilter filter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);
  109. context.getApplicationContext().registerReceiver(this, filter);
  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. context.startService(i);
  118. }
  119. private void handleConnectivityAction(Context context, Intent intent) {
  120. if (!instantUploadEnabled(context)) {
  121. Log_OC.d(TAG, "Instant upload disabled, abording uploading");
  122. return;
  123. }
  124. if (!intent.hasExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY)
  125. && isOnline(context)
  126. && (!instantUploadViaWiFiOnly(context) || (instantUploadViaWiFiOnly(context) == isConnectedViaWiFi(context) == true))) {
  127. DbHandler db = new DbHandler(context);
  128. Cursor c = db.getAwaitingFiles();
  129. if (c.moveToFirst()) {
  130. IntentFilter filter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);
  131. context.getApplicationContext().registerReceiver(this, filter);
  132. do {
  133. String account_name = c.getString(c.getColumnIndex("account"));
  134. String file_path = c.getString(c.getColumnIndex("path"));
  135. File f = new File(file_path);
  136. if (f.exists()) {
  137. Account account = new Account(account_name, AccountAuthenticator.ACCOUNT_TYPE);
  138. String mimeType = null;
  139. try {
  140. mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
  141. f.getName().substring(f.getName().lastIndexOf('.') + 1));
  142. } catch (Throwable e) {
  143. Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + f.getName());
  144. }
  145. if (mimeType == null)
  146. mimeType = "application/octet-stream";
  147. Intent i = new Intent(context, FileUploader.class);
  148. i.putExtra(FileUploader.KEY_ACCOUNT, account);
  149. i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
  150. i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, f.getName()));
  151. i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
  152. i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
  153. context.startService(i);
  154. } else {
  155. Log_OC.w(TAG, "Instant upload file " + f.getAbsolutePath() + " dont exist anymore");
  156. }
  157. } while (c.moveToNext());
  158. }
  159. c.close();
  160. db.close();
  161. }
  162. }
  163. public static boolean isOnline(Context context) {
  164. ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  165. return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
  166. }
  167. public static boolean isConnectedViaWiFi(Context context) {
  168. ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  169. return cm != null && cm.getActiveNetworkInfo() != null
  170. && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI
  171. && cm.getActiveNetworkInfo().getState() == State.CONNECTED;
  172. }
  173. public static boolean instantUploadEnabled(Context context) {
  174. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_uploading", false);
  175. }
  176. public static boolean instantUploadViaWiFiOnly(Context context) {
  177. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_upload_on_wifi", false);
  178. }
  179. }