593ccd64c130b92d80b8e7001cc6f9dc1586d136
[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 version 2,
7 * as published by the Free Software Foundation.
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.authentication.AccountAuthenticator;
24 import com.owncloud.android.authentication.AccountUtils;
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.webkit.MimeTypeMap;
39
40 import com.owncloud.android.Log_OC;
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 //Unofficial action, works for most devices but not HTC. See: https://github.com/owncloud/android/issues/6
48 private static String NEW_PHOTO_ACTION_INOFFICIAL = "com.android.camera.NEW_PICTURE";
49 //Officially supported action since SDK 14: http://developer.android.com/reference/android/hardware/Camera.html#ACTION_NEW_PICTURE
50 private static String NEW_PHOTO_ACTION = "android.hardware.action.NEW_PICTURE";
51
52 @Override
53 public void onReceive(Context context, Intent intent) {
54 Log_OC.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_INOFFICIAL)) {
58 handleNewPhotoAction(context, intent);
59 } else if (intent.getAction().equals(NEW_PHOTO_ACTION)) {
60 handleNewPhotoAction(context, intent);
61 } else if (intent.getAction().equals(FileUploader.UPLOAD_FINISH_MESSAGE)) {
62 handleUploadFinished(context, intent);
63 } else {
64 Log_OC.e(TAG, "Incorrect intent sent: " + intent.getAction());
65 }
66 }
67
68 private void handleUploadFinished(Context context, Intent intent) {
69 // remove successfull uploading, ignore rest for reupload on reconnect
70 if (intent.getBooleanExtra(FileUploader.EXTRA_UPLOAD_RESULT, false)) {
71 DbHandler db = new DbHandler(context);
72 String localPath = intent.getStringExtra(FileUploader.EXTRA_OLD_FILE_PATH);
73 if (!db.removeIUPendingFile(localPath)) {
74 Log_OC.w(TAG, "Tried to remove non existing instant upload file " + localPath);
75 }
76 db.close();
77 }
78 }
79
80 private void handleNewPhotoAction(Context context, Intent intent) {
81 if (!instantUploadEnabled(context)) {
82 Log_OC.d(TAG, "Instant upload disabled, aborting uploading");
83 return;
84 }
85
86 Account account = AccountUtils.getCurrentOwnCloudAccount(context);
87 if (account == null) {
88 Log_OC.w(TAG, "No owncloud account found for instant upload, aborting");
89 return;
90 }
91
92 Cursor c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
93
94 if (!c.moveToFirst()) {
95 Log_OC.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_OC.e(TAG, file_path + "");
105
106 // same always temporally the picture to upload
107 DbHandler db = new DbHandler(context);
108 db.putFileForLater(file_path, account.name, null);
109 db.close();
110
111 if (!isOnline(context) || (instantUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))) {
112 return;
113 }
114
115 // register for upload finishe message
116 // there is a litte problem with android API, we can register for
117 // particular
118 // intent in registerReceiver but we cannot unregister from precise
119 // intent
120 // we can unregister from entire listenings but thats suck a bit.
121 // On the other hand this might be only for dynamicly registered
122 // broadcast receivers, needs investigation.
123 IntentFilter filter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);
124 context.getApplicationContext().registerReceiver(this, filter);
125
126 Intent i = new Intent(context, FileUploader.class);
127 i.putExtra(FileUploader.KEY_ACCOUNT, account);
128 i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
129 i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, file_name));
130 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
131 i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
132 i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
133 context.startService(i);
134
135 }
136
137 private void handleConnectivityAction(Context context, Intent intent) {
138 if (!instantUploadEnabled(context)) {
139 Log_OC.d(TAG, "Instant upload disabled, abording uploading");
140 return;
141 }
142
143 if (!intent.hasExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY)
144 && isOnline(context)
145 && (!instantUploadViaWiFiOnly(context) || (instantUploadViaWiFiOnly(context) == isConnectedViaWiFi(context) == true))) {
146 DbHandler db = new DbHandler(context);
147 Cursor c = db.getAwaitingFiles();
148 if (c.moveToFirst()) {
149 IntentFilter filter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);
150 context.getApplicationContext().registerReceiver(this, filter);
151 do {
152 String account_name = c.getString(c.getColumnIndex("account"));
153 String file_path = c.getString(c.getColumnIndex("path"));
154 File f = new File(file_path);
155 if (f.exists()) {
156 Account account = new Account(account_name, AccountAuthenticator.ACCOUNT_TYPE);
157
158 String mimeType = null;
159 try {
160 mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
161 f.getName().substring(f.getName().lastIndexOf('.') + 1));
162
163 } catch (Throwable e) {
164 Log_OC.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, FileStorageUtils.getInstantUploadFilePath(context, 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_OC.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 public static boolean isOnline(Context context) {
189 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
190 return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
191 }
192
193 public static 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 public static boolean instantUploadEnabled(Context context) {
201 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_uploading", false);
202 }
203
204 public static boolean instantUploadViaWiFiOnly(Context context) {
205 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_upload_on_wifi", false);
206 }
207 }