758413f552a03f37d5eb9eb9c3ff4fcef5c6f97a
[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 eu.alefzero.owncloud.AccountUtils;
22 import eu.alefzero.owncloud.R;
23 import eu.alefzero.owncloud.files.services.InstantUploadService;
24 import android.accounts.Account;
25 import android.content.BroadcastReceiver;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.database.Cursor;
29 import android.preference.Preference;
30 import android.preference.PreferenceManager;
31 import android.provider.MediaStore.Images.Media;
32 import android.util.Log;
33
34 public class PhotoTakenBroadcastReceiver extends BroadcastReceiver {
35
36 private static String TAG = "PhotoTakenBroadcastReceiver";
37 private static final String[] CONTENT_PROJECTION = { Media.DATA, Media.DISPLAY_NAME, Media.MIME_TYPE, Media.SIZE };
38
39 private static String NEW_PHOTO_ACTION = "com.android.camera.NEW_PICTURE";
40
41 @Override
42 public void onReceive(Context context, Intent intent) {
43 if (!PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_uploading", false)) {
44 Log.d(TAG, "Instant upload disabled, abording uploading");
45 return;
46 }
47 if (!intent.getAction().equals(NEW_PHOTO_ACTION)) {
48 Log.e(TAG, "Incorrect intent sent: " + intent.getAction());
49 return;
50 }
51 Account account = AccountUtils.getCurrentOwnCloudAccount(context);
52 if (account == null) {
53 Log.w(TAG, "No owncloud account found for instant upload, abording");
54 return;
55 }
56
57 Cursor c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
58
59 if (!c.moveToFirst()) {
60 Log.e(TAG, "Couldn't resolve given uri!");
61 return;
62 }
63
64 String file_path = c.getString(c.getColumnIndex(Media.DATA));
65 String file_name = c.getString(c.getColumnIndex(Media.DISPLAY_NAME));
66 String mime_type = c.getString(c.getColumnIndex(Media.MIME_TYPE));
67 long file_size = c.getLong(c.getColumnIndex(Media.SIZE));
68
69 c.close();
70
71 Intent upload_intent = new Intent(context, InstantUploadService.class);
72 upload_intent.putExtra(InstantUploadService.KEY_ACCOUNT, account);
73 upload_intent.putExtra(InstantUploadService.KEY_FILE_PATH, file_path);
74 upload_intent.putExtra(InstantUploadService.KEY_DISPLAY_NAME, file_name);
75 upload_intent.putExtra(InstantUploadService.KEY_FILE_SIZE, file_size);
76 upload_intent.putExtra(InstantUploadService.KEY_MIME_TYPE, mime_type);
77
78 context.startService(upload_intent);
79 }
80
81 }