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
.authenticator
.AccountAuthenticator
;
39 import com
.owncloud
.android
.db
.DbHandler
;
40 import com
.owncloud
.android
.files
.services
.FileUploader
;
41 import com
.owncloud
.android
.utils
.FileStorageUtils
;
43 public class InstantUploadBroadcastReceiver
extends BroadcastReceiver
{
45 private static String TAG
= "PhotoTakenBroadcastReceiver";
46 private static final String
[] CONTENT_PROJECTION
= { Media
.DATA
, Media
.DISPLAY_NAME
, Media
.MIME_TYPE
, Media
.SIZE
};
47 private static String NEW_PHOTO_ACTION
= "com.android.camera.NEW_PICTURE";
50 public void onReceive(Context context
, Intent intent
) {
51 Log
.d(TAG
, "Received: " + intent
.getAction());
52 if (intent
.getAction().equals(android
.net
.ConnectivityManager
.CONNECTIVITY_ACTION
)) {
53 handleConnectivityAction(context
, intent
);
54 } else if (intent
.getAction().equals(NEW_PHOTO_ACTION
)) {
55 handleNewPhotoAction(context
, intent
);
56 } else if (intent
.getAction().equals(FileUploader
.UPLOAD_FINISH_MESSAGE
)) {
57 handleUploadFinished(context
, intent
);
59 Log
.e(TAG
, "Incorrect intent sent: " + intent
.getAction());
63 private void handleUploadFinished(Context context
, Intent intent
) {
64 // remove successfull uploading, ignore rest for reupload on reconnect
65 if (intent
.getBooleanExtra(FileUploader
.EXTRA_UPLOAD_RESULT
, false
)) {
66 DbHandler db
= new DbHandler(context
);
67 String localPath
= intent
.getStringExtra(FileUploader
.EXTRA_OLD_FILE_PATH
);
68 if (!db
.removeIUPendingFile(localPath
)) {
69 Log
.w(TAG
, "Tried to remove non existing instant upload file " + localPath
);
75 private void handleNewPhotoAction(Context context
, Intent intent
) {
76 if (!instantUploadEnabled(context
)) {
77 Log
.d(TAG
, "Instant upload disabled, abording uploading");
81 Account account
= AccountUtils
.getCurrentOwnCloudAccount(context
);
82 if (account
== null
) {
83 Log
.w(TAG
, "No owncloud account found for instant upload, aborting");
87 Cursor c
= context
.getContentResolver().query(intent
.getData(), CONTENT_PROJECTION
, null
, null
, null
);
89 if (!c
.moveToFirst()) {
90 Log
.e(TAG
, "Couldn't resolve given uri: " + intent
.getDataString());
94 String file_path
= c
.getString(c
.getColumnIndex(Media
.DATA
));
95 String file_name
= c
.getString(c
.getColumnIndex(Media
.DISPLAY_NAME
));
96 String mime_type
= c
.getString(c
.getColumnIndex(Media
.MIME_TYPE
));
99 Log
.e(TAG
, file_path
+ "");
101 // same always temporally the picture to upload
102 DbHandler db
= new DbHandler(context
);
103 db
.putFileForLater(file_path
, account
.name
, null
);
106 if (!isOnline(context
) || (instantUploadViaWiFiOnly(context
) && !isConnectedViaWiFi(context
))) {
110 // register for upload finishe message
111 // there is a litte problem with android API, we can register for
113 // intent in registerReceiver but we cannot unregister from precise
115 // we can unregister from entire listenings but thats suck a bit.
116 // On the other hand this might be only for dynamicly registered
117 // broadcast receivers, needs investigation.
118 IntentFilter filter
= new IntentFilter(FileUploader
.UPLOAD_FINISH_MESSAGE
);
119 context
.getApplicationContext().registerReceiver(this, filter
);
121 Intent i
= new Intent(context
, FileUploader
.class);
122 i
.putExtra(FileUploader
.KEY_ACCOUNT
, account
);
123 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, file_path
);
124 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, FileStorageUtils
.getInstantUploadFilePath(file_name
));
125 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
126 i
.putExtra(FileUploader
.KEY_MIME_TYPE
, mime_type
);
127 i
.putExtra(FileUploader
.KEY_INSTANT_UPLOAD
, true
);
128 context
.startService(i
);
132 private void handleConnectivityAction(Context context
, Intent intent
) {
133 if (!instantUploadEnabled(context
)) {
134 Log
.d(TAG
, "Instant upload disabled, abording uploading");
138 if (!intent
.hasExtra(ConnectivityManager
.EXTRA_NO_CONNECTIVITY
)
140 && (!instantUploadViaWiFiOnly(context
) || (instantUploadViaWiFiOnly(context
) == isConnectedViaWiFi(context
) == true
))) {
141 DbHandler db
= new DbHandler(context
);
142 Cursor c
= db
.getAwaitingFiles();
143 if (c
.moveToFirst()) {
144 IntentFilter filter
= new IntentFilter(FileUploader
.UPLOAD_FINISH_MESSAGE
);
145 context
.getApplicationContext().registerReceiver(this, filter
);
147 String account_name
= c
.getString(c
.getColumnIndex("account"));
148 String file_path
= c
.getString(c
.getColumnIndex("path"));
149 File f
= new File(file_path
);
151 Account account
= new Account(account_name
, AccountAuthenticator
.ACCOUNT_TYPE
);
153 String mimeType
= null
;
155 mimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(
156 f
.getName().substring(f
.getName().lastIndexOf('.') + 1));
158 } catch (Throwable e
) {
159 Log
.e(TAG
, "Trying to find out MIME type of a file without extension: " + f
.getName());
161 if (mimeType
== null
)
162 mimeType
= "application/octet-stream";
164 Intent i
= new Intent(context
, FileUploader
.class);
165 i
.putExtra(FileUploader
.KEY_ACCOUNT
, account
);
166 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, file_path
);
167 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, FileStorageUtils
.getInstantUploadFilePath(f
.getName()));
168 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
169 i
.putExtra(FileUploader
.KEY_INSTANT_UPLOAD
, true
);
170 context
.startService(i
);
173 Log
.w(TAG
, "Instant upload file " + f
.getAbsolutePath() + " dont exist anymore");
175 } while (c
.moveToNext());
183 public static boolean isOnline(Context context
) {
184 ConnectivityManager cm
= (ConnectivityManager
) context
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
185 return cm
.getActiveNetworkInfo() != null
&& cm
.getActiveNetworkInfo().isConnected();
188 public static boolean isConnectedViaWiFi(Context context
) {
189 ConnectivityManager cm
= (ConnectivityManager
) context
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
190 return cm
!= null
&& cm
.getActiveNetworkInfo() != null
191 && cm
.getActiveNetworkInfo().getType() == ConnectivityManager
.TYPE_WIFI
192 && cm
.getActiveNetworkInfo().getState() == State
.CONNECTED
;
195 public static boolean instantUploadEnabled(Context context
) {
196 return PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean("instant_uploading", false
);
199 public static boolean instantUploadViaWiFiOnly(Context context
) {
200 return PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean("instant_upload_on_wifi", false
);