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
.InstantUploadService
;
28 import com
.owncloud
.android
.R
;
29 import android
.accounts
.Account
;
30 import android
.content
.BroadcastReceiver
;
31 import android
.content
.Context
;
32 import android
.content
.Intent
;
33 import android
.database
.Cursor
;
34 import android
.net
.ConnectivityManager
;
35 import android
.preference
.Preference
;
36 import android
.preference
.PreferenceManager
;
37 import android
.provider
.MediaStore
.Images
.Media
;
38 import android
.util
.Log
;
39 import android
.webkit
.MimeTypeMap
;
41 public class PhotoTakenBroadcastReceiver
extends BroadcastReceiver
{
43 private static String TAG
= "PhotoTakenBroadcastReceiver";
44 private static final String
[] CONTENT_PROJECTION
= { Media
.DATA
, Media
.DISPLAY_NAME
, Media
.MIME_TYPE
, Media
.SIZE
};
46 private static String NEW_PHOTO_ACTION
= "com.android.camera.NEW_PICTURE";
49 public void onReceive(Context context
, Intent intent
) {
50 if (!PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean("instant_uploading", false
)) {
51 Log
.d(TAG
, "Instant upload disabled, abording uploading");
54 if (intent
.getAction().equals(android
.net
.ConnectivityManager
.CONNECTIVITY_ACTION
)) {
55 handleConnectivityAction(context
, intent
);
56 } else if (intent
.getAction().equals(NEW_PHOTO_ACTION
)) {
57 handleNewPhontoAction(context
, intent
);
59 Log
.e(TAG
, "Incorrect intent sent: " + intent
.getAction());
63 private void handleNewPhontoAction(Context context
, Intent intent
) {
64 Account account
= AccountUtils
.getCurrentOwnCloudAccount(context
);
65 if (account
== null
) {
66 Log
.w(TAG
, "No owncloud account found for instant upload, abording");
70 Cursor c
= context
.getContentResolver().query(intent
.getData(), CONTENT_PROJECTION
, null
, null
, null
);
72 if (!c
.moveToFirst()) {
73 Log
.e(TAG
, "Couldn't resolve given uri!");
77 String file_path
= c
.getString(c
.getColumnIndex(Media
.DATA
));
78 String file_name
= c
.getString(c
.getColumnIndex(Media
.DISPLAY_NAME
));
79 String mime_type
= c
.getString(c
.getColumnIndex(Media
.MIME_TYPE
));
80 long file_size
= c
.getLong(c
.getColumnIndex(Media
.SIZE
));
84 if (!isOnline(context
)) {
85 DbHandler db
= new DbHandler(context
);
86 db
.putFileForLater(file_path
, account
.name
);
91 Intent upload_intent
= new Intent(context
, InstantUploadService
.class);
92 upload_intent
.putExtra(InstantUploadService
.KEY_ACCOUNT
, account
);
93 upload_intent
.putExtra(InstantUploadService
.KEY_FILE_PATH
, file_path
);
94 upload_intent
.putExtra(InstantUploadService
.KEY_DISPLAY_NAME
, file_name
);
95 upload_intent
.putExtra(InstantUploadService
.KEY_FILE_SIZE
, file_size
);
96 upload_intent
.putExtra(InstantUploadService
.KEY_MIME_TYPE
, mime_type
);
98 context
.startService(upload_intent
);
101 private void handleConnectivityAction(Context context
, Intent intent
) {
102 if (!intent
.hasExtra(ConnectivityManager
.EXTRA_NO_CONNECTIVITY
) ||
104 DbHandler db
= new DbHandler(context
);
105 Cursor c
= db
.getAwaitingFiles();
106 if (c
.moveToFirst()) {
108 String account_name
= c
.getString(c
.getColumnIndex("account"));
109 String file_path
= c
.getString(c
.getColumnIndex("path"));
110 File f
= new File(file_path
);
112 Intent upload_intent
= new Intent(context
, InstantUploadService
.class);
113 Account account
= new Account(account_name
, AccountAuthenticator
.ACCOUNT_TYPE
);
115 String mimeType
= null
;
117 mimeType
= MimeTypeMap
.getSingleton()
118 .getMimeTypeFromExtension(
119 f
.getName().substring(f
.getName().lastIndexOf('.') + 1));
121 } catch (IndexOutOfBoundsException e
) {
122 Log
.e(TAG
, "Trying to find out MIME type of a file without extension: " + f
.getName());
124 if (mimeType
== null
)
125 mimeType
= "application/octet-stream";
127 upload_intent
.putExtra(InstantUploadService
.KEY_ACCOUNT
, account
);
128 upload_intent
.putExtra(InstantUploadService
.KEY_FILE_PATH
, file_path
);
129 upload_intent
.putExtra(InstantUploadService
.KEY_DISPLAY_NAME
, f
.getName());
130 upload_intent
.putExtra(InstantUploadService
.KEY_FILE_SIZE
, f
.length());
131 upload_intent
.putExtra(InstantUploadService
.KEY_MIME_TYPE
, mimeType
);
133 context
.startService(upload_intent
);
135 Log
.w(TAG
, "Instant upload file " + f
.getName() + " dont exist anymore");
137 } while(c
.moveToNext());
146 private boolean isOnline(Context context
) {
147 ConnectivityManager cm
= (ConnectivityManager
) context
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
148 return cm
.getActiveNetworkInfo() != null
&& cm
.getActiveNetworkInfo().isConnected();