Quick patch to fix operations not ready for HTTPS
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / files / PhotoTakenBroadcastReceiver.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package eu.alefzero.owncloud.files;
20
21 import java.io.File;
22
23 import eu.alefzero.owncloud.AccountUtils;
24 import eu.alefzero.owncloud.R;
25 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
26 import eu.alefzero.owncloud.db.DbHandler;
27 import eu.alefzero.owncloud.files.services.InstantUploadService;
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.Preference;
35 import android.preference.PreferenceManager;
36 import android.provider.MediaStore.Images.Media;
37 import android.util.Log;
38 import android.webkit.MimeTypeMap;
39
40 public class PhotoTakenBroadcastReceiver extends BroadcastReceiver {
41
42 private static String TAG = "PhotoTakenBroadcastReceiver";
43 private static final String[] CONTENT_PROJECTION = { Media.DATA, Media.DISPLAY_NAME, Media.MIME_TYPE, Media.SIZE };
44
45 private static String NEW_PHOTO_ACTION = "com.android.camera.NEW_PICTURE";
46
47 @Override
48 public void onReceive(Context context, Intent intent) {
49 if (!PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_uploading", false)) {
50 Log.d(TAG, "Instant upload disabled, abording uploading");
51 return;
52 }
53 if (intent.getAction().equals(android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
54 handleConnectivityAction(context, intent);
55 } else if (intent.getAction().equals(NEW_PHOTO_ACTION)) {
56 handleNewPhontoAction(context, intent);
57 } else {
58 Log.e(TAG, "Incorrect intent sent: " + intent.getAction());
59 }
60 }
61
62 private void handleNewPhontoAction(Context context, Intent intent) {
63 Account account = AccountUtils.getCurrentOwnCloudAccount(context);
64 if (account == null) {
65 Log.w(TAG, "No owncloud account found for instant upload, abording");
66 return;
67 }
68
69 Cursor c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
70
71 if (!c.moveToFirst()) {
72 Log.e(TAG, "Couldn't resolve given uri!");
73 return;
74 }
75
76 String file_path = c.getString(c.getColumnIndex(Media.DATA));
77 String file_name = c.getString(c.getColumnIndex(Media.DISPLAY_NAME));
78 String mime_type = c.getString(c.getColumnIndex(Media.MIME_TYPE));
79 long file_size = c.getLong(c.getColumnIndex(Media.SIZE));
80
81 c.close();
82
83 if (!isOnline(context)) {
84 DbHandler db = new DbHandler(context);
85 db.putFileForLater(file_path, account.name);
86 db.close();
87 return;
88 }
89
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);
96
97 context.startService(upload_intent);
98 }
99
100 private void handleConnectivityAction(Context context, Intent intent) {
101 if (!intent.hasExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY) ||
102 isOnline(context)) {
103 DbHandler db = new DbHandler(context);
104 Cursor c = db.getAwaitingFiles();
105 if (c.moveToFirst()) {
106 do {
107 String account_name = c.getString(c.getColumnIndex("account"));
108 String file_path = c.getString(c.getColumnIndex("path"));
109 File f = new File(file_path);
110 if (f.exists()) {
111 Intent upload_intent = new Intent(context, InstantUploadService.class);
112 Account account = new Account(account_name, AccountAuthenticator.ACCOUNT_TYPE);
113
114 String mimeType = null;
115 try {
116 mimeType = MimeTypeMap.getSingleton()
117 .getMimeTypeFromExtension(
118 f.getName().substring(f.getName().lastIndexOf('.') + 1));
119
120 } catch (IndexOutOfBoundsException e) {
121 Log.e(TAG, "Trying to find out MIME type of a file without extension: " + f.getName());
122 }
123 if (mimeType == null)
124 mimeType = "application/octet-stream";
125
126 upload_intent.putExtra(InstantUploadService.KEY_ACCOUNT, account);
127 upload_intent.putExtra(InstantUploadService.KEY_FILE_PATH, file_path);
128 upload_intent.putExtra(InstantUploadService.KEY_DISPLAY_NAME, f.getName());
129 upload_intent.putExtra(InstantUploadService.KEY_FILE_SIZE, f.length());
130 upload_intent.putExtra(InstantUploadService.KEY_MIME_TYPE, mimeType);
131
132 context.startService(upload_intent);
133 } else {
134 Log.w(TAG, "Instant upload file " + f.getName() + " dont exist anymore");
135 }
136 } while(c.moveToNext());
137 c.close();
138 }
139 db.clearFiles();
140 db.close();
141 }
142
143 }
144
145 private boolean isOnline(Context context) {
146 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
147 return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
148 }
149
150 }