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 as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * 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 com
.owncloud
.android
.authenticator
.AccountAuthenticator
;
30 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
31 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
32 import com
.owncloud
.android
.datamodel
.OCFile
;
33 import com
.owncloud
.android
.files
.services
.FileUploader
;
34 import com
.owncloud
.android
.network
.OwnCloudClientUtils
;
36 import android
.accounts
.Account
;
37 import android
.accounts
.AccountManager
;
38 import android
.app
.AlertDialog
;
39 import android
.app
.AlertDialog
.Builder
;
40 import android
.app
.Dialog
;
41 import android
.app
.ListActivity
;
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
.database
.Cursor
;
49 import android
.net
.Uri
;
50 import android
.os
.Bundle
;
51 import android
.os
.Parcelable
;
52 import android
.provider
.MediaStore
.Images
.Media
;
53 import android
.util
.Log
;
54 import android
.view
.View
;
55 import android
.view
.Window
;
56 import android
.widget
.AdapterView
;
57 import android
.widget
.AdapterView
.OnItemClickListener
;
58 import android
.widget
.Button
;
59 import android
.widget
.EditText
;
60 import android
.widget
.SimpleAdapter
;
61 import android
.widget
.Toast
;
63 import com
.owncloud
.android
.R
;
64 import eu
.alefzero
.webdav
.WebdavClient
;
67 * This can be used to upload things to an ownCloud instance.
69 * @author Bartek Przybylski
72 public class Uploader
extends ListActivity
implements OnItemClickListener
, android
.view
.View
.OnClickListener
{
73 private static final String TAG
= "ownCloudUploader";
75 private Account mAccount
;
76 private AccountManager mAccountManager
;
77 private Stack
<String
> mParents
;
78 private ArrayList
<Parcelable
> mStreamsToUpload
;
79 private boolean mCreateDir
;
80 private String mUploadPath
;
81 private static final String
[] CONTENT_PROJECTION
= { Media
.DATA
, Media
.DISPLAY_NAME
, Media
.MIME_TYPE
, Media
.SIZE
};
82 private DataStorageManager mStorageManager
;
85 private final static int DIALOG_NO_ACCOUNT
= 0;
86 private final static int DIALOG_WAITING
= 1;
87 private final static int DIALOG_NO_STREAM
= 2;
88 private final static int DIALOG_MULTIPLE_ACCOUNT
= 3;
89 //private final static int DIALOG_GET_DIRNAME = 4;
91 private final static int REQUEST_CODE_SETUP_ACCOUNT
= 0;
94 protected void onCreate(Bundle savedInstanceState
) {
95 super.onCreate(savedInstanceState
);
96 getWindow().requestFeature(Window
.FEATURE_NO_TITLE
);
97 mParents
= new Stack
<String
>();
99 /*if (getIntent().hasExtra(Intent.EXTRA_STREAM)) {
100 prepareStreamsToUpload();*/
101 if (prepareStreamsToUpload()) {
102 mAccountManager
= (AccountManager
) getSystemService(Context
.ACCOUNT_SERVICE
);
103 Account
[] accounts
= mAccountManager
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
104 if (accounts
.length
== 0) {
105 Log
.i(TAG
, "No ownCloud account is available");
106 showDialog(DIALOG_NO_ACCOUNT
);
107 } else if (accounts
.length
> 1) {
108 Log
.i(TAG
, "More then one ownCloud is available");
109 showDialog(DIALOG_MULTIPLE_ACCOUNT
);
111 mAccount
= accounts
[0];
112 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
113 populateDirectoryList();
116 showDialog(DIALOG_NO_STREAM
);
121 protected Dialog
onCreateDialog(final int id
) {
122 final AlertDialog
.Builder builder
= new Builder(this);
125 ProgressDialog pDialog
= new ProgressDialog(this);
126 pDialog
.setIndeterminate(false
);
127 pDialog
.setCancelable(false
);
128 pDialog
.setMessage(getResources().getString(R
.string
.uploader_info_uploading
));
130 case DIALOG_NO_ACCOUNT
:
131 builder
.setIcon(android
.R
.drawable
.ic_dialog_alert
);
132 builder
.setTitle(R
.string
.uploader_wrn_no_account_title
);
133 builder
.setMessage(String
.format(getString(R
.string
.uploader_wrn_no_account_text
), getString(R
.string
.app_name
)));
134 builder
.setCancelable(false
);
135 builder
.setPositiveButton(R
.string
.uploader_wrn_no_account_setup_btn_text
, new OnClickListener() {
137 public void onClick(DialogInterface dialog
, int which
) {
138 if (android
.os
.Build
.VERSION
.SDK_INT
> android
.os
.Build
.VERSION_CODES
.ECLAIR_MR1
) {
139 // using string value since in API7 this
140 // constatn is not defined
141 // in API7 < this constatant is defined in
142 // Settings.ADD_ACCOUNT_SETTINGS
143 // and Settings.EXTRA_AUTHORITIES
144 Intent intent
= new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
145 intent
.putExtra("authorities", new String
[] { AccountAuthenticator
.AUTH_TOKEN_TYPE
});
146 startActivityForResult(intent
, REQUEST_CODE_SETUP_ACCOUNT
);
148 // since in API7 there is no direct call for
149 // account setup, so we need to
150 // show our own AccountSetupAcricity, get
151 // desired results and setup
152 // everything for ourself
153 Intent intent
= new Intent(getBaseContext(), AccountAuthenticator
.class);
154 startActivityForResult(intent
, REQUEST_CODE_SETUP_ACCOUNT
);
158 builder
.setNegativeButton(R
.string
.uploader_wrn_no_account_quit_btn_text
, new OnClickListener() {
160 public void onClick(DialogInterface dialog
, int which
) {
164 return builder
.create();
165 /*case DIALOG_GET_DIRNAME:
166 final EditText dirName = new EditText(getBaseContext());
167 builder.setView(dirName);
168 builder.setTitle(R.string.uploader_info_dirname);
170 if (mParents.empty()) {
173 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, mParents.peek()), null,
175 mCursor.moveToFirst();
176 pathToUpload = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_PATH))
177 + mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_NAME)).replace(" ", "%20"); // TODO don't make this ; use WebdavUtils.encode in the right moment
179 a a = new a(pathToUpload, dirName);
180 builder.setPositiveButton(R.string.common_ok, a);
181 builder.setNegativeButton(R.string.common_cancel, new OnClickListener() {
182 public void onClick(DialogInterface dialog, int which) {
186 return builder.create();*/
187 case DIALOG_MULTIPLE_ACCOUNT
:
188 CharSequence ac
[] = new CharSequence
[mAccountManager
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
).length
];
189 for (int i
= 0; i
< ac
.length
; ++i
) {
190 ac
[i
] = mAccountManager
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)[i
].name
;
192 builder
.setTitle(R
.string
.common_choose_account
);
193 builder
.setItems(ac
, new OnClickListener() {
195 public void onClick(DialogInterface dialog
, int which
) {
196 mAccount
= mAccountManager
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)[which
];
197 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
198 populateDirectoryList();
201 builder
.setCancelable(true
);
202 builder
.setOnCancelListener(new OnCancelListener() {
204 public void onCancel(DialogInterface dialog
) {
209 return builder
.create();
210 case DIALOG_NO_STREAM
:
211 builder
.setIcon(android
.R
.drawable
.ic_dialog_alert
);
212 builder
.setTitle(R
.string
.uploader_wrn_no_content_title
);
213 builder
.setMessage(R
.string
.uploader_wrn_no_content_text
);
214 builder
.setCancelable(false
);
215 builder
.setNegativeButton(R
.string
.common_cancel
, new OnClickListener() {
217 public void onClick(DialogInterface dialog
, int which
) {
221 return builder
.create();
223 throw new IllegalArgumentException("Unknown dialog id: " + id
);
227 class a
implements OnClickListener
{
231 public a(String path
, EditText dirname
) {
237 public void onClick(DialogInterface dialog
, int which
) {
238 Uploader
.this.mUploadPath
= mPath
+ mDirname
.getText().toString();
239 Uploader
.this.mCreateDir
= true
;
245 public void onBackPressed() {
247 if (mParents
.size() <= 1) {
248 super.onBackPressed();
252 populateDirectoryList();
257 public void onItemClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
258 // click on folder in the list
259 Log
.d(TAG
, "on item click");
260 Vector
<OCFile
> tmpfiles
= mStorageManager
.getDirectoryContent(mFile
);
261 if (tmpfiles
.size() <= 0) return;
263 Vector
<OCFile
> files
= new Vector
<OCFile
>();
264 for (OCFile f
: tmpfiles
)
267 if (files
.size() < position
) {
268 throw new IndexOutOfBoundsException("Incorrect item selected");
270 mParents
.push(files
.get(position
).getFileName());
271 populateDirectoryList();
275 public void onClick(View v
) {
278 case R
.id
.uploader_choose_folder
:
279 mUploadPath
= ""; // first element in mParents is root dir, represented by ""; init mUploadPath with "/" results in a "//" prefix
280 for (String p
: mParents
)
281 mUploadPath
+= p
+ OCFile
.PATH_SEPARATOR
;
282 Log
.d(TAG
, "Uploading file to dir " + mUploadPath
);
287 /*case android.R.id.button1: // dynamic action for create aditional dir
288 showDialog(DIALOG_GET_DIRNAME);
291 throw new IllegalArgumentException("Wrong element clicked");
296 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
297 super.onActivityResult(requestCode
, resultCode
, data
);
298 Log
.i(TAG
, "result received. req: " + requestCode
+ " res: " + resultCode
);
299 if (requestCode
== REQUEST_CODE_SETUP_ACCOUNT
) {
300 dismissDialog(DIALOG_NO_ACCOUNT
);
301 if (resultCode
== RESULT_CANCELED
) {
304 Account
[] accounts
= mAccountManager
.getAccountsByType(AccountAuthenticator
.AUTH_TOKEN_TYPE
);
305 if (accounts
.length
== 0) {
306 showDialog(DIALOG_NO_ACCOUNT
);
308 // there is no need for checking for is there more then one
309 // account at this point
310 // since account setup can set only one account at time
311 mAccount
= accounts
[0];
312 populateDirectoryList();
317 private void populateDirectoryList() {
318 setContentView(R
.layout
.uploader_layout
);
320 String full_path
= "";
321 for (String a
: mParents
)
322 full_path
+= a
+ "/";
324 Log
.d(TAG
, "Populating view with content of : " + full_path
);
326 mFile
= mStorageManager
.getFileByPath(full_path
);
328 Vector
<OCFile
> files
= mStorageManager
.getDirectoryContent(mFile
);
329 List
<HashMap
<String
, Object
>> data
= new LinkedList
<HashMap
<String
,Object
>>();
330 for (OCFile f
: files
) {
331 HashMap
<String
, Object
> h
= new HashMap
<String
, Object
>();
332 if (f
.isDirectory()) {
333 h
.put("dirname", f
.getFileName());
337 SimpleAdapter sa
= new SimpleAdapter(this,
339 R
.layout
.uploader_list_item_layout
,
340 new String
[] {"dirname"},
341 new int[] {R
.id
.textView1
});
343 Button btn
= (Button
) findViewById(R
.id
.uploader_choose_folder
);
344 btn
.setOnClickListener(this);
345 getListView().setOnItemClickListener(this);
349 private boolean prepareStreamsToUpload() {
350 if (getIntent().getAction().equals(Intent
.ACTION_SEND
)) {
351 mStreamsToUpload
= new ArrayList
<Parcelable
>();
352 mStreamsToUpload
.add(getIntent().getParcelableExtra(Intent
.EXTRA_STREAM
));
353 } else if (getIntent().getAction().equals(Intent
.ACTION_SEND_MULTIPLE
)) {
354 mStreamsToUpload
= getIntent().getParcelableArrayListExtra(Intent
.EXTRA_STREAM
);
356 return (mStreamsToUpload
!= null
&& mStreamsToUpload
.get(0) != null
);
359 public void uploadFiles() {
361 WebdavClient wdc
= OwnCloudClientUtils
.createOwnCloudClient(mAccount
, getApplicationContext());
363 // create last directory in path if necessary
365 wdc
.createDirectory(mUploadPath
);
368 String
[] local
= new String
[mStreamsToUpload
.size()], remote
= new String
[mStreamsToUpload
.size()];
370 for (int i
= 0; i
< mStreamsToUpload
.size(); ++i
) {
371 Uri uri
= (Uri
) mStreamsToUpload
.get(i
);
372 if (uri
.getScheme().equals("content")) {
373 Cursor c
= getContentResolver().query((Uri
) mStreamsToUpload
.get(i
),
379 if (!c
.moveToFirst())
382 final String display_name
= c
.getString(c
.getColumnIndex(Media
.DISPLAY_NAME
)),
383 data
= c
.getString(c
.getColumnIndex(Media
.DATA
));
385 remote
[i
] = mUploadPath
+ display_name
;
386 } else if (uri
.getScheme().equals("file")) {
387 final File file
= new File(Uri
.decode(uri
.toString()).replace(uri
.getScheme() + "://", ""));
388 local
[i
] = file
.getAbsolutePath();
389 remote
[i
] = mUploadPath
+ file
.getName();
393 Intent intent
= new Intent(getApplicationContext(), FileUploader
.class);
394 intent
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
395 intent
.putExtra(FileUploader
.KEY_LOCAL_FILE
, local
);
396 intent
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remote
);
397 intent
.putExtra(FileUploader
.KEY_ACCOUNT
, mAccount
);
398 startService(intent
);
401 } catch (SecurityException e
) {
402 String message
= String
.format(getString(R
.string
.uploader_error_forbidden_content
), getString(R
.string
.app_name
));
403 Toast
.makeText(this, message
, Toast
.LENGTH_LONG
).show();