new icons, new uploader
[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 @Override
58 public void handleMessage(Message msg) {
59 uploadFile();
60 stopSelf(msg.arg1);
61 }
62 }
63
64 @Override
65 public void onCreate() {
66 super.onCreate();
67 mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
68 HandlerThread thread = new HandlerThread("FileUploaderThread", Process.THREAD_PRIORITY_BACKGROUND);
69 thread.start();
70 mServiceLooper = thread.getLooper();
71 mServiceHandler = new ServiceHandler(mServiceLooper);
72 mAccountManager = AccountManager.get(this);
73 }
74
75 @Override
76 public int onStartCommand(Intent intent, int flags, int startId) {
77 if (!intent.hasExtra(KEY_ACCOUNT) && !intent.hasExtra(KEY_UPLOAD_TYPE)) {
78 Log.e(TAG, "Not enought data in intent provided");
79 return Service.START_NOT_STICKY;
80 }
81 mAccount = intent.getParcelableExtra(KEY_ACCOUNT);
82 mUploadType = intent.getIntExtra(KEY_UPLOAD_TYPE, -1);
83 if (mUploadType == -1) {
84 Log.e(TAG, "Incorrect upload type provided");
85 return Service.START_NOT_STICKY;
86 }
87 if (mUploadType == UPLOAD_SINGLE_FILE) {
88 mLocalPaths = new String[] { intent.getStringExtra(KEY_LOCAL_FILE) };
89 mRemotePaths = new String[] { intent.getStringExtra(KEY_REMOTE_FILE) };
90 } else { // mUploadType == UPLOAD_MULTIPLE_FILES
91 mLocalPaths = intent.getStringArrayExtra(KEY_LOCAL_FILE);
92 mRemotePaths = intent.getStringArrayExtra(KEY_REMOTE_FILE);
93 }
94
95 for (int i = 0; i < mRemotePaths.length; ++i)
96 mRemotePaths[i] = mRemotePaths[i].replace(' ', '+');
97
98 if (mLocalPaths.length != mRemotePaths.length) {
99 Log.e(TAG, "Remote paths and local paths are not equal!");
100 return Service.START_NOT_STICKY;
101 }
102
103 Message msg = mServiceHandler.obtainMessage();
104 msg.arg1 = startId;
105 mServiceHandler.sendMessage(msg);
106
107 return Service.START_NOT_STICKY;
108 }
109
110 public void run() {
111 if (mResult) {
112 Toast.makeText(this, "Upload successfull", Toast.LENGTH_SHORT).show();
113 } else {
114 Toast.makeText(this, "No i kupa", Toast.LENGTH_SHORT).show();
115 }
116 }
117
118 public void uploadFile() {
119 String baseUrl = mAccountManager.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL),
120 ocVerStr = mAccountManager.getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION);
121 OwnCloudVersion ocVer = new OwnCloudVersion(ocVerStr);
122 String webdav_path = AccountUtils.getWebdavPath(ocVer);
123 Uri ocUri = Uri.parse(baseUrl + webdav_path);
124 String username = mAccount.name.substring(0, mAccount.name.lastIndexOf('@'));
125 String password = mAccountManager.getPassword(mAccount);
126 Notification notification = new Notification(eu.alefzero.owncloud.R.drawable.icon,
127 "Uploading...", System.currentTimeMillis());
128 notification.flags |= Notification.FLAG_ONGOING_EVENT;
129 notification.contentView = new RemoteViews(getApplicationContext().getPackageName(),
130 R.layout.progressbar_layout);
131 notification.contentView.setProgressBar(R.id.status_progress, mLocalPaths.length-1, 0, false);
132 notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.icon);
133
134 mNotificationManager.notify(42, notification);
135
136 WebdavClient wc = new WebdavClient(ocUri);
137 wc.setCredentials(username, password);
138
139 for (int i = 0; i < mLocalPaths.length; ++i) {
140 String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(mLocalPaths[i].substring(mLocalPaths[i].lastIndexOf('.')+1));
141 mResult = false;
142 if (wc.putFile(mLocalPaths[i], mRemotePaths[i], mimeType)) {
143 mResult |= true;
144 }
145 notification.contentView.setProgressBar(R.id.status_progress, mLocalPaths.length-1, i+1, false);
146
147 mNotificationManager.notify(42, notification);
148 }
149 //notification.contentView.setProgressBar(R.id.status_progress, mLocalPaths.length-1, mLocalPaths.length-1, false);
150 mNotificationManager.cancel(42);
151 run();
152 }
153 }