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
;
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
.ListActivity
;
35 import android
.app
.ProgressDialog
;
36 import android
.content
.Context
;
37 import android
.content
.DialogInterface
;
38 import android
.content
.DialogInterface
.OnCancelListener
;
39 import android
.content
.DialogInterface
.OnClickListener
;
40 import android
.content
.Intent
;
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
.SimpleAdapter
;
55 import android
.widget
.Toast
;
57 import com
.owncloud
.android
.authentication
.AccountAuthenticator
;
58 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
59 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
60 import com
.owncloud
.android
.datamodel
.OCFile
;
61 import com
.owncloud
.android
.files
.services
.FileUploader
;
63 import com
.owncloud
.android
.R
;
66 * This can be used to upload things to an ownCloud instance.
68 * @author Bartek Przybylski
71 public class Uploader
extends ListActivity
implements OnItemClickListener
, android
.view
.View
.OnClickListener
{
72 private static final String TAG
= "ownCloudUploader";
74 private Account mAccount
;
75 private AccountManager mAccountManager
;
76 private Stack
<String
> mParents
;
77 private ArrayList
<Parcelable
> mStreamsToUpload
;
78 private boolean mCreateDir
;
79 private String mUploadPath
;
80 private DataStorageManager mStorageManager
;
83 private final static int DIALOG_NO_ACCOUNT
= 0;
84 private final static int DIALOG_WAITING
= 1;
85 private final static int DIALOG_NO_STREAM
= 2;
86 private final static int DIALOG_MULTIPLE_ACCOUNT
= 3;
88 private final static int REQUEST_CODE_SETUP_ACCOUNT
= 0;
91 protected void onCreate(Bundle savedInstanceState
) {
92 super.onCreate(savedInstanceState
);
93 getWindow().requestFeature(Window
.FEATURE_NO_TITLE
);
94 mParents
= new Stack
<String
>();
96 if (prepareStreamsToUpload()) {
97 mAccountManager
= (AccountManager
) getSystemService(Context
.ACCOUNT_SERVICE
);
98 Account
[] accounts
= mAccountManager
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
99 if (accounts
.length
== 0) {
100 Log_OC
.i(TAG
, "No ownCloud account is available");
101 showDialog(DIALOG_NO_ACCOUNT
);
102 } else if (accounts
.length
> 1) {
103 Log_OC
.i(TAG
, "More then one ownCloud is available");
104 showDialog(DIALOG_MULTIPLE_ACCOUNT
);
106 mAccount
= accounts
[0];
107 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
108 populateDirectoryList();
111 showDialog(DIALOG_NO_STREAM
);
116 protected Dialog
onCreateDialog(final int id
) {
117 final AlertDialog
.Builder builder
= new Builder(this);
120 ProgressDialog pDialog
= new ProgressDialog(this);
121 pDialog
.setIndeterminate(false
);
122 pDialog
.setCancelable(false
);
123 pDialog
.setMessage(getResources().getString(R
.string
.uploader_info_uploading
));
125 case DIALOG_NO_ACCOUNT
:
126 builder
.setIcon(android
.R
.drawable
.ic_dialog_alert
);
127 builder
.setTitle(R
.string
.uploader_wrn_no_account_title
);
128 builder
.setMessage(String
.format(getString(R
.string
.uploader_wrn_no_account_text
), getString(R
.string
.app_name
)));
129 builder
.setCancelable(false
);
130 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
.provider
.Settings
.ACTION_ADD_ACCOUNT
);
140 intent
.putExtra("authorities", new String
[] { AccountAuthenticator
.AUTHORITY
});
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() {
155 public void onClick(DialogInterface dialog
, int which
) {
159 return builder
.create();
160 case DIALOG_MULTIPLE_ACCOUNT
:
161 CharSequence ac
[] = new CharSequence
[mAccountManager
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
).length
];
162 for (int i
= 0; i
< ac
.length
; ++i
) {
163 ac
[i
] = mAccountManager
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)[i
].name
;
165 builder
.setTitle(R
.string
.common_choose_account
);
166 builder
.setItems(ac
, new OnClickListener() {
168 public void onClick(DialogInterface dialog
, int which
) {
169 mAccount
= mAccountManager
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)[which
];
170 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
171 populateDirectoryList();
174 builder
.setCancelable(true
);
175 builder
.setOnCancelListener(new OnCancelListener() {
177 public void onCancel(DialogInterface dialog
) {
182 return builder
.create();
183 case DIALOG_NO_STREAM
:
184 builder
.setIcon(android
.R
.drawable
.ic_dialog_alert
);
185 builder
.setTitle(R
.string
.uploader_wrn_no_content_title
);
186 builder
.setMessage(R
.string
.uploader_wrn_no_content_text
);
187 builder
.setCancelable(false
);
188 builder
.setNegativeButton(R
.string
.common_cancel
, new OnClickListener() {
190 public void onClick(DialogInterface dialog
, int which
) {
194 return builder
.create();
196 throw new IllegalArgumentException("Unknown dialog id: " + id
);
200 class a
implements OnClickListener
{
204 public a(String path
, EditText dirname
) {
210 public void onClick(DialogInterface dialog
, int which
) {
211 Uploader
.this.mUploadPath
= mPath
+ mDirname
.getText().toString();
212 Uploader
.this.mCreateDir
= true
;
218 public void onBackPressed() {
220 if (mParents
.size() <= 1) {
221 super.onBackPressed();
225 populateDirectoryList();
230 public void onItemClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
231 // click on folder in the list
232 Log_OC
.d(TAG
, "on item click");
233 Vector
<OCFile
> tmpfiles
= mStorageManager
.getDirectoryContent(mFile
);
234 if (tmpfiles
.size() <= 0) return;
236 Vector
<OCFile
> files
= new Vector
<OCFile
>();
237 for (OCFile f
: tmpfiles
)
240 if (files
.size() < position
) {
241 throw new IndexOutOfBoundsException("Incorrect item selected");
243 mParents
.push(files
.get(position
).getFileName());
244 populateDirectoryList();
248 public void onClick(View v
) {
251 case R
.id
.uploader_choose_folder
:
252 mUploadPath
= ""; // first element in mParents is root dir, represented by ""; init mUploadPath with "/" results in a "//" prefix
253 for (String p
: mParents
)
254 mUploadPath
+= p
+ OCFile
.PATH_SEPARATOR
;
255 Log_OC
.d(TAG
, "Uploading file to dir " + mUploadPath
);
261 throw new IllegalArgumentException("Wrong element clicked");
266 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
267 super.onActivityResult(requestCode
, resultCode
, data
);
268 Log_OC
.i(TAG
, "result received. req: " + requestCode
+ " res: " + resultCode
);
269 if (requestCode
== REQUEST_CODE_SETUP_ACCOUNT
) {
270 dismissDialog(DIALOG_NO_ACCOUNT
);
271 if (resultCode
== RESULT_CANCELED
) {
274 Account
[] accounts
= mAccountManager
.getAccountsByType(AccountAuthenticator
.AUTH_TOKEN_TYPE
);
275 if (accounts
.length
== 0) {
276 showDialog(DIALOG_NO_ACCOUNT
);
278 // there is no need for checking for is there more then one
279 // account at this point
280 // since account setup can set only one account at time
281 mAccount
= accounts
[0];
282 populateDirectoryList();
287 private void populateDirectoryList() {
288 setContentView(R
.layout
.uploader_layout
);
290 String full_path
= "";
291 for (String a
: mParents
)
292 full_path
+= a
+ "/";
294 Log_OC
.d(TAG
, "Populating view with content of : " + full_path
);
296 mFile
= mStorageManager
.getFileByPath(full_path
);
298 Vector
<OCFile
> files
= mStorageManager
.getDirectoryContent(mFile
);
299 List
<HashMap
<String
, Object
>> data
= new LinkedList
<HashMap
<String
,Object
>>();
300 for (OCFile f
: files
) {
301 HashMap
<String
, Object
> h
= new HashMap
<String
, Object
>();
302 if (f
.isDirectory()) {
303 h
.put("dirname", f
.getFileName());
307 SimpleAdapter sa
= new SimpleAdapter(this,
309 R
.layout
.uploader_list_item_layout
,
310 new String
[] {"dirname"},
311 new int[] {R
.id
.textView1
});
313 Button btn
= (Button
) findViewById(R
.id
.uploader_choose_folder
);
314 btn
.setOnClickListener(this);
315 getListView().setOnItemClickListener(this);
319 private boolean prepareStreamsToUpload() {
320 if (getIntent().getAction().equals(Intent
.ACTION_SEND
)) {
321 mStreamsToUpload
= new ArrayList
<Parcelable
>();
322 mStreamsToUpload
.add(getIntent().getParcelableExtra(Intent
.EXTRA_STREAM
));
323 } else if (getIntent().getAction().equals(Intent
.ACTION_SEND_MULTIPLE
)) {
324 mStreamsToUpload
= getIntent().getParcelableArrayListExtra(Intent
.EXTRA_STREAM
);
326 return (mStreamsToUpload
!= null
&& mStreamsToUpload
.get(0) != null
);
329 public void uploadFiles() {
331 //WebdavClient webdav = OwnCloudClientUtils.createOwnCloudClient(mAccount, getApplicationContext());
333 ArrayList
<String
> local
= new ArrayList
<String
>();
334 ArrayList
<String
> remote
= new ArrayList
<String
>();
336 /* TODO - mCreateDir can never be true at this moment; we will replace wdc.createDirectory by CreateFolderOperation when that is fixed
337 WebdavClient wdc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getApplicationContext());
338 // create last directory in path if necessary
340 wdc.createDirectory(mUploadPath);
344 // this checks the mimeType
345 for (Parcelable mStream
: mStreamsToUpload
) {
347 Uri uri
= (Uri
) mStream
;
349 if (uri
.getScheme().equals("content")) {
351 String mimeType
= getContentResolver().getType(uri
);
353 if (mimeType
.contains("image")) {
354 String
[] CONTENT_PROJECTION
= { Images
.Media
.DATA
, Images
.Media
.DISPLAY_NAME
, Images
.Media
.MIME_TYPE
, Images
.Media
.SIZE
};
355 Cursor c
= getContentResolver().query(uri
, CONTENT_PROJECTION
, null
, null
, null
);
357 int index
= c
.getColumnIndex(Images
.Media
.DATA
);
358 String data
= c
.getString(index
);
360 remote
.add(mUploadPath
+ c
.getString(c
.getColumnIndex(Images
.Media
.DISPLAY_NAME
)));
363 else if (mimeType
.contains("video")) {
364 String
[] CONTENT_PROJECTION
= { Video
.Media
.DATA
, Video
.Media
.DISPLAY_NAME
, Video
.Media
.MIME_TYPE
, Video
.Media
.SIZE
, Video
.Media
.DATE_MODIFIED
};
365 Cursor c
= getContentResolver().query(uri
, CONTENT_PROJECTION
, null
, null
, null
);
367 int index
= c
.getColumnIndex(Video
.Media
.DATA
);
368 String data
= c
.getString(index
);
370 remote
.add(mUploadPath
+ c
.getString(c
.getColumnIndex(Video
.Media
.DISPLAY_NAME
)));
373 else if (mimeType
.contains("audio")) {
374 String
[] CONTENT_PROJECTION
= { Audio
.Media
.DATA
, Audio
.Media
.DISPLAY_NAME
, Audio
.Media
.MIME_TYPE
, Audio
.Media
.SIZE
};
375 Cursor c
= getContentResolver().query(uri
, CONTENT_PROJECTION
, null
, null
, null
);
377 int index
= c
.getColumnIndex(Audio
.Media
.DATA
);
378 String data
= c
.getString(index
);
380 remote
.add(mUploadPath
+ c
.getString(c
.getColumnIndex(Audio
.Media
.DISPLAY_NAME
)));
384 String filePath
= Uri
.decode(uri
.toString()).replace(uri
.getScheme() + "://", "");
385 // cut everything whats before mnt. It occured to me that sometimes apps send their name into the URI
386 if (filePath
.contains("mnt")) {
387 String splitedFilePath
[] = filePath
.split("/mnt");
388 filePath
= splitedFilePath
[1];
390 final File file
= new File(filePath
);
391 local
.add(file
.getAbsolutePath());
392 remote
.add(mUploadPath
+ file
.getName());
395 } else if (uri
.getScheme().equals("file")) {
396 String filePath
= Uri
.decode(uri
.toString()).replace(uri
.getScheme() + "://", "");
397 if (filePath
.contains("mnt")) {
398 String splitedFilePath
[] = filePath
.split("/mnt");
399 filePath
= splitedFilePath
[1];
401 final File file
= new File(filePath
);
402 local
.add(file
.getAbsolutePath());
403 remote
.add(mUploadPath
+ file
.getName());
406 throw new SecurityException();
410 throw new SecurityException();
413 Intent intent
= new Intent(getApplicationContext(), FileUploader
.class);
414 intent
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
415 intent
.putExtra(FileUploader
.KEY_LOCAL_FILE
, local
.toArray(new String
[local
.size()]));
416 intent
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remote
.toArray(new String
[remote
.size()]));
417 intent
.putExtra(FileUploader
.KEY_ACCOUNT
, mAccount
);
418 startService(intent
);
422 } catch (SecurityException e
) {
423 String message
= String
.format(getString(R
.string
.uploader_error_forbidden_content
), getString(R
.string
.app_name
));
424 Toast
.makeText(this, message
, Toast
.LENGTH_LONG
).show();