f8254ff0cec7548d4b20ab0192131d05ac394a65
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / InstantUploadBroadcastReceiver.java
1 /**
2 * ownCloud Android client application
3 *
4 * Copyright (C) 2012 Bartek Przybylski
5 * Copyright (C) 2015 ownCloud Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 package com.owncloud.android.files;
22
23 import java.io.File;
24
25 import com.owncloud.android.MainApp;
26 import com.owncloud.android.authentication.AccountUtils;
27 import com.owncloud.android.db.DbHandler;
28 import com.owncloud.android.files.services.FileUploader;
29 import com.owncloud.android.lib.common.utils.Log_OC;
30 import com.owncloud.android.utils.FileStorageUtils;
31
32
33 import android.accounts.Account;
34 import android.content.BroadcastReceiver;
35 import android.content.Context;
36 import android.content.Intent;
37 import android.content.SharedPreferences;
38 import android.database.Cursor;
39 import android.net.ConnectivityManager;
40 import android.net.NetworkInfo.State;
41 import android.preference.PreferenceManager;
42 import android.provider.MediaStore.Images;
43 import android.provider.MediaStore.Video;
44 import android.webkit.MimeTypeMap;
45
46
47 public class InstantUploadBroadcastReceiver extends BroadcastReceiver {
48
49 private static String TAG = InstantUploadBroadcastReceiver.class.getName();
50 // Image action
51 // Unofficial action, works for most devices but not HTC. See: https://github.com/owncloud/android/issues/6
52 private static String NEW_PHOTO_ACTION_UNOFFICIAL = "com.android.camera.NEW_PICTURE";
53 // Officially supported action since SDK 14: http://developer.android.com/reference/android/hardware/Camera.html#ACTION_NEW_PICTURE
54 private static String NEW_PHOTO_ACTION = "android.hardware.action.NEW_PICTURE";
55 // Video action
56 // Officially supported action since SDK 14: http://developer.android.com/reference/android/hardware/Camera.html#ACTION_NEW_VIDEO
57 private static String NEW_VIDEO_ACTION = "android.hardware.action.NEW_VIDEO";
58
59 @Override
60 public void onReceive(Context context, Intent intent) {
61 Log_OC.d(TAG, "Received: " + intent.getAction());
62 if (intent.getAction().equals(android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
63 handleConnectivityAction(context, intent);
64 }else if (intent.getAction().equals(NEW_PHOTO_ACTION_UNOFFICIAL)) {
65 handleNewPictureAction(context, intent);
66 Log_OC.d(TAG, "UNOFFICIAL processed: com.android.camera.NEW_PICTURE");
67 } else if (intent.getAction().equals(NEW_PHOTO_ACTION)) {
68 handleNewPictureAction(context, intent);
69 Log_OC.d(TAG, "OFFICIAL processed: android.hardware.action.NEW_PICTURE");
70 } else if (intent.getAction().equals(NEW_VIDEO_ACTION)) {
71 Log_OC.d(TAG, "OFFICIAL processed: android.hardware.action.NEW_VIDEO");
72 handleNewVideoAction(context, intent);
73 } else {
74 Log_OC.e(TAG, "Incorrect intent sent: " + intent.getAction());
75 }
76 }
77
78 private void handleNewPictureAction(Context context, Intent intent) {
79 Cursor c = null;
80 String file_path = null;
81 String file_name = null;
82 String mime_type = null;
83
84 Log_OC.w(TAG, "New photo received");
85
86 if (!instantPictureUploadEnabled(context)) {
87 Log_OC.d(TAG, "Instant picture upload disabled, ignoring new picture");
88 return;
89 }
90
91 Account account = AccountUtils.getCurrentOwnCloudAccount(context);
92 if (account == null) {
93 Log_OC.w(TAG, "No ownCloud account found for instant upload, aborting");
94 return;
95 }
96
97 String[] CONTENT_PROJECTION = { Images.Media.DATA, Images.Media.DISPLAY_NAME, Images.Media.MIME_TYPE, Images.Media.SIZE };
98 c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
99 if (!c.moveToFirst()) {
100 Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
101 return;
102 }
103 file_path = c.getString(c.getColumnIndex(Images.Media.DATA));
104 file_name = c.getString(c.getColumnIndex(Images.Media.DISPLAY_NAME));
105 mime_type = c.getString(c.getColumnIndex(Images.Media.MIME_TYPE));
106 c.close();
107
108 Log_OC.d(TAG, file_path + "");
109
110 // save always temporally the picture to upload
111 DbHandler db = new DbHandler(context);
112 db.putFileForLater(file_path, account.name, null);
113 db.close();
114
115 if (!isOnline(context) || (instantPictureUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))) {
116 return;
117 }
118
119 Intent i = new Intent(context, FileUploader.class);
120 i.putExtra(FileUploader.KEY_ACCOUNT, account);
121 i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
122 i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, file_name));
123 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
124 i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
125 i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
126
127 // instant upload behaviour
128 SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(context);
129 String behaviour = appPreferences.getString("prefs_instant_behaviour", "NOTHING");
130
131 if (behaviour.equalsIgnoreCase("NOTHING")) {
132 Log_OC.d(TAG, "upload file and do nothing");
133 i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_FORGET);
134 } else if (behaviour.equalsIgnoreCase("COPY")) {
135 i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_COPY);
136 Log_OC.d(TAG, "upload file and copy file to oc folder");
137 } else if (behaviour.equalsIgnoreCase("MOVE")) {
138 i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE);
139 Log_OC.d(TAG, "upload file and move file to oc folder");
140 } else if (behaviour.equalsIgnoreCase("DELETE")){
141 i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_REMOVE);
142 Log_OC.d(TAG, "upload file and delete file in original place");
143 }
144
145 context.startService(i);
146 }
147
148 private void handleNewVideoAction(Context context, Intent intent) {
149 Cursor c = null;
150 String file_path = null;
151 String file_name = null;
152 String mime_type = null;
153
154 Log_OC.w(TAG, "New video received");
155
156 if (!instantVideoUploadEnabled(context)) {
157 Log_OC.d(TAG, "Instant video upload disabled, ignoring new video");
158 return;
159 }
160
161 Account account = AccountUtils.getCurrentOwnCloudAccount(context);
162 if (account == null) {
163 Log_OC.w(TAG, "No owncloud account found for instant upload, aborting");
164 return;
165 }
166
167 String[] CONTENT_PROJECTION = { Video.Media.DATA, Video.Media.DISPLAY_NAME, Video.Media.MIME_TYPE, Video.Media.SIZE };
168 c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
169 if (!c.moveToFirst()) {
170 Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
171 return;
172 }
173 file_path = c.getString(c.getColumnIndex(Video.Media.DATA));
174 file_name = c.getString(c.getColumnIndex(Video.Media.DISPLAY_NAME));
175 mime_type = c.getString(c.getColumnIndex(Video.Media.MIME_TYPE));
176 c.close();
177 Log_OC.d(TAG, file_path + "");
178
179 if (!isOnline(context) || (instantVideoUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))) {
180 return;
181 }
182
183 Intent i = new Intent(context, FileUploader.class);
184 i.putExtra(FileUploader.KEY_ACCOUNT, account);
185 i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
186 i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantVideoUploadFilePath(context, file_name));
187 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
188 i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
189 i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
190
191 // instant upload behaviour
192 SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(context);
193 String behaviour = appPreferences.getString("prefs_instant_behaviour", "NOTHING");
194
195 if (behaviour.equalsIgnoreCase("NOTHING")) {
196 Log_OC.d(TAG, "upload file and do nothing");
197 i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_FORGET);
198 } else if (behaviour.equalsIgnoreCase("COPY")) {
199 i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_COPY);
200 Log_OC.d(TAG, "upload file and copy file to oc folder");
201 } else if (behaviour.equalsIgnoreCase("MOVE")) {
202 i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE);
203 Log_OC.d(TAG, "upload file and move file to oc folder");
204 } else if (behaviour.equalsIgnoreCase("DELETE")){
205 i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_REMOVE);
206 Log_OC.d(TAG, "upload file and delete file in original place");
207 }
208
209 context.startService(i);
210
211 }
212
213 private void handleConnectivityAction(Context context, Intent intent) {
214 if (!instantPictureUploadEnabled(context)) {
215 Log_OC.d(TAG, "Instant upload disabled, don't upload anything");
216 return;
217 }
218
219 if (!intent.hasExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY)
220 && isOnline(context)
221 && (!instantPictureUploadViaWiFiOnly(context) || (instantPictureUploadViaWiFiOnly(context) == isConnectedViaWiFi(context) == true))) {
222 DbHandler db = new DbHandler(context);
223 Cursor c = db.getAwaitingFiles();
224 if (c.moveToFirst()) {
225 do {
226 String account_name = c.getString(c.getColumnIndex("account"));
227 String file_path = c.getString(c.getColumnIndex("path"));
228 File f = new File(file_path);
229 if (f.exists()) {
230 Account account = new Account(account_name, MainApp.getAccountType());
231
232 String mimeType = null;
233 try {
234 mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
235 f.getName().substring(f.getName().lastIndexOf('.') + 1));
236
237 } catch (Throwable e) {
238 Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + f.getName());
239 }
240 if (mimeType == null)
241 mimeType = "application/octet-stream";
242
243 Intent i = new Intent(context, FileUploader.class);
244 i.putExtra(FileUploader.KEY_ACCOUNT, account);
245 i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
246 i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, f.getName()));
247 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
248 i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
249
250 // instant upload behaviour
251 SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(context);
252 String behaviour = appPreferences.getString("prefs_instant_behaviour", "NOTHING");
253
254 if (behaviour.equalsIgnoreCase("NOTHING")) {
255 Log_OC.d(TAG, "upload file and do nothing");
256 i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_FORGET);
257 } else if (behaviour.equalsIgnoreCase("COPY")) {
258 i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_COPY);
259 Log_OC.d(TAG, "upload file and copy file to oc folder");
260 } else if (behaviour.equalsIgnoreCase("MOVE")) {
261 i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE);
262 Log_OC.d(TAG, "upload file and move file to oc folder");
263 } else if (behaviour.equalsIgnoreCase("DELETE")){
264 i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_REMOVE);
265 Log_OC.d(TAG, "upload file and delete file in original place");
266 }
267
268 context.startService(i);
269
270 } else {
271 Log_OC.w(TAG, "Instant upload file " + f.getAbsolutePath() + " dont exist anymore");
272 }
273 } while (c.moveToNext());
274 }
275 c.close();
276 db.close();
277 }
278
279 }
280
281 public static boolean isOnline(Context context) {
282 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
283 return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
284 }
285
286 public static boolean isConnectedViaWiFi(Context context) {
287 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
288 return cm != null && cm.getActiveNetworkInfo() != null
289 && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI
290 && cm.getActiveNetworkInfo().getState() == State.CONNECTED;
291 }
292
293 public static boolean instantPictureUploadEnabled(Context context) {
294 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_uploading", false);
295 }
296
297 public static boolean instantVideoUploadEnabled(Context context) {
298 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_uploading", false);
299 }
300
301 public static boolean instantPictureUploadViaWiFiOnly(Context context) {
302 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_upload_on_wifi", false);
303 }
304
305 public static boolean instantVideoUploadViaWiFiOnly(Context context) {
306 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_upload_on_wifi", false);
307 }
308 }