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 com
.owncloud
.android
.MainApp
;
30 import com
.owncloud
.android
.R
;
31 import com
.owncloud
.android
.authentication
.AccountAuthenticator
;
32 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
33 import com
.owncloud
.android
.datamodel
.OCFile
;
34 import com
.owncloud
.android
.files
.services
.FileUploader
;
35 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
37 import android
.accounts
.Account
;
38 import android
.accounts
.AccountManager
;
39 import android
.app
.AlertDialog
;
40 import android
.app
.AlertDialog
.Builder
;
41 import android
.app
.Dialog
;
42 import android
.app
.ProgressDialog
;
43 import android
.content
.Context
;
44 import android
.content
.DialogInterface
;
45 import android
.content
.DialogInterface
.OnCancelListener
;
46 import android
.content
.DialogInterface
.OnClickListener
;
47 import android
.content
.Intent
;
48 import android
.content
.SharedPreferences
;
49 import android
.database
.Cursor
;
50 import android
.net
.Uri
;
51 import android
.os
.Bundle
;
52 import android
.os
.Parcelable
;
53 import android
.preference
.PreferenceManager
;
54 import android
.provider
.MediaStore
.Audio
;
55 import android
.provider
.MediaStore
.Images
;
56 import android
.provider
.MediaStore
.Video
;
57 import android
.view
.View
;
58 import android
.widget
.AdapterView
;
59 import android
.widget
.AdapterView
.OnItemClickListener
;
60 import android
.widget
.Button
;
61 import android
.widget
.EditText
;
62 import android
.widget
.SimpleAdapter
;
63 import android
.widget
.Toast
;
65 import com
.actionbarsherlock
.app
.ActionBar
;
66 import com
.actionbarsherlock
.app
.SherlockListActivity
;
67 import com
.actionbarsherlock
.view
.MenuItem
;
68 import com
.owncloud
.android
.utils
.DisplayUtils
;
71 * This can be used to upload things to an ownCloud instance.
73 * @author Bartek Przybylski
76 public class Uploader
extends SherlockListActivity
implements OnItemClickListener
, android
.view
.View
.OnClickListener
{
77 private static final String TAG
= "ownCloudUploader";
79 private Account mAccount
;
80 private AccountManager mAccountManager
;
81 private Stack
<String
> mParents
;
82 private ArrayList
<Parcelable
> mStreamsToUpload
;
83 private boolean mCreateDir
;
84 private String mUploadPath
;
85 private FileDataStorageManager mStorageManager
;
88 private final static int DIALOG_NO_ACCOUNT
= 0;
89 private final static int DIALOG_WAITING
= 1;
90 private final static int DIALOG_NO_STREAM
= 2;
91 private final static int DIALOG_MULTIPLE_ACCOUNT
= 3;
93 private final static int REQUEST_CODE_SETUP_ACCOUNT
= 0;
96 protected void onCreate(Bundle savedInstanceState
) {
97 super.onCreate(savedInstanceState
);
98 mParents
= new Stack
<String
>();
100 ActionBar actionBar
= getSupportActionBar();
101 actionBar
.setIcon(DisplayUtils
.getSeasonalIconId());
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());
116 populateDirectoryList();
121 showDialog(DIALOG_NO_STREAM
);
126 protected Dialog
onCreateDialog(final int id
) {
127 final AlertDialog
.Builder builder
= new Builder(this);
130 ProgressDialog pDialog
= new ProgressDialog(this);
131 pDialog
.setIndeterminate(false
);
132 pDialog
.setCancelable(false
);
133 pDialog
.setMessage(getResources().getString(R
.string
.uploader_info_uploading
));
135 case DIALOG_NO_ACCOUNT
:
136 builder
.setIcon(android
.R
.drawable
.ic_dialog_alert
);
137 builder
.setTitle(R
.string
.uploader_wrn_no_account_title
);
138 builder
.setMessage(String
.format(getString(R
.string
.uploader_wrn_no_account_text
), getString(R
.string
.app_name
)));
139 builder
.setCancelable(false
);
140 builder
.setPositiveButton(R
.string
.uploader_wrn_no_account_setup_btn_text
, new OnClickListener() {
142 public void onClick(DialogInterface dialog
, int which
) {
143 if (android
.os
.Build
.VERSION
.SDK_INT
> android
.os
.Build
.VERSION_CODES
.ECLAIR_MR1
) {
144 // using string value since in API7 this
145 // constatn is not defined
146 // in API7 < this constatant is defined in
147 // Settings.ADD_ACCOUNT_SETTINGS
148 // and Settings.EXTRA_AUTHORITIES
149 Intent intent
= new Intent(android
.provider
.Settings
.ACTION_ADD_ACCOUNT
);
150 intent
.putExtra("authorities", new String
[] { MainApp
.getAuthTokenType() });
151 startActivityForResult(intent
, REQUEST_CODE_SETUP_ACCOUNT
);
153 // since in API7 there is no direct call for
154 // account setup, so we need to
155 // show our own AccountSetupAcricity, get
156 // desired results and setup
157 // everything for ourself
158 Intent intent
= new Intent(getBaseContext(), AccountAuthenticator
.class);
159 startActivityForResult(intent
, REQUEST_CODE_SETUP_ACCOUNT
);
163 builder
.setNegativeButton(R
.string
.uploader_wrn_no_account_quit_btn_text
, new OnClickListener() {
165 public void onClick(DialogInterface dialog
, int which
) {
169 return builder
.create();
170 case DIALOG_MULTIPLE_ACCOUNT
:
171 CharSequence ac
[] = new CharSequence
[mAccountManager
.getAccountsByType(MainApp
.getAccountType()).length
];
172 for (int i
= 0; i
< ac
.length
; ++i
) {
173 ac
[i
] = DisplayUtils
.convertIdn(mAccountManager
.getAccountsByType(MainApp
.getAccountType())[i
].name
, false
);
175 builder
.setTitle(R
.string
.common_choose_account
);
176 builder
.setItems(ac
, new OnClickListener() {
178 public void onClick(DialogInterface dialog
, int which
) {
179 mAccount
= mAccountManager
.getAccountsByType(MainApp
.getAccountType())[which
];
180 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
182 populateDirectoryList();
185 builder
.setCancelable(true
);
186 builder
.setOnCancelListener(new OnCancelListener() {
188 public void onCancel(DialogInterface dialog
) {
193 return builder
.create();
194 case DIALOG_NO_STREAM
:
195 builder
.setIcon(android
.R
.drawable
.ic_dialog_alert
);
196 builder
.setTitle(R
.string
.uploader_wrn_no_content_title
);
197 builder
.setMessage(R
.string
.uploader_wrn_no_content_text
);
198 builder
.setCancelable(false
);
199 builder
.setNegativeButton(R
.string
.common_cancel
, new OnClickListener() {
201 public void onClick(DialogInterface dialog
, int which
) {
205 return builder
.create();
207 throw new IllegalArgumentException("Unknown dialog id: " + id
);
211 class a
implements OnClickListener
{
215 public a(String path
, EditText dirname
) {
221 public void onClick(DialogInterface dialog
, int which
) {
222 Uploader
.this.mUploadPath
= mPath
+ mDirname
.getText().toString();
223 Uploader
.this.mCreateDir
= true
;
229 public void onBackPressed() {
231 if (mParents
.size() <= 1) {
232 super.onBackPressed();
236 populateDirectoryList();
241 public void onItemClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
242 // click on folder in the list
243 Log_OC
.d(TAG
, "on item click");
244 Vector
<OCFile
> tmpfiles
= mStorageManager
.getFolderContent(mFile
);
245 if (tmpfiles
.size() <= 0) return;
247 Vector
<OCFile
> files
= new Vector
<OCFile
>();
248 for (OCFile f
: tmpfiles
)
251 if (files
.size() < position
) {
252 throw new IndexOutOfBoundsException("Incorrect item selected");
254 mParents
.push(files
.get(position
).getFileName());
255 populateDirectoryList();
259 public void onClick(View v
) {
262 case R
.id
.uploader_choose_folder
:
263 mUploadPath
= ""; // first element in mParents is root dir, represented by ""; init mUploadPath with "/" results in a "//" prefix
264 for (String p
: mParents
)
265 mUploadPath
+= p
+ OCFile
.PATH_SEPARATOR
;
266 Log_OC
.d(TAG
, "Uploading file to dir " + mUploadPath
);
272 throw new IllegalArgumentException("Wrong element clicked");
277 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
278 super.onActivityResult(requestCode
, resultCode
, data
);
279 Log_OC
.i(TAG
, "result received. req: " + requestCode
+ " res: " + resultCode
);
280 if (requestCode
== REQUEST_CODE_SETUP_ACCOUNT
) {
281 dismissDialog(DIALOG_NO_ACCOUNT
);
282 if (resultCode
== RESULT_CANCELED
) {
285 Account
[] accounts
= mAccountManager
.getAccountsByType(MainApp
.getAuthTokenType());
286 if (accounts
.length
== 0) {
287 showDialog(DIALOG_NO_ACCOUNT
);
289 // there is no need for checking for is there more then one
290 // account at this point
291 // since account setup can set only one account at time
292 mAccount
= accounts
[0];
293 populateDirectoryList();
298 private void populateDirectoryList() {
299 setContentView(R
.layout
.uploader_layout
);
301 String current_dir
= mParents
.peek();
302 if(current_dir
.equals("")){
303 getSupportActionBar().setTitle(getString(R
.string
.default_display_name_for_root_folder
));
306 getSupportActionBar().setTitle(current_dir
);
308 boolean notRoot
= (mParents
.size() > 1);
309 ActionBar actionBar
= getSupportActionBar();
310 actionBar
.setDisplayHomeAsUpEnabled(notRoot
);
311 actionBar
.setHomeButtonEnabled(notRoot
);
313 String full_path
= generatePath(mParents
);
315 Log_OC
.d(TAG
, "Populating view with content of : " + full_path
);
317 mFile
= mStorageManager
.getFileByPath(full_path
);
319 Vector
<OCFile
> files
= mStorageManager
.getFolderContent(mFile
);
320 List
<HashMap
<String
, Object
>> data
= new LinkedList
<HashMap
<String
,Object
>>();
321 for (OCFile f
: files
) {
322 HashMap
<String
, Object
> h
= new HashMap
<String
, Object
>();
324 h
.put("dirname", f
.getFileName());
328 SimpleAdapter sa
= new SimpleAdapter(this,
330 R
.layout
.uploader_list_item_layout
,
331 new String
[] {"dirname"},
332 new int[] {R
.id
.textView1
});
334 Button btn
= (Button
) findViewById(R
.id
.uploader_choose_folder
);
335 btn
.setOnClickListener(this);
336 getListView().setOnItemClickListener(this);
340 private String
generatePath(Stack
<String
> dirs
) {
341 String full_path
= "";
343 for (String a
: dirs
)
344 full_path
+= a
+ "/";
348 private boolean prepareStreamsToUpload() {
349 if (getIntent().getAction().equals(Intent
.ACTION_SEND
)) {
350 mStreamsToUpload
= new ArrayList
<Parcelable
>();
351 mStreamsToUpload
.add(getIntent().getParcelableExtra(Intent
.EXTRA_STREAM
));
352 } else if (getIntent().getAction().equals(Intent
.ACTION_SEND_MULTIPLE
)) {
353 mStreamsToUpload
= getIntent().getParcelableArrayListExtra(Intent
.EXTRA_STREAM
);
355 return (mStreamsToUpload
!= null
&& mStreamsToUpload
.get(0) != null
);
358 public void uploadFiles() {
361 ArrayList
<String
> local
= new ArrayList
<String
>();
362 ArrayList
<String
> remote
= new ArrayList
<String
>();
364 // this checks the mimeType
365 for (Parcelable mStream
: mStreamsToUpload
) {
367 Uri uri
= (Uri
) mStream
;
369 if (uri
.getScheme().equals("content")) {
371 String mimeType
= getContentResolver().getType(uri
);
373 if (mimeType
.contains("image")) {
374 String
[] CONTENT_PROJECTION
= { Images
.Media
.DATA
, Images
.Media
.DISPLAY_NAME
, Images
.Media
.MIME_TYPE
, Images
.Media
.SIZE
};
375 Cursor c
= getContentResolver().query(uri
, CONTENT_PROJECTION
, null
, null
, null
);
377 int index
= c
.getColumnIndex(Images
.Media
.DATA
);
378 String data
= c
.getString(index
);
380 remote
.add(mUploadPath
+ c
.getString(c
.getColumnIndex(Images
.Media
.DISPLAY_NAME
)));
383 else if (mimeType
.contains("video")) {
384 String
[] CONTENT_PROJECTION
= { Video
.Media
.DATA
, Video
.Media
.DISPLAY_NAME
, Video
.Media
.MIME_TYPE
, Video
.Media
.SIZE
, Video
.Media
.DATE_MODIFIED
};
385 Cursor c
= getContentResolver().query(uri
, CONTENT_PROJECTION
, null
, null
, null
);
387 int index
= c
.getColumnIndex(Video
.Media
.DATA
);
388 String data
= c
.getString(index
);
390 remote
.add(mUploadPath
+ c
.getString(c
.getColumnIndex(Video
.Media
.DISPLAY_NAME
)));
393 else if (mimeType
.contains("audio")) {
394 String
[] CONTENT_PROJECTION
= { Audio
.Media
.DATA
, Audio
.Media
.DISPLAY_NAME
, Audio
.Media
.MIME_TYPE
, Audio
.Media
.SIZE
};
395 Cursor c
= getContentResolver().query(uri
, CONTENT_PROJECTION
, null
, null
, null
);
397 int index
= c
.getColumnIndex(Audio
.Media
.DATA
);
398 String data
= c
.getString(index
);
400 remote
.add(mUploadPath
+ c
.getString(c
.getColumnIndex(Audio
.Media
.DISPLAY_NAME
)));
404 String filePath
= Uri
.decode(uri
.toString()).replace(uri
.getScheme() + "://", "");
405 // cut everything whats before mnt. It occured to me that sometimes apps send their name into the URI
406 if (filePath
.contains("mnt")) {
407 String splitedFilePath
[] = filePath
.split("/mnt");
408 filePath
= splitedFilePath
[1];
410 final File file
= new File(filePath
);
411 local
.add(file
.getAbsolutePath());
412 remote
.add(mUploadPath
+ file
.getName());
415 } else if (uri
.getScheme().equals("file")) {
416 String filePath
= Uri
.decode(uri
.toString()).replace(uri
.getScheme() + "://", "");
417 if (filePath
.contains("mnt")) {
418 String splitedFilePath
[] = filePath
.split("/mnt");
419 filePath
= splitedFilePath
[1];
421 final File file
= new File(filePath
);
422 local
.add(file
.getAbsolutePath());
423 remote
.add(mUploadPath
+ file
.getName());
426 throw new SecurityException();
430 throw new SecurityException();
433 Intent intent
= new Intent(getApplicationContext(), FileUploader
.class);
434 intent
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
435 intent
.putExtra(FileUploader
.KEY_LOCAL_FILE
, local
.toArray(new String
[local
.size()]));
436 intent
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remote
.toArray(new String
[remote
.size()]));
437 intent
.putExtra(FileUploader
.KEY_ACCOUNT
, mAccount
);
438 startService(intent
);
440 //Save the path to shared preferences
441 SharedPreferences
.Editor appPrefs
= PreferenceManager
442 .getDefaultSharedPreferences(getApplicationContext()).edit();
443 appPrefs
.putString("last_upload_path", mUploadPath
);
449 } catch (SecurityException e
) {
450 String message
= String
.format(getString(R
.string
.uploader_error_forbidden_content
), getString(R
.string
.app_name
));
451 Toast
.makeText(this, message
, Toast
.LENGTH_LONG
).show();
456 * Loads the target folder initialize shown to the user.
458 * The target account has to be chosen before this method is called.
460 private void initTargetFolder() {
461 if (mStorageManager
== null
) {
462 throw new IllegalStateException("Do not call this method before initializing mStorageManager");
465 SharedPreferences appPreferences
= PreferenceManager
466 .getDefaultSharedPreferences(getApplicationContext());
468 String last_path
= appPreferences
.getString("last_upload_path", "");
469 // "/" equals root-directory
470 if(last_path
.equals("/")) {
474 String
[] dir_names
= last_path
.split("/");
475 for (String dir
: dir_names
)
478 //Make sure that path still exists, if it doesn't pop the stack and try the previous path
479 while(!mStorageManager
.fileExists(generatePath(mParents
)) && mParents
.size() > 1){
486 public boolean onOptionsItemSelected(MenuItem item
) {
487 boolean retval
= true
;
488 switch (item
.getItemId()) {
489 case android
.R
.id
.home
: {
490 if((mParents
.size() > 1)) {
496 retval
= super.onOptionsItemSelected(item
);