2 * ownCloud Android client application
4 * @author Bartek Przybylski
5 * Copyright (C) 2012 Bartek Przybylski
6 * Copyright (C) 2015 ownCloud Inc.
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 package com
.owncloud
.android
.ui
.activity
;
25 import java
.util
.ArrayList
;
26 import java
.util
.HashMap
;
27 import java
.util
.LinkedList
;
28 import java
.util
.List
;
29 import java
.util
.Stack
;
30 import java
.util
.Vector
;
32 import android
.accounts
.Account
;
33 import android
.accounts
.AccountManager
;
34 import android
.app
.AlertDialog
;
35 import android
.app
.AlertDialog
.Builder
;
36 import android
.app
.Dialog
;
37 import android
.app
.ProgressDialog
;
38 import android
.content
.Context
;
39 import android
.content
.DialogInterface
;
40 import android
.content
.DialogInterface
.OnCancelListener
;
41 import android
.content
.DialogInterface
.OnClickListener
;
42 import android
.content
.Intent
;
43 import android
.content
.SharedPreferences
;
44 import android
.content
.res
.Resources
.NotFoundException
;
45 import android
.database
.Cursor
;
46 import android
.net
.Uri
;
47 import android
.os
.Bundle
;
48 import android
.os
.Parcelable
;
49 import android
.preference
.PreferenceManager
;
50 import android
.provider
.MediaStore
.Audio
;
51 import android
.provider
.MediaStore
.Images
;
52 import android
.provider
.MediaStore
.Video
;
53 import android
.view
.View
;
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
.ListView
;
59 import android
.widget
.SimpleAdapter
;
60 import android
.widget
.Toast
;
62 import com
.actionbarsherlock
.app
.ActionBar
;
63 import com
.actionbarsherlock
.view
.MenuItem
;
64 import com
.owncloud
.android
.MainApp
;
65 import com
.owncloud
.android
.R
;
66 import com
.owncloud
.android
.authentication
.AccountAuthenticator
;
67 import com
.owncloud
.android
.datamodel
.OCFile
;
68 import com
.owncloud
.android
.files
.services
.FileUploader
;
69 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
70 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
71 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
72 import com
.owncloud
.android
.operations
.CreateFolderOperation
;
73 import com
.owncloud
.android
.ui
.dialog
.CreateFolderDialogFragment
;
74 import com
.owncloud
.android
.utils
.DisplayUtils
;
75 import com
.owncloud
.android
.utils
.ErrorMessageAdapter
;
79 * This can be used to upload things to an ownCloud instance.
81 public class Uploader
extends FileActivity
82 implements OnItemClickListener
, android
.view
.View
.OnClickListener
{
84 private static final String TAG
= Uploader
.class.getSimpleName();
86 private AccountManager mAccountManager
;
87 private Stack
<String
> mParents
;
88 private ArrayList
<Parcelable
> mStreamsToUpload
;
89 private boolean mCreateDir
;
90 private String mUploadPath
;
92 private boolean mAccountSelected
;
94 private final static int DIALOG_NO_ACCOUNT
= 0;
95 private final static int DIALOG_WAITING
= 1;
96 private final static int DIALOG_NO_STREAM
= 2;
97 private final static int DIALOG_MULTIPLE_ACCOUNT
= 3;
99 private final static int REQUEST_CODE_SETUP_ACCOUNT
= 0;
101 private final static String KEY_PARENTS
= "PARENTS";
102 private final static String KEY_FILE
= "FILE";
103 private final static String KEY_ACCOUNT_SELECTED
= "ACCOUNT_SELECTED";
106 protected void onCreate(Bundle savedInstanceState
) {
107 prepareStreamsToUpload();
109 if (savedInstanceState
== null
) {
110 mParents
= new Stack
<String
>();
111 mAccountSelected
= false
;
113 mParents
= (Stack
<String
>) savedInstanceState
.getSerializable(KEY_PARENTS
);
114 mFile
= savedInstanceState
.getParcelable(KEY_FILE
);
115 mAccountSelected
= savedInstanceState
.getBoolean(KEY_ACCOUNT_SELECTED
);
117 super.onCreate(savedInstanceState
);
119 ActionBar actionBar
= getSupportActionBar();
120 actionBar
.setIcon(DisplayUtils
.getSeasonalIconId());
125 protected void setAccount(Account account
, boolean savedAccount
) {
126 if (somethingToUpload()) {
127 mAccountManager
= (AccountManager
) getSystemService(Context
.ACCOUNT_SERVICE
);
128 Account
[] accounts
= mAccountManager
.getAccountsByType(MainApp
.getAccountType());
129 if (accounts
.length
== 0) {
130 Log_OC
.i(TAG
, "No ownCloud account is available");
131 showDialog(DIALOG_NO_ACCOUNT
);
132 } else if (accounts
.length
> 1 && !mAccountSelected
) {
133 Log_OC
.i(TAG
, "More than one ownCloud is available");
134 showDialog(DIALOG_MULTIPLE_ACCOUNT
);
137 setAccount(accounts
[0]);
142 showDialog(DIALOG_NO_STREAM
);
145 super.setAccount(account
, savedAccount
);
149 protected void onAccountSet(boolean stateWasRecovered
) {
150 super.onAccountSet(mAccountWasRestored
);
152 populateDirectoryList();
156 protected void onSaveInstanceState(Bundle outState
) {
157 Log_OC
.d(TAG
, "onSaveInstanceState() start");
158 super.onSaveInstanceState(outState
);
159 outState
.putSerializable(KEY_PARENTS
, mParents
);
160 //outState.putParcelable(KEY_ACCOUNT, mAccount);
161 outState
.putParcelable(KEY_FILE
, mFile
);
162 outState
.putBoolean(KEY_ACCOUNT_SELECTED
, mAccountSelected
);
164 Log_OC
.d(TAG
, "onSaveInstanceState() end");
168 protected Dialog
onCreateDialog(final int id
) {
169 final AlertDialog
.Builder builder
= new Builder(this);
172 ProgressDialog pDialog
= new ProgressDialog(this);
173 pDialog
.setIndeterminate(false
);
174 pDialog
.setCancelable(false
);
175 pDialog
.setMessage(getResources().getString(R
.string
.uploader_info_uploading
));
177 case DIALOG_NO_ACCOUNT
:
178 builder
.setIcon(android
.R
.drawable
.ic_dialog_alert
);
179 builder
.setTitle(R
.string
.uploader_wrn_no_account_title
);
180 builder
.setMessage(String
.format(
181 getString(R
.string
.uploader_wrn_no_account_text
), getString(R
.string
.app_name
)));
182 builder
.setCancelable(false
);
183 builder
.setPositiveButton(R
.string
.uploader_wrn_no_account_setup_btn_text
, new OnClickListener() {
185 public void onClick(DialogInterface dialog
, int which
) {
186 if (android
.os
.Build
.VERSION
.SDK_INT
> android
.os
.Build
.VERSION_CODES
.ECLAIR_MR1
) {
187 // using string value since in API7 this
188 // constatn is not defined
189 // in API7 < this constatant is defined in
190 // Settings.ADD_ACCOUNT_SETTINGS
191 // and Settings.EXTRA_AUTHORITIES
192 Intent intent
= new Intent(android
.provider
.Settings
.ACTION_ADD_ACCOUNT
);
193 intent
.putExtra("authorities", new String
[] { MainApp
.getAuthTokenType() });
194 startActivityForResult(intent
, REQUEST_CODE_SETUP_ACCOUNT
);
196 // since in API7 there is no direct call for
197 // account setup, so we need to
198 // show our own AccountSetupAcricity, get
199 // desired results and setup
200 // everything for ourself
201 Intent intent
= new Intent(getBaseContext(), AccountAuthenticator
.class);
202 startActivityForResult(intent
, REQUEST_CODE_SETUP_ACCOUNT
);
206 builder
.setNegativeButton(R
.string
.uploader_wrn_no_account_quit_btn_text
, new OnClickListener() {
208 public void onClick(DialogInterface dialog
, int which
) {
212 return builder
.create();
213 case DIALOG_MULTIPLE_ACCOUNT
:
214 CharSequence ac
[] = new CharSequence
[
215 mAccountManager
.getAccountsByType(MainApp
.getAccountType()).length
];
216 for (int i
= 0; i
< ac
.length
; ++i
) {
217 ac
[i
] = DisplayUtils
.convertIdn(
218 mAccountManager
.getAccountsByType(MainApp
.getAccountType())[i
].name
, false
);
220 builder
.setTitle(R
.string
.common_choose_account
);
221 builder
.setItems(ac
, new OnClickListener() {
223 public void onClick(DialogInterface dialog
, int which
) {
224 setAccount(mAccountManager
.getAccountsByType(MainApp
.getAccountType())[which
]);
225 onAccountSet(mAccountWasRestored
);
227 mAccountSelected
= true
;
230 builder
.setCancelable(true
);
231 builder
.setOnCancelListener(new OnCancelListener() {
233 public void onCancel(DialogInterface dialog
) {
238 return builder
.create();
239 case DIALOG_NO_STREAM
:
240 builder
.setIcon(android
.R
.drawable
.ic_dialog_alert
);
241 builder
.setTitle(R
.string
.uploader_wrn_no_content_title
);
242 builder
.setMessage(R
.string
.uploader_wrn_no_content_text
);
243 builder
.setCancelable(false
);
244 builder
.setNegativeButton(R
.string
.common_cancel
, new OnClickListener() {
246 public void onClick(DialogInterface dialog
, int which
) {
250 return builder
.create();
252 throw new IllegalArgumentException("Unknown dialog id: " + id
);
256 class a
implements OnClickListener
{
260 public a(String path
, EditText dirname
) {
266 public void onClick(DialogInterface dialog
, int which
) {
267 Uploader
.this.mUploadPath
= mPath
+ mDirname
.getText().toString();
268 Uploader
.this.mCreateDir
= true
;
274 public void onBackPressed() {
276 if (mParents
.size() <= 1) {
277 super.onBackPressed();
281 populateDirectoryList();
286 public void onItemClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
287 // click on folder in the list
288 Log_OC
.d(TAG
, "on item click");
289 Vector
<OCFile
> tmpfiles
= getStorageManager().getFolderContent(mFile
);
290 if (tmpfiles
.size() <= 0) return;
292 Vector
<OCFile
> files
= new Vector
<OCFile
>();
293 for (OCFile f
: tmpfiles
)
296 if (files
.size() < position
) {
297 throw new IndexOutOfBoundsException("Incorrect item selected");
299 mParents
.push(files
.get(position
).getFileName());
300 populateDirectoryList();
304 public void onClick(View v
) {
307 case R
.id
.uploader_choose_folder
:
308 mUploadPath
= ""; // first element in mParents is root dir, represented by "";
309 // init mUploadPath with "/" results in a "//" prefix
310 for (String p
: mParents
)
311 mUploadPath
+= p
+ OCFile
.PATH_SEPARATOR
;
312 Log_OC
.d(TAG
, "Uploading file to dir " + mUploadPath
);
318 case R
.id
.uploader_new_folder
:
319 CreateFolderDialogFragment dialog
= CreateFolderDialogFragment
.newInstance(mFile
);
320 dialog
.show(getSupportFragmentManager(), "createdirdialog");
325 throw new IllegalArgumentException("Wrong element clicked");
330 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
331 super.onActivityResult(requestCode
, resultCode
, data
);
332 Log_OC
.i(TAG
, "result received. req: " + requestCode
+ " res: " + resultCode
);
333 if (requestCode
== REQUEST_CODE_SETUP_ACCOUNT
) {
334 dismissDialog(DIALOG_NO_ACCOUNT
);
335 if (resultCode
== RESULT_CANCELED
) {
338 Account
[] accounts
= mAccountManager
.getAccountsByType(MainApp
.getAuthTokenType());
339 if (accounts
.length
== 0) {
340 showDialog(DIALOG_NO_ACCOUNT
);
342 // there is no need for checking for is there more then one
343 // account at this point
344 // since account setup can set only one account at time
345 setAccount(accounts
[0]);
346 populateDirectoryList();
351 private void populateDirectoryList() {
352 setContentView(R
.layout
.uploader_layout
);
354 ListView mListView
= (ListView
) findViewById(android
.R
.id
.list
);
356 String current_dir
= mParents
.peek();
357 if(current_dir
.equals("")){
358 getSupportActionBar().setTitle(getString(R
.string
.default_display_name_for_root_folder
));
361 getSupportActionBar().setTitle(current_dir
);
363 boolean notRoot
= (mParents
.size() > 1);
364 ActionBar actionBar
= getSupportActionBar();
365 actionBar
.setDisplayHomeAsUpEnabled(notRoot
);
366 actionBar
.setHomeButtonEnabled(notRoot
);
368 String full_path
= generatePath(mParents
);
370 Log_OC
.d(TAG
, "Populating view with content of : " + full_path
);
372 mFile
= getStorageManager().getFileByPath(full_path
);
374 Vector
<OCFile
> files
= getStorageManager().getFolderContent(mFile
);
375 List
<HashMap
<String
, Object
>> data
= new LinkedList
<HashMap
<String
,Object
>>();
376 for (OCFile f
: files
) {
377 HashMap
<String
, Object
> h
= new HashMap
<String
, Object
>();
379 h
.put("dirname", f
.getFileName());
383 SimpleAdapter sa
= new SimpleAdapter(this,
385 R
.layout
.uploader_list_item_layout
,
386 new String
[] {"dirname"},
387 new int[] {R
.id
.textView1
});
389 mListView
.setAdapter(sa
);
390 Button btnChooseFolder
= (Button
) findViewById(R
.id
.uploader_choose_folder
);
391 btnChooseFolder
.setOnClickListener(this);
393 Button btnNewFolder
= (Button
) findViewById(R
.id
.uploader_new_folder
);
394 btnNewFolder
.setOnClickListener(this);
396 mListView
.setOnItemClickListener(this);
400 private String
generatePath(Stack
<String
> dirs
) {
401 String full_path
= "";
403 for (String a
: dirs
)
404 full_path
+= a
+ "/";
408 private void prepareStreamsToUpload() {
409 if (getIntent().getAction().equals(Intent
.ACTION_SEND
)) {
410 mStreamsToUpload
= new ArrayList
<Parcelable
>();
411 mStreamsToUpload
.add(getIntent().getParcelableExtra(Intent
.EXTRA_STREAM
));
412 } else if (getIntent().getAction().equals(Intent
.ACTION_SEND_MULTIPLE
)) {
413 mStreamsToUpload
= getIntent().getParcelableArrayListExtra(Intent
.EXTRA_STREAM
);
417 private boolean somethingToUpload() {
418 return (mStreamsToUpload
!= null
&& mStreamsToUpload
.get(0) != null
);
421 public void uploadFiles() {
424 ArrayList
<String
> local
= new ArrayList
<String
>();
425 ArrayList
<String
> remote
= new ArrayList
<String
>();
427 // this checks the mimeType
428 for (Parcelable mStream
: mStreamsToUpload
) {
430 Uri uri
= (Uri
) mStream
;
432 if (uri
.getScheme().equals("content")) {
434 String mimeType
= getContentResolver().getType(uri
);
436 if (mimeType
.contains("image")) {
437 String
[] CONTENT_PROJECTION
= { Images
.Media
.DATA
,
438 Images
.Media
.DISPLAY_NAME
, Images
.Media
.MIME_TYPE
,
440 Cursor c
= getContentResolver().query(uri
, CONTENT_PROJECTION
, null
,
443 int index
= c
.getColumnIndex(Images
.Media
.DATA
);
444 String data
= c
.getString(index
);
446 remote
.add(mUploadPath
+
447 c
.getString(c
.getColumnIndex(Images
.Media
.DISPLAY_NAME
)));
450 else if (mimeType
.contains("video")) {
451 String
[] CONTENT_PROJECTION
= { Video
.Media
.DATA
,
452 Video
.Media
.DISPLAY_NAME
, Video
.Media
.MIME_TYPE
,
453 Video
.Media
.SIZE
, Video
.Media
.DATE_MODIFIED
};
454 Cursor c
= getContentResolver().query(uri
, CONTENT_PROJECTION
, null
,
457 int index
= c
.getColumnIndex(Video
.Media
.DATA
);
458 String data
= c
.getString(index
);
460 remote
.add(mUploadPath
+
461 c
.getString(c
.getColumnIndex(Video
.Media
.DISPLAY_NAME
)));
464 else if (mimeType
.contains("audio")) {
465 String
[] CONTENT_PROJECTION
= { Audio
.Media
.DATA
,
466 Audio
.Media
.DISPLAY_NAME
, Audio
.Media
.MIME_TYPE
,
468 Cursor c
= getContentResolver().query(uri
, CONTENT_PROJECTION
, null
,
471 int index
= c
.getColumnIndex(Audio
.Media
.DATA
);
472 String data
= c
.getString(index
);
474 remote
.add(mUploadPath
+
475 c
.getString(c
.getColumnIndex(Audio
.Media
.DISPLAY_NAME
)));
479 String filePath
= Uri
.decode(uri
.toString()).replace(uri
.getScheme() +
481 // cut everything whats before mnt. It occurred to me that sometimes
482 // apps send their name into the URI
483 if (filePath
.contains("mnt")) {
484 String splitedFilePath
[] = filePath
.split("/mnt");
485 filePath
= splitedFilePath
[1];
487 final File file
= new File(filePath
);
488 local
.add(file
.getAbsolutePath());
489 remote
.add(mUploadPath
+ file
.getName());
492 } else if (uri
.getScheme().equals("file")) {
493 String filePath
= Uri
.decode(uri
.toString()).replace(uri
.getScheme() +
495 if (filePath
.contains("mnt")) {
496 String splitedFilePath
[] = filePath
.split("/mnt");
497 filePath
= splitedFilePath
[1];
499 final File file
= new File(filePath
);
500 local
.add(file
.getAbsolutePath());
501 remote
.add(mUploadPath
+ file
.getName());
504 throw new SecurityException();
508 throw new SecurityException();
511 Intent intent
= new Intent(getApplicationContext(), FileUploader
.class);
512 intent
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
513 intent
.putExtra(FileUploader
.KEY_LOCAL_FILE
, local
.toArray(new String
[local
.size()]));
514 intent
.putExtra(FileUploader
.KEY_REMOTE_FILE
,
515 remote
.toArray(new String
[remote
.size()]));
516 intent
.putExtra(FileUploader
.KEY_ACCOUNT
, getAccount());
517 startService(intent
);
519 //Save the path to shared preferences
520 SharedPreferences
.Editor appPrefs
= PreferenceManager
521 .getDefaultSharedPreferences(getApplicationContext()).edit();
522 appPrefs
.putString("last_upload_path", mUploadPath
);
528 } catch (SecurityException e
) {
529 String message
= String
.format(getString(R
.string
.uploader_error_forbidden_content
),
530 getString(R
.string
.app_name
));
531 Toast
.makeText(this, message
, Toast
.LENGTH_LONG
).show();
536 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
537 super.onRemoteOperationFinish(operation
, result
);
540 if (operation
instanceof CreateFolderOperation
) {
541 onCreateFolderOperationFinish((CreateFolderOperation
)operation
, result
);
547 * Updates the view associated to the activity after the finish of an operation
548 * trying create a new folder
550 * @param operation Creation operation performed.
551 * @param result Result of the creation.
553 private void onCreateFolderOperationFinish(CreateFolderOperation operation
,
554 RemoteOperationResult result
) {
555 if (result
.isSuccess()) {
556 dismissLoadingDialog();
557 populateDirectoryList();
559 dismissLoadingDialog();
561 Toast msg
= Toast
.makeText(this,
562 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
566 } catch (NotFoundException e
) {
567 Log_OC
.e(TAG
, "Error while trying to show fail message " , e
);
574 * Loads the target folder initialize shown to the user.
576 * The target account has to be chosen before this method is called.
578 private void initTargetFolder() {
579 if (getStorageManager() == null
) {
580 throw new IllegalStateException("Do not call this method before " +
581 "initializing mStorageManager");
584 SharedPreferences appPreferences
= PreferenceManager
585 .getDefaultSharedPreferences(getApplicationContext());
587 String last_path
= appPreferences
.getString("last_upload_path", "");
588 // "/" equals root-directory
589 if(last_path
.equals("/")) {
593 String
[] dir_names
= last_path
.split("/");
594 for (String dir
: dir_names
)
597 //Make sure that path still exists, if it doesn't pop the stack and try the previous path
598 while(!getStorageManager().fileExists(generatePath(mParents
)) && mParents
.size() > 1){
605 public boolean onOptionsItemSelected(MenuItem item
) {
606 boolean retval
= true
;
607 switch (item
.getItemId()) {
608 case android
.R
.id
.home
: {
609 if((mParents
.size() > 1)) {
615 retval
= super.onOptionsItemSelected(item
);