1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
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/>.
19 package com
.owncloud
.android
.ui
.activity
;
22 import java
.util
.ArrayList
;
23 import java
.util
.HashMap
;
24 import java
.util
.LinkedList
;
25 import java
.util
.List
;
26 import java
.util
.Stack
;
27 import java
.util
.Vector
;
29 import android
.accounts
.Account
;
30 import android
.accounts
.AccountManager
;
31 import android
.app
.AlertDialog
;
32 import android
.app
.AlertDialog
.Builder
;
33 import android
.app
.Dialog
;
34 import android
.app
.ProgressDialog
;
35 import android
.content
.Context
;
36 import android
.content
.DialogInterface
;
37 import android
.content
.DialogInterface
.OnCancelListener
;
38 import android
.content
.DialogInterface
.OnClickListener
;
39 import android
.content
.Intent
;
40 import android
.content
.res
.Resources
.NotFoundException
;
41 import android
.database
.Cursor
;
42 import android
.net
.Uri
;
43 import android
.os
.Bundle
;
44 import android
.os
.Parcelable
;
45 import android
.provider
.MediaStore
.Audio
;
46 import android
.provider
.MediaStore
.Images
;
47 import android
.provider
.MediaStore
.Video
;
48 import android
.view
.View
;
49 import android
.view
.Window
;
50 import android
.widget
.AdapterView
;
51 import android
.widget
.AdapterView
.OnItemClickListener
;
52 import android
.widget
.Button
;
53 import android
.widget
.EditText
;
54 import android
.widget
.ListView
;
55 import android
.widget
.SimpleAdapter
;
56 import android
.widget
.Toast
;
58 import com
.owncloud
.android
.MainApp
;
59 import com
.owncloud
.android
.R
;
60 import com
.owncloud
.android
.authentication
.AccountAuthenticator
;
61 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
62 import com
.owncloud
.android
.datamodel
.OCFile
;
63 import com
.owncloud
.android
.files
.services
.FileUploader
;
64 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
65 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
66 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
67 import com
.owncloud
.android
.operations
.CreateFolderOperation
;
68 import com
.owncloud
.android
.ui
.dialog
.CreateFolderDialogFragment
;
69 import com
.owncloud
.android
.utils
.ErrorMessageAdapter
;
73 * This can be used to upload things to an ownCloud instance.
75 * @author Bartek Przybylski
78 public class Uploader
extends FileActivity
implements OnItemClickListener
, android
.view
.View
.OnClickListener
{
79 private static final String TAG
= "ownCloudUploader";
81 private Account mAccount
;
82 private AccountManager mAccountManager
;
83 private Stack
<String
> mParents
;
84 private ArrayList
<Parcelable
> mStreamsToUpload
;
85 private boolean mCreateDir
;
86 private String mUploadPath
;
87 private FileDataStorageManager mStorageManager
;
90 private final static int DIALOG_NO_ACCOUNT
= 0;
91 private final static int DIALOG_WAITING
= 1;
92 private final static int DIALOG_NO_STREAM
= 2;
93 private final static int DIALOG_MULTIPLE_ACCOUNT
= 3;
95 private final static int REQUEST_CODE_SETUP_ACCOUNT
= 0;
98 protected void onCreate(Bundle savedInstanceState
) {
99 super.onCreate(savedInstanceState
);
100 getWindow().requestFeature(Window
.FEATURE_NO_TITLE
);
101 mParents
= new Stack
<String
>();
103 if (prepareStreamsToUpload()) {
104 mAccountManager
= (AccountManager
) getSystemService(Context
.ACCOUNT_SERVICE
);
105 Account
[] accounts
= mAccountManager
.getAccountsByType(MainApp
.getAccountType());
106 if (accounts
.length
== 0) {
107 Log_OC
.i(TAG
, "No ownCloud account is available");
108 showDialog(DIALOG_NO_ACCOUNT
);
109 } else if (accounts
.length
> 1) {
110 Log_OC
.i(TAG
, "More then one ownCloud is available");
111 showDialog(DIALOG_MULTIPLE_ACCOUNT
);
113 mAccount
= accounts
[0];
114 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
115 populateDirectoryList();
118 showDialog(DIALOG_NO_STREAM
);
123 protected Dialog
onCreateDialog(final int id
) {
124 final AlertDialog
.Builder builder
= new Builder(this);
127 ProgressDialog pDialog
= new ProgressDialog(this);
128 pDialog
.setIndeterminate(false
);
129 pDialog
.setCancelable(false
);
130 pDialog
.setMessage(getResources().getString(R
.string
.uploader_info_uploading
));
132 case DIALOG_NO_ACCOUNT
:
133 builder
.setIcon(android
.R
.drawable
.ic_dialog_alert
);
134 builder
.setTitle(R
.string
.uploader_wrn_no_account_title
);
135 builder
.setMessage(String
.format(getString(R
.string
.uploader_wrn_no_account_text
), getString(R
.string
.app_name
)));
136 builder
.setCancelable(false
);
137 builder
.setPositiveButton(R
.string
.uploader_wrn_no_account_setup_btn_text
, new OnClickListener() {
139 public void onClick(DialogInterface dialog
, int which
) {
140 if (android
.os
.Build
.VERSION
.SDK_INT
> android
.os
.Build
.VERSION_CODES
.ECLAIR_MR1
) {
141 // using string value since in API7 this
142 // constatn is not defined
143 // in API7 < this constatant is defined in
144 // Settings.ADD_ACCOUNT_SETTINGS
145 // and Settings.EXTRA_AUTHORITIES
146 Intent intent
= new Intent(android
.provider
.Settings
.ACTION_ADD_ACCOUNT
);
147 intent
.putExtra("authorities", new String
[] { MainApp
.getAuthTokenType() });
148 startActivityForResult(intent
, REQUEST_CODE_SETUP_ACCOUNT
);
150 // since in API7 there is no direct call for
151 // account setup, so we need to
152 // show our own AccountSetupAcricity, get
153 // desired results and setup
154 // everything for ourself
155 Intent intent
= new Intent(getBaseContext(), AccountAuthenticator
.class);
156 startActivityForResult(intent
, REQUEST_CODE_SETUP_ACCOUNT
);
160 builder
.setNegativeButton(R
.string
.uploader_wrn_no_account_quit_btn_text
, new OnClickListener() {
162 public void onClick(DialogInterface dialog
, int which
) {
166 return builder
.create();
167 case DIALOG_MULTIPLE_ACCOUNT
:
168 CharSequence ac
[] = new CharSequence
[mAccountManager
.getAccountsByType(MainApp
.getAccountType()).length
];
169 for (int i
= 0; i
< ac
.length
; ++i
) {
170 ac
[i
] = mAccountManager
.getAccountsByType(MainApp
.getAccountType())[i
].name
;
172 builder
.setTitle(R
.string
.common_choose_account
);
173 builder
.setItems(ac
, new OnClickListener() {
175 public void onClick(DialogInterface dialog
, int which
) {
176 mAccount
= mAccountManager
.getAccountsByType(MainApp
.getAccountType())[which
];
177 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
178 populateDirectoryList();
181 builder
.setCancelable(true
);
182 builder
.setOnCancelListener(new OnCancelListener() {
184 public void onCancel(DialogInterface dialog
) {
189 return builder
.create();
190 case DIALOG_NO_STREAM
:
191 builder
.setIcon(android
.R
.drawable
.ic_dialog_alert
);
192 builder
.setTitle(R
.string
.uploader_wrn_no_content_title
);
193 builder
.setMessage(R
.string
.uploader_wrn_no_content_text
);
194 builder
.setCancelable(false
);
195 builder
.setNegativeButton(R
.string
.common_cancel
, new OnClickListener() {
197 public void onClick(DialogInterface dialog
, int which
) {
201 return builder
.create();
203 throw new IllegalArgumentException("Unknown dialog id: " + id
);
207 class a
implements OnClickListener
{
211 public a(String path
, EditText dirname
) {
217 public void onClick(DialogInterface dialog
, int which
) {
218 Uploader
.this.mUploadPath
= mPath
+ mDirname
.getText().toString();
219 Uploader
.this.mCreateDir
= true
;
225 public void onBackPressed() {
227 if (mParents
.size() <= 1) {
228 super.onBackPressed();
232 populateDirectoryList();
237 public void onItemClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
238 // click on folder in the list
239 Log_OC
.d(TAG
, "on item click");
240 Vector
<OCFile
> tmpfiles
= mStorageManager
.getFolderContent(mFile
);
241 if (tmpfiles
.size() <= 0) return;
243 Vector
<OCFile
> files
= new Vector
<OCFile
>();
244 for (OCFile f
: tmpfiles
)
247 if (files
.size() < position
) {
248 throw new IndexOutOfBoundsException("Incorrect item selected");
250 mParents
.push(files
.get(position
).getFileName());
251 populateDirectoryList();
255 public void onClick(View v
) {
258 case R
.id
.uploader_choose_folder
:
259 mUploadPath
= ""; // first element in mParents is root dir, represented by ""; init mUploadPath with "/" results in a "//" prefix
260 for (String p
: mParents
)
261 mUploadPath
+= p
+ OCFile
.PATH_SEPARATOR
;
262 Log_OC
.d(TAG
, "Uploading file to dir " + mUploadPath
);
268 case R
.id
.uploader_new_folder
:
269 CreateFolderDialogFragment dialog
= CreateFolderDialogFragment
.newInstance(mFile
);
270 dialog
.show(getSupportFragmentManager(), "createdirdialog");
275 throw new IllegalArgumentException("Wrong element clicked");
280 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
281 super.onActivityResult(requestCode
, resultCode
, data
);
282 Log_OC
.i(TAG
, "result received. req: " + requestCode
+ " res: " + resultCode
);
283 if (requestCode
== REQUEST_CODE_SETUP_ACCOUNT
) {
284 dismissDialog(DIALOG_NO_ACCOUNT
);
285 if (resultCode
== RESULT_CANCELED
) {
288 Account
[] accounts
= mAccountManager
.getAccountsByType(MainApp
.getAuthTokenType());
289 if (accounts
.length
== 0) {
290 showDialog(DIALOG_NO_ACCOUNT
);
292 // there is no need for checking for is there more then one
293 // account at this point
294 // since account setup can set only one account at time
295 mAccount
= accounts
[0];
296 populateDirectoryList();
301 private void populateDirectoryList() {
302 setContentView(R
.layout
.uploader_layout
);
304 ListView mListView
= (ListView
) findViewById(android
.R
.id
.list
);
306 String full_path
= "";
307 for (String a
: mParents
)
308 full_path
+= a
+ "/";
310 Log_OC
.d(TAG
, "Populating view with content of : " + full_path
);
312 mFile
= mStorageManager
.getFileByPath(full_path
);
314 Vector
<OCFile
> files
= mStorageManager
.getFolderContent(mFile
);
315 List
<HashMap
<String
, Object
>> data
= new LinkedList
<HashMap
<String
,Object
>>();
316 for (OCFile f
: files
) {
317 HashMap
<String
, Object
> h
= new HashMap
<String
, Object
>();
319 h
.put("dirname", f
.getFileName());
323 SimpleAdapter sa
= new SimpleAdapter(this,
325 R
.layout
.uploader_list_item_layout
,
326 new String
[] {"dirname"},
327 new int[] {R
.id
.textView1
});
329 mListView
.setAdapter(sa
);
330 Button btnChooseFolder
= (Button
) findViewById(R
.id
.uploader_choose_folder
);
331 btnChooseFolder
.setOnClickListener(this);
333 Button btnNewFolder
= (Button
) findViewById(R
.id
.uploader_new_folder
);
334 btnNewFolder
.setOnClickListener(this);
336 mListView
.setOnItemClickListener(this);
340 private boolean prepareStreamsToUpload() {
341 if (getIntent().getAction().equals(Intent
.ACTION_SEND
)) {
342 mStreamsToUpload
= new ArrayList
<Parcelable
>();
343 mStreamsToUpload
.add(getIntent().getParcelableExtra(Intent
.EXTRA_STREAM
));
344 } else if (getIntent().getAction().equals(Intent
.ACTION_SEND_MULTIPLE
)) {
345 mStreamsToUpload
= getIntent().getParcelableArrayListExtra(Intent
.EXTRA_STREAM
);
347 return (mStreamsToUpload
!= null
&& mStreamsToUpload
.get(0) != null
);
350 public void uploadFiles() {
353 ArrayList
<String
> local
= new ArrayList
<String
>();
354 ArrayList
<String
> remote
= new ArrayList
<String
>();
356 // this checks the mimeType
357 for (Parcelable mStream
: mStreamsToUpload
) {
359 Uri uri
= (Uri
) mStream
;
361 if (uri
.getScheme().equals("content")) {
363 String mimeType
= getContentResolver().getType(uri
);
365 if (mimeType
.contains("image")) {
366 String
[] CONTENT_PROJECTION
= { Images
.Media
.DATA
, Images
.Media
.DISPLAY_NAME
, Images
.Media
.MIME_TYPE
, Images
.Media
.SIZE
};
367 Cursor c
= getContentResolver().query(uri
, CONTENT_PROJECTION
, null
, null
, null
);
369 int index
= c
.getColumnIndex(Images
.Media
.DATA
);
370 String data
= c
.getString(index
);
372 remote
.add(mUploadPath
+ c
.getString(c
.getColumnIndex(Images
.Media
.DISPLAY_NAME
)));
375 else if (mimeType
.contains("video")) {
376 String
[] CONTENT_PROJECTION
= { Video
.Media
.DATA
, Video
.Media
.DISPLAY_NAME
, Video
.Media
.MIME_TYPE
, Video
.Media
.SIZE
, Video
.Media
.DATE_MODIFIED
};
377 Cursor c
= getContentResolver().query(uri
, CONTENT_PROJECTION
, null
, null
, null
);
379 int index
= c
.getColumnIndex(Video
.Media
.DATA
);
380 String data
= c
.getString(index
);
382 remote
.add(mUploadPath
+ c
.getString(c
.getColumnIndex(Video
.Media
.DISPLAY_NAME
)));
385 else if (mimeType
.contains("audio")) {
386 String
[] CONTENT_PROJECTION
= { Audio
.Media
.DATA
, Audio
.Media
.DISPLAY_NAME
, Audio
.Media
.MIME_TYPE
, Audio
.Media
.SIZE
};
387 Cursor c
= getContentResolver().query(uri
, CONTENT_PROJECTION
, null
, null
, null
);
389 int index
= c
.getColumnIndex(Audio
.Media
.DATA
);
390 String data
= c
.getString(index
);
392 remote
.add(mUploadPath
+ c
.getString(c
.getColumnIndex(Audio
.Media
.DISPLAY_NAME
)));
396 String filePath
= Uri
.decode(uri
.toString()).replace(uri
.getScheme() + "://", "");
397 // cut everything whats before mnt. It occured to me that sometimes apps send their name into the URI
398 if (filePath
.contains("mnt")) {
399 String splitedFilePath
[] = filePath
.split("/mnt");
400 filePath
= splitedFilePath
[1];
402 final File file
= new File(filePath
);
403 local
.add(file
.getAbsolutePath());
404 remote
.add(mUploadPath
+ file
.getName());
407 } else if (uri
.getScheme().equals("file")) {
408 String filePath
= Uri
.decode(uri
.toString()).replace(uri
.getScheme() + "://", "");
409 if (filePath
.contains("mnt")) {
410 String splitedFilePath
[] = filePath
.split("/mnt");
411 filePath
= splitedFilePath
[1];
413 final File file
= new File(filePath
);
414 local
.add(file
.getAbsolutePath());
415 remote
.add(mUploadPath
+ file
.getName());
418 throw new SecurityException();
422 throw new SecurityException();
425 Intent intent
= new Intent(getApplicationContext(), FileUploader
.class);
426 intent
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
427 intent
.putExtra(FileUploader
.KEY_LOCAL_FILE
, local
.toArray(new String
[local
.size()]));
428 intent
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remote
.toArray(new String
[remote
.size()]));
429 intent
.putExtra(FileUploader
.KEY_ACCOUNT
, mAccount
);
430 startService(intent
);
434 } catch (SecurityException e
) {
435 String message
= String
.format(getString(R
.string
.uploader_error_forbidden_content
), getString(R
.string
.app_name
));
436 Toast
.makeText(this, message
, Toast
.LENGTH_LONG
).show();
441 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
442 super.onRemoteOperationFinish(operation
, result
);
445 if (operation
instanceof CreateFolderOperation
) {
446 onCreateFolderOperationFinish((CreateFolderOperation
)operation
, result
);
452 * Updates the view associated to the activity after the finish of an operation trying create a new folder
454 * @param operation Creation operation performed.
455 * @param result Result of the creation.
457 private void onCreateFolderOperationFinish(CreateFolderOperation operation
, RemoteOperationResult result
) {
458 if (result
.isSuccess()) {
459 dismissLoadingDialog();
460 populateDirectoryList();
462 dismissLoadingDialog();
464 Toast msg
= Toast
.makeText(this,
465 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
469 } catch (NotFoundException e
) {
470 Log_OC
.e(TAG
, "Error while trying to show fail message " , e
);