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