Add new preference on settings for setting the upload video path
[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.lib.common.utils.Log_OC;
28 import com.owncloud.android.utils.FileStorageUtils;
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.database.Cursor;
36 import android.net.ConnectivityManager;
37 import android.net.NetworkInfo.State;
38 import android.preference.PreferenceManager;
39 import android.provider.MediaStore.Images;
40 import android.provider.MediaStore.Video;
41 import android.webkit.MimeTypeMap;
42
43
44 public class InstantUploadBroadcastReceiver extends BroadcastReceiver {
45
46 private static String TAG = InstantUploadBroadcastReceiver.class.getName();
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 handleNewPictureAction(context, intent);
63 Log_OC.d(TAG, "UNOFFICIAL processed: com.android.camera.NEW_PICTURE");
64 } else if (intent.getAction().equals(NEW_PHOTO_ACTION)) {
65 handleNewPictureAction(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 handleNewVideoAction(context, intent);
70 } else {
71 Log_OC.e(TAG, "Incorrect intent sent: " + intent.getAction());
72 }
73 }
74
75 private void handleNewPictureAction(Context context, Intent intent) {
76 Cursor c = null;
77 String file_path = null;
78 String file_name = null;
79 String mime_type = null;
80
81 Log_OC.w(TAG, "New photo received");
82
83 if (!instantPictureUploadEnabled(context)) {
84 Log_OC.d(TAG, "Instant picture upload disabled, ignoring new picture");
85 return;
86 }
87
88 Account account = AccountUtils.getCurrentOwnCloudAccount(context);
89 if (account == null) {
90 Log_OC.w(TAG, "No owncloud account found for instant upload, aborting");
91 return;
92 }
93
94 String[] CONTENT_PROJECTION = { Images.Media.DATA, Images.Media.DISPLAY_NAME, Images.Media.MIME_TYPE, Images.Media.SIZE };
95 c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
96 if (!c.moveToFirst()) {
97 Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
98 return;
99 }
100 file_path = c.getString(c.getColumnIndex(Images.Media.DATA));
101 file_name = c.getString(c.getColumnIndex(Images.Media.DISPLAY_NAME));
102 mime_type = c.getString(c.getColumnIndex(Images.Media.MIME_TYPE));
103 c.close();
104
105 Log_OC.d(TAG, file_path + "");
106
107 // save always temporally the picture to upload
108 DbHandler db = new DbHandler(context);
109 db.putFileForLater(file_path, account.name, null);
110 db.close();
111
112 if (!isOnline(context) || (instantPictureUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))) {
113 return;
114 }
115
116 Intent i = new Intent(context, FileUploader.class);
117 i.putExtra(FileUploader.KEY_ACCOUNT, account);
118 i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
119 i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, file_name));
120 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
121 i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
122 i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
123 context.startService(i);
124 }
125
126 private void handleNewVideoAction(Context context, Intent intent) {
127 Cursor c = null;
128 String file_path = null;
129 String file_name = null;
130 String mime_type = null;
131
132 Log_OC.w(TAG, "New video received");
133
134 if (!instantVideoUploadEnabled(context)) {
135 Log_OC.d(TAG, "Instant video upload disabled, ignoring new video");
136 return;
137 }
138
139 Account account = AccountUtils.getCurrentOwnCloudAccount(context);
140 if (account == null) {
141 Log_OC.w(TAG, "No owncloud account found for instant upload, aborting");
142 return;
143 }
144
145 String[] CONTENT_PROJECTION = { Video.Media.DATA, Video.Media.DISPLAY_NAME, Video.Media.MIME_TYPE, Video.Media.SIZE };
146 c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
147 if (!c.moveToFirst()) {
148 Log_OC.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
149 return;
150 }
151 file_path = c.getString(c.getColumnIndex(Video.Media.DATA));
152 file_name = c.getString(c.getColumnIndex(Video.Media.DISPLAY_NAME));
153 mime_type = c.getString(c.getColumnIndex(Video.Media.MIME_TYPE));
154 c.close();
155 Log_OC.d(TAG, file_path + "");
156
157 if (!isOnline(context) || (instantVideoUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))) {
158 return;
159 }
160
161 Intent i = new Intent(context, FileUploader.class);
162 i.putExtra(FileUploader.KEY_ACCOUNT, account);
163 i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
164 i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantVideoUploadFilePath(context, file_name));
165 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
166 i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
167 i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
168 context.startService(i);
169
170 }
171
172 private void handleConnectivityAction(Context context, Intent intent) {
173 if (!instantPictureUploadEnabled(context)) {
174 Log_OC.d(TAG, "Instant upload disabled, don't upload anything");
175 return;
176 }
177
178 if (!intent.hasExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY)
179 && isOnline(context)
180 && (!instantPictureUploadViaWiFiOnly(context) || (instantPictureUploadViaWiFiOnly(context) == isConnectedViaWiFi(context) == true))) {
181 DbHandler db = new DbHandler(context);
182 Cursor c = db.getAwaitingFiles();
183 if (c.moveToFirst()) {
184 do {
185 String account_name = c.getString(c.getColumnIndex("account"));
186 String file_path = c.getString(c.getColumnIndex("path"));
187 File f = new File(file_path);
188 if (f.exists()) {
189 Account account = new Account(account_name, MainApp.getAccountType());
190
191 String mimeType = null;
192 try {
193 mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
194 f.getName().substring(f.getName().lastIndexOf('.') + 1));
195
196 } catch (Throwable e) {
197 Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + f.getName());
198 }
199 if (mimeType == null)
200 mimeType = "application/octet-stream";
201
202 Intent i = new Intent(context, FileUploader.class);
203 i.putExtra(FileUploader.KEY_ACCOUNT, account);
204 i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
205 i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, f.getName()));
206 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
207 i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
208 context.startService(i);
209
210 } else {
211 Log_OC.w(TAG, "Instant upload file " + f.getAbsolutePath() + " dont exist anymore");
212 }
213 } while (c.moveToNext());
214 }
215 c.close();
216 db.close();
217 }
218
219 }
220
221 public static boolean isOnline(Context context) {
222 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
223 return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
224 }
225
226 public static boolean isConnectedViaWiFi(Context context) {
227 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
228 return cm != null && cm.getActiveNetworkInfo() != null
229 && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI
230 && cm.getActiveNetworkInfo().getState() == State.CONNECTED;
231 }
232
233 public static boolean instantPictureUploadEnabled(Context context) {
234 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_uploading", false);
235 }
236
237 public static boolean instantVideoUploadEnabled(Context context) {
238 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_uploading", false);
239 }
240
241 public static boolean instantPictureUploadViaWiFiOnly(Context context) {
242 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_upload_on_wifi", false);
243 }
244
245 public static boolean instantVideoUploadViaWiFiOnly(Context context) {
246 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_upload_on_wifi", false);
247 }
248 }