1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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 eu
.alefzero
.owncloud
.ui
.activity
;
22 import java
.util
.ArrayList
;
24 import android
.accounts
.Account
;
25 import android
.accounts
.AccountManager
;
26 import android
.app
.AlertDialog
;
27 import android
.app
.AlertDialog
.Builder
;
28 import android
.app
.Dialog
;
29 import android
.content
.BroadcastReceiver
;
30 import android
.content
.ContentResolver
;
31 import android
.content
.Context
;
32 import android
.content
.DialogInterface
;
33 import android
.content
.DialogInterface
.OnClickListener
;
34 import android
.content
.Intent
;
35 import android
.content
.IntentFilter
;
36 import android
.content
.pm
.PackageInfo
;
37 import android
.content
.pm
.PackageManager
.NameNotFoundException
;
38 import android
.database
.Cursor
;
39 import android
.net
.Uri
;
40 import android
.os
.Bundle
;
41 import android
.provider
.MediaStore
;
42 import android
.util
.Log
;
43 import android
.view
.View
;
44 import android
.view
.ViewGroup
;
45 import android
.widget
.ArrayAdapter
;
46 import android
.widget
.EditText
;
47 import android
.widget
.TextView
;
48 import android
.widget
.Toast
;
50 import com
.actionbarsherlock
.app
.ActionBar
;
51 import com
.actionbarsherlock
.app
.ActionBar
.OnNavigationListener
;
52 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
53 import com
.actionbarsherlock
.view
.Menu
;
54 import com
.actionbarsherlock
.view
.MenuInflater
;
55 import com
.actionbarsherlock
.view
.MenuItem
;
56 import com
.actionbarsherlock
.view
.Window
;
58 import eu
.alefzero
.owncloud
.AccountUtils
;
59 import eu
.alefzero
.owncloud
.CrashHandler
;
60 import eu
.alefzero
.owncloud
.R
;
61 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
62 import eu
.alefzero
.owncloud
.datamodel
.DataStorageManager
;
63 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
;
64 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
65 import eu
.alefzero
.owncloud
.files
.services
.FileDownloader
;
66 import eu
.alefzero
.owncloud
.files
.services
.FileUploader
;
67 import eu
.alefzero
.owncloud
.syncadapter
.FileSyncService
;
68 import eu
.alefzero
.owncloud
.ui
.fragment
.FileDetailFragment
;
69 import eu
.alefzero
.owncloud
.ui
.fragment
.FileListFragment
;
70 import eu
.alefzero
.webdav
.WebdavClient
;
73 * Displays, what files the user has available in his ownCloud.
75 * @author Bartek Przybylski
79 public class FileDisplayActivity
extends SherlockFragmentActivity
implements
80 OnNavigationListener
, OnClickListener
, android
.view
.View
.OnClickListener
{
82 private ArrayAdapter
<String
> mDirectories
;
83 private OCFile mCurrentDir
;
84 private String
[] mDirs
= null
;
86 private DataStorageManager mStorageManager
;
87 private SyncBroadcastReceiver mSyncBroadcastReceiver
;
88 private UploadFinishReceiver mUploadFinishReceiver
;
90 private View mLayoutView
= null
;
91 private FileListFragment mFileList
;
93 private static final String KEY_DIR_ARRAY
= "DIR_ARRAY";
94 private static final String KEY_CURRENT_DIR
= "DIR";
96 private static final int DIALOG_SETUP_ACCOUNT
= 0;
97 private static final int DIALOG_CREATE_DIR
= 1;
98 private static final int DIALOG_ABOUT_APP
= 2;
100 private static final int ACTION_SELECT_FILE
= 1;
101 //private static final int ACTION_CREATE_FIRST_ACCOUNT = 2; dvelasco: WIP
104 public void onCreate(Bundle savedInstanceState
) {
105 Log
.i(getClass().toString(), "onCreate() start");
106 super.onCreate(savedInstanceState
);
108 requestWindowFeature(Window
.FEATURE_INDETERMINATE_PROGRESS
);
109 setSupportProgressBarIndeterminateVisibility(false
);
111 Thread
.setDefaultUncaughtExceptionHandler(new CrashHandler(getApplicationContext()));
113 if(savedInstanceState
!= null
){
114 mCurrentDir
= (OCFile
) savedInstanceState
.getParcelable(KEY_CURRENT_DIR
); // this is never saved with this key :S
117 mLayoutView
= getLayoutInflater().inflate(R
.layout
.files
, null
); // always inflate this at onCreate() ; just once!
119 if (AccountUtils
.accountsAreSetup(this)) {
120 setContentView(mLayoutView
);
123 setContentView(R
.layout
.no_account_available
);
124 setProgressBarIndeterminateVisibility(false
);
125 getSupportActionBar().setNavigationMode(ActionBar
.DISPLAY_SHOW_TITLE
);
126 findViewById(R
.id
.setup_account
).setOnClickListener(this);
129 Log
.i(getClass().toString(), "onCreate() end");
133 public boolean onCreateOptionsMenu(Menu menu
) {
134 MenuInflater inflater
= getSherlock().getMenuInflater();
135 inflater
.inflate(R
.menu
.menu
, menu
);
140 public boolean onOptionsItemSelected(MenuItem item
) {
141 boolean retval
= true
;
142 switch (item
.getItemId()) {
143 case R
.id
.createDirectoryItem
: {
144 showDialog(DIALOG_CREATE_DIR
);
147 case R
.id
.startSync
: {
148 Bundle bundle
= new Bundle();
149 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
150 bundle
.putString("PROBANDO", "PARAMETRO PASADO AL SYNC");
151 ContentResolver
.requestSync(
152 AccountUtils
.getCurrentOwnCloudAccount(this),
153 "org.owncloud", bundle
);
156 case R
.id
.action_upload
: {
157 Intent action
= new Intent(Intent
.ACTION_GET_CONTENT
);
158 action
= action
.setType("*/*")
159 .addCategory(Intent
.CATEGORY_OPENABLE
);
160 startActivityForResult(
161 Intent
.createChooser(action
, "Upload file from..."),
165 case R
.id
.action_settings
: {
166 Intent settingsIntent
= new Intent(this, Preferences
.class);
167 startActivity(settingsIntent
);
170 case R
.id
.about_app
: {
171 showDialog(DIALOG_ABOUT_APP
);
174 case android
.R
.id
.home
: {
175 if(mCurrentDir
!= null
&& mCurrentDir
.getParentId() != 0){
187 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
188 int i
= itemPosition
;
196 * Called, when the user selected something for uploading
198 public void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
199 if (requestCode
== ACTION_SELECT_FILE
) {
200 if (resultCode
== RESULT_OK
) {
201 Uri selectedImageUri
= data
.getData();
203 String filemanagerstring
= selectedImageUri
.getPath();
204 String selectedImagePath
= getPath(selectedImageUri
);
207 if (selectedImagePath
!= null
)
208 filepath
= selectedImagePath
;
210 filepath
= filemanagerstring
;
212 if (filepath
== null
) {
213 Log
.e("FileDisplay", "Couldnt resolve path to file");
217 Intent i
= new Intent(this, FileUploader
.class);
218 i
.putExtra(FileUploader
.KEY_ACCOUNT
,
219 AccountUtils
.getCurrentOwnCloudAccount(this));
220 String remotepath
= new String();
221 for (int j
= mDirectories
.getCount() - 2; j
>= 0; --j
) {
222 remotepath
+= "/" + mDirectories
.getItem(j
);
224 if (!remotepath
.endsWith("/"))
226 remotepath
+= new File(filepath
).getName();
227 remotepath
= Uri
.encode(remotepath
, "/");
229 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filepath
);
230 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotepath
);
231 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
235 }/* dvelasco: WIP - not working as expected ... yet :)
236 else if (requestCode == ACTION_CREATE_FIRST_ACCOUNT) {
237 if (resultCode != RESULT_OK) {
238 finish(); // the user cancelled the AuthenticatorActivity
244 public void onBackPressed() {
245 if (mDirectories
== null
|| mDirectories
.getCount() <= 1) {
250 mFileList
.onNavigateUp();
251 mCurrentDir
= mFileList
.getCurrentFile();
253 if(mCurrentDir
.getParentId() == 0){
254 ActionBar actionBar
= getSupportActionBar();
255 actionBar
.setDisplayHomeAsUpEnabled(false
);
260 protected void onRestoreInstanceState(Bundle savedInstanceState
) {
261 Log
.i(getClass().toString(), "onRestoreInstanceState() start");
262 super.onRestoreInstanceState(savedInstanceState
);
263 mDirs
= savedInstanceState
.getStringArray(KEY_DIR_ARRAY
);
264 mDirectories
= new CustomArrayAdapter
<String
>(this, R
.layout
.sherlock_spinner_dropdown_item
);
265 mDirectories
.add("/");
267 for (String s
: mDirs
)
268 mDirectories
.insert(s
, 0);
269 mCurrentDir
= savedInstanceState
.getParcelable(FileDetailFragment
.EXTRA_FILE
);
270 Log
.i(getClass().toString(), "onRestoreInstanceState() end");
274 protected void onSaveInstanceState(Bundle outState
) {
275 Log
.i(getClass().toString(), "onSaveInstanceState() start");
276 super.onSaveInstanceState(outState
);
277 if(mDirectories
!= null
&& mDirectories
.getCount() != 0){
278 mDirs
= new String
[mDirectories
.getCount()-1];
279 for (int j
= mDirectories
.getCount() - 2, i
= 0; j
>= 0; --j
, ++i
) {
280 mDirs
[i
] = mDirectories
.getItem(j
);
283 outState
.putStringArray(KEY_DIR_ARRAY
, mDirs
);
284 outState
.putParcelable(FileDetailFragment
.EXTRA_FILE
, mCurrentDir
);
285 Log
.i(getClass().toString(), "onSaveInstanceState() end");
289 protected void onResume() {
290 Log
.i(getClass().toString(), "onResume() start");
293 if (!AccountUtils
.accountsAreSetup(this)) {
294 /*Intent intent = new Intent(android.provider.Settings.ACTION_ADD_ACCOUNT);
295 intent.putExtra(android.provider.Settings.EXTRA_AUTHORITIES, new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });
296 //startActivity(intent);
297 startActivityForResult(intent, ACTION_CREATE_FIRST_ACCOUNT);*/
299 } else { // at least an account exist: normal operation
301 // set the layout only if it couldn't be set in onCreate
302 if (findViewById(R
.id
.file_list_view
) == null
)
303 setContentView(mLayoutView
);
305 // Listen for sync messages
306 IntentFilter syncIntentFilter
= new IntentFilter(FileSyncService
.SYNC_MESSAGE
);
307 mSyncBroadcastReceiver
= new SyncBroadcastReceiver();
308 registerReceiver(mSyncBroadcastReceiver
, syncIntentFilter
);
310 // Listen for upload messages
311 IntentFilter uploadIntentFilter
= new IntentFilter(FileUploader
.UPLOAD_FINISH_MESSAGE
);
312 mUploadFinishReceiver
= new UploadFinishReceiver();
313 registerReceiver(mUploadFinishReceiver
, uploadIntentFilter
);
315 // Storage manager initialization
316 mStorageManager
= new FileDataStorageManager(
317 AccountUtils
.getCurrentOwnCloudAccount(this),
318 getContentResolver());
321 mFileList
= (FileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
323 // Figure out what directory to list.
324 // Priority: Intent (here), savedInstanceState (onCreate), root dir (dir is null)
325 if(getIntent().hasExtra(FileDetailFragment
.EXTRA_FILE
)){
326 mCurrentDir
= (OCFile
) getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
327 if(mCurrentDir
!= null
&& !mCurrentDir
.isDirectory()){
328 mCurrentDir
= mStorageManager
.getFileById(mCurrentDir
.getParentId());
331 // Clear intent extra, so rotating the screen will not return us to this directory
332 getIntent().removeExtra(FileDetailFragment
.EXTRA_FILE
);
335 if (mCurrentDir
== null
)
336 mCurrentDir
= mStorageManager
.getFileByPath("/");
338 // Drop-Down navigation and file list restore
339 mDirectories
= new CustomArrayAdapter
<String
>(this, R
.layout
.sherlock_spinner_dropdown_item
);
342 // Given the case we have a file to display:
343 if(mCurrentDir
!= null
){
344 ArrayList
<OCFile
> files
= new ArrayList
<OCFile
>();
345 OCFile currFile
= mCurrentDir
;
346 while(currFile
!= null
){
348 currFile
= mStorageManager
.getFileById(currFile
.getParentId());
352 mDirs
= new String
[files
.size()];
353 for(int i
= files
.size() - 1; i
>= 0; i
--){
354 mDirs
[i
] = files
.get(i
).getFileName();
359 for (String s
: mDirs
)
362 mDirectories
.add("/");
366 ActionBar action_bar
= getSupportActionBar();
367 action_bar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_LIST
);
368 action_bar
.setDisplayShowTitleEnabled(false
);
369 action_bar
.setListNavigationCallbacks(mDirectories
, this);
370 if(mCurrentDir
!= null
&& mCurrentDir
.getParentId() != 0){
371 action_bar
.setDisplayHomeAsUpEnabled(true
);
373 action_bar
.setDisplayHomeAsUpEnabled(false
);
377 mFileList
.listDirectory(mCurrentDir
);
379 Log
.i(getClass().toString(), "onResume() end");
383 protected void onPause() {
384 Log
.i(getClass().toString(), "onPause() start");
386 if (mSyncBroadcastReceiver
!= null
) {
387 unregisterReceiver(mSyncBroadcastReceiver
);
388 mSyncBroadcastReceiver
= null
;
390 if (mUploadFinishReceiver
!= null
) {
391 unregisterReceiver(mUploadFinishReceiver
);
392 mUploadFinishReceiver
= null
;
394 getIntent().putExtra(FileDetailFragment
.EXTRA_FILE
, mCurrentDir
);
395 Log
.i(getClass().toString(), "onPause() end");
399 protected Dialog
onCreateDialog(int id
) {
400 Dialog dialog
= null
;
401 AlertDialog
.Builder builder
;
403 case DIALOG_SETUP_ACCOUNT
:
404 builder
= new AlertDialog
.Builder(this);
405 builder
.setTitle(R
.string
.main_tit_accsetup
);
406 builder
.setMessage(R
.string
.main_wrn_accsetup
);
407 builder
.setCancelable(false
);
408 builder
.setPositiveButton(android
.R
.string
.ok
, this);
409 builder
.setNegativeButton(android
.R
.string
.cancel
, this);
410 dialog
= builder
.create();
412 case DIALOG_ABOUT_APP
: {
413 builder
= new AlertDialog
.Builder(this);
414 builder
.setTitle("About");
417 pkg
= getPackageManager().getPackageInfo(getPackageName(), 0);
418 builder
.setMessage("ownCloud android client\n\nversion: " + pkg
.versionName
);
419 builder
.setIcon(android
.R
.drawable
.ic_menu_info_details
);
420 dialog
= builder
.create();
421 } catch (NameNotFoundException e
) {
428 case DIALOG_CREATE_DIR
: {
429 builder
= new Builder(this);
430 final EditText dirNameInput
= new EditText(getBaseContext());
431 final Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
432 builder
.setView(dirNameInput
);
433 builder
.setTitle(R
.string
.uploader_info_dirname
);
434 int typed_color
= getResources().getColor(R
.color
.setup_text_typed
);
435 dirNameInput
.setTextColor(typed_color
);
436 builder
.setPositiveButton(android
.R
.string
.ok
,
437 new OnClickListener() {
438 public void onClick(DialogInterface dialog
, int which
) {
439 String directoryName
= dirNameInput
.getText().toString();
440 if (directoryName
.trim().length() == 0) {
445 // Figure out the path where the dir needs to be created
447 if (mCurrentDir
== null
) {
448 // this is just a patch; we should ensure that mCurrentDir never is null
449 if (!mStorageManager
.fileExists("/")) {
450 OCFile file
= new OCFile("/");
451 mStorageManager
.saveFile(file
);
453 mCurrentDir
= mStorageManager
.getFileByPath("/");
455 path
= FileDisplayActivity
.this.mCurrentDir
.getRemotePath();
458 path
+= Uri
.encode(directoryName
) + "/";
459 Thread thread
= new Thread(new DirectoryCreator(path
, a
));
462 // Save new directory in local database
463 OCFile newDir
= new OCFile(path
);
464 newDir
.setMimetype("DIR");
465 newDir
.setParentId(mCurrentDir
.getFileId());
466 mStorageManager
.saveFile(newDir
);
468 // Display the new folder right away
470 mFileList
.listDirectory(mCurrentDir
);
473 builder
.setNegativeButton(R
.string
.common_cancel
,
474 new OnClickListener() {
475 public void onClick(DialogInterface dialog
, int which
) {
479 dialog
= builder
.create();
491 * Responds to the "There are no ownCloud Accounts setup" dialog
492 * TODO: Dialog is 100% useless -> Remove
495 public void onClick(DialogInterface dialog
, int which
) {
496 // In any case - we won't need it anymore
499 case DialogInterface
.BUTTON_POSITIVE
:
500 Intent intent
= new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
501 intent
.putExtra("authorities",
502 new String
[] { AccountAuthenticator
.AUTH_TOKEN_TYPE
});
503 startActivity(intent
);
505 case DialogInterface
.BUTTON_NEGATIVE
:
512 * Translates a content URI of an image to a physical path
514 * @param uri The URI to resolve
515 * @return The path to the image or null if it could not be found
517 public String
getPath(Uri uri
) {
518 String
[] projection
= { MediaStore
.Images
.Media
.DATA
};
519 Cursor cursor
= managedQuery(uri
, projection
, null
, null
, null
);
520 if (cursor
!= null
) {
521 int column_index
= cursor
522 .getColumnIndexOrThrow(MediaStore
.Images
.Media
.DATA
);
523 cursor
.moveToFirst();
524 return cursor
.getString(column_index
);
530 * Pushes a directory to the drop down list
531 * @param directory to push
532 * @throws IllegalArgumentException If the {@link OCFile#isDirectory()} returns false.
534 public void pushDirname(OCFile directory
) {
535 if(!directory
.isDirectory()){
536 throw new IllegalArgumentException("Only directories may be pushed!");
538 mDirectories
.insert(directory
.getFileName(), 0);
539 mCurrentDir
= directory
;
543 * Pops a directory name from the drop down list
544 * @return True, unless the stack is empty
546 public boolean popDirname() {
547 mDirectories
.remove(mDirectories
.getItem(0));
548 return !mDirectories
.isEmpty();
551 private class DirectoryCreator
implements Runnable
{
552 private String mTargetPath
;
553 private Account mAccount
;
554 private AccountManager mAm
;
556 public DirectoryCreator(String targetPath
, Account account
) {
557 mTargetPath
= targetPath
;
559 mAm
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
564 WebdavClient wdc
= new WebdavClient(Uri
.parse(mAm
.getUserData(
565 mAccount
, AccountAuthenticator
.KEY_OC_URL
)));
567 String username
= mAccount
.name
.substring(0,
568 mAccount
.name
.lastIndexOf('@'));
569 String password
= mAm
.getPassword(mAccount
);
571 wdc
.setCredentials(username
, password
);
572 wdc
.allowSelfsignedCertificates();
573 wdc
.createDirectory(mTargetPath
);
578 // Custom array adapter to override text colors
579 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
581 public CustomArrayAdapter(FileDisplayActivity ctx
, int view
) {
585 public View
getView(int position
, View convertView
, ViewGroup parent
) {
586 View v
= super.getView(position
, convertView
, parent
);
588 ((TextView
) v
).setTextColor(getResources().getColorStateList(
589 android
.R
.color
.white
));
593 public View
getDropDownView(int position
, View convertView
,
595 View v
= super.getDropDownView(position
, convertView
, parent
);
597 ((TextView
) v
).setTextColor(getResources().getColorStateList(
598 android
.R
.color
.white
));
605 private class SyncBroadcastReceiver
extends BroadcastReceiver
{
607 * {@link BroadcastReceiver} to enable syncing feedback in UI
610 public void onReceive(Context context
, Intent intent
) {
611 boolean inProgress
= intent
.getBooleanExtra(
612 FileSyncService
.IN_PROGRESS
, false
);
613 String account_name
= intent
614 .getStringExtra(FileSyncService
.ACCOUNT_NAME
);
615 Log
.d("FileDisplay", "sync of account " + account_name
616 + " is in_progress: " + inProgress
);
617 setSupportProgressBarIndeterminateVisibility(inProgress
);
619 long OCDirId
= intent
.getLongExtra(FileSyncService
.SYNC_FOLDER
, -1);
621 OCFile syncDir
= mStorageManager
.getFileById(OCDirId
);
622 if (syncDir
!= null
&& (
623 (mCurrentDir
== null
&& syncDir
.getFileName().equals("/")) ||
624 syncDir
.equals(mCurrentDir
))
626 FileListFragment fileListFragment
= (FileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
627 if (fileListFragment
!= null
) {
628 fileListFragment
.listDirectory();
634 FileListFragment fileListFragment
= (FileListFragment
) getSupportFragmentManager()
635 .findFragmentById(R
.id
.fileList
);
636 if (fileListFragment
!= null
)
637 fileListFragment
.listDirectory();
644 private class UploadFinishReceiver
extends BroadcastReceiver
{
646 * Once the file upload has finished -> update view
647 * @author David A. Velasco
648 * {@link BroadcastReceiver} to enable upload feedback in UI
651 public void onReceive(Context context
, Intent intent
) {
652 long parentDirId
= intent
.getLongExtra(FileUploader
.EXTRA_PARENT_DIR_ID
, -1);
653 OCFile parentDir
= mStorageManager
.getFileById(parentDirId
);
655 if (parentDir
!= null
&& (
656 (mCurrentDir
== null
&& parentDir
.getFileName().equals("/")) ||
657 parentDir
.equals(mCurrentDir
))
659 FileListFragment fileListFragment
= (FileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
660 if (fileListFragment
!= null
) {
661 fileListFragment
.listDirectory();
670 public void onClick(View v
) {
671 if (v
.getId() == R
.id
.setup_account
) {
672 Intent intent
= new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
673 intent
.putExtra("authorities", new String
[] { AccountAuthenticator
.AUTH_TOKEN_TYPE
});
674 startActivity(intent
);
678 public DataStorageManager
getStorageManager() {
679 return mStorageManager
;