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 com
.owncloud
.android
.authentication
.AccountAuthenticator
;
24 import com
.owncloud
.android
.authentication
.AccountUtils
;
25 import com
.owncloud
.android
.db
.DbHandler
;
26 import com
.owncloud
.android
.files
.services
.FileUploader
;
28 import android
.accounts
.Account
;
29 import android
.content
.BroadcastReceiver
;
30 import android
.content
.Context
;
31 import android
.content
.Intent
;
32 import android
.content
.IntentFilter
;
33 import android
.database
.Cursor
;
34 import android
.net
.ConnectivityManager
;
35 import android
.net
.NetworkInfo
.State
;
36 import android
.preference
.PreferenceManager
;
37 import android
.provider
.MediaStore
.Images
.Media
;
38 import android
.webkit
.MimeTypeMap
;
40 import com
.owncloud
.android
.Log_OC
;
41 import com
.owncloud
.android
.utils
.FileStorageUtils
;
43 public class InstantUploadBroadcastReceiver
extends BroadcastReceiver
{
45 private static String TAG
= "InstantUploadBroadcastReceiver";
46 private static final String
[] CONTENT_PROJECTION
= { Media
.DATA
, Media
.DISPLAY_NAME
, Media
.MIME_TYPE
, Media
.SIZE
};
47 //Unofficial action, works for most devices but not HTC. See: https://github.com/owncloud/android/issues/6
48 private static String NEW_PHOTO_ACTION_UNOFFICIAL
= "com.android.camera.NEW_PICTURE";
49 //Officially supported action since SDK 14: http://developer.android.com/reference/android/hardware/Camera.html#ACTION_NEW_PICTURE
50 private static String NEW_PHOTO_ACTION
= "android.hardware.action.NEW_PICTURE";
53 public void onReceive(Context context
, Intent intent
) {
54 Log_OC
.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_UNOFFICIAL
)) {
58 handleNewPhotoAction(context
, intent
);
59 Log_OC
.d(TAG
, "UNOFFICIAL processed: com.android.camera.NEW_PICTURE");
60 } else if (intent
.getAction().equals(NEW_PHOTO_ACTION
)) {
61 handleNewPhotoAction(context
, intent
);
62 Log_OC
.d(TAG
, "OFFICIAL processed: android.hardware.action.NEW_PICTURE");
63 } else if (intent
.getAction().equals(FileUploader
.UPLOAD_FINISH_MESSAGE
)) {
64 handleUploadFinished(context
, intent
);
66 Log_OC
.e(TAG
, "Incorrect intent sent: " + intent
.getAction());
70 private void handleUploadFinished(Context context
, Intent intent
) {
71 // remove successfull uploading, ignore rest for reupload on reconnect
72 if (intent
.getBooleanExtra(FileUploader
.EXTRA_UPLOAD_RESULT
, false
)) {
73 DbHandler db
= new DbHandler(context
);
74 String localPath
= intent
.getStringExtra(FileUploader
.EXTRA_OLD_FILE_PATH
);
75 if (!db
.removeIUPendingFile(localPath
)) {
76 Log_OC
.w(TAG
, "Tried to remove non existing instant upload file " + localPath
);
82 private void handleNewPhotoAction(Context context
, Intent intent
) {
83 if (!instantUploadEnabled(context
)) {
84 Log_OC
.d(TAG
, "Instant upload disabled, aborting uploading");
88 Account account
= AccountUtils
.getCurrentOwnCloudAccount(context
);
89 if (account
== null
) {
90 Log_OC
.w(TAG
, "No owncloud account found for instant upload, aborting");
94 Cursor c
= context
.getContentResolver().query(intent
.getData(), CONTENT_PROJECTION
, null
, null
, null
);
96 if (!c
.moveToFirst()) {
97 Log_OC
.e(TAG
, "Couldn't resolve given uri: " + intent
.getDataString());
101 String file_path
= c
.getString(c
.getColumnIndex(Media
.DATA
));
102 String file_name
= c
.getString(c
.getColumnIndex(Media
.DISPLAY_NAME
));
103 String mime_type
= c
.getString(c
.getColumnIndex(Media
.MIME_TYPE
));
106 Log_OC
.e(TAG
, file_path
+ "");
108 // same always temporally the picture to upload
109 DbHandler db
= new DbHandler(context
);
110 db
.putFileForLater(file_path
, account
.name
, null
);
113 if (!isOnline(context
) || (instantUploadViaWiFiOnly(context
) && !isConnectedViaWiFi(context
))) {
117 // register for upload finishe message
118 // there is a litte problem with android API, we can register for
120 // intent in registerReceiver but we cannot unregister from precise
122 // we can unregister from entire listenings but thats suck a bit.
123 // On the other hand this might be only for dynamicly registered
124 // broadcast receivers, needs investigation.
125 IntentFilter filter
= new IntentFilter(FileUploader
.UPLOAD_FINISH_MESSAGE
);
126 context
.getApplicationContext().registerReceiver(this, filter
);
128 Intent i
= new Intent(context
, FileUploader
.class);
129 i
.putExtra(FileUploader
.KEY_ACCOUNT
, account
);
130 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, file_path
);
131 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, FileStorageUtils
.getInstantUploadFilePath(context
, file_name
));
132 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
133 i
.putExtra(FileUploader
.KEY_MIME_TYPE
, mime_type
);
134 i
.putExtra(FileUploader
.KEY_INSTANT_UPLOAD
, true
);
135 context
.startService(i
);
139 private void handleConnectivityAction(Context context
, Intent intent
) {
140 if (!instantUploadEnabled(context
)) {
141 Log_OC
.d(TAG
, "Instant upload disabled, abording uploading");
145 if (!intent
.hasExtra(ConnectivityManager
.EXTRA_NO_CONNECTIVITY
)
147 && (!instantUploadViaWiFiOnly(context
) || (instantUploadViaWiFiOnly(context
) == isConnectedViaWiFi(context
) == true
))) {
148 DbHandler db
= new DbHandler(context
);
149 Cursor c
= db
.getAwaitingFiles();
150 if (c
.moveToFirst()) {
151 IntentFilter filter
= new IntentFilter(FileUploader
.UPLOAD_FINISH_MESSAGE
);
152 context
.getApplicationContext().registerReceiver(this, filter
);
154 String account_name
= c
.getString(c
.getColumnIndex("account"));
155 String file_path
= c
.getString(c
.getColumnIndex("path"));
156 File f
= new File(file_path
);
158 Account account
= new Account(account_name
, AccountAuthenticator
.ACCOUNT_TYPE
);
160 String mimeType
= null
;
162 mimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(
163 f
.getName().substring(f
.getName().lastIndexOf('.') + 1));
165 } catch (Throwable e
) {
166 Log_OC
.e(TAG
, "Trying to find out MIME type of a file without extension: " + f
.getName());
168 if (mimeType
== null
)
169 mimeType
= "application/octet-stream";
171 Intent i
= new Intent(context
, FileUploader
.class);
172 i
.putExtra(FileUploader
.KEY_ACCOUNT
, account
);
173 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, file_path
);
174 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, FileStorageUtils
.getInstantUploadFilePath(context
, f
.getName()));
175 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
176 i
.putExtra(FileUploader
.KEY_INSTANT_UPLOAD
, true
);
177 context
.startService(i
);
180 Log_OC
.w(TAG
, "Instant upload file " + f
.getAbsolutePath() + " dont exist anymore");
182 } while (c
.moveToNext());
190 public static boolean isOnline(Context context
) {
191 ConnectivityManager cm
= (ConnectivityManager
) context
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
192 return cm
.getActiveNetworkInfo() != null
&& cm
.getActiveNetworkInfo().isConnected();
195 public static boolean isConnectedViaWiFi(Context context
) {
196 ConnectivityManager cm
= (ConnectivityManager
) context
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
197 return cm
!= null
&& cm
.getActiveNetworkInfo() != null
198 && cm
.getActiveNetworkInfo().getType() == ConnectivityManager
.TYPE_WIFI
199 && cm
.getActiveNetworkInfo().getState() == State
.CONNECTED
;
202 public static boolean instantUploadEnabled(Context context
) {
203 return PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean("instant_uploading", false
);
206 public static boolean instantUploadViaWiFiOnly(Context context
) {
207 return PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean("instant_upload_on_wifi", false
);