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
.owncloud
.android
.MainApp
;
68 import com
.owncloud
.android
.R
;
69 import com
.owncloud
.android
.authentication
.AccountAuthenticator
;
70 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
71 import com
.owncloud
.android
.datamodel
.OCFile
;
72 import com
.owncloud
.android
.files
.services
.FileUploader
;
73 import com
.owncloud
.android
.utils
.DisplayUtils
;
74 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
77 import java
.util
.ArrayList
;
78 import java
.util
.HashMap
;
79 import java
.util
.LinkedList
;
80 import java
.util
.List
;
81 import java
.util
.Stack
;
82 import java
.util
.Vector
;
86 * This can be used to upload things to an ownCloud instance.
88 * @author Bartek Przybylski
91 public class Uploader
extends SherlockListActivity
implements OnItemClickListener
, android
.view
.View
.OnClickListener
{
92 private static final String TAG
= "ownCloudUploader";
94 private Account mAccount
;
95 private AccountManager mAccountManager
;
96 private Stack
<String
> mParents
;
97 private ArrayList
<Parcelable
> mStreamsToUpload
;
98 private boolean mCreateDir
;
99 private String mUploadPath
;
100 private FileDataStorageManager mStorageManager
;
101 private OCFile mFile
;
103 private final static int DIALOG_NO_ACCOUNT
= 0;
104 private final static int DIALOG_WAITING
= 1;
105 private final static int DIALOG_NO_STREAM
= 2;
106 private final static int DIALOG_MULTIPLE_ACCOUNT
= 3;
108 private final static int REQUEST_CODE_SETUP_ACCOUNT
= 0;
111 protected void onCreate(Bundle savedInstanceState
) {
112 super.onCreate(savedInstanceState
);
113 mParents
= new Stack
<String
>();
115 ActionBar actionBar
= getSupportActionBar();
116 actionBar
.setIcon(DisplayUtils
.getSeasonalIconId());
118 if (prepareStreamsToUpload()) {
119 mAccountManager
= (AccountManager
) getSystemService(Context
.ACCOUNT_SERVICE
);
120 Account
[] accounts
= mAccountManager
.getAccountsByType(MainApp
.getAccountType());
121 if (accounts
.length
== 0) {
122 Log_OC
.i(TAG
, "No ownCloud account is available");
123 showDialog(DIALOG_NO_ACCOUNT
);
124 } else if (accounts
.length
> 1) {
125 Log_OC
.i(TAG
, "More then one ownCloud is available");
126 showDialog(DIALOG_MULTIPLE_ACCOUNT
);
128 mAccount
= accounts
[0];
129 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
131 populateDirectoryList();
136 showDialog(DIALOG_NO_STREAM
);
141 protected Dialog
onCreateDialog(final int id
) {
142 final AlertDialog
.Builder builder
= new Builder(this);
145 ProgressDialog pDialog
= new ProgressDialog(this);
146 pDialog
.setIndeterminate(false
);
147 pDialog
.setCancelable(false
);
148 pDialog
.setMessage(getResources().getString(R
.string
.uploader_info_uploading
));
150 case DIALOG_NO_ACCOUNT
:
151 builder
.setIcon(android
.R
.drawable
.ic_dialog_alert
);
152 builder
.setTitle(R
.string
.uploader_wrn_no_account_title
);
153 builder
.setMessage(String
.format(getString(R
.string
.uploader_wrn_no_account_text
), getString(R
.string
.app_name
)));
154 builder
.setCancelable(false
);
155 builder
.setPositiveButton(R
.string
.uploader_wrn_no_account_setup_btn_text
, new OnClickListener() {
157 public void onClick(DialogInterface dialog
, int which
) {
158 if (android
.os
.Build
.VERSION
.SDK_INT
> android
.os
.Build
.VERSION_CODES
.ECLAIR_MR1
) {
159 // using string value since in API7 this
160 // constatn is not defined
161 // in API7 < this constatant is defined in
162 // Settings.ADD_ACCOUNT_SETTINGS
163 // and Settings.EXTRA_AUTHORITIES
164 Intent intent
= new Intent(android
.provider
.Settings
.ACTION_ADD_ACCOUNT
);
165 intent
.putExtra("authorities", new String
[] { MainApp
.getAuthTokenType() });
166 startActivityForResult(intent
, REQUEST_CODE_SETUP_ACCOUNT
);
168 // since in API7 there is no direct call for
169 // account setup, so we need to
170 // show our own AccountSetupAcricity, get
171 // desired results and setup
172 // everything for ourself
173 Intent intent
= new Intent(getBaseContext(), AccountAuthenticator
.class);
174 startActivityForResult(intent
, REQUEST_CODE_SETUP_ACCOUNT
);
178 builder
.setNegativeButton(R
.string
.uploader_wrn_no_account_quit_btn_text
, new OnClickListener() {
180 public void onClick(DialogInterface dialog
, int which
) {
184 return builder
.create();
185 case DIALOG_MULTIPLE_ACCOUNT
:
186 CharSequence ac
[] = new CharSequence
[mAccountManager
.getAccountsByType(MainApp
.getAccountType()).length
];
187 for (int i
= 0; i
< ac
.length
; ++i
) {
188 ac
[i
] = mAccountManager
.getAccountsByType(MainApp
.getAccountType())[i
].name
;
190 builder
.setTitle(R
.string
.common_choose_account
);
191 builder
.setItems(ac
, new OnClickListener() {
193 public void onClick(DialogInterface dialog
, int which
) {
194 mAccount
= mAccountManager
.getAccountsByType(MainApp
.getAccountType())[which
];
195 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
197 populateDirectoryList();
200 builder
.setCancelable(true
);
201 builder
.setOnCancelListener(new OnCancelListener() {
203 public void onCancel(DialogInterface dialog
) {
208 return builder
.create();
209 case DIALOG_NO_STREAM
:
210 builder
.setIcon(android
.R
.drawable
.ic_dialog_alert
);
211 builder
.setTitle(R
.string
.uploader_wrn_no_content_title
);
212 builder
.setMessage(R
.string
.uploader_wrn_no_content_text
);
213 builder
.setCancelable(false
);
214 builder
.setNegativeButton(R
.string
.common_cancel
, new OnClickListener() {
216 public void onClick(DialogInterface dialog
, int which
) {
220 return builder
.create();
222 throw new IllegalArgumentException("Unknown dialog id: " + id
);
226 class a
implements OnClickListener
{
230 public a(String path
, EditText dirname
) {
236 public void onClick(DialogInterface dialog
, int which
) {
237 Uploader
.this.mUploadPath
= mPath
+ mDirname
.getText().toString();
238 Uploader
.this.mCreateDir
= true
;
244 public void onBackPressed() {
246 if (mParents
.size() <= 1) {
247 super.onBackPressed();
251 populateDirectoryList();
256 public void onItemClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
257 // click on folder in the list
258 Log_OC
.d(TAG
, "on item click");
259 Vector
<OCFile
> tmpfiles
= mStorageManager
.getFolderContent(mFile
);
260 if (tmpfiles
.size() <= 0) return;
262 Vector
<OCFile
> files
= new Vector
<OCFile
>();
263 for (OCFile f
: tmpfiles
)
266 if (files
.size() < position
) {
267 throw new IndexOutOfBoundsException("Incorrect item selected");
269 mParents
.push(files
.get(position
).getFileName());
270 populateDirectoryList();
274 public void onClick(View v
) {
277 case R
.id
.uploader_choose_folder
:
278 mUploadPath
= ""; // first element in mParents is root dir, represented by ""; init mUploadPath with "/" results in a "//" prefix
279 for (String p
: mParents
)
280 mUploadPath
+= p
+ OCFile
.PATH_SEPARATOR
;
281 Log_OC
.d(TAG
, "Uploading file to dir " + mUploadPath
);
287 throw new IllegalArgumentException("Wrong element clicked");
292 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
293 super.onActivityResult(requestCode
, resultCode
, data
);
294 Log_OC
.i(TAG
, "result received. req: " + requestCode
+ " res: " + resultCode
);
295 if (requestCode
== REQUEST_CODE_SETUP_ACCOUNT
) {
296 dismissDialog(DIALOG_NO_ACCOUNT
);
297 if (resultCode
== RESULT_CANCELED
) {
300 Account
[] accounts
= mAccountManager
.getAccountsByType(MainApp
.getAuthTokenType());
301 if (accounts
.length
== 0) {
302 showDialog(DIALOG_NO_ACCOUNT
);
304 // there is no need for checking for is there more then one
305 // account at this point
306 // since account setup can set only one account at time
307 mAccount
= accounts
[0];
308 populateDirectoryList();
313 private void populateDirectoryList() {
314 setContentView(R
.layout
.uploader_layout
);
316 String current_dir
= mParents
.peek();
317 if(current_dir
.equals("")){
318 getSupportActionBar().setTitle(getString(R
.string
.default_display_name_for_root_folder
));
321 getSupportActionBar().setTitle(current_dir
);
324 String full_path
= generatePath(mParents
);
326 Log_OC
.d(TAG
, "Populating view with content of : " + full_path
);
328 mFile
= mStorageManager
.getFileByPath(full_path
);
330 Vector
<OCFile
> files
= mStorageManager
.getFolderContent(mFile
);
331 List
<HashMap
<String
, Object
>> data
= new LinkedList
<HashMap
<String
,Object
>>();
332 for (OCFile f
: files
) {
333 HashMap
<String
, Object
> h
= new HashMap
<String
, Object
>();
335 h
.put("dirname", f
.getFileName());
339 SimpleAdapter sa
= new SimpleAdapter(this,
341 R
.layout
.uploader_list_item_layout
,
342 new String
[] {"dirname"},
343 new int[] {R
.id
.textView1
});
345 Button btn
= (Button
) findViewById(R
.id
.uploader_choose_folder
);
346 btn
.setOnClickListener(this);
347 getListView().setOnItemClickListener(this);
351 private String
generatePath(Stack
<String
> dirs
) {
352 String full_path
= "";
354 for (String a
: dirs
)
355 full_path
+= a
+ "/";
359 private boolean prepareStreamsToUpload() {
360 if (getIntent().getAction().equals(Intent
.ACTION_SEND
)) {
361 mStreamsToUpload
= new ArrayList
<Parcelable
>();
362 mStreamsToUpload
.add(getIntent().getParcelableExtra(Intent
.EXTRA_STREAM
));
363 } else if (getIntent().getAction().equals(Intent
.ACTION_SEND_MULTIPLE
)) {
364 mStreamsToUpload
= getIntent().getParcelableArrayListExtra(Intent
.EXTRA_STREAM
);
366 return (mStreamsToUpload
!= null
&& mStreamsToUpload
.get(0) != null
);
369 public void uploadFiles() {
372 ArrayList
<String
> local
= new ArrayList
<String
>();
373 ArrayList
<String
> remote
= new ArrayList
<String
>();
375 // this checks the mimeType
376 for (Parcelable mStream
: mStreamsToUpload
) {
378 Uri uri
= (Uri
) mStream
;
380 if (uri
.getScheme().equals("content")) {
382 String mimeType
= getContentResolver().getType(uri
);
384 if (mimeType
.contains("image")) {
385 String
[] CONTENT_PROJECTION
= { Images
.Media
.DATA
, Images
.Media
.DISPLAY_NAME
, Images
.Media
.MIME_TYPE
, Images
.Media
.SIZE
};
386 Cursor c
= getContentResolver().query(uri
, CONTENT_PROJECTION
, null
, null
, null
);
388 int index
= c
.getColumnIndex(Images
.Media
.DATA
);
389 String data
= c
.getString(index
);
391 remote
.add(mUploadPath
+ c
.getString(c
.getColumnIndex(Images
.Media
.DISPLAY_NAME
)));
394 else if (mimeType
.contains("video")) {
395 String
[] CONTENT_PROJECTION
= { Video
.Media
.DATA
, Video
.Media
.DISPLAY_NAME
, Video
.Media
.MIME_TYPE
, Video
.Media
.SIZE
, Video
.Media
.DATE_MODIFIED
};
396 Cursor c
= getContentResolver().query(uri
, CONTENT_PROJECTION
, null
, null
, null
);
398 int index
= c
.getColumnIndex(Video
.Media
.DATA
);
399 String data
= c
.getString(index
);
401 remote
.add(mUploadPath
+ c
.getString(c
.getColumnIndex(Video
.Media
.DISPLAY_NAME
)));
404 else if (mimeType
.contains("audio")) {
405 String
[] CONTENT_PROJECTION
= { Audio
.Media
.DATA
, Audio
.Media
.DISPLAY_NAME
, Audio
.Media
.MIME_TYPE
, Audio
.Media
.SIZE
};
406 Cursor c
= getContentResolver().query(uri
, CONTENT_PROJECTION
, null
, null
, null
);
408 int index
= c
.getColumnIndex(Audio
.Media
.DATA
);
409 String data
= c
.getString(index
);
411 remote
.add(mUploadPath
+ c
.getString(c
.getColumnIndex(Audio
.Media
.DISPLAY_NAME
)));
415 String filePath
= Uri
.decode(uri
.toString()).replace(uri
.getScheme() + "://", "");
416 // cut everything whats before mnt. It occured to me that sometimes apps send their name into the URI
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 } else if (uri
.getScheme().equals("file")) {
427 String filePath
= Uri
.decode(uri
.toString()).replace(uri
.getScheme() + "://", "");
428 if (filePath
.contains("mnt")) {
429 String splitedFilePath
[] = filePath
.split("/mnt");
430 filePath
= splitedFilePath
[1];
432 final File file
= new File(filePath
);
433 local
.add(file
.getAbsolutePath());
434 remote
.add(mUploadPath
+ file
.getName());
437 throw new SecurityException();
441 throw new SecurityException();
444 Intent intent
= new Intent(getApplicationContext(), FileUploader
.class);
445 intent
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
446 intent
.putExtra(FileUploader
.KEY_LOCAL_FILE
, local
.toArray(new String
[local
.size()]));
447 intent
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remote
.toArray(new String
[remote
.size()]));
448 intent
.putExtra(FileUploader
.KEY_ACCOUNT
, mAccount
);
449 startService(intent
);
451 //Save the path to shared preferences
452 SharedPreferences
.Editor appPrefs
= PreferenceManager
453 .getDefaultSharedPreferences(getApplicationContext()).edit();
454 appPrefs
.putString("last_upload_path", mUploadPath
);
460 } catch (SecurityException e
) {
461 String message
= String
.format(getString(R
.string
.uploader_error_forbidden_content
), getString(R
.string
.app_name
));
462 Toast
.makeText(this, message
, Toast
.LENGTH_LONG
).show();
467 * Loads the target folder initialize shown to the user.
469 * The target account has to be chosen before this method is called.
471 private void initTargetFolder() {
472 if (mStorageManager
== null
) {
473 throw new IllegalStateException("Do not call this method before initializing mStorageManager");
476 SharedPreferences appPreferences
= PreferenceManager
477 .getDefaultSharedPreferences(getApplicationContext());
479 String last_path
= appPreferences
.getString("last_upload_path", "");
480 // "/" equals root-directory
481 if(last_path
.equals("/")) {
485 String
[] dir_names
= last_path
.split("/");
486 for (String dir
: dir_names
)
489 //Make sure that path still exists, if it doesn't pop the stack and try the previous path
490 while(!mStorageManager
.fileExists(generatePath(mParents
))){