Merge remote-tracking branch 'remotes/upstream/avoidDuplicateFiles' into beta
[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 import android.accounts.Account;
33 import android.content.BroadcastReceiver;
34 import android.content.Context;
35 import android.content.Intent;
36 import android.content.IntentFilter;
37 import android.content.SharedPreferences;
38 import android.database.Cursor;
39 import android.net.ConnectivityManager;
40 import android.net.NetworkInfo.State;
41 import android.os.BatteryManager;
42 import android.preference.PreferenceManager;
43 import android.provider.MediaStore.Images;
44 import android.provider.MediaStore.Video;
45 import android.webkit.MimeTypeMap;
46
47
48 public class InstantUploadBroadcastReceiver extends BroadcastReceiver {
49
50 private static String TAG = InstantUploadBroadcastReceiver.class.getName();
51 // Image action
52 // Unofficial action, works for most devices but not HTC. See: https://github.com/owncloud/android/issues/6
53 private static String NEW_PHOTO_ACTION_UNOFFICIAL = "com.android.camera.NEW_PICTURE";
54 // Officially supported action since SDK 14: http://developer.android.com/reference/android/hardware/Camera.html#ACTION_NEW_PICTURE
55 private static String NEW_PHOTO_ACTION = "android.hardware.action.NEW_PICTURE";
56 // Video action
57 // Officially supported action since SDK 14: http://developer.android.com/reference/android/hardware/Camera.html#ACTION_NEW_VIDEO
58 private static String NEW_VIDEO_ACTION = "android.hardware.action.NEW_VIDEO";
59
60 @Override
61 public void onReceive(Context context, Intent intent) {
62 Log_OC.d(TAG, "Received: " + intent.getAction());
63 if (intent.getAction().equals(android.net.ConnectivityManager.CONNECTIVITY_ACTION) || intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
64 handleConnectivityAction(context, intent);
65 }else if (intent.getAction().equals(NEW_PHOTO_ACTION_UNOFFICIAL)) {
66 handleNewPictureAction(context, intent);
67 Log_OC.d(TAG, "UNOFFICIAL processed: com.android.camera.NEW_PICTURE");
68 } else if (intent.getAction().equals(NEW_PHOTO_ACTION)) {
69 handleNewPictureAction(context, intent);
70 Log_OC.d(TAG, "OFFICIAL processed: android.hardware.action.NEW_PICTURE");
71 } else if (intent.getAction().equals(NEW_VIDEO_ACTION)) {
72 Log_OC.d(TAG, "OFFICIAL processed: android.hardware.action.NEW_VIDEO");
73 handleNewVideoAction(context, intent);
74 } else {
75 Log_OC.e(TAG, "Incorrect intent sent: " + intent.getAction());
76 }
77 }
78
79 private void handleNewPictureAction(Context context, Intent intent) {
80 Cursor c = null;
81 String file_path = null;
82 String file_name = null;
83 String mime_type = null;
84
85 Log_OC.w(TAG, "New photo received");
86
87 if (!instantPictureUploadEnabled(context)) {
88 Log_OC.d(TAG, "Instant picture upload disabled, ignoring new picture");
89 return;
90 }
91
92 Account account = AccountUtils.getCurrentOwnCloudAccount(context);
93 if (account == null) {
94 Log_OC.w(TAG, "No ownCloud account found for instant upload, aborting");
95 return;
96 }
97
98 String[] CONTENT_PROJECTION = { Images.Media.DATA, Images.Media.DISPLAY_NAME, Images.Media.MIME_TYPE, Images.Media.SIZE };
99 c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
100 if (!c.moveToFirst()) {
101 Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
102 return;
103 }
104 file_path = c.getString(c.getColumnIndex(Images.Media.DATA));
105 file_name = c.getString(c.getColumnIndex(Images.Media.DISPLAY_NAME));
106 mime_type = c.getString(c.getColumnIndex(Images.Media.MIME_TYPE));
107 c.close();
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)
116 || (instantPictureUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))
117 || (instantUploadWhenChargingOnly(context) && !isCharging(context))
118 ) {
119 return;
120 }
121
122 Intent i = new Intent(context, FileUploader.class);
123 i.putExtra(FileUploader.KEY_ACCOUNT, account);
124 i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
125 i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, file_name));
126 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
127 i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
128 i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
129
130 // instant upload behaviour
131 i = addInstantUploadBehaviour(i, context);
132
133 context.startService(i);
134 }
135
136 private Intent addInstantUploadBehaviour(Intent i, Context context){
137 SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(context);
138 String behaviour = appPreferences.getString("prefs_instant_behaviour", "NOTHING");
139
140 if (behaviour.equalsIgnoreCase("NOTHING")) {
141 Log_OC.d(TAG, "upload file and do nothing");
142 i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_FORGET);
143 } else if (behaviour.equalsIgnoreCase("MOVE")) {
144 i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE);
145 Log_OC.d(TAG, "upload file and move file to oc folder");
146 }
147 return i;
148 }
149
150 private void handleNewVideoAction(Context context, Intent intent) {
151 Cursor c = null;
152 String file_path = null;
153 String file_name = null;
154 String mime_type = null;
155
156 Log_OC.w(TAG, "New video received");
157
158 if (!instantVideoUploadEnabled(context)) {
159 Log_OC.d(TAG, "Instant video upload disabled, ignoring new video");
160 return;
161 }
162
163 Account account = AccountUtils.getCurrentOwnCloudAccount(context);
164 if (account == null) {
165 Log_OC.w(TAG, "No owncloud account found for instant upload, aborting");
166 return;
167 }
168
169 String[] CONTENT_PROJECTION = { Video.Media.DATA, Video.Media.DISPLAY_NAME, Video.Media.MIME_TYPE, Video.Media.SIZE };
170 c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
171 if (!c.moveToFirst()) {
172 Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
173 return;
174 }
175 file_path = c.getString(c.getColumnIndex(Video.Media.DATA));
176 file_name = c.getString(c.getColumnIndex(Video.Media.DISPLAY_NAME));
177 mime_type = c.getString(c.getColumnIndex(Video.Media.MIME_TYPE));
178 c.close();
179 Log_OC.d(TAG, file_path + "");
180
181 // save always temporally the picture to upload
182 DbHandler db = new DbHandler(context);
183 db.putFileForLater(file_path, account.name, null);
184 db.close();
185
186 if (!isOnline(context)
187 || (instantVideoUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))
188 || (instantVideoUploadWhenChargingOnly(context) && !isCharging(context))
189 ) {
190 return;
191 }
192
193 Intent i = new Intent(context, FileUploader.class);
194 i.putExtra(FileUploader.KEY_ACCOUNT, account);
195 i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
196 i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantVideoUploadFilePath(context, file_name));
197 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
198 i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
199 i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
200
201 // instant upload behaviour
202 i = addInstantUploadBehaviour(i, context);
203
204 context.startService(i);
205
206 }
207
208 private void handleConnectivityAction(Context context, Intent intent) {
209 if (!instantPictureUploadEnabled(context) && !instantVideoUploadEnabled(context)) {
210 Log_OC.d(TAG, "Instant upload disabled, don't upload anything");
211 return;
212 }
213
214 if (instantPictureUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context)){
215 Account account = AccountUtils.getCurrentOwnCloudAccount(context);
216 if (account == null) {
217 Log_OC.w(TAG, "No owncloud account found for instant upload, aborting");
218 return;
219 }
220
221 Intent i = new Intent(context, FileUploader.class);
222 i.putExtra(FileUploader.KEY_ACCOUNT, account);
223 i.putExtra(FileUploader.KEY_CANCEL_ALL, true);
224 context.startService(i);
225 }
226
227 if (!intent.hasExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY)
228 && isOnline(context)
229 && (!instantUploadWhenChargingOnly(context) || (instantUploadWhenChargingOnly(context) && isCharging(context)))
230 && (!instantVideoUploadWhenChargingOnly(context) || (instantVideoUploadWhenChargingOnly(context) && isCharging(context)))
231 && (!instantPictureUploadViaWiFiOnly(context) || (instantPictureUploadViaWiFiOnly(context) && isConnectedViaWiFi(context)))
232 && (!instantVideoUploadViaWiFiOnly(context) || (instantVideoUploadViaWiFiOnly(context) && isConnectedViaWiFi(context)))
233 ) {
234 DbHandler db = new DbHandler(context);
235 Cursor c = db.getAwaitingFiles();
236 if (c.moveToFirst()) {
237 do {
238 if (instantPictureUploadViaWiFiOnly(context) &&
239 !isConnectedViaWiFi(context)){
240 break;
241 }
242
243 String account_name = c.getString(c.getColumnIndex("account"));
244 String file_path = c.getString(c.getColumnIndex("path"));
245 File f = new File(file_path);
246 if (f.exists()) {
247 Account account = new Account(account_name, MainApp.getAccountType());
248
249 String mimeType = null;
250 try {
251 mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
252 f.getName().substring(f.getName().lastIndexOf('.') + 1));
253
254 } catch (Throwable e) {
255 Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + f.getName());
256 }
257 if (mimeType == null)
258 mimeType = "application/octet-stream";
259
260 Intent i = new Intent(context, FileUploader.class);
261 i.putExtra(FileUploader.KEY_ACCOUNT, account);
262 i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
263 i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, f.getName()));
264 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
265 i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
266
267 // instant upload behaviour
268 i = addInstantUploadBehaviour(i, context);
269
270 context.startService(i);
271
272 } else {
273 Log_OC.w(TAG, "Instant upload file " + f.getAbsolutePath() + " dont exist anymore");
274 }
275 } while (c.moveToNext());
276 }
277 c.close();
278 db.close();
279 }
280 }
281
282 public static boolean isOnline(Context context) {
283 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
284 return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
285 }
286
287 public static boolean isConnectedViaWiFi(Context context) {
288 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
289 return cm != null && cm.getActiveNetworkInfo() != null
290 && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI
291 && cm.getActiveNetworkInfo().getState() == State.CONNECTED;
292 }
293
294 public static boolean isCharging(Context context){
295 IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
296 Intent batteryStatus = context.registerReceiver(null, ifilter);
297
298 int status = 0;
299 if (batteryStatus != null) {
300 status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
301 }
302 return status == BatteryManager.BATTERY_STATUS_CHARGING ||
303 status == BatteryManager.BATTERY_STATUS_FULL;
304 }
305
306 public static boolean instantPictureUploadEnabled(Context context) {
307 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_uploading", false);
308 }
309
310 public static boolean instantVideoUploadEnabled(Context context) {
311 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_uploading", false);
312 }
313
314 public static boolean instantPictureUploadViaWiFiOnly(Context context) {
315 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_upload_on_wifi", false);
316 }
317
318 public static boolean instantVideoUploadViaWiFiOnly(Context context) {
319 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_upload_on_wifi", false);
320 }
321 public static boolean instantUploadWhenChargingOnly(Context context) {
322 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_upload_on_charging", false);
323 }
324 public static boolean instantVideoUploadWhenChargingOnly(Context context) {
325 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_upload_on_charging", false);
326 }
327 }