627aec8cbce1336c392d79356a06503677bc584e
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / InstantUploadBroadcastReceiver.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2014 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
19 package com.owncloud.android.files;
20
21 import java.io.File;
22
23 import com.owncloud.android.MainApp;
24 import com.owncloud.android.authentication.AccountUtils;
25 import com.owncloud.android.db.DbHandler;
26 import com.owncloud.android.files.services.FileUploader;
27 import com.owncloud.android.utils.FileStorageUtils;
28 import com.owncloud.android.utils.Log_OC;
29
30
31 import android.accounts.Account;
32 import android.content.BroadcastReceiver;
33 import android.content.Context;
34 import android.content.Intent;
35 //import android.content.IntentFilter;
36 import android.database.Cursor;
37 import android.net.ConnectivityManager;
38 import android.net.NetworkInfo.State;
39 import android.preference.PreferenceManager;
40 import android.provider.MediaStore.*;
41 import android.webkit.MimeTypeMap;
42
43
44 public class InstantUploadBroadcastReceiver extends BroadcastReceiver {
45
46 private static String TAG = "InstantUploadBroadcastReceiver";
47 // Image action
48 // Unofficial action, works for most devices but not HTC. See: https://github.com/owncloud/android/issues/6
49 private static String NEW_PHOTO_ACTION_UNOFFICIAL = "com.android.camera.NEW_PICTURE";
50 // Officially supported action since SDK 14: http://developer.android.com/reference/android/hardware/Camera.html#ACTION_NEW_PICTURE
51 private static String NEW_PHOTO_ACTION = "android.hardware.action.NEW_PICTURE";
52 // Video action
53 // Officially supported action since SDK 14: http://developer.android.com/reference/android/hardware/Camera.html#ACTION_NEW_VIDEO
54 private static String NEW_VIDEO_ACTION = "android.hardware.action.NEW_VIDEO";
55
56 @Override
57 public void onReceive(Context context, Intent intent) {
58 Log_OC.d(TAG, "Received: " + intent.getAction());
59 if (intent.getAction().equals(android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
60 handleConnectivityAction(context, intent);
61 }else if (intent.getAction().equals(NEW_PHOTO_ACTION_UNOFFICIAL)) {
62 handleNewMediaAction(context, intent);
63 Log_OC.d(TAG, "UNOFFICIAL processed: com.android.camera.NEW_PICTURE");
64 } else if (intent.getAction().equals(NEW_PHOTO_ACTION)) {
65 handleNewMediaAction(context, intent);
66 Log_OC.d(TAG, "OFFICIAL processed: android.hardware.action.NEW_PICTURE");
67 } else if (intent.getAction().equals(NEW_VIDEO_ACTION)) {
68 Log_OC.d(TAG, "OFFICIAL processed: android.hardware.action.NEW_VIDEO");
69 handleNewMediaAction(context, intent);
70 } else if (intent.getAction().equals(FileUploader.getUploadFinishMessage())) {
71 handleUploadFinished(context, intent);
72 } else {
73 Log_OC.e(TAG, "Incorrect intent sent: " + intent.getAction());
74 }
75 }
76
77 private void handleUploadFinished(Context context, Intent intent) {
78 // remove successfull uploading, ignore rest for reupload on reconnect
79 /*
80 if (intent.getBooleanExtra(FileUploader.EXTRA_UPLOAD_RESULT, false)) {
81 DbHandler db = new DbHandler(context);
82 String localPath = intent.getStringExtra(FileUploader.EXTRA_OLD_FILE_PATH);
83 if (!db.removeIUPendingFile(localPath)) {
84 Log_OC.w(TAG, "Tried to remove non existing instant upload file " + localPath);
85 }
86 db.close();
87 }
88 */
89 }
90
91 private void handleNewMediaAction(Context context, Intent intent) {
92 Cursor c = null;
93 String file_path = null;
94 String file_name = null;
95 String mime_type = null;
96
97 if (!instantUploadEnabled(context)) {
98 Log_OC.d(TAG, "Instant upload disabled, aborting uploading");
99 return;
100 }
101
102 Account account = AccountUtils.getCurrentOwnCloudAccount(context);
103 if (account == null) {
104 Log_OC.w(TAG, "No owncloud account found for instant upload, aborting");
105 return;
106 }
107
108 if (intent.getAction().equals(NEW_PHOTO_ACTION) || intent.getAction().equals(NEW_PHOTO_ACTION_UNOFFICIAL)) {
109 String[] CONTENT_PROJECTION = { Images.Media.DATA, Images.Media.DISPLAY_NAME, Images.Media.MIME_TYPE, Images.Media.SIZE };
110 c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
111 if (!c.moveToFirst()) {
112 Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
113 return;
114 }
115 file_path = c.getString(c.getColumnIndex(Images.Media.DATA));
116 file_name = c.getString(c.getColumnIndex(Images.Media.DISPLAY_NAME));
117 mime_type = c.getString(c.getColumnIndex(Images.Media.MIME_TYPE));
118 Log_OC.w(TAG, "New photo received");
119 }
120 else if (intent.getAction().equals(NEW_VIDEO_ACTION)) {
121 String[] CONTENT_PROJECTION = { Video.Media.DATA, Video.Media.DISPLAY_NAME, Video.Media.MIME_TYPE, Video.Media.SIZE };
122 c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
123 if (!c.moveToFirst()) {
124 Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
125 return;
126 }
127 file_path = c.getString(c.getColumnIndex(Video.Media.DATA));
128 file_name = c.getString(c.getColumnIndex(Video.Media.DISPLAY_NAME));
129 mime_type = c.getString(c.getColumnIndex(Video.Media.MIME_TYPE));
130 Log_OC.w(TAG, "New video received");
131 }
132 c.close();
133 Log_OC.d(TAG, file_path + "");
134
135 // same always temporally the picture to upload
136 DbHandler db = new DbHandler(context);
137 db.putFileForLater(file_path, account.name, null);
138 db.close();
139
140 if (!isOnline(context) || (instantUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))) {
141 return;
142 }
143
144 // register for upload finishe message
145 // there is a litte problem with android API, we can register for
146 // particular
147 // intent in registerReceiver but we cannot unregister from precise
148 // intent
149 // we can unregister from entire listenings but thats suck a bit.
150 // On the other hand this might be only for dynamicly registered
151 // broadcast receivers, needs investigation.
152 /*IntentFilter filter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);
153 context.getApplicationContext().registerReceiver(this, filter);*/
154
155 Intent i = new Intent(context, FileUploader.class);
156 i.putExtra(FileUploader.KEY_ACCOUNT, account);
157 i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
158 i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, file_name));
159 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
160 i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
161 i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
162 context.startService(i);
163
164 }
165
166 private void handleConnectivityAction(Context context, Intent intent) {
167 if (!instantUploadEnabled(context)) {
168 Log_OC.d(TAG, "Instant upload disabled, abording uploading");
169 return;
170 }
171
172 if (!intent.hasExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY)
173 && isOnline(context)
174 && (!instantUploadViaWiFiOnly(context) || (instantUploadViaWiFiOnly(context) == isConnectedViaWiFi(context) == true))) {
175 DbHandler db = new DbHandler(context);
176 Cursor c = db.getAwaitingFiles();
177 if (c.moveToFirst()) {
178 //IntentFilter filter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);
179 //context.getApplicationContext().registerReceiver(this, filter);
180 do {
181 String account_name = c.getString(c.getColumnIndex("account"));
182 String file_path = c.getString(c.getColumnIndex("path"));
183 File f = new File(file_path);
184 if (f.exists()) {
185 Account account = new Account(account_name, MainApp.getAccountType());
186
187 String mimeType = null;
188 try {
189 mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
190 f.getName().substring(f.getName().lastIndexOf('.') + 1));
191
192 } catch (Throwable e) {
193 Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + f.getName());
194 }
195 if (mimeType == null)
196 mimeType = "application/octet-stream";
197
198 Intent i = new Intent(context, FileUploader.class);
199 i.putExtra(FileUploader.KEY_ACCOUNT, account);
200 i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
201 i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, f.getName()));
202 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
203 i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
204 context.startService(i);
205
206 } else {
207 Log_OC.w(TAG, "Instant upload file " + f.getAbsolutePath() + " dont exist anymore");
208 }
209 } while (c.moveToNext());
210 }
211 c.close();
212 db.close();
213 }
214
215 }
216
217 public static boolean isOnline(Context context) {
218 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
219 return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
220 }
221
222 public static boolean isConnectedViaWiFi(Context context) {
223 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
224 return cm != null && cm.getActiveNetworkInfo() != null
225 && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI
226 && cm.getActiveNetworkInfo().getState() == State.CONNECTED;
227 }
228
229 public static boolean instantUploadEnabled(Context context) {
230 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_uploading", false);
231 }
232
233 public static boolean instantUploadViaWiFiOnly(Context context) {
234 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_upload_on_wifi", false);
235 }
236 }