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