1 package eu
.alefzero
.owncloud
;
4 import java
.net
.FileNameMap
;
6 import java
.net
.URLConnection
;
7 import java
.util
.ArrayList
;
8 import java
.util
.Stack
;
10 import android
.accounts
.Account
;
11 import android
.accounts
.AccountManager
;
12 import android
.app
.AlertDialog
;
13 import android
.app
.Dialog
;
14 import android
.app
.ListActivity
;
15 import android
.app
.ProgressDialog
;
16 import android
.app
.AlertDialog
.Builder
;
17 import android
.content
.ContentValues
;
18 import android
.content
.Context
;
19 import android
.content
.DialogInterface
;
20 import android
.content
.Intent
;
21 import android
.content
.DialogInterface
.OnCancelListener
;
22 import android
.content
.DialogInterface
.OnClickListener
;
23 import android
.database
.Cursor
;
24 import android
.net
.Uri
;
25 import android
.os
.Bundle
;
26 import android
.os
.Handler
;
27 import android
.os
.Parcelable
;
28 import android
.provider
.MediaStore
.Images
.Media
;
29 import android
.util
.Log
;
30 import android
.view
.View
;
31 import android
.view
.Window
;
32 import android
.view
.ViewGroup
.LayoutParams
;
33 import android
.widget
.AdapterView
;
34 import android
.widget
.Button
;
35 import android
.widget
.EditText
;
36 import android
.widget
.LinearLayout
;
37 import android
.widget
.ListView
;
38 import android
.widget
.SimpleCursorAdapter
;
39 import android
.widget
.Toast
;
40 import android
.widget
.AdapterView
.OnItemClickListener
;
41 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
42 import eu
.alefzero
.owncloud
.db
.ProviderMeta
;
43 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
;
44 import eu
.alefzero
.webdav
.WebdavUtils
;
46 public class Uploader
extends ListActivity
implements OnItemClickListener
, android
.view
.View
.OnClickListener
{
47 private static final String TAG
= "ownCloudUploader";
49 private Account mAccount
;
50 private AccountManager mAccountManager
;
51 private String mUsername
, mPassword
;
52 private Cursor mCursor
;
53 private Stack
<String
> mParents
;
54 private Thread mUploadThread
;
55 private Handler mHandler
;
56 private ArrayList
<Parcelable
> mStreamsToUpload
;
58 private final static int DIALOG_NO_ACCOUNT
= 0;
59 private final static int DIALOG_WAITING
= 1;
60 private final static int DIALOG_NO_STREAM
= 2;
61 private final static int DIALOG_MULTIPLE_ACCOUNT
= 3;
62 private final static int DIALOG_GET_DIRNAME
= 4;
64 private final static int REQUEST_CODE_SETUP_ACCOUNT
= 0;
67 protected void onCreate(Bundle savedInstanceState
) {
68 super.onCreate(savedInstanceState
);
69 getWindow().requestFeature(Window
.FEATURE_NO_TITLE
);
70 mParents
= new Stack
<String
>();
71 mHandler
= new Handler();
72 if (getIntent().hasExtra(Intent
.EXTRA_STREAM
)) {
73 prepareStreamsToUpload();
74 mAccountManager
= (AccountManager
)getSystemService(Context
.ACCOUNT_SERVICE
);
75 Account
[] accounts
= mAccountManager
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
76 if (accounts
.length
== 0) {
77 Log
.i(TAG
, "No ownCloud account is available");
78 showDialog(DIALOG_NO_ACCOUNT
);
79 } else if (accounts
.length
> 1) {
80 Log
.i(TAG
, "More then one ownCloud is available");
81 showDialog(DIALOG_MULTIPLE_ACCOUNT
);
83 mAccount
= accounts
[0];
84 setContentView(R
.layout
.uploader_layout
);
85 populateDirectoryList();
88 showDialog(DIALOG_NO_STREAM
);
93 protected Dialog
onCreateDialog(final int id
) {
94 final AlertDialog
.Builder builder
= new Builder(this);
97 ProgressDialog pDialog
= new ProgressDialog(this);
98 pDialog
.setIndeterminate(false
);
99 pDialog
.setCancelable(false
);
100 pDialog
.setMessage(getResources().getString(R
.string
.uploader_info_uploading
));
102 case DIALOG_NO_ACCOUNT
:
103 builder
.setIcon(android
.R
.drawable
.ic_dialog_alert
);
104 builder
.setTitle(R
.string
.uploader_wrn_no_account_title
);
105 builder
.setMessage(R
.string
.uploader_wrn_no_account_text
);
106 builder
.setCancelable(false
);
107 builder
.setPositiveButton(R
.string
.uploader_wrn_no_account_setup_btn_text
, new OnClickListener() {
108 public void onClick(DialogInterface dialog
, int which
) {
109 if (android
.os
.Build
.VERSION
.SDK_INT
> android
.os
.Build
.VERSION_CODES
.ECLAIR_MR1
) {
110 // using string value since in API7 this constatn is not defined
111 // in API7 < this constatant is defined in Settings.ADD_ACCOUNT_SETTINGS
112 // and Settings.EXTRA_AUTHORITIES
113 Intent intent
= new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
114 intent
.putExtra("authorities", new String
[] {AccountAuthenticator
.AUTH_TOKEN_TYPE
});
115 startActivityForResult(intent
, REQUEST_CODE_SETUP_ACCOUNT
);
117 // since in API7 there is no direct call for account setup, so we need to
118 // show our own AccountSetupAcricity, get desired results and setup
119 // everything for ourself
120 Intent intent
= new Intent(getBaseContext(), AccountAuthenticator
.class);
121 startActivityForResult(intent
, REQUEST_CODE_SETUP_ACCOUNT
);
125 builder
.setNegativeButton(R
.string
.uploader_wrn_no_account_quit_btn_text
, new OnClickListener() {
126 public void onClick(DialogInterface dialog
, int which
) {
130 return builder
.create();
131 case DIALOG_GET_DIRNAME
:
132 final EditText dirName
= new EditText(getBaseContext());
133 builder
.setView(dirName
);
134 builder
.setTitle(R
.string
.uploader_info_dirname
);
136 if (mParents
.empty()) {
139 mCursor
= managedQuery(Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_FILE
, mParents
.peek()),
144 mCursor
.moveToFirst();
145 pathToUpload
= mCursor
.getString(mCursor
.getColumnIndex(ProviderTableMeta
.FILE_PATH
)) +
146 mCursor
.getString(mCursor
.getColumnIndex(ProviderTableMeta
.FILE_NAME
)).replace(" ", "%20");
148 a a
= new a(pathToUpload
, dirName
);
149 builder
.setPositiveButton(R
.string
.common_ok
, a
);
150 builder
.setNegativeButton(R
.string
.common_cancel
, new OnClickListener() {
151 public void onClick(DialogInterface dialog
, int which
) {
155 return builder
.create();
156 case DIALOG_MULTIPLE_ACCOUNT
:
157 CharSequence ac
[] = new CharSequence
[mAccountManager
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
).length
];
158 for (int i
= 0; i
< ac
.length
; ++i
) {
159 ac
[i
] = mAccountManager
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)[i
].name
;
161 builder
.setTitle(R
.string
.common_choose_account
);
162 builder
.setItems(ac
, new OnClickListener() {
163 public void onClick(DialogInterface dialog
, int which
) {
164 mAccount
= mAccountManager
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)[which
];
165 populateDirectoryList();
168 builder
.setCancelable(true
);
169 builder
.setOnCancelListener(new OnCancelListener() {
170 public void onCancel(DialogInterface dialog
) {
175 return builder
.create();
177 throw new IllegalArgumentException("Unknown dialog id: " + id
);
181 class a
implements OnClickListener
{
184 public a(String path
, EditText dirname
) {
185 mPath
= path
; mDirname
= dirname
;
187 public void onClick(DialogInterface dialog
, int which
) {
188 showDialog(DIALOG_WAITING
);
189 mUploadThread
= new Thread(new BackgroundUploader(mPath
+mDirname
.getText().toString(), mStreamsToUpload
, mHandler
, true
));
190 mUploadThread
.start();
195 public void onBackPressed() {
197 if (mParents
.size()==0) {
198 super.onBackPressed();
200 } else if (mParents
.size() == 1) {
202 mCursor
= managedQuery(ProviderTableMeta
.CONTENT_URI
,
204 ProviderTableMeta
.FILE_CONTENT_TYPE
+"=?",
209 mCursor
= managedQuery(Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, mParents
.peek()),
211 ProviderTableMeta
.FILE_CONTENT_TYPE
+"=?",
216 SimpleCursorAdapter sca
= new SimpleCursorAdapter(this, R
.layout
.uploader_list_item_layout
,
218 new String
[]{ProviderTableMeta
.FILE_NAME
},
219 new int[]{R
.id
.textView1
});
223 public void onItemClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
224 if (!mCursor
.moveToPosition(position
)) {
225 throw new IndexOutOfBoundsException("Incorrect item selected");
227 String _id
= mCursor
.getString(mCursor
.getColumnIndex(ProviderTableMeta
._ID
));
231 mCursor
= managedQuery(Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, _id
),
233 ProviderTableMeta
.FILE_CONTENT_TYPE
+"=?",
236 SimpleCursorAdapter sca
= new SimpleCursorAdapter(this, R
.layout
.uploader_list_item_layout
,
238 new String
[]{ProviderTableMeta
.FILE_NAME
},
239 new int[]{R
.id
.textView1
});
241 getListView().invalidate();
244 public void onClick(View v
) {
246 case R
.id
.uploader_choose_folder
:
247 String pathToUpload
= null
;
248 if (mParents
.empty()) {
251 mCursor
= managedQuery(Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_FILE
, mParents
.peek()),
256 mCursor
.moveToFirst();
257 pathToUpload
= mCursor
.getString(mCursor
.getColumnIndex(ProviderTableMeta
.FILE_PATH
)) +
258 mCursor
.getString(mCursor
.getColumnIndex(ProviderTableMeta
.FILE_NAME
)).replace(" ", "%20");
261 showDialog(DIALOG_WAITING
);
262 mUploadThread
= new Thread(new BackgroundUploader(pathToUpload
, mStreamsToUpload
, mHandler
));
263 mUploadThread
.start();
266 case android
.R
.id
.button1
: // dynamic action for create aditional dir
267 showDialog(DIALOG_GET_DIRNAME
);
270 throw new IllegalArgumentException("Wrong element clicked");
274 public void onUploadComplete(boolean uploadSucc
, String message
) {
275 dismissDialog(DIALOG_WAITING
);
276 Log
.i(TAG
, "UploadSucc: " + uploadSucc
+ " message: " + message
);
278 Toast
.makeText(this, getResources().getString(R
.string
.uploader_upload_succeed
), Toast
.LENGTH_SHORT
).show();
280 Toast
.makeText(this, getResources().getString(R
.string
.uploader_upload_failed
) + message
, Toast
.LENGTH_LONG
).show();
286 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
287 super.onActivityResult(requestCode
, resultCode
, data
);
288 Log
.i(TAG
, "result received. req: " + requestCode
+ " res: " + resultCode
);
289 if (requestCode
== REQUEST_CODE_SETUP_ACCOUNT
) {
290 dismissDialog(DIALOG_NO_ACCOUNT
);
291 if (resultCode
== RESULT_CANCELED
) {
294 Account
[] accounts
= mAccountManager
.getAccountsByType(AccountAuthenticator
.AUTH_TOKEN_TYPE
);
295 if (accounts
.length
== 0) {
296 showDialog(DIALOG_NO_ACCOUNT
);
298 // there is no need for checking for is there more then one account at this point
299 // since account setup can set only one account at time
300 mAccount
= accounts
[0];
301 populateDirectoryList();
306 private void populateDirectoryList() {
307 mUsername
= mAccount
.name
.substring(0, mAccount
.name
.indexOf('@'));
308 mPassword
= mAccountManager
.getPassword(mAccount
);
309 setContentView(R
.layout
.uploader_layout
);
310 mCursor
= managedQuery(ProviderMeta
.ProviderTableMeta
.CONTENT_URI
,
312 ProviderTableMeta
.FILE_CONTENT_TYPE
+"=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
313 new String
[]{"DIR", mAccount
.name
},
316 ListView lv
= getListView();
317 lv
.setOnItemClickListener(this);
318 SimpleCursorAdapter sca
= new SimpleCursorAdapter(this,
319 R
.layout
.uploader_list_item_layout
,
321 new String
[]{ProviderTableMeta
.FILE_NAME
},
322 new int[]{R
.id
.textView1
});
324 Button btn
= (Button
) findViewById(R
.id
.uploader_choose_folder
);
325 btn
.setOnClickListener(this);
326 // insert create new directory for multiple items uploading
327 if (getIntent().getAction().equals(Intent
.ACTION_SEND_MULTIPLE
)) {
328 Button createDirBtn
= new Button(this);
329 createDirBtn
.setId(android
.R
.id
.button1
);
330 createDirBtn
.setText(R
.string
.uploader_btn_create_dir_text
);
331 createDirBtn
.setOnClickListener(this);
332 ((LinearLayout
)findViewById(R
.id
.linearLayout1
)).addView(createDirBtn
, LayoutParams
.FILL_PARENT
, LayoutParams
.WRAP_CONTENT
);
336 private void prepareStreamsToUpload() {
337 if (getIntent().getAction().equals(Intent
.ACTION_SEND
)) {
338 mStreamsToUpload
= new ArrayList
<Parcelable
>();
339 mStreamsToUpload
.add(getIntent().getParcelableExtra(Intent
.EXTRA_STREAM
));
340 } else if (getIntent().getAction().equals(Intent
.ACTION_SEND_MULTIPLE
)) {
341 mStreamsToUpload
= getIntent().getParcelableArrayListExtra(Intent
.EXTRA_STREAM
);
343 // unknow action inserted
344 throw new IllegalArgumentException("Unknown action given: " + getIntent().getAction());
348 public void PartialupdateUpload(String fileLocalPath
, String filename
, String filepath
, String contentType
, String contentLength
) {
349 ContentValues cv
= new ContentValues();
350 cv
.put(ProviderTableMeta
.FILE_NAME
, filename
);
351 cv
.put(ProviderTableMeta
.FILE_PATH
, filepath
);
352 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, fileLocalPath
);
353 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, WebdavUtils
.DISPLAY_DATE_FORMAT
.format(new java
.util
.Date()));
354 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, contentType
);
355 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, contentLength
);
356 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
357 Log
.d(TAG
, filename
+" ++ "+filepath
+" ++ " + contentLength
+ " ++ " + contentType
+ " ++ " + fileLocalPath
);
358 if (!mParents
.empty()) {
359 Cursor c
= managedQuery(Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_FILE
, mParents
.peek()),
365 cv
.put(ProviderTableMeta
.FILE_PARENT
, c
.getString(c
.getColumnIndex(ProviderTableMeta
._ID
)));
368 getContentResolver().insert(ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
371 class BackgroundUploader
implements Runnable
{
372 private ArrayList
<Parcelable
> mUploadStreams
;
373 private Handler mHandler
;
374 private String mUploadPath
;
375 private boolean mCreateDir
;
377 public BackgroundUploader(String pathToUpload
, ArrayList
<Parcelable
> streamsToUpload
,
379 mUploadStreams
= streamsToUpload
;
381 mUploadPath
= pathToUpload
.replace(" ", "%20");
385 public BackgroundUploader(String pathToUpload
, ArrayList
<Parcelable
> streamsToUpload
,
386 Handler handler
, boolean createDir
) {
387 mUploadStreams
= streamsToUpload
;
389 mUploadPath
= pathToUpload
.replace(" ", "%20");
390 mCreateDir
= createDir
;
394 WebdavClient wdc
= new WebdavClient(Uri
.parse(mAccountManager
.getUserData(mAccount
,
395 AccountAuthenticator
.KEY_OC_URL
)));
396 wdc
.setCredentials(mUsername
, mPassword
);
397 wdc
.allowUnsignedCertificates();
399 // create last directory in path if nessesary
401 wdc
.createDirectory(mUploadPath
);
404 for (int i
= 0; i
< mUploadStreams
.size(); ++i
) {
405 Uri uri
= (Uri
) mUploadStreams
.get(i
);
406 if (uri
.getScheme().equals("content")) {
407 final Cursor c
= getContentResolver().query((Uri
) mUploadStreams
.get(i
), null
, null
, null
, null
);
410 if (!wdc
.putFile(c
.getString(c
.getColumnIndex(Media
.DATA
)),
411 mUploadPath
+"/"+c
.getString(c
.getColumnIndex(Media
.DISPLAY_NAME
)),
412 c
.getString(c
.getColumnIndex(Media
.MIME_TYPE
)))) {
413 mHandler
.post(new Runnable() {
415 Uploader
.this.onUploadComplete(false
, "Error while uploading file: " + c
.getString(c
.getColumnIndex(Media
.DISPLAY_NAME
)));
419 } else if (uri
.getScheme().equals("file")) {
420 final File file
= new File(Uri
.decode(uri
.toString()).replace(uri
.getScheme()+"://", ""));
421 FileNameMap fileNameMap
= URLConnection
.getFileNameMap();
422 String contentType
= fileNameMap
.getContentTypeFor(uri
.toString());
423 if (contentType
== null
) {
424 contentType
= "text/plain";
426 if (!wdc
.putFile(file
.getAbsolutePath(), mUploadPath
+"/"+file
.getName(), contentType
)) {
427 mHandler
.post(new Runnable() {
429 Uploader
.this.onUploadComplete(false
, "Error while uploading file: " + file
.getName());
436 mHandler
.post(new Runnable() {
438 Uploader
.this.onUploadComplete(true
, null
);