1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
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.
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.
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/>.
18 package eu
.alefzero
.owncloud
;
21 import java
.net
.URLEncoder
;
22 import java
.util
.ArrayList
;
23 import java
.util
.Stack
;
25 import android
.accounts
.Account
;
26 import android
.accounts
.AccountManager
;
27 import android
.app
.AlertDialog
;
28 import android
.app
.Dialog
;
29 import android
.app
.ListActivity
;
30 import android
.app
.ProgressDialog
;
31 import android
.app
.AlertDialog
.Builder
;
32 import android
.content
.Context
;
33 import android
.content
.DialogInterface
;
34 import android
.content
.Intent
;
35 import android
.content
.DialogInterface
.OnCancelListener
;
36 import android
.content
.DialogInterface
.OnClickListener
;
37 import android
.database
.Cursor
;
38 import android
.net
.Uri
;
39 import android
.os
.Bundle
;
40 import android
.os
.Parcelable
;
41 import android
.provider
.MediaStore
.Images
.Media
;
42 import android
.util
.Log
;
43 import android
.view
.View
;
44 import android
.view
.Window
;
45 import android
.widget
.AdapterView
;
46 import android
.widget
.Button
;
47 import android
.widget
.EditText
;
48 import android
.widget
.ListView
;
49 import android
.widget
.SimpleCursorAdapter
;
50 import android
.widget
.AdapterView
.OnItemClickListener
;
51 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
52 import eu
.alefzero
.owncloud
.db
.ProviderMeta
;
53 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
;
54 import eu
.alefzero
.owncloud
.files
.services
.FileUploader
;
55 import eu
.alefzero
.owncloud
.utils
.OwnCloudVersion
;
56 import eu
.alefzero
.webdav
.WebdavClient
;
59 * This can be used to upload things to an ownCloud instance.
61 * @author Bartek Przybylski
64 public class Uploader
extends ListActivity
implements OnItemClickListener
, android
.view
.View
.OnClickListener
{
65 private static final String TAG
= "ownCloudUploader";
67 private Account mAccount
;
68 private AccountManager mAccountManager
;
69 private String mUsername
, mPassword
;
70 private Cursor mCursor
;
71 private Stack
<String
> mParents
;
72 private ArrayList
<Parcelable
> mStreamsToUpload
;
73 private boolean mCreateDir
;
74 private String mUploadPath
;
75 private static final String
[] CONTENT_PROJECTION
= { Media
.DATA
, Media
.DISPLAY_NAME
, Media
.MIME_TYPE
, Media
.SIZE
};
77 private final static int DIALOG_NO_ACCOUNT
= 0;
78 private final static int DIALOG_WAITING
= 1;
79 private final static int DIALOG_NO_STREAM
= 2;
80 private final static int DIALOG_MULTIPLE_ACCOUNT
= 3;
81 private final static int DIALOG_GET_DIRNAME
= 4;
83 private final static int REQUEST_CODE_SETUP_ACCOUNT
= 0;
86 protected void onCreate(Bundle savedInstanceState
) {
87 super.onCreate(savedInstanceState
);
88 getWindow().requestFeature(Window
.FEATURE_NO_TITLE
);
89 mParents
= new Stack
<String
>();
90 if (getIntent().hasExtra(Intent
.EXTRA_STREAM
)) {
91 prepareStreamsToUpload();
92 mAccountManager
= (AccountManager
) getSystemService(Context
.ACCOUNT_SERVICE
);
93 Account
[] accounts
= mAccountManager
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
94 if (accounts
.length
== 0) {
95 Log
.i(TAG
, "No ownCloud account is available");
96 showDialog(DIALOG_NO_ACCOUNT
);
97 } else if (accounts
.length
> 1) {
98 Log
.i(TAG
, "More then one ownCloud is available");
99 showDialog(DIALOG_MULTIPLE_ACCOUNT
);
101 mAccount
= accounts
[0];
102 setContentView(R
.layout
.uploader_layout
);
103 populateDirectoryList();
106 showDialog(DIALOG_NO_STREAM
);
111 protected Dialog
onCreateDialog(final int id
) {
112 final AlertDialog
.Builder builder
= new Builder(this);
115 ProgressDialog pDialog
= new ProgressDialog(this);
116 pDialog
.setIndeterminate(false
);
117 pDialog
.setCancelable(false
);
118 pDialog
.setMessage(getResources().getString(R
.string
.uploader_info_uploading
));
120 case DIALOG_NO_ACCOUNT
:
121 builder
.setIcon(android
.R
.drawable
.ic_dialog_alert
);
122 builder
.setTitle(R
.string
.uploader_wrn_no_account_title
);
123 builder
.setMessage(R
.string
.uploader_wrn_no_account_text
);
124 builder
.setCancelable(false
);
125 builder
.setPositiveButton(R
.string
.uploader_wrn_no_account_setup_btn_text
, new OnClickListener() {
126 public void onClick(DialogInterface dialog
, int which
) {
127 if (android
.os
.Build
.VERSION
.SDK_INT
> android
.os
.Build
.VERSION_CODES
.ECLAIR_MR1
) {
128 // using string value since in API7 this
129 // constatn is not defined
130 // in API7 < this constatant is defined in
131 // Settings.ADD_ACCOUNT_SETTINGS
132 // and Settings.EXTRA_AUTHORITIES
133 Intent intent
= new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
134 intent
.putExtra("authorities", new String
[] { AccountAuthenticator
.AUTH_TOKEN_TYPE
});
135 startActivityForResult(intent
, REQUEST_CODE_SETUP_ACCOUNT
);
137 // since in API7 there is no direct call for
138 // account setup, so we need to
139 // show our own AccountSetupAcricity, get
140 // desired results and setup
141 // everything for ourself
142 Intent intent
= new Intent(getBaseContext(), AccountAuthenticator
.class);
143 startActivityForResult(intent
, REQUEST_CODE_SETUP_ACCOUNT
);
147 builder
.setNegativeButton(R
.string
.uploader_wrn_no_account_quit_btn_text
, new OnClickListener() {
148 public void onClick(DialogInterface dialog
, int which
) {
152 return builder
.create();
153 case DIALOG_GET_DIRNAME
:
154 final EditText dirName
= new EditText(getBaseContext());
155 builder
.setView(dirName
);
156 builder
.setTitle(R
.string
.uploader_info_dirname
);
158 if (mParents
.empty()) {
161 mCursor
= managedQuery(Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_FILE
, mParents
.peek()), null
,
163 mCursor
.moveToFirst();
164 pathToUpload
= mCursor
.getString(mCursor
.getColumnIndex(ProviderTableMeta
.FILE_PATH
))
165 + mCursor
.getString(mCursor
.getColumnIndex(ProviderTableMeta
.FILE_NAME
)).replace(" ", "%20");
167 a a
= new a(pathToUpload
, dirName
);
168 builder
.setPositiveButton(R
.string
.common_ok
, a
);
169 builder
.setNegativeButton(R
.string
.common_cancel
, new OnClickListener() {
170 public void onClick(DialogInterface dialog
, int which
) {
174 return builder
.create();
175 case DIALOG_MULTIPLE_ACCOUNT
:
176 CharSequence ac
[] = new CharSequence
[mAccountManager
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
).length
];
177 for (int i
= 0; i
< ac
.length
; ++i
) {
178 ac
[i
] = mAccountManager
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)[i
].name
;
180 builder
.setTitle(R
.string
.common_choose_account
);
181 builder
.setItems(ac
, new OnClickListener() {
182 public void onClick(DialogInterface dialog
, int which
) {
183 mAccount
= mAccountManager
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)[which
];
184 populateDirectoryList();
187 builder
.setCancelable(true
);
188 builder
.setOnCancelListener(new OnCancelListener() {
189 public void onCancel(DialogInterface dialog
) {
194 return builder
.create();
196 throw new IllegalArgumentException("Unknown dialog id: " + id
);
200 class a
implements OnClickListener
{
204 public a(String path
, EditText dirname
) {
209 public void onClick(DialogInterface dialog
, int which
) {
210 Uploader
.this.mUploadPath
= mPath
+ mDirname
.getText().toString();
211 Uploader
.this.mCreateDir
= true
;
217 public void onBackPressed() {
219 if (mParents
.size() == 0) {
220 super.onBackPressed();
222 } else if (mParents
.size() == 1) {
224 mCursor
= managedQuery(ProviderTableMeta
.CONTENT_URI
, null
, ProviderTableMeta
.FILE_CONTENT_TYPE
+ "=?",
225 new String
[] { "DIR" }, null
);
228 mCursor
= managedQuery(Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, mParents
.peek()), null
,
229 ProviderTableMeta
.FILE_CONTENT_TYPE
+ "=?", new String
[] { "DIR" }, null
);
232 SimpleCursorAdapter sca
= new SimpleCursorAdapter(this, R
.layout
.uploader_list_item_layout
, mCursor
,
233 new String
[] { ProviderTableMeta
.FILE_NAME
}, new int[] { R
.id
.textView1
});
237 public void onItemClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
238 if (!mCursor
.moveToPosition(position
)) {
239 throw new IndexOutOfBoundsException("Incorrect item selected");
241 String _id
= mCursor
.getString(mCursor
.getColumnIndex(ProviderTableMeta
._ID
));
245 mCursor
= managedQuery(Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, _id
), null
,
246 ProviderTableMeta
.FILE_CONTENT_TYPE
+ "=?", new String
[] { "DIR" }, null
);
247 SimpleCursorAdapter sca
= new SimpleCursorAdapter(this, R
.layout
.uploader_list_item_layout
, mCursor
,
248 new String
[] { ProviderTableMeta
.FILE_NAME
}, new int[] { R
.id
.textView1
});
250 getListView().invalidate();
253 public void onClick(View v
) {
255 case R
.id
.uploader_choose_folder
:
256 String pathToUpload
= null
;
257 if (mParents
.empty()) {
260 mCursor
= managedQuery(Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_FILE
, mParents
.peek()), null
,
262 mCursor
.moveToFirst();
263 pathToUpload
= mCursor
.getString(mCursor
.getColumnIndex(ProviderTableMeta
.FILE_PATH
)).replace(" ",
266 Log
.d(TAG
, "Uploading file to dir " + pathToUpload
);
269 for (String s
: pathToUpload
.split("/"))
270 mUploadPath
= "/" + URLEncoder
.encode(s
);
271 if (!mUploadPath
.endsWith("/")) mUploadPath
+= "/";
276 case android
.R
.id
.button1
: // dynamic action for create aditional dir
277 showDialog(DIALOG_GET_DIRNAME
);
280 throw new IllegalArgumentException("Wrong element clicked");
285 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
286 super.onActivityResult(requestCode
, resultCode
, data
);
287 Log
.i(TAG
, "result received. req: " + requestCode
+ " res: " + resultCode
);
288 if (requestCode
== REQUEST_CODE_SETUP_ACCOUNT
) {
289 dismissDialog(DIALOG_NO_ACCOUNT
);
290 if (resultCode
== RESULT_CANCELED
) {
293 Account
[] accounts
= mAccountManager
.getAccountsByType(AccountAuthenticator
.AUTH_TOKEN_TYPE
);
294 if (accounts
.length
== 0) {
295 showDialog(DIALOG_NO_ACCOUNT
);
297 // there is no need for checking for is there more then one
298 // 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
);
311 mCursor
= managedQuery(ProviderMeta
.ProviderTableMeta
.CONTENT_URI
, null
, ProviderTableMeta
.FILE_NAME
312 + "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?", new String
[] { "/", mAccount
.name
}, null
);
314 if (mCursor
.moveToFirst()) {
315 mCursor
= managedQuery(
316 ProviderMeta
.ProviderTableMeta
.CONTENT_URI
,
318 ProviderTableMeta
.FILE_CONTENT_TYPE
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND "
319 + ProviderTableMeta
.FILE_PARENT
+ "=?",
320 new String
[] { "DIR", mAccount
.name
,
321 mCursor
.getString(mCursor
.getColumnIndex(ProviderTableMeta
._ID
)) }, null
);
323 ListView lv
= getListView();
324 lv
.setOnItemClickListener(this);
325 SimpleCursorAdapter sca
= new SimpleCursorAdapter(this, R
.layout
.uploader_list_item_layout
, mCursor
,
326 new String
[] { ProviderTableMeta
.FILE_NAME
}, new int[] { R
.id
.textView1
});
328 Button btn
= (Button
) findViewById(R
.id
.uploader_choose_folder
);
329 btn
.setOnClickListener(this);
331 * disable this until new server interaction service wont be created
332 * // insert create new directory for multiple items uploading if
333 * (getIntent().getAction().equals(Intent.ACTION_SEND_MULTIPLE)) {
334 * Button createDirBtn = new Button(this);
335 * createDirBtn.setId(android.R.id.button1);
336 * createDirBtn.setText(R.string.uploader_btn_create_dir_text);
337 * createDirBtn.setOnClickListener(this); ((LinearLayout)
338 * findViewById(R.id.linearLayout1)).addView( createDirBtn,
339 * LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); }
344 private void prepareStreamsToUpload() {
345 if (getIntent().getAction().equals(Intent
.ACTION_SEND
)) {
346 mStreamsToUpload
= new ArrayList
<Parcelable
>();
347 mStreamsToUpload
.add(getIntent().getParcelableExtra(Intent
.EXTRA_STREAM
));
348 } else if (getIntent().getAction().equals(Intent
.ACTION_SEND_MULTIPLE
)) {
349 mStreamsToUpload
= getIntent().getParcelableArrayListExtra(Intent
.EXTRA_STREAM
);
351 // unknow action inserted
352 throw new IllegalArgumentException("Unknown action given: " + getIntent().getAction());
356 public void uploadFiles() {
357 OwnCloudVersion ocv
= new OwnCloudVersion(mAccountManager
.getUserData(mAccount
,
358 AccountAuthenticator
.KEY_OC_VERSION
));
359 String base_url
= mAccountManager
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_BASE_URL
);
360 String webdav_path
= AccountUtils
.getWebdavPath(ocv
);
361 Uri oc_uri
= Uri
.parse(base_url
+ webdav_path
);
363 WebdavClient wdc
= new WebdavClient(oc_uri
);
364 wdc
.setCredentials(mUsername
, mPassword
);
365 wdc
.allowSelfsignedCertificates();
367 // create last directory in path if nessesary
369 wdc
.createDirectory(mUploadPath
);
372 String
[] local
= new String
[mStreamsToUpload
.size()], remote
= new String
[mStreamsToUpload
.size()];
374 for (int i
= 0; i
< mStreamsToUpload
.size(); ++i
) {
375 Uri uri
= (Uri
) mStreamsToUpload
.get(i
);
376 if (uri
.getScheme().equals("content")) {
377 Cursor c
= getContentResolver().query((Uri
) mStreamsToUpload
.get(i
),
383 if (!c
.moveToFirst())
386 final String display_name
= c
.getString(c
.getColumnIndex(Media
.DISPLAY_NAME
)),
387 data
= c
.getString(c
.getColumnIndex(Media
.DATA
));
389 remote
[i
] = mUploadPath
+ display_name
;
390 } else if (uri
.getScheme().equals("file")) {
391 final File file
= new File(Uri
.decode(uri
.toString()).replace(uri
.getScheme() + "://", ""));
392 local
[i
] = file
.getAbsolutePath();
393 remote
[i
] = mUploadPath
+ file
.getName();
397 Intent intent
= new Intent(getApplicationContext(), FileUploader
.class);
398 intent
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
399 intent
.putExtra(FileUploader
.KEY_LOCAL_FILE
, local
);
400 intent
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remote
);
401 intent
.putExtra(FileUploader
.KEY_ACCOUNT
, mAccount
);
402 startService(intent
);