1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
19 package com
.owncloud
.android
.files
;
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
;
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
;
42 public class InstantUploadBroadcastReceiver
extends BroadcastReceiver
{
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";
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
);
58 Log_OC
.e(TAG
, "Incorrect intent sent: " + intent
.getAction());
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
);
74 private void handleNewMediaAction(Context context
, Intent intent
) {
76 String file_path
= null
;
77 String file_name
= null
;
78 String mime_type
= null
;
80 if (!instantUploadEnabled(context
)) {
81 Log_OC
.d(TAG
, "Instant upload disabled, abording uploading");
85 Account account
= AccountUtils
.getCurrentOwnCloudAccount(context
);
86 if (account
== null
) {
87 Log_OC
.w(TAG
, "No owncloud account found for instant upload, aborting");
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());
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");
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");
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());
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");
120 Log_OC
.d(TAG
, file_path
+ "");
122 // same always temporally the picture to upload
123 DbHandler db
= new DbHandler(context
);
124 db
.putFileForLater(file_path
, account
.name
, null
);
127 if (!isOnline(context
) || (instantUploadViaWiFiOnly(context
) && !isConnectedViaWiFi(context
))) {
131 // register for upload finishe message
132 // there is a litte problem with android API, we can register for
134 // intent in registerReceiver but we cannot unregister from precise
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
);
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
);
153 private void handleConnectivityAction(Context context
, Intent intent
) {
154 if (!instantUploadEnabled(context
)) {
155 Log_OC
.d(TAG
, "Instant upload disabled, abording uploading");
159 if (!intent
.hasExtra(ConnectivityManager
.EXTRA_NO_CONNECTIVITY
)
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
);
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
);
172 Account account
= new Account(account_name
, AccountAuthenticator
.ACCOUNT_TYPE
);
174 String mimeType
= null
;
176 mimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(
177 f
.getName().substring(f
.getName().lastIndexOf('.') + 1));
179 } catch (Throwable e
) {
180 Log_OC
.e(TAG
, "Trying to find out MIME type of a file without extension: " + f
.getName());
182 if (mimeType
== null
)
183 mimeType
= "application/octet-stream";
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
);
194 Log_OC
.w(TAG
, "Instant upload file " + f
.getAbsolutePath() + " dont exist anymore");
196 } while (c
.moveToNext());
204 public static boolean isOnline(Context context
) {
205 ConnectivityManager cm
= (ConnectivityManager
) context
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
206 return cm
.getActiveNetworkInfo() != null
&& cm
.getActiveNetworkInfo().isConnected();
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
;
216 public static boolean instantUploadEnabled(Context context
) {
217 return PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean("instant_uploading", false
);
220 public static boolean instantUploadViaWiFiOnly(Context context
) {
221 return PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean("instant_upload_on_wifi", false
);