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