SSL connections update: notice about untrusted certificates and allow the user save...
[pub/Android/ownCloud.git] / src / com / owncloud / android / 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 com.owncloud.android.files;
20
21 import java.io.File;
22
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;
27
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;
38
39 public class PhotoTakenBroadcastReceiver extends BroadcastReceiver {
40
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";
45
46 @Override
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");
50 return;
51 }
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);
56 } else {
57 Log.e(TAG, "Incorrect intent sent: " + intent.getAction());
58 }
59 }
60
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");
65 return;
66 }
67
68 Cursor c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
69
70 if (!c.moveToFirst()) {
71 Log.e(TAG, "Couldn't resolve given uri!");
72 return;
73 }
74
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));
79
80 c.close();
81
82 if (!isOnline(context)) {
83 DbHandler db = new DbHandler(context);
84 db.putFileForLater(file_path, account.name);
85 db.close();
86 return;
87 }
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 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);
108
109 }
110
111 private void handleConnectivityAction(Context context, Intent intent) {
112 if (!intent.hasExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY) ||
113 isOnline(context)) {
114 DbHandler db = new DbHandler(context);
115 Cursor c = db.getAwaitingFiles();
116 if (c.moveToFirst()) {
117 do {
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);
121 if (f.exists()) {
122 //Intent upload_intent = new Intent(context, InstantUploadService.class);
123 Account account = new Account(account_name, AccountAuthenticator.ACCOUNT_TYPE);
124
125 String mimeType = null;
126 try {
127 mimeType = MimeTypeMap.getSingleton()
128 .getMimeTypeFromExtension(
129 f.getName().substring(f.getName().lastIndexOf('.') + 1));
130
131 } catch (IndexOutOfBoundsException e) {
132 Log.e(TAG, "Trying to find out MIME type of a file without extension: " + f.getName());
133 }
134 if (mimeType == null)
135 mimeType = "application/octet-stream";
136
137 /*
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);
143
144 context.startService(upload_intent);
145 */
146
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);
154
155 } else {
156 Log.w(TAG, "Instant upload file " + f.getName() + " dont exist anymore");
157 }
158 } while(c.moveToNext());
159 c.close();
160 }
161 db.clearFiles();
162 db.close();
163 }
164
165 }
166
167 private boolean isOnline(Context context) {
168 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
169 return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
170 }
171
172 }