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 as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 package com
.owncloud
.android
.files
;
24 import com
.owncloud
.android
.AccountUtils
;
25 import com
.owncloud
.android
.authenticator
.AccountAuthenticator
;
26 import com
.owncloud
.android
.db
.DbHandler
;
27 import com
.owncloud
.android
.files
.services
.FileUploader
;
29 import android
.accounts
.Account
;
30 import android
.content
.BroadcastReceiver
;
31 import android
.content
.Context
;
32 import android
.content
.Intent
;
33 import android
.content
.IntentFilter
;
34 import android
.database
.Cursor
;
35 import android
.net
.ConnectivityManager
;
36 import android
.net
.NetworkInfo
.State
;
37 import android
.preference
.PreferenceManager
;
38 import android
.provider
.MediaStore
.Images
.Media
;
39 import android
.util
.Log
;
40 import android
.webkit
.MimeTypeMap
;
42 public class InstantUploadBroadcastReceiver
extends BroadcastReceiver
{
44 public static String INSTANT_UPLOAD_DIR
= "/InstantUpload/";
45 private static String TAG
= "PhotoTakenBroadcastReceiver";
46 private static final String
[] CONTENT_PROJECTION
= { Media
.DATA
,
50 private static String NEW_PHOTO_ACTION
= "com.android.camera.NEW_PICTURE";
53 public void onReceive(Context context
, Intent intent
) {
54 Log
.d(TAG
, "Received: " + intent
.getAction());
55 if (intent
.getAction().equals(android
.net
.ConnectivityManager
.CONNECTIVITY_ACTION
)) {
56 handleConnectivityAction(context
, intent
);
57 } else if (intent
.getAction().equals(NEW_PHOTO_ACTION
)) {
58 handleNewPhotoAction(context
, intent
);
59 } else if (intent
.getAction().equals(FileUploader
.UPLOAD_FINISH_MESSAGE
)) {
60 handleUploadFinished(context
, intent
);
62 Log
.e(TAG
, "Incorrect intent sent: " + intent
.getAction());
66 private void handleUploadFinished(Context context
, Intent intent
) {
67 // remove successfull uploading, ignore rest for reupload on reconnect
68 if (intent
.getBooleanExtra(FileUploader
.EXTRA_UPLOAD_RESULT
, false
)) {
69 DbHandler db
= new DbHandler(context
);
70 String localPath
= intent
.getStringExtra(FileUploader
.EXTRA_OLD_FILE_PATH
);
71 if (!db
.removeIUPendingFile(localPath
,
72 intent
.getStringExtra(FileUploader
.ACCOUNT_NAME
))) {
73 Log
.w(TAG
, "Tried to remove non existing instant upload file " + localPath
);
79 private void handleNewPhotoAction(Context context
, Intent intent
) {
80 if (!instantUploadEnabled(context
)) {
81 Log
.d(TAG
, "Instant upload disabled, abording uploading");
85 Account account
= AccountUtils
.getCurrentOwnCloudAccount(context
);
86 if (account
== null
) {
87 Log
.w(TAG
, "No owncloud account found for instant upload, aborting");
91 Cursor c
= context
.getContentResolver().query(intent
.getData(),
95 if (!c
.moveToFirst()) {
96 Log
.e(TAG
, "Couldn't resolve given uri: " + intent
.getDataString());
100 String file_path
= c
.getString(c
.getColumnIndex(Media
.DATA
));
101 String file_name
= c
.getString(c
.getColumnIndex(Media
.DISPLAY_NAME
));
102 String mime_type
= c
.getString(c
.getColumnIndex(Media
.MIME_TYPE
));
105 Log
.e(TAG
, file_path
+"");
107 if (!isOnline(context
) ||
108 (instantUploadViaWiFiOnly(context
) && !isConnectedViaWiFi(context
))) {
109 DbHandler db
= new DbHandler(context
);
110 db
.putFileForLater(file_path
, account
.name
);
115 // register for upload finishe message
116 // there is a litte problem with android API, we can register for particular
117 // intent in registerReceiver but we cannot unregister from precise intent
118 // we can unregister from entire listenings but thats suck a bit.
119 // On the other hand this might be only for dynamicly registered
120 // broadcast receivers, needs investigation.
121 IntentFilter filter
= new IntentFilter(FileUploader
.UPLOAD_FINISH_MESSAGE
);
122 context
.getApplicationContext().registerReceiver(this, filter
);
124 Intent i
= new Intent(context
, FileUploader
.class);
125 i
.putExtra(FileUploader
.KEY_ACCOUNT
, account
);
126 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, file_path
);
127 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, INSTANT_UPLOAD_DIR
+ file_name
);
128 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
129 i
.putExtra(FileUploader
.KEY_MIME_TYPE
, mime_type
);
130 i
.putExtra(FileUploader
.KEY_INSTANT_UPLOAD
, true
);
131 context
.startService(i
);
135 private void handleConnectivityAction(Context context
, Intent intent
) {
136 if (!instantUploadEnabled(context
)) {
137 Log
.d(TAG
, "Instant upload disabled, abording uploading");
141 if (!intent
.hasExtra(ConnectivityManager
.EXTRA_NO_CONNECTIVITY
) &&
143 (!instantUploadViaWiFiOnly(context
) ||
144 (instantUploadViaWiFiOnly(context
) == isConnectedViaWiFi(context
) == true
)) ) {
145 DbHandler db
= new DbHandler(context
);
146 Cursor c
= db
.getAwaitingFiles();
147 if (c
.moveToFirst()) {
148 IntentFilter filter
= new IntentFilter(FileUploader
.UPLOAD_FINISH_MESSAGE
);
149 context
.getApplicationContext().registerReceiver(this, filter
);
151 String account_name
= c
.getString(c
.getColumnIndex("account"));
152 String file_path
= c
.getString(c
.getColumnIndex("path"));
153 File f
= new File(file_path
);
155 Account account
= new Account(account_name
, AccountAuthenticator
.ACCOUNT_TYPE
);
157 String mimeType
= null
;
159 mimeType
= MimeTypeMap
.getSingleton()
160 .getMimeTypeFromExtension(
161 f
.getName().substring(f
.getName().lastIndexOf('.') + 1));
163 } catch (Throwable e
) {
164 Log
.e(TAG
, "Trying to find out MIME type of a file without extension: " + f
.getName());
166 if (mimeType
== null
)
167 mimeType
= "application/octet-stream";
169 Intent i
= new Intent(context
, FileUploader
.class);
170 i
.putExtra(FileUploader
.KEY_ACCOUNT
, account
);
171 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, file_path
);
172 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, INSTANT_UPLOAD_DIR
+ f
.getName());
173 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
174 i
.putExtra(FileUploader
.KEY_INSTANT_UPLOAD
, true
);
175 context
.startService(i
);
178 Log
.w(TAG
, "Instant upload file " + f
.getAbsolutePath() + " dont exist anymore");
180 } while(c
.moveToNext());
188 private boolean isOnline(Context context
) {
189 ConnectivityManager cm
= (ConnectivityManager
) context
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
190 return cm
.getActiveNetworkInfo() != null
&& cm
.getActiveNetworkInfo().isConnected();
193 private boolean isConnectedViaWiFi(Context context
) {
194 ConnectivityManager cm
= (ConnectivityManager
) context
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
195 return cm
!= null
&& cm
.getActiveNetworkInfo() != null
&&
196 cm
.getActiveNetworkInfo().getType() == ConnectivityManager
.TYPE_WIFI
&&
197 cm
.getActiveNetworkInfo().getState() == State
.CONNECTED
;
200 private boolean instantUploadEnabled(Context context
) {
201 return PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean("instant_uploading", false
);
204 private boolean instantUploadViaWiFiOnly(Context context
) {
205 return PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean("instant_upload_on_wifi", false
);