moving from eu.alefzero.eu to com.owncloud.android
[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.InstantUploadService;
27
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;
40
41 public class PhotoTakenBroadcastReceiver extends BroadcastReceiver {
42
43 private static String TAG = "PhotoTakenBroadcastReceiver";
44 private static final String[] CONTENT_PROJECTION = { Media.DATA, Media.DISPLAY_NAME, Media.MIME_TYPE, Media.SIZE };
45
46 private static String NEW_PHOTO_ACTION = "com.android.camera.NEW_PICTURE";
47
48 @Override
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");
52 return;
53 }
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);
58 } else {
59 Log.e(TAG, "Incorrect intent sent: " + intent.getAction());
60 }
61 }
62
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");
67 return;
68 }
69
70 Cursor c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
71
72 if (!c.moveToFirst()) {
73 Log.e(TAG, "Couldn't resolve given uri!");
74 return;
75 }
76
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));
81
82 c.close();
83
84 if (!isOnline(context)) {
85 DbHandler db = new DbHandler(context);
86 db.putFileForLater(file_path, account.name);
87 db.close();
88 return;
89 }
90
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);
97
98 context.startService(upload_intent);
99 }
100
101 private void handleConnectivityAction(Context context, Intent intent) {
102 if (!intent.hasExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY) ||
103 isOnline(context)) {
104 DbHandler db = new DbHandler(context);
105 Cursor c = db.getAwaitingFiles();
106 if (c.moveToFirst()) {
107 do {
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);
111 if (f.exists()) {
112 Intent upload_intent = new Intent(context, InstantUploadService.class);
113 Account account = new Account(account_name, AccountAuthenticator.ACCOUNT_TYPE);
114
115 String mimeType = null;
116 try {
117 mimeType = MimeTypeMap.getSingleton()
118 .getMimeTypeFromExtension(
119 f.getName().substring(f.getName().lastIndexOf('.') + 1));
120
121 } catch (IndexOutOfBoundsException e) {
122 Log.e(TAG, "Trying to find out MIME type of a file without extension: " + f.getName());
123 }
124 if (mimeType == null)
125 mimeType = "application/octet-stream";
126
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);
132
133 context.startService(upload_intent);
134 } else {
135 Log.w(TAG, "Instant upload file " + f.getName() + " dont exist anymore");
136 }
137 } while(c.moveToNext());
138 c.close();
139 }
140 db.clearFiles();
141 db.close();
142 }
143
144 }
145
146 private boolean isOnline(Context context) {
147 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
148 return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
149 }
150
151 }