Merge remote-tracking branch 'origin/sync_review_2'
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / InstantUploadBroadcastReceiver.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.content.IntentFilter;
33 import android.database.Cursor;
34 import android.net.ConnectivityManager;
35 import android.net.NetworkInfo.State;
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 InstantUploadBroadcastReceiver extends BroadcastReceiver {
42
43 public static String INSTANT_UPLOAD_DIR = "/InstantUpload/";
44 private static String TAG = "PhotoTakenBroadcastReceiver";
45 private static final String[] CONTENT_PROJECTION = { Media.DATA,
46 Media.DISPLAY_NAME,
47 Media.MIME_TYPE,
48 Media.SIZE };
49 private static String NEW_PHOTO_ACTION = "com.android.camera.NEW_PICTURE";
50
51 @Override
52 public void onReceive(Context context, Intent intent) {
53 Log.d(TAG, "Received: " + intent.getAction());
54 if (intent.getAction().equals(android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
55 handleConnectivityAction(context, intent);
56 } else if (intent.getAction().equals(NEW_PHOTO_ACTION)) {
57 handleNewPhotoAction(context, intent);
58 } else if (intent.getAction().equals(FileUploader.UPLOAD_FINISH_MESSAGE)) {
59 handleUploadFinished(context, intent);
60 } else {
61 Log.e(TAG, "Incorrect intent sent: " + intent.getAction());
62 }
63 }
64
65 private void handleUploadFinished(Context context, Intent intent) {
66 // remove successfull uploading, ignore rest for reupload on reconnect
67 if (intent.getBooleanExtra(FileUploader.EXTRA_UPLOAD_RESULT, false)) {
68 DbHandler db = new DbHandler(context);
69 String localPath = intent.getStringExtra(FileUploader.EXTRA_OLD_FILE_PATH);
70 if (!db.removeIUPendingFile(localPath,
71 intent.getStringExtra(FileUploader.ACCOUNT_NAME))) {
72 Log.w(TAG, "Tried to remove non existing instant upload file " + localPath);
73 }
74 db.close();
75 }
76 }
77
78 private void handleNewPhotoAction(Context context, Intent intent) {
79 if (!instantUploadEnabled(context)) {
80 Log.d(TAG, "Instant upload disabled, abording uploading");
81 return;
82 }
83
84 Account account = AccountUtils.getCurrentOwnCloudAccount(context);
85 if (account == null) {
86 Log.w(TAG, "No owncloud account found for instant upload, aborting");
87 return;
88 }
89
90 Cursor c = context.getContentResolver().query(intent.getData(),
91 CONTENT_PROJECTION,
92 null, null, null);
93
94 if (!c.moveToFirst()) {
95 Log.e(TAG, "Couldn't resolve given uri: " + intent.getDataString());
96 return;
97 }
98
99 String file_path = c.getString(c.getColumnIndex(Media.DATA));
100 String file_name = c.getString(c.getColumnIndex(Media.DISPLAY_NAME));
101 String mime_type = c.getString(c.getColumnIndex(Media.MIME_TYPE));
102
103 c.close();
104 Log.e(TAG, file_path+"");
105
106 if (!isOnline(context) ||
107 (instantUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))) {
108 DbHandler db = new DbHandler(context);
109 db.putFileForLater(file_path, account.name);
110 db.close();
111 return;
112 }
113
114 // register for upload finishe message
115 // there is a litte problem with android API, we can register for particular
116 // intent in registerReceiver but we cannot unregister from precise intent
117 // we can unregister from entire listenings but thats suck a bit.
118 // On the other hand this might be only for dynamicly registered
119 // broadcast receivers, needs investigation.
120 IntentFilter filter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);
121 context.getApplicationContext().registerReceiver(this, filter);
122
123 Intent i = new Intent(context, FileUploader.class);
124 i.putExtra(FileUploader.KEY_ACCOUNT, account);
125 i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
126 i.putExtra(FileUploader.KEY_REMOTE_FILE, INSTANT_UPLOAD_DIR + file_name);
127 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
128 i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
129 i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
130 context.startService(i);
131
132 }
133
134 private void handleConnectivityAction(Context context, Intent intent) {
135 if (!instantUploadEnabled(context)) {
136 Log.d(TAG, "Instant upload disabled, abording uploading");
137 return;
138 }
139
140 if (!intent.hasExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY) &&
141 isOnline(context) &&
142 (!instantUploadViaWiFiOnly(context) ||
143 (instantUploadViaWiFiOnly(context) == isConnectedViaWiFi(context) == true)) ) {
144 DbHandler db = new DbHandler(context);
145 Cursor c = db.getAwaitingFiles();
146 if (c.moveToFirst()) {
147 IntentFilter filter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);
148 context.getApplicationContext().registerReceiver(this, filter);
149 do {
150 String account_name = c.getString(c.getColumnIndex("account"));
151 String file_path = c.getString(c.getColumnIndex("path"));
152 File f = new File(file_path);
153 if (f.exists()) {
154 Account account = new Account(account_name, AccountAuthenticator.ACCOUNT_TYPE);
155
156 String mimeType = null;
157 try {
158 mimeType = MimeTypeMap.getSingleton()
159 .getMimeTypeFromExtension(
160 f.getName().substring(f.getName().lastIndexOf('.') + 1));
161
162 } catch (Throwable e) {
163 Log.e(TAG, "Trying to find out MIME type of a file without extension: " + f.getName());
164 }
165 if (mimeType == null)
166 mimeType = "application/octet-stream";
167
168 Intent i = new Intent(context, FileUploader.class);
169 i.putExtra(FileUploader.KEY_ACCOUNT, account);
170 i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
171 i.putExtra(FileUploader.KEY_REMOTE_FILE, INSTANT_UPLOAD_DIR + f.getName());
172 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
173 i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
174 context.startService(i);
175
176 } else {
177 Log.w(TAG, "Instant upload file " + f.getAbsolutePath() + " dont exist anymore");
178 }
179 } while(c.moveToNext());
180 }
181 c.close();
182 db.close();
183 }
184
185 }
186
187 private boolean isOnline(Context context) {
188 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
189 return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
190 }
191
192 private boolean isConnectedViaWiFi(Context context) {
193 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
194 return cm != null && cm.getActiveNetworkInfo() != null &&
195 cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI &&
196 cm.getActiveNetworkInfo().getState() == State.CONNECTED;
197 }
198
199 private boolean instantUploadEnabled(Context context) {
200 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_uploading", false);
201 }
202
203 private boolean instantUploadViaWiFiOnly(Context context) {
204 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_upload_on_wifi", false);
205 }
206 }