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