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
);
129 mAccount
= accounts
[0];
130 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
132 SharedPreferences appPreferences
= PreferenceManager
133 .getDefaultSharedPreferences(getApplicationContext());
135 String last_path
= appPreferences
.getString("last_upload_path", "");
136 // "/" equals root-directory
137 if(last_path
.equals("/")) {
141 String
[] dir_names
= last_path
.split("/");
142 for (String dir
: dir_names
)
145 //Make sure that path still exists, if it doesn't pop the stack and try the previous path
146 while(!mStorageManager
.fileExists(generatePath(mParents
))){
151 populateDirectoryList();
154 showDialog(DIALOG_NO_STREAM
);
159 protected Dialog
onCreateDialog(final int id
) {
160 final AlertDialog
.Builder builder
= new Builder(this);
163 ProgressDialog pDialog
= new ProgressDialog(this);
164 pDialog
.setIndeterminate(false
);
165 pDialog
.setCancelable(false
);
166 pDialog
.setMessage(getResources().getString(R
.string
.uploader_info_uploading
));
168 case DIALOG_NO_ACCOUNT
:
169 builder
.setIcon(android
.R
.drawable
.ic_dialog_alert
);
170 builder
.setTitle(R
.string
.uploader_wrn_no_account_title
);
171 builder
.setMessage(String
.format(getString(R
.string
.uploader_wrn_no_account_text
), getString(R
.string
.app_name
)));
172 builder
.setCancelable(false
);
173 builder
.setPositiveButton(R
.string
.uploader_wrn_no_account_setup_btn_text
, new OnClickListener() {
175 public void onClick(DialogInterface dialog
, int which
) {
176 if (android
.os
.Build
.VERSION
.SDK_INT
> android
.os
.Build
.VERSION_CODES
.ECLAIR_MR1
) {
177 // using string value since in API7 this
178 // constatn is not defined
179 // in API7 < this constatant is defined in
180 // Settings.ADD_ACCOUNT_SETTINGS
181 // and Settings.EXTRA_AUTHORITIES
182 Intent intent
= new Intent(android
.provider
.Settings
.ACTION_ADD_ACCOUNT
);
183 intent
.putExtra("authorities", new String
[] { MainApp
.getAuthTokenType() });
184 startActivityForResult(intent
, REQUEST_CODE_SETUP_ACCOUNT
);
186 // since in API7 there is no direct call for
187 // account setup, so we need to
188 // show our own AccountSetupAcricity, get
189 // desired results and setup
190 // everything for ourself
191 Intent intent
= new Intent(getBaseContext(), AccountAuthenticator
.class);
192 startActivityForResult(intent
, REQUEST_CODE_SETUP_ACCOUNT
);
196 builder
.setNegativeButton(R
.string
.uploader_wrn_no_account_quit_btn_text
, new OnClickListener() {
198 public void onClick(DialogInterface dialog
, int which
) {
202 return builder
.create();
203 case DIALOG_MULTIPLE_ACCOUNT
:
204 CharSequence ac
[] = new CharSequence
[mAccountManager
.getAccountsByType(MainApp
.getAccountType()).length
];
205 for (int i
= 0; i
< ac
.length
; ++i
) {
206 ac
[i
] = mAccountManager
.getAccountsByType(MainApp
.getAccountType())[i
].name
;
208 builder
.setTitle(R
.string
.common_choose_account
);
209 builder
.setItems(ac
, new OnClickListener() {
211 public void onClick(DialogInterface dialog
, int which
) {
212 mAccount
= mAccountManager
.getAccountsByType(MainApp
.getAccountType())[which
];
213 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
214 populateDirectoryList();
217 builder
.setCancelable(true
);
218 builder
.setOnCancelListener(new OnCancelListener() {
220 public void onCancel(DialogInterface dialog
) {
225 return builder
.create();
226 case DIALOG_NO_STREAM
:
227 builder
.setIcon(android
.R
.drawable
.ic_dialog_alert
);
228 builder
.setTitle(R
.string
.uploader_wrn_no_content_title
);
229 builder
.setMessage(R
.string
.uploader_wrn_no_content_text
);
230 builder
.setCancelable(false
);
231 builder
.setNegativeButton(R
.string
.common_cancel
, new OnClickListener() {
233 public void onClick(DialogInterface dialog
, int which
) {
237 return builder
.create();
239 throw new IllegalArgumentException("Unknown dialog id: " + id
);
243 class a
implements OnClickListener
{
247 public a(String path
, EditText dirname
) {
253 public void onClick(DialogInterface dialog
, int which
) {
254 Uploader
.this.mUploadPath
= mPath
+ mDirname
.getText().toString();
255 Uploader
.this.mCreateDir
= true
;
261 public void onBackPressed() {
263 if (mParents
.size() <= 1) {
264 super.onBackPressed();
268 populateDirectoryList();
273 public void onItemClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
274 // click on folder in the list
275 Log_OC
.d(TAG
, "on item click");
276 Vector
<OCFile
> tmpfiles
= mStorageManager
.getFolderContent(mFile
);
277 if (tmpfiles
.size() <= 0) return;
279 Vector
<OCFile
> files
= new Vector
<OCFile
>();
280 for (OCFile f
: tmpfiles
)
283 if (files
.size() < position
) {
284 throw new IndexOutOfBoundsException("Incorrect item selected");
286 mParents
.push(files
.get(position
).getFileName());
287 populateDirectoryList();
291 public void onClick(View v
) {
294 case R
.id
.uploader_choose_folder
:
295 mUploadPath
= ""; // first element in mParents is root dir, represented by ""; init mUploadPath with "/" results in a "//" prefix
296 for (String p
: mParents
)
297 mUploadPath
+= p
+ OCFile
.PATH_SEPARATOR
;
298 Log_OC
.d(TAG
, "Uploading file to dir " + mUploadPath
);
304 throw new IllegalArgumentException("Wrong element clicked");
309 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
310 super.onActivityResult(requestCode
, resultCode
, data
);
311 Log_OC
.i(TAG
, "result received. req: " + requestCode
+ " res: " + resultCode
);
312 if (requestCode
== REQUEST_CODE_SETUP_ACCOUNT
) {
313 dismissDialog(DIALOG_NO_ACCOUNT
);
314 if (resultCode
== RESULT_CANCELED
) {
317 Account
[] accounts
= mAccountManager
.getAccountsByType(MainApp
.getAuthTokenType());
318 if (accounts
.length
== 0) {
319 showDialog(DIALOG_NO_ACCOUNT
);
321 // there is no need for checking for is there more then one
322 // account at this point
323 // since account setup can set only one account at time
324 mAccount
= accounts
[0];
325 populateDirectoryList();
330 private void populateDirectoryList() {
331 setContentView(R
.layout
.uploader_layout
);
333 String current_dir
= mParents
.peek();
334 if(current_dir
.equals("")){
335 getSupportActionBar().setTitle(getString(R
.string
.default_display_name_for_root_folder
));
338 getSupportActionBar().setTitle(current_dir
);
341 String full_path
= generatePath(mParents
);
343 Log_OC
.d(TAG
, "Populating view with content of : " + full_path
);
345 mFile
= mStorageManager
.getFileByPath(full_path
);
347 Vector
<OCFile
> files
= mStorageManager
.getFolderContent(mFile
);
348 List
<HashMap
<String
, Object
>> data
= new LinkedList
<HashMap
<String
,Object
>>();
349 for (OCFile f
: files
) {
350 HashMap
<String
, Object
> h
= new HashMap
<String
, Object
>();
352 h
.put("dirname", f
.getFileName());
356 SimpleAdapter sa
= new SimpleAdapter(this,
358 R
.layout
.uploader_list_item_layout
,
359 new String
[] {"dirname"},
360 new int[] {R
.id
.textView1
});
362 Button btn
= (Button
) findViewById(R
.id
.uploader_choose_folder
);
363 btn
.setOnClickListener(this);
364 getListView().setOnItemClickListener(this);
368 private String
generatePath(Stack
<String
> dirs
) {
369 String full_path
= "";
371 for (String a
: dirs
)
372 full_path
+= a
+ "/";
376 private boolean prepareStreamsToUpload() {
377 if (getIntent().getAction().equals(Intent
.ACTION_SEND
)) {
378 mStreamsToUpload
= new ArrayList
<Parcelable
>();
379 mStreamsToUpload
.add(getIntent().getParcelableExtra(Intent
.EXTRA_STREAM
));
380 } else if (getIntent().getAction().equals(Intent
.ACTION_SEND_MULTIPLE
)) {
381 mStreamsToUpload
= getIntent().getParcelableArrayListExtra(Intent
.EXTRA_STREAM
);
383 return (mStreamsToUpload
!= null
&& mStreamsToUpload
.get(0) != null
);
386 public void uploadFiles() {
389 ArrayList
<String
> local
= new ArrayList
<String
>();
390 ArrayList
<String
> remote
= new ArrayList
<String
>();
392 // this checks the mimeType
393 for (Parcelable mStream
: mStreamsToUpload
) {
395 Uri uri
= (Uri
) mStream
;
397 if (uri
.getScheme().equals("content")) {
399 String mimeType
= getContentResolver().getType(uri
);
401 if (mimeType
.contains("image")) {
402 String
[] CONTENT_PROJECTION
= { Images
.Media
.DATA
, Images
.Media
.DISPLAY_NAME
, Images
.Media
.MIME_TYPE
, Images
.Media
.SIZE
};
403 Cursor c
= getContentResolver().query(uri
, CONTENT_PROJECTION
, null
, null
, null
);
405 int index
= c
.getColumnIndex(Images
.Media
.DATA
);
406 String data
= c
.getString(index
);
408 remote
.add(mUploadPath
+ c
.getString(c
.getColumnIndex(Images
.Media
.DISPLAY_NAME
)));
411 else if (mimeType
.contains("video")) {
412 String
[] CONTENT_PROJECTION
= { Video
.Media
.DATA
, Video
.Media
.DISPLAY_NAME
, Video
.Media
.MIME_TYPE
, Video
.Media
.SIZE
, Video
.Media
.DATE_MODIFIED
};
413 Cursor c
= getContentResolver().query(uri
, CONTENT_PROJECTION
, null
, null
, null
);
415 int index
= c
.getColumnIndex(Video
.Media
.DATA
);
416 String data
= c
.getString(index
);
418 remote
.add(mUploadPath
+ c
.getString(c
.getColumnIndex(Video
.Media
.DISPLAY_NAME
)));
421 else if (mimeType
.contains("audio")) {
422 String
[] CONTENT_PROJECTION
= { Audio
.Media
.DATA
, Audio
.Media
.DISPLAY_NAME
, Audio
.Media
.MIME_TYPE
, Audio
.Media
.SIZE
};
423 Cursor c
= getContentResolver().query(uri
, CONTENT_PROJECTION
, null
, null
, null
);
425 int index
= c
.getColumnIndex(Audio
.Media
.DATA
);
426 String data
= c
.getString(index
);
428 remote
.add(mUploadPath
+ c
.getString(c
.getColumnIndex(Audio
.Media
.DISPLAY_NAME
)));
432 String filePath
= Uri
.decode(uri
.toString()).replace(uri
.getScheme() + "://", "");
433 // cut everything whats before mnt. It occured to me that sometimes apps send their name into the URI
434 if (filePath
.contains("mnt")) {
435 String splitedFilePath
[] = filePath
.split("/mnt");
436 filePath
= splitedFilePath
[1];
438 final File file
= new File(filePath
);
439 local
.add(file
.getAbsolutePath());
440 remote
.add(mUploadPath
+ file
.getName());
443 } else if (uri
.getScheme().equals("file")) {
444 String filePath
= Uri
.decode(uri
.toString()).replace(uri
.getScheme() + "://", "");
445 if (filePath
.contains("mnt")) {
446 String splitedFilePath
[] = filePath
.split("/mnt");
447 filePath
= splitedFilePath
[1];
449 final File file
= new File(filePath
);
450 local
.add(file
.getAbsolutePath());
451 remote
.add(mUploadPath
+ file
.getName());
454 throw new SecurityException();
458 throw new SecurityException();
461 Intent intent
= new Intent(getApplicationContext(), FileUploader
.class);
462 intent
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
463 intent
.putExtra(FileUploader
.KEY_LOCAL_FILE
, local
.toArray(new String
[local
.size()]));
464 intent
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remote
.toArray(new String
[remote
.size()]));
465 intent
.putExtra(FileUploader
.KEY_ACCOUNT
, mAccount
);
466 startService(intent
);
468 //Save the path to shared preferences
469 SharedPreferences
.Editor appPrefs
= PreferenceManager
470 .getDefaultSharedPreferences(getApplicationContext()).edit();
471 appPrefs
.putString("last_upload_path", mUploadPath
);
477 } catch (SecurityException e
) {
478 String message
= String
.format(getString(R
.string
.uploader_error_forbidden_content
), getString(R
.string
.app_name
));
479 Toast
.makeText(this, message
, Toast
.LENGTH_LONG
).show();