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