add upload via wifi only option
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / PhotoTakenBroadcastReceiver.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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.AccountUtils;
24 import com.owncloud.android.authenticator.AccountAuthenticator;
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.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.util.Log;
38 import android.webkit.MimeTypeMap;
39
40 public class PhotoTakenBroadcastReceiver extends BroadcastReceiver {
41
42 public static String INSTANT_UPLOAD_DIR = "/InstantUpload/";
43 private static String TAG = "PhotoTakenBroadcastReceiver";
44 private static final String[] CONTENT_PROJECTION = { Media.DATA, Media.DISPLAY_NAME, Media.MIME_TYPE, Media.SIZE };
45 private static String NEW_PHOTO_ACTION = "com.android.camera.NEW_PICTURE";
46
47 @Override
48 public void onReceive(Context context, Intent intent) {
49 boolean iu_enabled = PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_uploading", false);
50
51 if (!iu_enabled) {
52 Log.d(TAG, "Instant upload disabled, abording uploading");
53 return;
54 }
55
56 if (intent.getAction().equals(android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
57 handleConnectivityAction(context, intent);
58 } else if (intent.getAction().equals(NEW_PHOTO_ACTION)) {
59 handleNewPhotoAction(context, intent);
60 } else {
61 Log.e(TAG, "Incorrect intent sent: " + intent.getAction());
62 }
63 }
64
65 private void handleNewPhotoAction(Context context, Intent intent) {
66 Account account = AccountUtils.getCurrentOwnCloudAccount(context);
67 if (account == null) {
68 Log.w(TAG, "No owncloud account found for instant upload, aborting");
69 return;
70 }
71
72 Cursor c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
73
74 if (!c.moveToFirst()) {
75 Log.e(TAG, "Couldn't resolve given uri!");
76 return;
77 }
78
79 boolean iu_via_wifi = PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_upload_on_wifi", false);
80 boolean is_conn_via_wifi = false;
81 if (iu_via_wifi) {
82 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
83 if (cm != null && cm.getActiveNetworkInfo() != null &&
84 cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI &&
85 cm.getActiveNetworkInfo().getState() == State.CONNECTED)
86 is_conn_via_wifi = true;
87 }
88
89
90 String file_path = c.getString(c.getColumnIndex(Media.DATA));
91 String file_name = c.getString(c.getColumnIndex(Media.DISPLAY_NAME));
92 String mime_type = c.getString(c.getColumnIndex(Media.MIME_TYPE));
93 //long file_size = c.getLong(c.getColumnIndex(Media.SIZE));
94
95 c.close();
96
97 if (!isOnline(context) || (iu_via_wifi && !is_conn_via_wifi)) {
98 DbHandler db = new DbHandler(context);
99 db.putFileForLater(file_path, account.name);
100 db.close();
101 return;
102 }
103
104 /*
105 Intent upload_intent = new Intent(context, InstantUploadService.class);
106 upload_intent.putExtra(InstantUploadService.KEY_ACCOUNT, account);
107 upload_intent.putExtra(InstantUploadService.KEY_FILE_PATH, file_path);
108 upload_intent.putExtra(InstantUploadService.KEY_DISPLAY_NAME, file_name);
109 upload_intent.putExtra(InstantUploadService.KEY_FILE_SIZE, file_size);
110 upload_intent.putExtra(InstantUploadService.KEY_MIME_TYPE, mime_type);
111
112 context.startService(upload_intent);
113 */
114
115 Intent i = new Intent(context, FileUploader.class);
116 i.putExtra(FileUploader.KEY_ACCOUNT, account);
117 i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
118 i.putExtra(FileUploader.KEY_REMOTE_FILE, INSTANT_UPLOAD_DIR + "/" + file_name);
119 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
120 i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
121 i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
122 context.startService(i);
123
124 }
125
126 private void handleConnectivityAction(Context context, Intent intent) {
127 if (!intent.hasExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY) ||
128 isOnline(context)) {
129 DbHandler db = new DbHandler(context);
130 Cursor c = db.getAwaitingFiles();
131 if (c.moveToFirst()) {
132 do {
133 String account_name = c.getString(c.getColumnIndex("account"));
134 String file_path = c.getString(c.getColumnIndex("path"));
135 File f = new File(file_path);
136 if (f.exists()) {
137 //Intent upload_intent = new Intent(context, InstantUploadService.class);
138 Account account = new Account(account_name, AccountAuthenticator.ACCOUNT_TYPE);
139
140 String mimeType = null;
141 try {
142 mimeType = MimeTypeMap.getSingleton()
143 .getMimeTypeFromExtension(
144 f.getName().substring(f.getName().lastIndexOf('.') + 1));
145
146 } catch (IndexOutOfBoundsException e) {
147 Log.e(TAG, "Trying to find out MIME type of a file without extension: " + f.getName());
148 }
149 if (mimeType == null)
150 mimeType = "application/octet-stream";
151
152 /*
153 upload_intent.putExtra(InstantUploadService.KEY_ACCOUNT, account);
154 upload_intent.putExtra(InstantUploadService.KEY_FILE_PATH, file_path);
155 upload_intent.putExtra(InstantUploadService.KEY_DISPLAY_NAME, f.getName());
156 upload_intent.putExtra(InstantUploadService.KEY_FILE_SIZE, f.length());
157 upload_intent.putExtra(InstantUploadService.KEY_MIME_TYPE, mimeType);
158
159 context.startService(upload_intent);
160 */
161
162 Intent i = new Intent(context, FileUploader.class);
163 i.putExtra(FileUploader.KEY_ACCOUNT, account);
164 i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
165 i.putExtra(FileUploader.KEY_REMOTE_FILE, INSTANT_UPLOAD_DIR + f.getName());
166 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
167 i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
168 context.startService(i);
169
170 } else {
171 Log.w(TAG, "Instant upload file " + f.getName() + " dont exist anymore");
172 }
173 } while(c.moveToNext());
174 c.close();
175 }
176 db.clearFiles();
177 db.close();
178 }
179
180 }
181
182 private boolean isOnline(Context context) {
183 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
184 return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
185 }
186
187 }