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
.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
.ListActivity
;
43 import android
.app
.ProgressDialog
;
44 import android
.content
.Context
;
45 import android
.content
.DialogInterface
;
46 import android
.content
.DialogInterface
.OnCancelListener
;
47 import android
.content
.DialogInterface
.OnClickListener
;
48 import android
.content
.Intent
;
49 import android
.content
.SharedPreferences
;
50 import android
.database
.Cursor
;
51 import android
.net
.Uri
;
52 import android
.os
.Bundle
;
53 import android
.os
.Parcelable
;
54 import android
.preference
.PreferenceManager
;
55 import android
.provider
.MediaStore
.Audio
;
56 import android
.provider
.MediaStore
.Images
;
57 import android
.provider
.MediaStore
.Video
;
58 import android
.view
.View
;
59 import android
.view
.Window
;
60 import android
.widget
.AdapterView
;
61 import android
.widget
.AdapterView
.OnItemClickListener
;
62 import android
.widget
.Button
;
63 import android
.widget
.EditText
;
64 import android
.widget
.SimpleAdapter
;
65 import android
.widget
.Toast
;
69 * This can be used to upload things to an ownCloud instance.
71 * @author Bartek Przybylski
74 public class Uploader
extends ListActivity
implements OnItemClickListener
, android
.view
.View
.OnClickListener
{
75 private static final String TAG
= "ownCloudUploader";
77 private Account mAccount
;
78 private AccountManager mAccountManager
;
79 private Stack
<String
> mParents
;
80 private ArrayList
<Parcelable
> mStreamsToUpload
;
81 private boolean mCreateDir
;
82 private String mUploadPath
;
83 private FileDataStorageManager mStorageManager
;
85 private boolean mSaveUploadLocation
;
87 private final static int DIALOG_NO_ACCOUNT
= 0;
88 private final static int DIALOG_WAITING
= 1;
89 private final static int DIALOG_NO_STREAM
= 2;
90 private final static int DIALOG_MULTIPLE_ACCOUNT
= 3;
92 private final static int REQUEST_CODE_SETUP_ACCOUNT
= 0;
95 protected void onCreate(Bundle savedInstanceState
) {
96 super.onCreate(savedInstanceState
);
97 getWindow().requestFeature(Window
.FEATURE_NO_TITLE
);
98 mParents
= new Stack
<String
>();
103 SharedPreferences appPreferences
= PreferenceManager
104 .getDefaultSharedPreferences(getApplicationContext());
106 mSaveUploadLocation
= appPreferences
.getBoolean("save_last_upload_location", false
);
108 if(mSaveUploadLocation
)
110 String last_path
= appPreferences
.getString("last_upload_path", "");
112 if(last_path
.equals("/")) {
116 String
[] dir_names
= last_path
.split("/");
117 for (String dir
: dir_names
)
125 if (prepareStreamsToUpload()) {
126 mAccountManager
= (AccountManager
) getSystemService(Context
.ACCOUNT_SERVICE
);
127 Account
[] accounts
= mAccountManager
.getAccountsByType(MainApp
.getAccountType());
128 if (accounts
.length
== 0) {
129 Log_OC
.i(TAG
, "No ownCloud account is available");
130 showDialog(DIALOG_NO_ACCOUNT
);
131 } else if (accounts
.length
> 1) {
132 Log_OC
.i(TAG
, "More then one ownCloud is available");
133 showDialog(DIALOG_MULTIPLE_ACCOUNT
);
135 mAccount
= accounts
[0];
136 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
137 populateDirectoryList();
140 showDialog(DIALOG_NO_STREAM
);
145 protected Dialog
onCreateDialog(final int id
) {
146 final AlertDialog
.Builder builder
= new Builder(this);
149 ProgressDialog pDialog
= new ProgressDialog(this);
150 pDialog
.setIndeterminate(false
);
151 pDialog
.setCancelable(false
);
152 pDialog
.setMessage(getResources().getString(R
.string
.uploader_info_uploading
));
154 case DIALOG_NO_ACCOUNT
:
155 builder
.setIcon(android
.R
.drawable
.ic_dialog_alert
);
156 builder
.setTitle(R
.string
.uploader_wrn_no_account_title
);
157 builder
.setMessage(String
.format(getString(R
.string
.uploader_wrn_no_account_text
), getString(R
.string
.app_name
)));
158 builder
.setCancelable(false
);
159 builder
.setPositiveButton(R
.string
.uploader_wrn_no_account_setup_btn_text
, new OnClickListener() {
161 public void onClick(DialogInterface dialog
, int which
) {
162 if (android
.os
.Build
.VERSION
.SDK_INT
> android
.os
.Build
.VERSION_CODES
.ECLAIR_MR1
) {
163 // using string value since in API7 this
164 // constatn is not defined
165 // in API7 < this constatant is defined in
166 // Settings.ADD_ACCOUNT_SETTINGS
167 // and Settings.EXTRA_AUTHORITIES
168 Intent intent
= new Intent(android
.provider
.Settings
.ACTION_ADD_ACCOUNT
);
169 intent
.putExtra("authorities", new String
[] { MainApp
.getAuthTokenType() });
170 startActivityForResult(intent
, REQUEST_CODE_SETUP_ACCOUNT
);
172 // since in API7 there is no direct call for
173 // account setup, so we need to
174 // show our own AccountSetupAcricity, get
175 // desired results and setup
176 // everything for ourself
177 Intent intent
= new Intent(getBaseContext(), AccountAuthenticator
.class);
178 startActivityForResult(intent
, REQUEST_CODE_SETUP_ACCOUNT
);
182 builder
.setNegativeButton(R
.string
.uploader_wrn_no_account_quit_btn_text
, new OnClickListener() {
184 public void onClick(DialogInterface dialog
, int which
) {
188 return builder
.create();
189 case DIALOG_MULTIPLE_ACCOUNT
:
190 CharSequence ac
[] = new CharSequence
[mAccountManager
.getAccountsByType(MainApp
.getAccountType()).length
];
191 for (int i
= 0; i
< ac
.length
; ++i
) {
192 ac
[i
] = mAccountManager
.getAccountsByType(MainApp
.getAccountType())[i
].name
;
194 builder
.setTitle(R
.string
.common_choose_account
);
195 builder
.setItems(ac
, new OnClickListener() {
197 public void onClick(DialogInterface dialog
, int which
) {
198 mAccount
= mAccountManager
.getAccountsByType(MainApp
.getAccountType())[which
];
199 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
200 populateDirectoryList();
203 builder
.setCancelable(true
);
204 builder
.setOnCancelListener(new OnCancelListener() {
206 public void onCancel(DialogInterface dialog
) {
211 return builder
.create();
212 case DIALOG_NO_STREAM
:
213 builder
.setIcon(android
.R
.drawable
.ic_dialog_alert
);
214 builder
.setTitle(R
.string
.uploader_wrn_no_content_title
);
215 builder
.setMessage(R
.string
.uploader_wrn_no_content_text
);
216 builder
.setCancelable(false
);
217 builder
.setNegativeButton(R
.string
.common_cancel
, new OnClickListener() {
219 public void onClick(DialogInterface dialog
, int which
) {
223 return builder
.create();
225 throw new IllegalArgumentException("Unknown dialog id: " + id
);
229 class a
implements OnClickListener
{
233 public a(String path
, EditText dirname
) {
239 public void onClick(DialogInterface dialog
, int which
) {
240 Uploader
.this.mUploadPath
= mPath
+ mDirname
.getText().toString();
241 Uploader
.this.mCreateDir
= true
;
247 public void onBackPressed() {
249 if (mParents
.size() <= 1) {
250 super.onBackPressed();
254 populateDirectoryList();
259 public void onItemClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
260 // click on folder in the list
261 Log_OC
.d(TAG
, "on item click");
262 Vector
<OCFile
> tmpfiles
= mStorageManager
.getFolderContent(mFile
);
263 if (tmpfiles
.size() <= 0) return;
265 Vector
<OCFile
> files
= new Vector
<OCFile
>();
266 for (OCFile f
: tmpfiles
)
269 if (files
.size() < position
) {
270 throw new IndexOutOfBoundsException("Incorrect item selected");
272 mParents
.push(files
.get(position
).getFileName());
273 populateDirectoryList();
277 public void onClick(View v
) {
280 case R
.id
.uploader_choose_folder
:
281 mUploadPath
= ""; // first element in mParents is root dir, represented by ""; init mUploadPath with "/" results in a "//" prefix
282 for (String p
: mParents
)
283 mUploadPath
+= p
+ OCFile
.PATH_SEPARATOR
;
284 Log_OC
.d(TAG
, "Uploading file to dir " + mUploadPath
);
290 throw new IllegalArgumentException("Wrong element clicked");
295 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
296 super.onActivityResult(requestCode
, resultCode
, data
);
297 Log_OC
.i(TAG
, "result received. req: " + requestCode
+ " res: " + resultCode
);
298 if (requestCode
== REQUEST_CODE_SETUP_ACCOUNT
) {
299 dismissDialog(DIALOG_NO_ACCOUNT
);
300 if (resultCode
== RESULT_CANCELED
) {
303 Account
[] accounts
= mAccountManager
.getAccountsByType(MainApp
.getAuthTokenType());
304 if (accounts
.length
== 0) {
305 showDialog(DIALOG_NO_ACCOUNT
);
307 // there is no need for checking for is there more then one
308 // account at this point
309 // since account setup can set only one account at time
310 mAccount
= accounts
[0];
311 populateDirectoryList();
316 private void populateDirectoryList() {
317 setContentView(R
.layout
.uploader_layout
);
319 String full_path
= "";
321 for (String a
: mParents
)
322 full_path
+= a
+ "/";
324 Log_OC
.d(TAG
, "Populating view with content of : " + full_path
);
326 mFile
= mStorageManager
.getFileByPath(full_path
);
328 Vector
<OCFile
> files
= mStorageManager
.getFolderContent(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
>();
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() {
362 ArrayList
<String
> local
= new ArrayList
<String
>();
363 ArrayList
<String
> remote
= new ArrayList
<String
>();
365 // this checks the mimeType
366 for (Parcelable mStream
: mStreamsToUpload
) {
368 Uri uri
= (Uri
) mStream
;
370 if (uri
.getScheme().equals("content")) {
372 String mimeType
= getContentResolver().getType(uri
);
374 if (mimeType
.contains("image")) {
375 String
[] CONTENT_PROJECTION
= { Images
.Media
.DATA
, Images
.Media
.DISPLAY_NAME
, Images
.Media
.MIME_TYPE
, Images
.Media
.SIZE
};
376 Cursor c
= getContentResolver().query(uri
, CONTENT_PROJECTION
, null
, null
, null
);
378 int index
= c
.getColumnIndex(Images
.Media
.DATA
);
379 String data
= c
.getString(index
);
381 remote
.add(mUploadPath
+ c
.getString(c
.getColumnIndex(Images
.Media
.DISPLAY_NAME
)));
384 else if (mimeType
.contains("video")) {
385 String
[] CONTENT_PROJECTION
= { Video
.Media
.DATA
, Video
.Media
.DISPLAY_NAME
, Video
.Media
.MIME_TYPE
, Video
.Media
.SIZE
, Video
.Media
.DATE_MODIFIED
};
386 Cursor c
= getContentResolver().query(uri
, CONTENT_PROJECTION
, null
, null
, null
);
388 int index
= c
.getColumnIndex(Video
.Media
.DATA
);
389 String data
= c
.getString(index
);
391 remote
.add(mUploadPath
+ c
.getString(c
.getColumnIndex(Video
.Media
.DISPLAY_NAME
)));
394 else if (mimeType
.contains("audio")) {
395 String
[] CONTENT_PROJECTION
= { Audio
.Media
.DATA
, Audio
.Media
.DISPLAY_NAME
, Audio
.Media
.MIME_TYPE
, Audio
.Media
.SIZE
};
396 Cursor c
= getContentResolver().query(uri
, CONTENT_PROJECTION
, null
, null
, null
);
398 int index
= c
.getColumnIndex(Audio
.Media
.DATA
);
399 String data
= c
.getString(index
);
401 remote
.add(mUploadPath
+ c
.getString(c
.getColumnIndex(Audio
.Media
.DISPLAY_NAME
)));
405 String filePath
= Uri
.decode(uri
.toString()).replace(uri
.getScheme() + "://", "");
406 // cut everything whats before mnt. It occured to me that sometimes apps send their name into the URI
407 if (filePath
.contains("mnt")) {
408 String splitedFilePath
[] = filePath
.split("/mnt");
409 filePath
= splitedFilePath
[1];
411 final File file
= new File(filePath
);
412 local
.add(file
.getAbsolutePath());
413 remote
.add(mUploadPath
+ file
.getName());
416 } else if (uri
.getScheme().equals("file")) {
417 String filePath
= Uri
.decode(uri
.toString()).replace(uri
.getScheme() + "://", "");
418 if (filePath
.contains("mnt")) {
419 String splitedFilePath
[] = filePath
.split("/mnt");
420 filePath
= splitedFilePath
[1];
422 final File file
= new File(filePath
);
423 local
.add(file
.getAbsolutePath());
424 remote
.add(mUploadPath
+ file
.getName());
427 throw new SecurityException();
431 throw new SecurityException();
434 Intent intent
= new Intent(getApplicationContext(), FileUploader
.class);
435 intent
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
436 intent
.putExtra(FileUploader
.KEY_LOCAL_FILE
, local
.toArray(new String
[local
.size()]));
437 intent
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remote
.toArray(new String
[remote
.size()]));
438 intent
.putExtra(FileUploader
.KEY_ACCOUNT
, mAccount
);
439 startService(intent
);
441 if(mSaveUploadLocation
){
442 SharedPreferences
.Editor appPrefs
= PreferenceManager
443 .getDefaultSharedPreferences(getApplicationContext()).edit();
444 appPrefs
.putString("last_upload_path", mUploadPath
);
451 } catch (SecurityException e
) {
452 String message
= String
.format(getString(R
.string
.uploader_error_forbidden_content
), getString(R
.string
.app_name
));
453 Toast
.makeText(this, message
, Toast
.LENGTH_LONG
).show();