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