Merge branch 'master' of github.com:owncloud/android into cancelWlan
[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.database.Cursor;
38 import android.net.ConnectivityManager;
39 import android.net.NetworkInfo.State;
40 import android.preference.PreferenceManager;
41 import android.provider.MediaStore.Images;
42 import android.provider.MediaStore.Video;
43 import android.webkit.MimeTypeMap;
44
45
46 public class InstantUploadBroadcastReceiver extends BroadcastReceiver {
47
48 private static String TAG = InstantUploadBroadcastReceiver.class.getName();
49 // Image action
50 // Unofficial action, works for most devices but not HTC. See: https://github.com/owncloud/android/issues/6
51 private static String NEW_PHOTO_ACTION_UNOFFICIAL = "com.android.camera.NEW_PICTURE";
52 // Officially supported action since SDK 14: http://developer.android.com/reference/android/hardware/Camera.html#ACTION_NEW_PICTURE
53 private static String NEW_PHOTO_ACTION = "android.hardware.action.NEW_PICTURE";
54 // Video action
55 // Officially supported action since SDK 14: http://developer.android.com/reference/android/hardware/Camera.html#ACTION_NEW_VIDEO
56 private static String NEW_VIDEO_ACTION = "android.hardware.action.NEW_VIDEO";
57
58 @Override
59 public void onReceive(Context context, Intent intent) {
60 Log_OC.d(TAG, "Received: " + intent.getAction());
61 if (intent.getAction().equals(android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
62 handleConnectivityAction(context, intent);
63 }else if (intent.getAction().equals(NEW_PHOTO_ACTION_UNOFFICIAL)) {
64 handleNewPictureAction(context, intent);
65 Log_OC.d(TAG, "UNOFFICIAL processed: com.android.camera.NEW_PICTURE");
66 } else if (intent.getAction().equals(NEW_PHOTO_ACTION)) {
67 handleNewPictureAction(context, intent);
68 Log_OC.d(TAG, "OFFICIAL processed: android.hardware.action.NEW_PICTURE");
69 } else if (intent.getAction().equals(NEW_VIDEO_ACTION)) {
70 Log_OC.d(TAG, "OFFICIAL processed: android.hardware.action.NEW_VIDEO");
71 handleNewVideoAction(context, intent);
72 } else {
73 Log_OC.e(TAG, "Incorrect intent sent: " + intent.getAction());
74 }
75 }
76
77 private void handleNewPictureAction(Context context, Intent intent) {
78 Cursor c = null;
79 String file_path = null;
80 String file_name = null;
81 String mime_type = null;
82
83 Log_OC.w(TAG, "New photo received");
84
85 if (!instantPictureUploadEnabled(context)) {
86 Log_OC.d(TAG, "Instant picture upload disabled, ignoring new picture");
87 return;
88 }
89
90 Account account = AccountUtils.getCurrentOwnCloudAccount(context);
91 if (account == null) {
92 Log_OC.w(TAG, "No ownCloud account found for instant upload, aborting");
93 return;
94 }
95
96 String[] CONTENT_PROJECTION = { Images.Media.DATA, Images.Media.DISPLAY_NAME, Images.Media.MIME_TYPE, Images.Media.SIZE };
97 c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
98 if (!c.moveToFirst()) {
99 Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
100 return;
101 }
102 file_path = c.getString(c.getColumnIndex(Images.Media.DATA));
103 file_name = c.getString(c.getColumnIndex(Images.Media.DISPLAY_NAME));
104 mime_type = c.getString(c.getColumnIndex(Images.Media.MIME_TYPE));
105 c.close();
106
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) || (instantPictureUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))) {
115 return;
116 }
117
118 Intent i = new Intent(context, FileUploader.class);
119 i.putExtra(FileUploader.KEY_ACCOUNT, account);
120 i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
121 i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, file_name));
122 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
123 i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
124 i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
125 context.startService(i);
126 }
127
128 private void handleNewVideoAction(Context context, Intent intent) {
129 Cursor c = null;
130 String file_path = null;
131 String file_name = null;
132 String mime_type = null;
133
134 Log_OC.w(TAG, "New video received");
135
136 if (!instantVideoUploadEnabled(context)) {
137 Log_OC.d(TAG, "Instant video upload disabled, ignoring new video");
138 return;
139 }
140
141 Account account = AccountUtils.getCurrentOwnCloudAccount(context);
142 if (account == null) {
143 Log_OC.w(TAG, "No owncloud account found for instant upload, aborting");
144 return;
145 }
146
147 String[] CONTENT_PROJECTION = { Video.Media.DATA, Video.Media.DISPLAY_NAME, Video.Media.MIME_TYPE, Video.Media.SIZE };
148 c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
149 if (!c.moveToFirst()) {
150 Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
151 return;
152 }
153 file_path = c.getString(c.getColumnIndex(Video.Media.DATA));
154 file_name = c.getString(c.getColumnIndex(Video.Media.DISPLAY_NAME));
155 mime_type = c.getString(c.getColumnIndex(Video.Media.MIME_TYPE));
156 c.close();
157 Log_OC.d(TAG, file_path + "");
158
159 if (!isOnline(context) || (instantVideoUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))) {
160 return;
161 }
162
163 Intent i = new Intent(context, FileUploader.class);
164 i.putExtra(FileUploader.KEY_ACCOUNT, account);
165 i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
166 i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantVideoUploadFilePath(context, file_name));
167 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
168 i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
169 i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
170 context.startService(i);
171
172 }
173
174 private void handleConnectivityAction(Context context, Intent intent) {
175 if (!instantPictureUploadEnabled(context)) {
176 Log_OC.d(TAG, "Instant upload disabled, don't upload anything");
177 return;
178 }
179
180 if (instantPictureUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context)){
181 Account account = AccountUtils.getCurrentOwnCloudAccount(context);
182 if (account == null) {
183 Log_OC.w(TAG, "No owncloud account found for instant upload, aborting");
184 return;
185 }
186
187 Intent i = new Intent(context, FileUploader.class);
188 i.putExtra(FileUploader.KEY_ACCOUNT, account);
189 i.putExtra(FileUploader.KEY_CANCEL_ALL, true);
190 context.startService(i);
191 }
192
193 if (!intent.hasExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY)
194 && isOnline(context)
195 && (!instantPictureUploadViaWiFiOnly(context) || (instantPictureUploadViaWiFiOnly(context) == isConnectedViaWiFi(context) == true))) {
196 DbHandler db = new DbHandler(context);
197 Cursor c = db.getAwaitingFiles();
198 if (c.moveToFirst() && isOnline(context)
199 && (!instantPictureUploadViaWiFiOnly(context) ||
200 (instantPictureUploadViaWiFiOnly(context) == isConnectedViaWiFi(context) == true))) {
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
238 public static boolean isOnline(Context context) {
239 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
240 return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
241 }
242
243 public static boolean isConnectedViaWiFi(Context context) {
244 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
245 return cm != null && cm.getActiveNetworkInfo() != null
246 && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI
247 && cm.getActiveNetworkInfo().getState() == State.CONNECTED;
248 }
249
250 public static boolean instantPictureUploadEnabled(Context context) {
251 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_uploading", false);
252 }
253
254 public static boolean instantVideoUploadEnabled(Context context) {
255 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_uploading", false);
256 }
257
258 public static boolean instantPictureUploadViaWiFiOnly(Context context) {
259 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_upload_on_wifi", false);
260 }
261
262 public static boolean instantVideoUploadViaWiFiOnly(Context context) {
263 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_upload_on_wifi", false);
264 }
265 }