1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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
.AccountUtils
;
24 import com
.owncloud
.android
.authenticator
.AccountAuthenticator
;
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
.database
.Cursor
;
33 import android
.net
.ConnectivityManager
;
34 import android
.net
.NetworkInfo
.State
;
35 import android
.preference
.PreferenceManager
;
36 import android
.provider
.MediaStore
.Images
.Media
;
37 import android
.util
.Log
;
38 import android
.webkit
.MimeTypeMap
;
40 public class PhotoTakenBroadcastReceiver
extends BroadcastReceiver
{
42 public static String INSTANT_UPLOAD_DIR
= "/InstantUpload/";
43 private static String TAG
= "PhotoTakenBroadcastReceiver";
44 private static final String
[] CONTENT_PROJECTION
= { Media
.DATA
, Media
.DISPLAY_NAME
, Media
.MIME_TYPE
, Media
.SIZE
};
45 private static String NEW_PHOTO_ACTION
= "com.android.camera.NEW_PICTURE";
48 public void onReceive(Context context
, Intent intent
) {
49 boolean iu_enabled
= PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean("instant_uploading", false
);
52 Log
.d(TAG
, "Instant upload disabled, abording uploading");
56 if (intent
.getAction().equals(android
.net
.ConnectivityManager
.CONNECTIVITY_ACTION
)) {
57 handleConnectivityAction(context
, intent
);
58 } else if (intent
.getAction().equals(NEW_PHOTO_ACTION
)) {
59 handleNewPhotoAction(context
, intent
);
61 Log
.e(TAG
, "Incorrect intent sent: " + intent
.getAction());
65 private void handleNewPhotoAction(Context context
, Intent intent
) {
66 Account account
= AccountUtils
.getCurrentOwnCloudAccount(context
);
67 if (account
== null
) {
68 Log
.w(TAG
, "No owncloud account found for instant upload, aborting");
72 Cursor c
= context
.getContentResolver().query(intent
.getData(), CONTENT_PROJECTION
, null
, null
, null
);
74 if (!c
.moveToFirst()) {
75 Log
.e(TAG
, "Couldn't resolve given uri!");
79 boolean iu_via_wifi
= PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean("instant_upload_on_wifi", false
);
80 boolean is_conn_via_wifi
= false
;
82 ConnectivityManager cm
= (ConnectivityManager
) context
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
83 if (cm
!= null
&& cm
.getActiveNetworkInfo() != null
&&
84 cm
.getActiveNetworkInfo().getType() == ConnectivityManager
.TYPE_WIFI
&&
85 cm
.getActiveNetworkInfo().getState() == State
.CONNECTED
)
86 is_conn_via_wifi
= true
;
90 String file_path
= c
.getString(c
.getColumnIndex(Media
.DATA
));
91 String file_name
= c
.getString(c
.getColumnIndex(Media
.DISPLAY_NAME
));
92 String mime_type
= c
.getString(c
.getColumnIndex(Media
.MIME_TYPE
));
93 //long file_size = c.getLong(c.getColumnIndex(Media.SIZE));
97 if (!isOnline(context
) || (iu_via_wifi
&& !is_conn_via_wifi
)) {
98 DbHandler db
= new DbHandler(context
);
99 db
.putFileForLater(file_path
, account
.name
);
105 Intent upload_intent = new Intent(context, InstantUploadService.class);
106 upload_intent.putExtra(InstantUploadService.KEY_ACCOUNT, account);
107 upload_intent.putExtra(InstantUploadService.KEY_FILE_PATH, file_path);
108 upload_intent.putExtra(InstantUploadService.KEY_DISPLAY_NAME, file_name);
109 upload_intent.putExtra(InstantUploadService.KEY_FILE_SIZE, file_size);
110 upload_intent.putExtra(InstantUploadService.KEY_MIME_TYPE, mime_type);
112 context.startService(upload_intent);
115 Intent i
= new Intent(context
, FileUploader
.class);
116 i
.putExtra(FileUploader
.KEY_ACCOUNT
, account
);
117 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, file_path
);
118 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, INSTANT_UPLOAD_DIR
+ "/" + file_name
);
119 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
120 i
.putExtra(FileUploader
.KEY_MIME_TYPE
, mime_type
);
121 i
.putExtra(FileUploader
.KEY_INSTANT_UPLOAD
, true
);
122 context
.startService(i
);
126 private void handleConnectivityAction(Context context
, Intent intent
) {
127 if (!intent
.hasExtra(ConnectivityManager
.EXTRA_NO_CONNECTIVITY
) ||
129 DbHandler db
= new DbHandler(context
);
130 Cursor c
= db
.getAwaitingFiles();
131 if (c
.moveToFirst()) {
133 String account_name
= c
.getString(c
.getColumnIndex("account"));
134 String file_path
= c
.getString(c
.getColumnIndex("path"));
135 File f
= new File(file_path
);
137 //Intent upload_intent = new Intent(context, InstantUploadService.class);
138 Account account
= new Account(account_name
, AccountAuthenticator
.ACCOUNT_TYPE
);
140 String mimeType
= null
;
142 mimeType
= MimeTypeMap
.getSingleton()
143 .getMimeTypeFromExtension(
144 f
.getName().substring(f
.getName().lastIndexOf('.') + 1));
146 } catch (IndexOutOfBoundsException e
) {
147 Log
.e(TAG
, "Trying to find out MIME type of a file without extension: " + f
.getName());
149 if (mimeType
== null
)
150 mimeType
= "application/octet-stream";
153 upload_intent.putExtra(InstantUploadService.KEY_ACCOUNT, account);
154 upload_intent.putExtra(InstantUploadService.KEY_FILE_PATH, file_path);
155 upload_intent.putExtra(InstantUploadService.KEY_DISPLAY_NAME, f.getName());
156 upload_intent.putExtra(InstantUploadService.KEY_FILE_SIZE, f.length());
157 upload_intent.putExtra(InstantUploadService.KEY_MIME_TYPE, mimeType);
159 context.startService(upload_intent);
162 Intent i
= new Intent(context
, FileUploader
.class);
163 i
.putExtra(FileUploader
.KEY_ACCOUNT
, account
);
164 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, file_path
);
165 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, INSTANT_UPLOAD_DIR
+ f
.getName());
166 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
167 i
.putExtra(FileUploader
.KEY_INSTANT_UPLOAD
, true
);
168 context
.startService(i
);
171 Log
.w(TAG
, "Instant upload file " + f
.getName() + " dont exist anymore");
173 } while(c
.moveToNext());
182 private boolean isOnline(Context context
) {
183 ConnectivityManager cm
= (ConnectivityManager
) context
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
184 return cm
.getActiveNetworkInfo() != null
&& cm
.getActiveNetworkInfo().isConnected();