InstantUploadBroadcastReceiver.java 9.4 KB

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