Merge remote-tracking branch 'remotes/upstream/sortInUploadFilesActivity' 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.database.Cursor;
38 import android.net.ConnectivityManager;
39 import android.net.NetworkInfo.State;
40 import android.os.BatteryManager;
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) || intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
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 Log_OC.d(TAG, file_path + "");
108
109 // save always temporally the picture to upload
110 DbHandler db = new DbHandler(context);
111 db.putFileForLater(file_path, account.name, null);
112 db.close();
113
114 if (!isOnline(context)
115 || (instantPictureUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))
116 || (instantUploadWhenChargingOnly(context) && !isCharging(context))
117 ) {
118 return;
119 }
120
121 Intent i = new Intent(context, FileUploader.class);
122 i.putExtra(FileUploader.KEY_ACCOUNT, account);
123 i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
124 i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, file_name));
125 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
126 i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
127 i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
128 context.startService(i);
129 }
130
131 private void handleNewVideoAction(Context context, Intent intent) {
132 Cursor c = null;
133 String file_path = null;
134 String file_name = null;
135 String mime_type = null;
136
137 Log_OC.w(TAG, "New video received");
138
139 if (!instantVideoUploadEnabled(context)) {
140 Log_OC.d(TAG, "Instant video upload disabled, ignoring new video");
141 return;
142 }
143
144 Account account = AccountUtils.getCurrentOwnCloudAccount(context);
145 if (account == null) {
146 Log_OC.w(TAG, "No owncloud account found for instant upload, aborting");
147 return;
148 }
149
150 String[] CONTENT_PROJECTION = { Video.Media.DATA, Video.Media.DISPLAY_NAME, Video.Media.MIME_TYPE, Video.Media.SIZE };
151 c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
152 if (!c.moveToFirst()) {
153 Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
154 return;
155 }
156 file_path = c.getString(c.getColumnIndex(Video.Media.DATA));
157 file_name = c.getString(c.getColumnIndex(Video.Media.DISPLAY_NAME));
158 mime_type = c.getString(c.getColumnIndex(Video.Media.MIME_TYPE));
159 c.close();
160 Log_OC.d(TAG, file_path + "");
161
162 // save always temporally the picture to upload
163 DbHandler db = new DbHandler(context);
164 db.putFileForLater(file_path, account.name, null);
165 db.close();
166
167 if (!isOnline(context)
168 || (instantVideoUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))
169 || (instantVideoUploadWhenChargingOnly(context) && !isCharging(context))
170 ) {
171 return;
172 }
173
174 Intent i = new Intent(context, FileUploader.class);
175 i.putExtra(FileUploader.KEY_ACCOUNT, account);
176 i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
177 i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantVideoUploadFilePath(context, file_name));
178 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
179 i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
180 i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
181 context.startService(i);
182
183 }
184
185 private void handleConnectivityAction(Context context, Intent intent) {
186 if (!instantPictureUploadEnabled(context) && !instantVideoUploadEnabled(context)) {
187 Log_OC.d(TAG, "Instant upload disabled, don't upload anything");
188 return;
189 }
190
191 if (!intent.hasExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY)
192 && isOnline(context)
193 && (!instantUploadWhenChargingOnly(context) || (instantUploadWhenChargingOnly(context) && isCharging(context)))
194 && (!instantVideoUploadWhenChargingOnly(context) || (instantVideoUploadWhenChargingOnly(context) && isCharging(context)))
195 && (!instantPictureUploadViaWiFiOnly(context) || (instantPictureUploadViaWiFiOnly(context) && isConnectedViaWiFi(context)))
196 && (!instantVideoUploadViaWiFiOnly(context) || (instantVideoUploadViaWiFiOnly(context) && isConnectedViaWiFi(context)))
197 ) {
198 DbHandler db = new DbHandler(context);
199 Cursor c = db.getAwaitingFiles();
200 if (c.moveToFirst()) {
201 do {
202 String account_name = c.getString(c.getColumnIndex("account"));
203 String file_path = c.getString(c.getColumnIndex("path"));
204 File f = new File(file_path);
205 if (f.exists()) {
206 Account account = new Account(account_name, MainApp.getAccountType());
207
208 String mimeType = null;
209 try {
210 mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
211 f.getName().substring(f.getName().lastIndexOf('.') + 1));
212
213 } catch (Throwable e) {
214 Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + f.getName());
215 }
216 if (mimeType == null)
217 mimeType = "application/octet-stream";
218
219 Intent i = new Intent(context, FileUploader.class);
220 i.putExtra(FileUploader.KEY_ACCOUNT, account);
221 i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
222 i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, f.getName()));
223 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
224 i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
225 context.startService(i);
226
227 } else {
228 Log_OC.w(TAG, "Instant upload file " + f.getAbsolutePath() + " dont exist anymore");
229 }
230 } while (c.moveToNext());
231 }
232 c.close();
233 db.close();
234 }
235 }
236
237 public static boolean isOnline(Context context) {
238 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
239 return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
240 }
241
242 public static boolean isConnectedViaWiFi(Context context) {
243 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
244 return cm != null && cm.getActiveNetworkInfo() != null
245 && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI
246 && cm.getActiveNetworkInfo().getState() == State.CONNECTED;
247 }
248
249 public static boolean isCharging(Context context){
250 IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
251 Intent batteryStatus = context.registerReceiver(null, ifilter);
252
253 int status = 0;
254 if (batteryStatus != null) {
255 status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
256 }
257 return status == BatteryManager.BATTERY_STATUS_CHARGING ||
258 status == BatteryManager.BATTERY_STATUS_FULL;
259 }
260
261 public static boolean instantPictureUploadEnabled(Context context) {
262 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_uploading", false);
263 }
264
265 public static boolean instantVideoUploadEnabled(Context context) {
266 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_uploading", false);
267 }
268
269 public static boolean instantPictureUploadViaWiFiOnly(Context context) {
270 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_upload_on_wifi", false);
271 }
272
273 public static boolean instantVideoUploadViaWiFiOnly(Context context) {
274 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_upload_on_wifi", false);
275 }
276 public static boolean instantUploadWhenChargingOnly(Context context) {
277 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_upload_on_charging", false);
278 }
279 public static boolean instantVideoUploadWhenChargingOnly(Context context) {
280 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_upload_on_charging", false);
281 }
282 }