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