7dd3cb7b963b526ef1fcc08e2a45d17e6bd21c1a
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / InstantUploadBroadcastReceiver.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 package com.owncloud.android.files;
21
22 import java.io.File;
23
24 import com.owncloud.android.AccountUtils;
25 import com.owncloud.android.authenticator.AccountAuthenticator;
26 import com.owncloud.android.db.DbHandler;
27 import com.owncloud.android.files.services.FileUploader;
28
29 import android.accounts.Account;
30 import android.content.BroadcastReceiver;
31 import android.content.Context;
32 import android.content.Intent;
33 import android.content.IntentFilter;
34 import android.database.Cursor;
35 import android.net.ConnectivityManager;
36 import android.net.NetworkInfo.State;
37 import android.preference.PreferenceManager;
38 import android.provider.MediaStore.Images.Media;
39 import android.util.Log;
40 import android.webkit.MimeTypeMap;
41
42 public class InstantUploadBroadcastReceiver extends BroadcastReceiver {
43
44 public static String INSTANT_UPLOAD_DIR = "/InstantUpload/";
45 private static String TAG = "PhotoTakenBroadcastReceiver";
46 private static final String[] CONTENT_PROJECTION = { Media.DATA,
47 Media.DISPLAY_NAME,
48 Media.MIME_TYPE,
49 Media.SIZE };
50 private static String NEW_PHOTO_ACTION = "com.android.camera.NEW_PICTURE";
51
52 @Override
53 public void onReceive(Context context, Intent intent) {
54 Log.d(TAG, "Received: " + intent.getAction());
55 if (intent.getAction().equals(android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
56 handleConnectivityAction(context, intent);
57 } else if (intent.getAction().equals(NEW_PHOTO_ACTION)) {
58 handleNewPhotoAction(context, intent);
59 } else if (intent.getAction().equals(FileUploader.UPLOAD_FINISH_MESSAGE)) {
60 handleUploadFinished(context, intent);
61 } else {
62 Log.e(TAG, "Incorrect intent sent: " + intent.getAction());
63 }
64 }
65
66 private void handleUploadFinished(Context context, Intent intent) {
67 // remove successfull uploading, ignore rest for reupload on reconnect
68 if (intent.getBooleanExtra(FileUploader.EXTRA_UPLOAD_RESULT, false)) {
69 DbHandler db = new DbHandler(context);
70 String localPath = intent.getStringExtra(FileUploader.EXTRA_OLD_FILE_PATH);
71 if (!db.removeIUPendingFile(localPath,
72 intent.getStringExtra(FileUploader.ACCOUNT_NAME))) {
73 Log.w(TAG, "Tried to remove non existing instant upload file " + localPath);
74 }
75 db.close();
76 }
77 }
78
79 private void handleNewPhotoAction(Context context, Intent intent) {
80 if (!instantUploadEnabled(context)) {
81 Log.d(TAG, "Instant upload disabled, abording uploading");
82 return;
83 }
84
85 Account account = AccountUtils.getCurrentOwnCloudAccount(context);
86 if (account == null) {
87 Log.w(TAG, "No owncloud account found for instant upload, aborting");
88 return;
89 }
90
91 Cursor c = context.getContentResolver().query(intent.getData(),
92 CONTENT_PROJECTION,
93 null, null, null);
94
95 if (!c.moveToFirst()) {
96 Log.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
97 return;
98 }
99
100 String file_path = c.getString(c.getColumnIndex(Media.DATA));
101 String file_name = c.getString(c.getColumnIndex(Media.DISPLAY_NAME));
102 String mime_type = c.getString(c.getColumnIndex(Media.MIME_TYPE));
103
104 c.close();
105 Log.e(TAG, file_path+"");
106
107 if (!isOnline(context) ||
108 (instantUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))) {
109 DbHandler db = new DbHandler(context);
110 db.putFileForLater(file_path, account.name);
111 db.close();
112 return;
113 }
114
115 // register for upload finishe message
116 // there is a litte problem with android API, we can register for particular
117 // intent in registerReceiver but we cannot unregister from precise intent
118 // we can unregister from entire listenings but thats suck a bit.
119 // On the other hand this might be only for dynamicly registered
120 // broadcast receivers, needs investigation.
121 IntentFilter filter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);
122 context.getApplicationContext().registerReceiver(this, filter);
123
124 Intent i = new Intent(context, FileUploader.class);
125 i.putExtra(FileUploader.KEY_ACCOUNT, account);
126 i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
127 i.putExtra(FileUploader.KEY_REMOTE_FILE, INSTANT_UPLOAD_DIR + file_name);
128 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
129 i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
130 i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
131 context.startService(i);
132
133 }
134
135 private void handleConnectivityAction(Context context, Intent intent) {
136 if (!instantUploadEnabled(context)) {
137 Log.d(TAG, "Instant upload disabled, abording uploading");
138 return;
139 }
140
141 if (!intent.hasExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY) &&
142 isOnline(context) &&
143 (!instantUploadViaWiFiOnly(context) ||
144 (instantUploadViaWiFiOnly(context) == isConnectedViaWiFi(context) == true)) ) {
145 DbHandler db = new DbHandler(context);
146 Cursor c = db.getAwaitingFiles();
147 if (c.moveToFirst()) {
148 IntentFilter filter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);
149 context.getApplicationContext().registerReceiver(this, filter);
150 do {
151 String account_name = c.getString(c.getColumnIndex("account"));
152 String file_path = c.getString(c.getColumnIndex("path"));
153 File f = new File(file_path);
154 if (f.exists()) {
155 Account account = new Account(account_name, AccountAuthenticator.ACCOUNT_TYPE);
156
157 String mimeType = null;
158 try {
159 mimeType = MimeTypeMap.getSingleton()
160 .getMimeTypeFromExtension(
161 f.getName().substring(f.getName().lastIndexOf('.') + 1));
162
163 } catch (Throwable e) {
164 Log.e(TAG, "Trying to find out MIME type of a file without extension: " + f.getName());
165 }
166 if (mimeType == null)
167 mimeType = "application/octet-stream";
168
169 Intent i = new Intent(context, FileUploader.class);
170 i.putExtra(FileUploader.KEY_ACCOUNT, account);
171 i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
172 i.putExtra(FileUploader.KEY_REMOTE_FILE, INSTANT_UPLOAD_DIR + f.getName());
173 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
174 i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
175 context.startService(i);
176
177 } else {
178 Log.w(TAG, "Instant upload file " + f.getAbsolutePath() + " dont exist anymore");
179 }
180 } while(c.moveToNext());
181 }
182 c.close();
183 db.close();
184 }
185
186 }
187
188 private boolean isOnline(Context context) {
189 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
190 return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
191 }
192
193 private boolean isConnectedViaWiFi(Context context) {
194 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
195 return cm != null && cm.getActiveNetworkInfo() != null &&
196 cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI &&
197 cm.getActiveNetworkInfo().getState() == State.CONNECTED;
198 }
199
200 private boolean instantUploadEnabled(Context context) {
201 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_uploading", false);
202 }
203
204 private boolean instantUploadViaWiFiOnly(Context context) {
205 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_upload_on_wifi", false);
206 }
207 }