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 2 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 android
.accounts
.Account
;
25 import android
.content
.BroadcastReceiver
;
26 import android
.content
.Context
;
27 import android
.content
.Intent
;
28 import android
.content
.IntentFilter
;
29 import android
.database
.Cursor
;
30 import android
.net
.ConnectivityManager
;
31 import android
.net
.NetworkInfo
.State
;
32 import android
.preference
.PreferenceManager
;
33 import android
.provider
.MediaStore
.Images
.Media
;
34 import android
.util
.Log
;
35 import android
.webkit
.MimeTypeMap
;
37 import com
.owncloud
.android
.AccountUtils
;
38 import com
.owncloud
.android
.Log_OC
;
39 import com
.owncloud
.android
.authenticator
.AccountAuthenticator
;
40 import com
.owncloud
.android
.db
.DbHandler
;
41 import com
.owncloud
.android
.files
.services
.FileUploader
;
43 public class InstantUploadBroadcastReceiver
extends BroadcastReceiver
{
45 public static String INSTANT_UPLOAD_DIR
= "/InstantUpload/";
46 private static String TAG
= "PhotoTakenBroadcastReceiver";
47 private static final String
[] CONTENT_PROJECTION
= { Media
.DATA
, Media
.DISPLAY_NAME
, Media
.MIME_TYPE
, Media
.SIZE
};
48 private static String NEW_PHOTO_ACTION
= "com.android.camera.NEW_PICTURE";
51 public void onReceive(Context context
, Intent intent
) {
52 Log_OC
.d(TAG
, "Received: " + intent
.getAction());
53 if (intent
.getAction().equals(android
.net
.ConnectivityManager
.CONNECTIVITY_ACTION
)) {
54 handleConnectivityAction(context
, intent
);
55 } else if (intent
.getAction().equals(NEW_PHOTO_ACTION
)) {
56 handleNewPhotoAction(context
, intent
);
57 } else if (intent
.getAction().equals(FileUploader
.UPLOAD_FINISH_MESSAGE
)) {
58 handleUploadFinished(context
, intent
);
60 Log_OC
.e(TAG
, "Incorrect intent sent: " + intent
.getAction());
64 private void handleUploadFinished(Context context
, Intent intent
) {
65 // remove successfull uploading, ignore rest for reupload on reconnect
66 if (intent
.getBooleanExtra(FileUploader
.EXTRA_UPLOAD_RESULT
, false
)) {
67 DbHandler db
= new DbHandler(context
);
68 String localPath
= intent
.getStringExtra(FileUploader
.EXTRA_OLD_FILE_PATH
);
69 if (!db
.removeIUPendingFile(localPath
)) {
70 Log_OC
.w(TAG
, "Tried to remove non existing instant upload file " + localPath
);
76 private void handleNewPhotoAction(Context context
, Intent intent
) {
77 if (!instantUploadEnabled(context
)) {
78 Log_OC
.d(TAG
, "Instant upload disabled, abording uploading");
82 Account account
= AccountUtils
.getCurrentOwnCloudAccount(context
);
83 if (account
== null
) {
84 Log_OC
.w(TAG
, "No owncloud account found for instant upload, aborting");
88 Cursor c
= context
.getContentResolver().query(intent
.getData(), CONTENT_PROJECTION
, null
, null
, null
);
90 if (!c
.moveToFirst()) {
91 Log_OC
.e(TAG
, "Couldn't resolve given uri: " + intent
.getDataString());
95 String file_path
= c
.getString(c
.getColumnIndex(Media
.DATA
));
96 String file_name
= c
.getString(c
.getColumnIndex(Media
.DISPLAY_NAME
));
97 String mime_type
= c
.getString(c
.getColumnIndex(Media
.MIME_TYPE
));
100 Log_OC
.e(TAG
, file_path
+ "");
102 // same always temporally the picture to upload
103 DbHandler db
= new DbHandler(context
);
104 db
.putFileForLater(file_path
, account
.name
);
107 if (!isOnline(context
) || (instantUploadViaWiFiOnly(context
) && !isConnectedViaWiFi(context
))) {
111 // register for upload finishe message
112 // there is a litte problem with android API, we can register for
114 // intent in registerReceiver but we cannot unregister from precise
116 // we can unregister from entire listenings but thats suck a bit.
117 // On the other hand this might be only for dynamicly registered
118 // broadcast receivers, needs investigation.
119 IntentFilter filter
= new IntentFilter(FileUploader
.UPLOAD_FINISH_MESSAGE
);
120 context
.getApplicationContext().registerReceiver(this, filter
);
122 Intent i
= new Intent(context
, FileUploader
.class);
123 i
.putExtra(FileUploader
.KEY_ACCOUNT
, account
);
124 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, file_path
);
125 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, INSTANT_UPLOAD_DIR
+ file_name
);
126 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
127 i
.putExtra(FileUploader
.KEY_MIME_TYPE
, mime_type
);
128 i
.putExtra(FileUploader
.KEY_INSTANT_UPLOAD
, true
);
129 context
.startService(i
);
133 private void handleConnectivityAction(Context context
, Intent intent
) {
134 if (!instantUploadEnabled(context
)) {
135 Log_OC
.d(TAG
, "Instant upload disabled, abording uploading");
139 if (!intent
.hasExtra(ConnectivityManager
.EXTRA_NO_CONNECTIVITY
)
141 && (!instantUploadViaWiFiOnly(context
) || (instantUploadViaWiFiOnly(context
) == isConnectedViaWiFi(context
) == true
))) {
142 DbHandler db
= new DbHandler(context
);
143 Cursor c
= db
.getAwaitingFiles();
144 if (c
.moveToFirst()) {
145 IntentFilter filter
= new IntentFilter(FileUploader
.UPLOAD_FINISH_MESSAGE
);
146 context
.getApplicationContext().registerReceiver(this, filter
);
148 String account_name
= c
.getString(c
.getColumnIndex("account"));
149 String file_path
= c
.getString(c
.getColumnIndex("path"));
150 File f
= new File(file_path
);
152 Account account
= new Account(account_name
, AccountAuthenticator
.ACCOUNT_TYPE
);
154 String mimeType
= null
;
156 mimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(
157 f
.getName().substring(f
.getName().lastIndexOf('.') + 1));
159 } catch (Throwable e
) {
160 Log_OC
.e(TAG
, "Trying to find out MIME type of a file without extension: " + f
.getName());
162 if (mimeType
== null
)
163 mimeType
= "application/octet-stream";
165 Intent i
= new Intent(context
, FileUploader
.class);
166 i
.putExtra(FileUploader
.KEY_ACCOUNT
, account
);
167 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, file_path
);
168 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, INSTANT_UPLOAD_DIR
+ f
.getName());
169 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
170 i
.putExtra(FileUploader
.KEY_INSTANT_UPLOAD
, true
);
171 context
.startService(i
);
174 Log_OC
.w(TAG
, "Instant upload file " + f
.getAbsolutePath() + " dont exist anymore");
176 } while (c
.moveToNext());
184 public static boolean isOnline(Context context
) {
185 ConnectivityManager cm
= (ConnectivityManager
) context
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
186 return cm
.getActiveNetworkInfo() != null
&& cm
.getActiveNetworkInfo().isConnected();
189 public static boolean isConnectedViaWiFi(Context context
) {
190 ConnectivityManager cm
= (ConnectivityManager
) context
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
191 return cm
!= null
&& cm
.getActiveNetworkInfo() != null
192 && cm
.getActiveNetworkInfo().getType() == ConnectivityManager
.TYPE_WIFI
193 && cm
.getActiveNetworkInfo().getState() == State
.CONNECTED
;
196 public static boolean instantUploadEnabled(Context context
) {
197 return PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean("instant_uploading", false
);
200 public static boolean instantUploadViaWiFiOnly(Context context
) {
201 return PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean("instant_upload_on_wifi", false
);