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
.preference
.PreferenceManager
;
35 import android
.provider
.MediaStore
.Images
.Media
;
36 import android
.util
.Log
;
37 import android
.webkit
.MimeTypeMap
;
39 public class PhotoTakenBroadcastReceiver
extends BroadcastReceiver
{
41 public static String INSTANT_UPLOAD_DIR
= "/InstantUpload/";
42 private static String TAG
= "PhotoTakenBroadcastReceiver";
43 private static final String
[] CONTENT_PROJECTION
= { Media
.DATA
, Media
.DISPLAY_NAME
, Media
.MIME_TYPE
, Media
.SIZE
};
44 private static String NEW_PHOTO_ACTION
= "com.android.camera.NEW_PICTURE";
47 public void onReceive(Context context
, Intent intent
) {
48 if (!PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean("instant_uploading", false
)) {
49 Log
.d(TAG
, "Instant upload disabled, abording uploading");
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
);
57 Log
.e(TAG
, "Incorrect intent sent: " + intent
.getAction());
61 private void handleNewPhotoAction(Context context
, Intent intent
) {
62 Account account
= AccountUtils
.getCurrentOwnCloudAccount(context
);
63 if (account
== null
) {
64 Log
.w(TAG
, "No owncloud account found for instant upload, aborting");
68 Cursor c
= context
.getContentResolver().query(intent
.getData(), CONTENT_PROJECTION
, null
, null
, null
);
70 if (!c
.moveToFirst()) {
71 Log
.e(TAG
, "Couldn't resolve given uri!");
75 String file_path
= c
.getString(c
.getColumnIndex(Media
.DATA
));
76 String file_name
= c
.getString(c
.getColumnIndex(Media
.DISPLAY_NAME
));
77 String mime_type
= c
.getString(c
.getColumnIndex(Media
.MIME_TYPE
));
78 //long file_size = c.getLong(c.getColumnIndex(Media.SIZE));
82 if (!isOnline(context
)) {
83 DbHandler db
= new DbHandler(context
);
84 db
.putFileForLater(file_path
, account
.name
);
90 Intent upload_intent = new Intent(context, InstantUploadService.class);
91 upload_intent.putExtra(InstantUploadService.KEY_ACCOUNT, account);
92 upload_intent.putExtra(InstantUploadService.KEY_FILE_PATH, file_path);
93 upload_intent.putExtra(InstantUploadService.KEY_DISPLAY_NAME, file_name);
94 upload_intent.putExtra(InstantUploadService.KEY_FILE_SIZE, file_size);
95 upload_intent.putExtra(InstantUploadService.KEY_MIME_TYPE, mime_type);
97 context.startService(upload_intent);
100 Intent i
= new Intent(context
, FileUploader
.class);
101 i
.putExtra(FileUploader
.KEY_ACCOUNT
, account
);
102 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, file_path
);
103 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, INSTANT_UPLOAD_DIR
+ "/" + file_name
);
104 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
105 i
.putExtra(FileUploader
.KEY_MIME_TYPE
, mime_type
);
106 i
.putExtra(FileUploader
.KEY_INSTANT_UPLOAD
, true
);
107 context
.startService(i
);
111 private void handleConnectivityAction(Context context
, Intent intent
) {
112 if (!intent
.hasExtra(ConnectivityManager
.EXTRA_NO_CONNECTIVITY
) ||
114 DbHandler db
= new DbHandler(context
);
115 Cursor c
= db
.getAwaitingFiles();
116 if (c
.moveToFirst()) {
118 String account_name
= c
.getString(c
.getColumnIndex("account"));
119 String file_path
= c
.getString(c
.getColumnIndex("path"));
120 File f
= new File(file_path
);
122 //Intent upload_intent = new Intent(context, InstantUploadService.class);
123 Account account
= new Account(account_name
, AccountAuthenticator
.ACCOUNT_TYPE
);
125 String mimeType
= null
;
127 mimeType
= MimeTypeMap
.getSingleton()
128 .getMimeTypeFromExtension(
129 f
.getName().substring(f
.getName().lastIndexOf('.') + 1));
131 } catch (IndexOutOfBoundsException e
) {
132 Log
.e(TAG
, "Trying to find out MIME type of a file without extension: " + f
.getName());
134 if (mimeType
== null
)
135 mimeType
= "application/octet-stream";
138 upload_intent.putExtra(InstantUploadService.KEY_ACCOUNT, account);
139 upload_intent.putExtra(InstantUploadService.KEY_FILE_PATH, file_path);
140 upload_intent.putExtra(InstantUploadService.KEY_DISPLAY_NAME, f.getName());
141 upload_intent.putExtra(InstantUploadService.KEY_FILE_SIZE, f.length());
142 upload_intent.putExtra(InstantUploadService.KEY_MIME_TYPE, mimeType);
144 context.startService(upload_intent);
147 Intent i
= new Intent(context
, FileUploader
.class);
148 i
.putExtra(FileUploader
.KEY_ACCOUNT
, account
);
149 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, file_path
);
150 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, INSTANT_UPLOAD_DIR
+ f
.getName());
151 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
152 i
.putExtra(FileUploader
.KEY_INSTANT_UPLOAD
, true
);
153 context
.startService(i
);
156 Log
.w(TAG
, "Instant upload file " + f
.getName() + " dont exist anymore");
158 } while(c
.moveToNext());
167 private boolean isOnline(Context context
) {
168 ConnectivityManager cm
= (ConnectivityManager
) context
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
169 return cm
.getActiveNetworkInfo() != null
&& cm
.getActiveNetworkInfo().isConnected();