2 * ownCloud Android client application
4 * Copyright (C) 2012 Bartek Przybylski
5 * Copyright (C) 2015 ownCloud Inc.
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.
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.
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/>.
21 package com
.owncloud
.android
.files
;
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
;
33 import android
.accounts
.Account
;
34 import android
.content
.BroadcastReceiver
;
35 import android
.content
.Context
;
36 import android
.content
.Intent
;
37 import android
.content
.SharedPreferences
;
38 import android
.database
.Cursor
;
39 import android
.net
.ConnectivityManager
;
40 import android
.net
.NetworkInfo
.State
;
41 import android
.preference
.PreferenceManager
;
42 import android
.provider
.MediaStore
.Images
;
43 import android
.provider
.MediaStore
.Video
;
44 import android
.webkit
.MimeTypeMap
;
47 public class InstantUploadBroadcastReceiver
extends BroadcastReceiver
{
49 private static String TAG
= InstantUploadBroadcastReceiver
.class.getName();
51 // Unofficial action, works for most devices but not HTC. See: https://github.com/owncloud/android/issues/6
52 private static String NEW_PHOTO_ACTION_UNOFFICIAL
= "com.android.camera.NEW_PICTURE";
53 // Officially supported action since SDK 14: http://developer.android.com/reference/android/hardware/Camera.html#ACTION_NEW_PICTURE
54 private static String NEW_PHOTO_ACTION
= "android.hardware.action.NEW_PICTURE";
56 // Officially supported action since SDK 14: http://developer.android.com/reference/android/hardware/Camera.html#ACTION_NEW_VIDEO
57 private static String NEW_VIDEO_ACTION
= "android.hardware.action.NEW_VIDEO";
60 public void onReceive(Context context
, Intent intent
) {
61 Log_OC
.d(TAG
, "Received: " + intent
.getAction());
62 if (intent
.getAction().equals(android
.net
.ConnectivityManager
.CONNECTIVITY_ACTION
)) {
63 handleConnectivityAction(context
, intent
);
64 }else if (intent
.getAction().equals(NEW_PHOTO_ACTION_UNOFFICIAL
)) {
65 handleNewPictureAction(context
, intent
);
66 Log_OC
.d(TAG
, "UNOFFICIAL processed: com.android.camera.NEW_PICTURE");
67 } else if (intent
.getAction().equals(NEW_PHOTO_ACTION
)) {
68 handleNewPictureAction(context
, intent
);
69 Log_OC
.d(TAG
, "OFFICIAL processed: android.hardware.action.NEW_PICTURE");
70 } else if (intent
.getAction().equals(NEW_VIDEO_ACTION
)) {
71 Log_OC
.d(TAG
, "OFFICIAL processed: android.hardware.action.NEW_VIDEO");
72 handleNewVideoAction(context
, intent
);
74 Log_OC
.e(TAG
, "Incorrect intent sent: " + intent
.getAction());
78 private void handleNewPictureAction(Context context
, Intent intent
) {
80 String file_path
= null
;
81 String file_name
= null
;
82 String mime_type
= null
;
84 Log_OC
.w(TAG
, "New photo received");
86 if (!instantPictureUploadEnabled(context
)) {
87 Log_OC
.d(TAG
, "Instant picture upload disabled, ignoring new picture");
91 Account account
= AccountUtils
.getCurrentOwnCloudAccount(context
);
92 if (account
== null
) {
93 Log_OC
.w(TAG
, "No ownCloud account found for instant upload, aborting");
97 String
[] CONTENT_PROJECTION
= { Images
.Media
.DATA
, Images
.Media
.DISPLAY_NAME
, Images
.Media
.MIME_TYPE
, Images
.Media
.SIZE
};
98 c
= context
.getContentResolver().query(intent
.getData(), CONTENT_PROJECTION
, null
, null
, null
);
99 if (!c
.moveToFirst()) {
100 Log_OC
.e(TAG
, "Couldn't resolve given uri: " + intent
.getDataString());
103 file_path
= c
.getString(c
.getColumnIndex(Images
.Media
.DATA
));
104 file_name
= c
.getString(c
.getColumnIndex(Images
.Media
.DISPLAY_NAME
));
105 mime_type
= c
.getString(c
.getColumnIndex(Images
.Media
.MIME_TYPE
));
108 Log_OC
.d(TAG
, file_path
+ "");
110 // save always temporally the picture to upload
111 DbHandler db
= new DbHandler(context
);
112 db
.putFileForLater(file_path
, account
.name
, null
);
115 if (!isOnline(context
) || (instantPictureUploadViaWiFiOnly(context
) && !isConnectedViaWiFi(context
))) {
119 Intent i
= new Intent(context
, FileUploader
.class);
120 i
.putExtra(FileUploader
.KEY_ACCOUNT
, account
);
121 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, file_path
);
122 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, FileStorageUtils
.getInstantUploadFilePath(context
, file_name
));
123 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
124 i
.putExtra(FileUploader
.KEY_MIME_TYPE
, mime_type
);
125 i
.putExtra(FileUploader
.KEY_INSTANT_UPLOAD
, true
);
127 // instant upload behaviour
128 i
= addInstantUploadBehaviour(i
, context
);
130 context
.startService(i
);
133 private Intent
addInstantUploadBehaviour(Intent i
, Context context
){
134 SharedPreferences appPreferences
= PreferenceManager
.getDefaultSharedPreferences(context
);
135 String behaviour
= appPreferences
.getString("prefs_instant_behaviour", "NOTHING");
137 if (behaviour
.equalsIgnoreCase("NOTHING")) {
138 Log_OC
.d(TAG
, "upload file and do nothing");
139 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_FORGET
);
140 } else if (behaviour
.equalsIgnoreCase("MOVE")) {
141 i
.putExtra(FileUploader
.KEY_LOCAL_BEHAVIOUR
, FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
142 Log_OC
.d(TAG
, "upload file and move file to oc folder");
147 private void handleNewVideoAction(Context context
, Intent intent
) {
149 String file_path
= null
;
150 String file_name
= null
;
151 String mime_type
= null
;
153 Log_OC
.w(TAG
, "New video received");
155 if (!instantVideoUploadEnabled(context
)) {
156 Log_OC
.d(TAG
, "Instant video upload disabled, ignoring new video");
160 Account account
= AccountUtils
.getCurrentOwnCloudAccount(context
);
161 if (account
== null
) {
162 Log_OC
.w(TAG
, "No owncloud account found for instant upload, aborting");
166 String
[] CONTENT_PROJECTION
= { Video
.Media
.DATA
, Video
.Media
.DISPLAY_NAME
, Video
.Media
.MIME_TYPE
, Video
.Media
.SIZE
};
167 c
= context
.getContentResolver().query(intent
.getData(), CONTENT_PROJECTION
, null
, null
, null
);
168 if (!c
.moveToFirst()) {
169 Log_OC
.e(TAG
, "Couldn't resolve given uri: " + intent
.getDataString());
172 file_path
= c
.getString(c
.getColumnIndex(Video
.Media
.DATA
));
173 file_name
= c
.getString(c
.getColumnIndex(Video
.Media
.DISPLAY_NAME
));
174 mime_type
= c
.getString(c
.getColumnIndex(Video
.Media
.MIME_TYPE
));
176 Log_OC
.d(TAG
, file_path
+ "");
178 if (!isOnline(context
) || (instantVideoUploadViaWiFiOnly(context
) && !isConnectedViaWiFi(context
))) {
182 Intent i
= new Intent(context
, FileUploader
.class);
183 i
.putExtra(FileUploader
.KEY_ACCOUNT
, account
);
184 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, file_path
);
185 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, FileStorageUtils
.getInstantVideoUploadFilePath(context
, file_name
));
186 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
187 i
.putExtra(FileUploader
.KEY_MIME_TYPE
, mime_type
);
188 i
.putExtra(FileUploader
.KEY_INSTANT_UPLOAD
, true
);
190 // instant upload behaviour
191 i
= addInstantUploadBehaviour(i
, context
);
193 context
.startService(i
);
197 private void handleConnectivityAction(Context context
, Intent intent
) {
198 if (!instantPictureUploadEnabled(context
)) {
199 Log_OC
.d(TAG
, "Instant upload disabled, don't upload anything");
203 if (!intent
.hasExtra(ConnectivityManager
.EXTRA_NO_CONNECTIVITY
)
205 && (!instantPictureUploadViaWiFiOnly(context
) || (instantPictureUploadViaWiFiOnly(context
) == isConnectedViaWiFi(context
) == true
))) {
206 DbHandler db
= new DbHandler(context
);
207 Cursor c
= db
.getAwaitingFiles();
208 if (c
.moveToFirst()) {
210 String account_name
= c
.getString(c
.getColumnIndex("account"));
211 String file_path
= c
.getString(c
.getColumnIndex("path"));
212 File f
= new File(file_path
);
214 Account account
= new Account(account_name
, MainApp
.getAccountType());
216 String mimeType
= null
;
218 mimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(
219 f
.getName().substring(f
.getName().lastIndexOf('.') + 1));
221 } catch (Throwable e
) {
222 Log_OC
.e(TAG
, "Trying to find out MIME type of a file without extension: " + f
.getName());
224 if (mimeType
== null
)
225 mimeType
= "application/octet-stream";
227 Intent i
= new Intent(context
, FileUploader
.class);
228 i
.putExtra(FileUploader
.KEY_ACCOUNT
, account
);
229 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, file_path
);
230 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, FileStorageUtils
.getInstantUploadFilePath(context
, f
.getName()));
231 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
232 i
.putExtra(FileUploader
.KEY_INSTANT_UPLOAD
, true
);
234 // instant upload behaviour
235 i
= addInstantUploadBehaviour(i
, context
);
237 context
.startService(i
);
240 Log_OC
.w(TAG
, "Instant upload file " + f
.getAbsolutePath() + " dont exist anymore");
242 } while (c
.moveToNext());
250 public static boolean isOnline(Context context
) {
251 ConnectivityManager cm
= (ConnectivityManager
) context
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
252 return cm
.getActiveNetworkInfo() != null
&& cm
.getActiveNetworkInfo().isConnected();
255 public static boolean isConnectedViaWiFi(Context context
) {
256 ConnectivityManager cm
= (ConnectivityManager
) context
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
257 return cm
!= null
&& cm
.getActiveNetworkInfo() != null
258 && cm
.getActiveNetworkInfo().getType() == ConnectivityManager
.TYPE_WIFI
259 && cm
.getActiveNetworkInfo().getState() == State
.CONNECTED
;
262 public static boolean instantPictureUploadEnabled(Context context
) {
263 return PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean("instant_uploading", false
);
266 public static boolean instantVideoUploadEnabled(Context context
) {
267 return PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean("instant_video_uploading", false
);
270 public static boolean instantPictureUploadViaWiFiOnly(Context context
) {
271 return PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean("instant_upload_on_wifi", false
);
274 public static boolean instantVideoUploadViaWiFiOnly(Context context
) {
275 return PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean("instant_video_upload_on_wifi", false
);