Force spaces in eclipse via project specifc settings
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / files / services / FileUploader.java
1 package eu.alefzero.owncloud.files.services;
2
3 import java.net.URLEncoder;
4
5 import eu.alefzero.owncloud.AccountUtils;
6 import eu.alefzero.owncloud.R;
7 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
8 import eu.alefzero.owncloud.utils.OwnCloudVersion;
9 import eu.alefzero.webdav.WebdavClient;
10 import android.accounts.Account;
11 import android.accounts.AccountManager;
12 import android.app.Notification;
13 import android.app.NotificationManager;
14 import android.app.Service;
15 import android.content.Intent;
16 import android.net.Uri;
17 import android.os.Handler;
18 import android.os.HandlerThread;
19 import android.os.IBinder;
20 import android.os.Looper;
21 import android.os.Message;
22 import android.os.Process;
23 import android.util.Log;
24 import android.webkit.MimeTypeMap;
25 import android.widget.RemoteViews;
26 import android.widget.Toast;
27
28 public class FileUploader extends Service {
29
30 public static final String KEY_LOCAL_FILE = "LOCAL_FILE";
31 public static final String KEY_REMOTE_FILE = "REMOTE_FILE";
32 public static final String KEY_ACCOUNT = "ACCOUNT";
33 public static final String KEY_UPLOAD_TYPE = "UPLOAD_TYPE";
34
35 public static final int UPLOAD_SINGLE_FILE = 0;
36 public static final int UPLOAD_MULTIPLE_FILES = 1;
37
38 private static final String TAG = "FileUploader";
39 private NotificationManager mNotificationManager;
40 private Looper mServiceLooper;
41 private ServiceHandler mServiceHandler;
42 private AccountManager mAccountManager;
43 private Account mAccount;
44 private String[] mLocalPaths, mRemotePaths;
45 private boolean mResult;
46 private int mUploadType;
47
48 @Override
49 public IBinder onBind(Intent arg0) {
50 return null;
51 }
52
53 private final class ServiceHandler extends Handler {
54 public ServiceHandler(Looper looper) {
55 super(looper);
56 }
57
58 @Override
59 public void handleMessage(Message msg) {
60 uploadFile();
61 stopSelf(msg.arg1);
62 }
63 }
64
65 @Override
66 public void onCreate() {
67 super.onCreate();
68 mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
69 HandlerThread thread = new HandlerThread("FileUploaderThread",
70 Process.THREAD_PRIORITY_BACKGROUND);
71 thread.start();
72 mServiceLooper = thread.getLooper();
73 mServiceHandler = new ServiceHandler(mServiceLooper);
74 mAccountManager = AccountManager.get(this);
75 }
76
77 @Override
78 public int onStartCommand(Intent intent, int flags, int startId) {
79 if (!intent.hasExtra(KEY_ACCOUNT) && !intent.hasExtra(KEY_UPLOAD_TYPE)) {
80 Log.e(TAG, "Not enought data in intent provided");
81 return Service.START_NOT_STICKY;
82 }
83 mAccount = intent.getParcelableExtra(KEY_ACCOUNT);
84 mUploadType = intent.getIntExtra(KEY_UPLOAD_TYPE, -1);
85 if (mUploadType == -1) {
86 Log.e(TAG, "Incorrect upload type provided");
87 return Service.START_NOT_STICKY;
88 }
89 if (mUploadType == UPLOAD_SINGLE_FILE) {
90 mLocalPaths = new String[] { intent.getStringExtra(KEY_LOCAL_FILE) };
91 mRemotePaths = new String[] { intent
92 .getStringExtra(KEY_REMOTE_FILE) };
93 } else { // mUploadType == UPLOAD_MULTIPLE_FILES
94 mLocalPaths = intent.getStringArrayExtra(KEY_LOCAL_FILE);
95 mRemotePaths = intent.getStringArrayExtra(KEY_REMOTE_FILE);
96 }
97
98 for (int i = 0; i < mRemotePaths.length; ++i)
99 mRemotePaths[i] = mRemotePaths[i].replace(' ', '+');
100
101 if (mLocalPaths.length != mRemotePaths.length) {
102 Log.e(TAG, "Remote paths and local paths are not equal!");
103 return Service.START_NOT_STICKY;
104 }
105
106 Message msg = mServiceHandler.obtainMessage();
107 msg.arg1 = startId;
108 mServiceHandler.sendMessage(msg);
109
110 return Service.START_NOT_STICKY;
111 }
112
113 public void run() {
114 if (mResult) {
115 Toast.makeText(this, "Upload successfull", Toast.LENGTH_SHORT)
116 .show();
117 } else {
118 Toast.makeText(this, "No i kupa", Toast.LENGTH_SHORT).show();
119 }
120 }
121
122 public void uploadFile() {
123 String baseUrl = mAccountManager.getUserData(mAccount,
124 AccountAuthenticator.KEY_OC_BASE_URL), ocVerStr = mAccountManager
125 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION);
126 OwnCloudVersion ocVer = new OwnCloudVersion(ocVerStr);
127 String webdav_path = AccountUtils.getWebdavPath(ocVer);
128 Uri ocUri = Uri.parse(baseUrl + webdav_path);
129 String username = mAccount.name.substring(0,
130 mAccount.name.lastIndexOf('@'));
131 String password = mAccountManager.getPassword(mAccount);
132 Notification notification = new Notification(
133 eu.alefzero.owncloud.R.drawable.icon, "Uploading...",
134 System.currentTimeMillis());
135 notification.flags |= Notification.FLAG_ONGOING_EVENT;
136 notification.contentView = new RemoteViews(getApplicationContext()
137 .getPackageName(), R.layout.progressbar_layout);
138 notification.contentView.setProgressBar(R.id.status_progress,
139 mLocalPaths.length - 1, 0, false);
140 notification.contentView.setImageViewResource(R.id.status_icon,
141 R.drawable.icon);
142
143 mNotificationManager.notify(42, notification);
144
145 WebdavClient wc = new WebdavClient(ocUri);
146 wc.setCredentials(username, password);
147
148 for (int i = 0; i < mLocalPaths.length; ++i) {
149 String mimeType = MimeTypeMap.getSingleton()
150 .getMimeTypeFromExtension(
151 mLocalPaths[i].substring(mLocalPaths[i]
152 .lastIndexOf('.') + 1));
153 mResult = false;
154 if (wc.putFile(mLocalPaths[i], mRemotePaths[i], mimeType)) {
155 mResult |= true;
156 }
157 notification.contentView.setProgressBar(R.id.status_progress,
158 mLocalPaths.length - 1, i + 1, false);
159
160 mNotificationManager.notify(42, notification);
161 }
162 // notification.contentView.setProgressBar(R.id.status_progress,
163 // mLocalPaths.length-1, mLocalPaths.length-1, false);
164 mNotificationManager.cancel(42);
165 run();
166 }
167 }