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 com
.owncloud
.android
;
21 import java
.util
.ArrayList
;
22 import java
.util
.HashMap
;
23 import java
.util
.LinkedList
;
24 import java
.util
.List
;
25 import java
.util
.Stack
;
26 import java
.util
.Vector
;
28 import com
.owncloud
.android
.authenticator
.AccountAuthenticator
;
29 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
30 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
31 import com
.owncloud
.android
.datamodel
.OCFile
;
32 import com
.owncloud
.android
.files
.services
.FileUploader
;
34 import android
.accounts
.Account
;
35 import android
.accounts
.AccountManager
;
36 import android
.app
.AlertDialog
;
37 import android
.app
.AlertDialog
.Builder
;
38 import android
.app
.Dialog
;
39 import android
.app
.ListActivity
;
40 import android
.app
.ProgressDialog
;
41 import android
.content
.Context
;
42 import android
.content
.DialogInterface
;
43 import android
.content
.DialogInterface
.OnCancelListener
;
44 import android
.content
.DialogInterface
.OnClickListener
;
45 import android
.content
.Intent
;
46 import android
.database
.Cursor
;
47 import android
.net
.Uri
;
48 import android
.os
.Bundle
;
49 import android
.os
.Parcelable
;
50 import android
.provider
.MediaStore
.Images
.Media
;
51 import android
.util
.Log
;
52 import android
.view
.View
;
53 import android
.view
.Window
;
54 import android
.widget
.AdapterView
;
55 import android
.widget
.AdapterView
.OnItemClickListener
;
56 import android
.widget
.Button
;
57 import android
.widget
.EditText
;
58 import android
.widget
.SimpleAdapter
;
59 import com
.owncloud
.android
.R
;
60 import eu
.alefzero
.webdav
.WebdavClient
;
63 * This can be used to upload things to an ownCloud instance.
65 * @author Bartek Przybylski
68 public class Uploader
extends ListActivity
implements OnItemClickListener
, android
.view
.View
.OnClickListener
{
69 private static final String TAG
= "ownCloudUploader";
71 private Account mAccount
;
72 private AccountManager mAccountManager
;
73 private Stack
<String
> mParents
;
74 private ArrayList
<Parcelable
> mStreamsToUpload
;
75 private boolean mCreateDir
;
76 private String mUploadPath
;
77 private static final String
[] CONTENT_PROJECTION
= { Media
.DATA
, Media
.DISPLAY_NAME
, Media
.MIME_TYPE
, Media
.SIZE
};
78 private DataStorageManager mStorageManager
;
81 private final static int DIALOG_NO_ACCOUNT
= 0;
82 private final static int DIALOG_WAITING
= 1;
83 private final static int DIALOG_NO_STREAM
= 2;
84 private final static int DIALOG_MULTIPLE_ACCOUNT
= 3;
85 private final static int DIALOG_GET_DIRNAME
= 4;
87 private final static int REQUEST_CODE_SETUP_ACCOUNT
= 0;
90 protected void onCreate(Bundle savedInstanceState
) {
91 super.onCreate(savedInstanceState
);
92 getWindow().requestFeature(Window
.FEATURE_NO_TITLE
);
93 mParents
= new Stack
<String
>();
95 if (getIntent().hasExtra(Intent
.EXTRA_STREAM
)) {
96 prepareStreamsToUpload();
97 mAccountManager
= (AccountManager
) getSystemService(Context
.ACCOUNT_SERVICE
);
98 Account
[] accounts
= mAccountManager
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
99 if (accounts
.length
== 0) {
100 Log
.i(TAG
, "No ownCloud account is available");
101 showDialog(DIALOG_NO_ACCOUNT
);
102 } else if (accounts
.length
> 1) {
103 Log
.i(TAG
, "More then one ownCloud is available");
104 showDialog(DIALOG_MULTIPLE_ACCOUNT
);
106 mAccount
= accounts
[0];
107 setContentView(R
.layout
.uploader_layout
);
108 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
109 populateDirectoryList();
112 showDialog(DIALOG_NO_STREAM
);
117 protected Dialog
onCreateDialog(final int id
) {
118 final AlertDialog
.Builder builder
= new Builder(this);
121 ProgressDialog pDialog
= new ProgressDialog(this);
122 pDialog
.setIndeterminate(false
);
123 pDialog
.setCancelable(false
);
124 pDialog
.setMessage(getResources().getString(R
.string
.uploader_info_uploading
));
126 case DIALOG_NO_ACCOUNT
:
127 builder
.setIcon(android
.R
.drawable
.ic_dialog_alert
);
128 builder
.setTitle(R
.string
.uploader_wrn_no_account_title
);
129 builder
.setMessage(R
.string
.uploader_wrn_no_account_text
);
130 builder
.setCancelable(false
);
131 builder
.setPositiveButton(R
.string
.uploader_wrn_no_account_setup_btn_text
, new OnClickListener() {
132 public void onClick(DialogInterface dialog
, int which
) {
133 if (android
.os
.Build
.VERSION
.SDK_INT
> android
.os
.Build
.VERSION_CODES
.ECLAIR_MR1
) {
134 // using string value since in API7 this
135 // constatn is not defined
136 // in API7 < this constatant is defined in
137 // Settings.ADD_ACCOUNT_SETTINGS
138 // and Settings.EXTRA_AUTHORITIES
139 Intent intent
= new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
140 intent
.putExtra("authorities", new String
[] { AccountAuthenticator
.AUTH_TOKEN_TYPE
});
141 startActivityForResult(intent
, REQUEST_CODE_SETUP_ACCOUNT
);
143 // since in API7 there is no direct call for
144 // account setup, so we need to
145 // show our own AccountSetupAcricity, get
146 // desired results and setup
147 // everything for ourself
148 Intent intent
= new Intent(getBaseContext(), AccountAuthenticator
.class);
149 startActivityForResult(intent
, REQUEST_CODE_SETUP_ACCOUNT
);
153 builder
.setNegativeButton(R
.string
.uploader_wrn_no_account_quit_btn_text
, new OnClickListener() {
154 public void onClick(DialogInterface dialog
, int which
) {
158 return builder
.create();
159 /*case DIALOG_GET_DIRNAME:
160 final EditText dirName = new EditText(getBaseContext());
161 builder.setView(dirName);
162 builder.setTitle(R.string.uploader_info_dirname);
164 if (mParents.empty()) {
167 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, mParents.peek()), null,
169 mCursor.moveToFirst();
170 pathToUpload = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_PATH))
171 + mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_NAME)).replace(" ", "%20"); // TODO don't make this ; use WebdavUtils.encode in the right moment
173 a a = new a(pathToUpload, dirName);
174 builder.setPositiveButton(R.string.common_ok, a);
175 builder.setNegativeButton(R.string.common_cancel, new OnClickListener() {
176 public void onClick(DialogInterface dialog, int which) {
180 return builder.create();*/
181 case DIALOG_MULTIPLE_ACCOUNT
:
182 CharSequence ac
[] = new CharSequence
[mAccountManager
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
).length
];
183 for (int i
= 0; i
< ac
.length
; ++i
) {
184 ac
[i
] = mAccountManager
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)[i
].name
;
186 builder
.setTitle(R
.string
.common_choose_account
);
187 builder
.setItems(ac
, new OnClickListener() {
188 public void onClick(DialogInterface dialog
, int which
) {
189 mAccount
= mAccountManager
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)[which
];
190 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
191 populateDirectoryList();
194 builder
.setCancelable(true
);
195 builder
.setOnCancelListener(new OnCancelListener() {
196 public void onCancel(DialogInterface dialog
) {
201 return builder
.create();
203 throw new IllegalArgumentException("Unknown dialog id: " + id
);
207 class a
implements OnClickListener
{
211 public a(String path
, EditText dirname
) {
216 public void onClick(DialogInterface dialog
, int which
) {
217 Uploader
.this.mUploadPath
= mPath
+ mDirname
.getText().toString();
218 Uploader
.this.mCreateDir
= true
;
224 public void onBackPressed() {
226 if (mParents
.size() <= 1) {
227 super.onBackPressed();
231 populateDirectoryList();
235 public void onItemClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
236 // click on folder in the list
237 Log
.d(TAG
, "on item click");
238 Vector
<OCFile
> tmpfiles
= mStorageManager
.getDirectoryContent(mFile
);
239 if (tmpfiles
== null
) return;
241 Vector
<OCFile
> files
= new Vector
<OCFile
>();
242 for (OCFile f
: tmpfiles
)
245 if (files
.size() < position
) {
246 throw new IndexOutOfBoundsException("Incorrect item selected");
248 mParents
.push(files
.get(position
).getFileName());
249 populateDirectoryList();
252 public void onClick(View v
) {
255 case R
.id
.uploader_choose_folder
:
256 mUploadPath
= ""; // first element in mParents is root dir, represented by ""; init mUploadPath with "/" results in a "//" prefix
257 for (String p
: mParents
)
258 mUploadPath
+= p
+ OCFile
.PATH_SEPARATOR
;
259 Log
.d(TAG
, "Uploading file to dir " + mUploadPath
);
264 case android
.R
.id
.button1
: // dynamic action for create aditional dir
265 showDialog(DIALOG_GET_DIRNAME
);
268 throw new IllegalArgumentException("Wrong element clicked");
273 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
274 super.onActivityResult(requestCode
, resultCode
, data
);
275 Log
.i(TAG
, "result received. req: " + requestCode
+ " res: " + resultCode
);
276 if (requestCode
== REQUEST_CODE_SETUP_ACCOUNT
) {
277 dismissDialog(DIALOG_NO_ACCOUNT
);
278 if (resultCode
== RESULT_CANCELED
) {
281 Account
[] accounts
= mAccountManager
.getAccountsByType(AccountAuthenticator
.AUTH_TOKEN_TYPE
);
282 if (accounts
.length
== 0) {
283 showDialog(DIALOG_NO_ACCOUNT
);
285 // there is no need for checking for is there more then one
286 // account at this point
287 // since account setup can set only one account at time
288 mAccount
= accounts
[0];
289 populateDirectoryList();
294 private void populateDirectoryList() {
295 setContentView(R
.layout
.uploader_layout
);
297 String full_path
= "";
298 for (String a
: mParents
)
299 full_path
+= a
+ "/";
301 Log
.d(TAG
, "Populating view with content of : " + full_path
);
303 mFile
= mStorageManager
.getFileByPath(full_path
);
305 Vector
<OCFile
> files
= mStorageManager
.getDirectoryContent(mFile
);
307 List
<HashMap
<String
, Object
>> data
= new LinkedList
<HashMap
<String
,Object
>>();
308 for (OCFile f
: files
) {
309 HashMap
<String
, Object
> h
= new HashMap
<String
, Object
>();
310 if (f
.isDirectory()) {
311 h
.put("dirname", f
.getFileName());
315 SimpleAdapter sa
= new SimpleAdapter(this,
317 R
.layout
.uploader_list_item_layout
,
318 new String
[] {"dirname"},
319 new int[] {R
.id
.textView1
});
321 Button btn
= (Button
) findViewById(R
.id
.uploader_choose_folder
);
322 btn
.setOnClickListener(this);
323 getListView().setOnItemClickListener(this);
327 mCursor = managedQuery(ProviderMeta.ProviderTableMeta.CONTENT_URI, null, ProviderTableMeta.FILE_NAME
328 + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?", new String[] { "/", mAccount.name }, null);
330 if (mCursor.moveToFirst()) {
331 mCursor = managedQuery(
332 ProviderMeta.ProviderTableMeta.CONTENT_URI,
334 ProviderTableMeta.FILE_CONTENT_TYPE + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND "
335 + ProviderTableMeta.FILE_PARENT + "=?",
336 new String[] { "DIR", mAccount.name,
337 mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta._ID)) }, null);
339 ListView lv = getListView();
340 lv.setOnItemClickListener(this);
341 SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.uploader_list_item_layout, mCursor,
342 new String[] { ProviderTableMeta.FILE_NAME }, new int[] { R.id.textView1 });
344 Button btn = (Button) findViewById(R.id.uploader_choose_folder);
345 btn.setOnClickListener(this);
347 * disable this until new server interaction service wont be created
348 * // insert create new directory for multiple items uploading if
349 * (getIntent().getAction().equals(Intent.ACTION_SEND_MULTIPLE)) {
350 * Button createDirBtn = new Button(this);
351 * createDirBtn.setId(android.R.id.button1);
352 * createDirBtn.setText(R.string.uploader_btn_create_dir_text);
353 * createDirBtn.setOnClickListener(this); ((LinearLayout)
354 * findViewById(R.id.linearLayout1)).addView( createDirBtn,
355 * LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); }
360 private void prepareStreamsToUpload() {
361 if (getIntent().getAction().equals(Intent
.ACTION_SEND
)) {
362 mStreamsToUpload
= new ArrayList
<Parcelable
>();
363 mStreamsToUpload
.add(getIntent().getParcelableExtra(Intent
.EXTRA_STREAM
));
364 } else if (getIntent().getAction().equals(Intent
.ACTION_SEND_MULTIPLE
)) {
365 mStreamsToUpload
= getIntent().getParcelableArrayListExtra(Intent
.EXTRA_STREAM
);
367 // unknow action inserted
368 throw new IllegalArgumentException("Unknown action given: " + getIntent().getAction());
372 public void uploadFiles() {
373 WebdavClient wdc
= new WebdavClient(mAccount
, getApplicationContext());
374 wdc
.allowSelfsignedCertificates();
376 // create last directory in path if nessesary
378 wdc
.createDirectory(mUploadPath
);
381 String
[] local
= new String
[mStreamsToUpload
.size()], remote
= new String
[mStreamsToUpload
.size()];
383 for (int i
= 0; i
< mStreamsToUpload
.size(); ++i
) {
384 Uri uri
= (Uri
) mStreamsToUpload
.get(i
);
385 if (uri
.getScheme().equals("content")) {
386 Cursor c
= getContentResolver().query((Uri
) mStreamsToUpload
.get(i
),
392 if (!c
.moveToFirst())
395 final String display_name
= c
.getString(c
.getColumnIndex(Media
.DISPLAY_NAME
)),
396 data
= c
.getString(c
.getColumnIndex(Media
.DATA
));
398 remote
[i
] = mUploadPath
+ display_name
;
399 } else if (uri
.getScheme().equals("file")) {
400 final File file
= new File(Uri
.decode(uri
.toString()).replace(uri
.getScheme() + "://", ""));
401 local
[i
] = file
.getAbsolutePath();
402 remote
[i
] = mUploadPath
+ file
.getName();
406 Intent intent
= new Intent(getApplicationContext(), FileUploader
.class);
407 intent
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
408 intent
.putExtra(FileUploader
.KEY_LOCAL_FILE
, local
);
409 intent
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remote
);
410 intent
.putExtra(FileUploader
.KEY_ACCOUNT
, mAccount
);
411 startService(intent
);