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