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 com
.owncloud
.android
.ui
.activity
;
23 import android
.accounts
.Account
;
24 import android
.accounts
.AccountManager
;
25 import android
.app
.AlertDialog
;
26 import android
.app
.ProgressDialog
;
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
.SharedPreferences
;
37 import android
.content
.pm
.PackageInfo
;
38 import android
.content
.pm
.PackageManager
.NameNotFoundException
;
39 import android
.content
.res
.Resources
.NotFoundException
;
40 import android
.database
.Cursor
;
41 import android
.net
.Uri
;
42 import android
.os
.Bundle
;
43 import android
.os
.Environment
;
44 import android
.os
.Handler
;
45 import android
.preference
.PreferenceManager
;
46 import android
.provider
.MediaStore
;
47 import android
.support
.v4
.app
.FragmentTransaction
;
48 import android
.util
.Log
;
49 import android
.view
.View
;
50 import android
.view
.ViewGroup
;
51 import android
.widget
.ArrayAdapter
;
52 import android
.widget
.EditText
;
53 import android
.widget
.TextView
;
54 import android
.widget
.Toast
;
56 import com
.actionbarsherlock
.app
.ActionBar
;
57 import com
.actionbarsherlock
.app
.ActionBar
.OnNavigationListener
;
58 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
59 import com
.actionbarsherlock
.view
.Menu
;
60 import com
.actionbarsherlock
.view
.MenuInflater
;
61 import com
.actionbarsherlock
.view
.MenuItem
;
62 import com
.actionbarsherlock
.view
.Window
;
63 import com
.owncloud
.android
.AccountUtils
;
64 import com
.owncloud
.android
.authenticator
.AccountAuthenticator
;
65 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
66 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
67 import com
.owncloud
.android
.datamodel
.OCFile
;
68 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
69 import com
.owncloud
.android
.files
.OwnCloudFileObserver
;
70 import com
.owncloud
.android
.files
.services
.FileDownloader
;
71 import com
.owncloud
.android
.files
.services
.FileObserverService
;
72 import com
.owncloud
.android
.files
.services
.FileUploader
;
73 import com
.owncloud
.android
.network
.OwnCloudClientUtils
;
74 import com
.owncloud
.android
.syncadapter
.FileSyncService
;
75 import com
.owncloud
.android
.ui
.fragment
.FileDetailFragment
;
76 import com
.owncloud
.android
.ui
.fragment
.OCFileListFragment
;
78 import com
.owncloud
.android
.R
;
79 import eu
.alefzero
.webdav
.WebdavClient
;
82 * Displays, what files the user has available in his ownCloud.
84 * @author Bartek Przybylski
88 public class FileDisplayActivity
extends SherlockFragmentActivity
implements
89 OCFileListFragment
.ContainerActivity
, FileDetailFragment
.ContainerActivity
, OnNavigationListener
{
91 private ArrayAdapter
<String
> mDirectories
;
92 private OCFile mCurrentDir
;
94 private DataStorageManager mStorageManager
;
95 private SyncBroadcastReceiver mSyncBroadcastReceiver
;
96 private UploadFinishReceiver mUploadFinishReceiver
;
97 private DownloadFinishReceiver mDownloadFinishReceiver
;
99 private OCFileListFragment mFileList
;
101 private boolean mDualPane
;
103 private static final int DIALOG_SETUP_ACCOUNT
= 0;
104 private static final int DIALOG_CREATE_DIR
= 1;
105 private static final int DIALOG_ABOUT_APP
= 2;
106 public static final int DIALOG_SHORT_WAIT
= 3;
107 private static final int DIALOG_CHOOSE_UPLOAD_SOURCE
= 4;
109 private static final int ACTION_SELECT_CONTENT_FROM_APPS
= 1;
110 private static final int ACTION_SELECT_MULTIPLE_FILES
= 2;
112 private static final String TAG
= "FileDisplayActivity";
115 public void onCreate(Bundle savedInstanceState
) {
116 Log
.d(getClass().toString(), "onCreate() start");
117 super.onCreate(savedInstanceState
);
119 /// saved instance state: keep this always before initDataFromCurrentAccount()
120 if(savedInstanceState
!= null
) {
121 mCurrentDir
= savedInstanceState
.getParcelable(FileDetailFragment
.EXTRA_FILE
);
124 if (!AccountUtils
.accountsAreSetup(this)) {
125 /// no account available: FORCE ACCOUNT CREATION
126 mStorageManager
= null
;
127 createFirstAccount();
129 } else { /// at least an account is available
131 initDataFromCurrentAccount();
135 // PIN CODE request ; best location is to decide, let's try this first
136 if (getIntent().getAction() != null
&& getIntent().getAction().equals(Intent
.ACTION_MAIN
) && savedInstanceState
== null
) {
141 /*Intent observer_intent = new Intent(this, FileObserverService.class);
142 observer_intent.putExtra(FileObserverService.KEY_FILE_CMD, FileObserverService.CMD_INIT_OBSERVED_LIST);
143 startService(observer_intent);
147 requestWindowFeature(Window
.FEATURE_INDETERMINATE_PROGRESS
);
149 // Drop-down navigation
150 mDirectories
= new CustomArrayAdapter
<String
>(this, R
.layout
.sherlock_spinner_dropdown_item
);
151 OCFile currFile
= mCurrentDir
;
152 while(currFile
!= null
&& currFile
.getFileName() != OCFile
.PATH_SEPARATOR
) {
153 mDirectories
.add(currFile
.getFileName());
154 currFile
= mStorageManager
.getFileById(currFile
.getParentId());
156 mDirectories
.add(OCFile
.PATH_SEPARATOR
);
158 // Inflate and set the layout view
159 setContentView(R
.layout
.files
);
160 mFileList
= (OCFileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
161 mDualPane
= (findViewById(R
.id
.file_details_container
) != null
);
162 if (mDualPane
&& getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
) == null
) {
163 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
164 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(null
, null
)); // empty FileDetailFragment
165 transaction
.commit();
169 ActionBar actionBar
= getSupportActionBar();
170 actionBar
.setHomeButtonEnabled(true
); // mandatory since Android ICS, according to the official documentation
171 actionBar
.setDisplayHomeAsUpEnabled(mCurrentDir
!= null
&& mCurrentDir
.getParentId() != 0);
172 actionBar
.setDisplayShowTitleEnabled(false
);
173 actionBar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_LIST
);
174 actionBar
.setListNavigationCallbacks(mDirectories
, this);
175 setSupportProgressBarIndeterminateVisibility(false
); // always AFTER setContentView(...) ; to workaround bug in its implementation
177 Log
.d(getClass().toString(), "onCreate() end");
182 * Launches the account creation activity. To use when no ownCloud account is available
184 private void createFirstAccount() {
185 Intent intent
= new Intent(android
.provider
.Settings
.ACTION_ADD_ACCOUNT
);
186 intent
.putExtra(android
.provider
.Settings
.EXTRA_AUTHORITIES
, new String
[] { AccountAuthenticator
.AUTH_TOKEN_TYPE
});
187 startActivity(intent
); // the new activity won't be created until this.onStart() and this.onResume() are finished;
192 * Load of state dependent of the existence of an ownCloud account
194 private void initDataFromCurrentAccount() {
195 /// Storage manager initialization - access to local database
196 mStorageManager
= new FileDataStorageManager(
197 AccountUtils
.getCurrentOwnCloudAccount(this),
198 getContentResolver());
200 /// State recovery - CURRENT DIRECTORY ; priority: 1/ getIntent(), 2/ savedInstanceState (in onCreate()), 3/ root dir
201 if(getIntent().hasExtra(FileDetailFragment
.EXTRA_FILE
)) {
202 mCurrentDir
= (OCFile
) getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
203 if(mCurrentDir
!= null
&& !mCurrentDir
.isDirectory()){
204 mCurrentDir
= mStorageManager
.getFileById(mCurrentDir
.getParentId());
206 // clear intent extra, so rotating the screen will not return us to this directory
207 getIntent().removeExtra(FileDetailFragment
.EXTRA_FILE
);
209 if (mCurrentDir
== null
)
210 mCurrentDir
= mStorageManager
.getFileByPath("/"); // this will return NULL if the database has not ever synchronized
215 public boolean onCreateOptionsMenu(Menu menu
) {
216 MenuInflater inflater
= getSherlock().getMenuInflater();
217 inflater
.inflate(R
.menu
.menu
, menu
);
222 public boolean onOptionsItemSelected(MenuItem item
) {
223 boolean retval
= true
;
224 switch (item
.getItemId()) {
225 case R
.id
.createDirectoryItem
: {
226 showDialog(DIALOG_CREATE_DIR
);
229 case R
.id
.startSync
: {
230 ContentResolver
.cancelSync(null
, AccountAuthenticator
.AUTH_TOKEN_TYPE
); // cancel the current synchronizations of any ownCloud account
231 Bundle bundle
= new Bundle();
232 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
233 ContentResolver
.requestSync(
234 AccountUtils
.getCurrentOwnCloudAccount(this),
235 AccountAuthenticator
.AUTH_TOKEN_TYPE
, bundle
);
238 case R
.id
.action_upload
: {
239 showDialog(DIALOG_CHOOSE_UPLOAD_SOURCE
);
242 case R
.id
.action_settings
: {
243 Intent settingsIntent
= new Intent(this, Preferences
.class);
244 startActivity(settingsIntent
);
247 case R
.id
.about_app
: {
248 showDialog(DIALOG_ABOUT_APP
);
251 case android
.R
.id
.home
: {
252 if(mCurrentDir
!= null
&& mCurrentDir
.getParentId() != 0){
264 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
265 int i
= itemPosition
;
269 // the next operation triggers a new call to this method, but it's necessary to
270 // ensure that the name exposed in the action bar is the current directory when the
271 // user selected it in the navigation list
272 if (itemPosition
!= 0)
273 getSupportActionBar().setSelectedNavigationItem(0);
278 * Called, when the user selected something for uploading
280 public void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
282 if (requestCode
== ACTION_SELECT_CONTENT_FROM_APPS
&& resultCode
== RESULT_OK
) {
283 requestSimpleUpload(data
);
285 } else if (requestCode
== ACTION_SELECT_MULTIPLE_FILES
&& resultCode
== RESULT_OK
) {
286 requestMultipleUpload(data
);
291 private void requestMultipleUpload(Intent data
) {
292 String
[] filePaths
= data
.getStringArrayExtra(UploadFilesActivity
.EXTRA_CHOSEN_FILES
);
293 if (filePaths
!= null
) {
294 String
[] remotePaths
= new String
[filePaths
.length
];
295 String remotePathBase
= "";
296 for (int j
= mDirectories
.getCount() - 2; j
>= 0; --j
) {
297 remotePathBase
+= OCFile
.PATH_SEPARATOR
+ mDirectories
.getItem(j
);
299 if (!remotePathBase
.endsWith(OCFile
.PATH_SEPARATOR
))
300 remotePathBase
+= OCFile
.PATH_SEPARATOR
;
301 for (int j
= 0; j
< remotePaths
.length
; j
++) {
302 remotePaths
[j
] = remotePathBase
+ (new File(filePaths
[j
])).getName();
305 Intent i
= new Intent(this, FileUploader
.class);
306 i
.putExtra(FileUploader
.KEY_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
307 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filePaths
);
308 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotePaths
);
309 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
313 Log
.d("FileDisplay", "User clicked on 'Update' with no selection");
314 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_no_file_selected
), Toast
.LENGTH_LONG
);
321 private void requestSimpleUpload(Intent data
) {
322 String filepath
= null
;
324 Uri selectedImageUri
= data
.getData();
326 String filemanagerstring
= selectedImageUri
.getPath();
327 String selectedImagePath
= getPath(selectedImageUri
);
329 if (selectedImagePath
!= null
)
330 filepath
= selectedImagePath
;
332 filepath
= filemanagerstring
;
334 } catch (Exception e
) {
335 Log
.e("FileDisplay", "Unexpected exception when trying to read the result of Intent.ACTION_GET_CONTENT", e
);
339 if (filepath
== null
) {
340 Log
.e("FileDisplay", "Couldnt resolve path to file");
341 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_unexpected_bad_get_content
), Toast
.LENGTH_LONG
);
347 Intent i
= new Intent(this, FileUploader
.class);
348 i
.putExtra(FileUploader
.KEY_ACCOUNT
,
349 AccountUtils
.getCurrentOwnCloudAccount(this));
350 String remotepath
= new String();
351 for (int j
= mDirectories
.getCount() - 2; j
>= 0; --j
) {
352 remotepath
+= OCFile
.PATH_SEPARATOR
+ mDirectories
.getItem(j
);
354 if (!remotepath
.endsWith(OCFile
.PATH_SEPARATOR
))
355 remotepath
+= OCFile
.PATH_SEPARATOR
;
356 remotepath
+= new File(filepath
).getName();
358 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filepath
);
359 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotepath
);
360 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
366 public void onBackPressed() {
367 if (mDirectories
.getCount() <= 1) {
372 mFileList
.onNavigateUp();
373 mCurrentDir
= mFileList
.getCurrentFile();
376 // Resets the FileDetailsFragment on Tablets so that it always displays
377 FileDetailFragment fileDetails
= (FileDetailFragment
) getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
378 if (fileDetails
!= null
&& !fileDetails
.isEmpty()) {
379 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
380 transaction
.remove(fileDetails
);
381 transaction
.add(R
.id
.file_details_container
, new FileDetailFragment(null
, null
));
382 transaction
.commit();
386 if(mCurrentDir
.getParentId() == 0){
387 ActionBar actionBar
= getSupportActionBar();
388 actionBar
.setDisplayHomeAsUpEnabled(false
);
393 protected void onSaveInstanceState(Bundle outState
) {
394 // responsibility of restore is preferred in onCreate() before than in onRestoreInstanceState when there are Fragments involved
395 Log
.d(getClass().toString(), "onSaveInstanceState() start");
396 super.onSaveInstanceState(outState
);
397 outState
.putParcelable(FileDetailFragment
.EXTRA_FILE
, mCurrentDir
);
398 Log
.d(getClass().toString(), "onSaveInstanceState() end");
402 protected void onResume() {
403 Log
.d(getClass().toString(), "onResume() start");
406 if (AccountUtils
.accountsAreSetup(this)) {
408 if (mStorageManager
== null
) {
409 // this is necessary for handling the come back to FileDisplayActivity when the first ownCloud account is created
410 initDataFromCurrentAccount();
413 // Listen for sync messages
414 IntentFilter syncIntentFilter
= new IntentFilter(FileSyncService
.SYNC_MESSAGE
);
415 mSyncBroadcastReceiver
= new SyncBroadcastReceiver();
416 registerReceiver(mSyncBroadcastReceiver
, syncIntentFilter
);
418 // Listen for upload messages
419 IntentFilter uploadIntentFilter
= new IntentFilter(FileUploader
.UPLOAD_FINISH_MESSAGE
);
420 mUploadFinishReceiver
= new UploadFinishReceiver();
421 registerReceiver(mUploadFinishReceiver
, uploadIntentFilter
);
423 // Listen for download messages
424 IntentFilter downloadIntentFilter
= new IntentFilter(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
425 mDownloadFinishReceiver
= new DownloadFinishReceiver();
426 registerReceiver(mDownloadFinishReceiver
, downloadIntentFilter
);
428 // List current directory
429 mFileList
.listDirectory(mCurrentDir
);
433 mStorageManager
= null
; // an invalid object will be there if all the ownCloud accounts are removed
434 showDialog(DIALOG_SETUP_ACCOUNT
);
437 Log
.d(getClass().toString(), "onResume() end");
441 protected void onPause() {
442 Log
.d(getClass().toString(), "onPause() start");
444 if (mSyncBroadcastReceiver
!= null
) {
445 unregisterReceiver(mSyncBroadcastReceiver
);
446 mSyncBroadcastReceiver
= null
;
448 if (mUploadFinishReceiver
!= null
) {
449 unregisterReceiver(mUploadFinishReceiver
);
450 mUploadFinishReceiver
= null
;
452 if (mDownloadFinishReceiver
!= null
) {
453 unregisterReceiver(mDownloadFinishReceiver
);
454 mDownloadFinishReceiver
= null
;
456 if (!AccountUtils
.accountsAreSetup(this)) {
457 dismissDialog(DIALOG_SETUP_ACCOUNT
);
460 getIntent().putExtra(FileDetailFragment
.EXTRA_FILE
, mCurrentDir
);
461 Log
.d(getClass().toString(), "onPause() end");
465 protected Dialog
onCreateDialog(int id
) {
466 Dialog dialog
= null
;
467 AlertDialog
.Builder builder
;
469 case DIALOG_SETUP_ACCOUNT
: {
470 builder
= new AlertDialog
.Builder(this);
471 builder
.setTitle(R
.string
.main_tit_accsetup
);
472 builder
.setMessage(R
.string
.main_wrn_accsetup
);
473 builder
.setCancelable(false
);
474 builder
.setPositiveButton(android
.R
.string
.ok
, new OnClickListener() {
475 public void onClick(DialogInterface dialog
, int which
) {
476 createFirstAccount();
480 builder
.setNegativeButton(R
.string
.common_exit
, new OnClickListener() {
481 public void onClick(DialogInterface dialog
, int which
) {
486 //builder.setNegativeButton(android.R.string.cancel, this);
487 dialog
= builder
.create();
490 case DIALOG_ABOUT_APP
: {
491 builder
= new AlertDialog
.Builder(this);
492 builder
.setTitle(getString(R
.string
.about_title
));
495 pkg
= getPackageManager().getPackageInfo(getPackageName(), 0);
496 builder
.setMessage("ownCloud android client\n\nversion: " + pkg
.versionName
);
497 builder
.setIcon(android
.R
.drawable
.ic_menu_info_details
);
498 dialog
= builder
.create();
499 } catch (NameNotFoundException e
) {
502 Log
.e(TAG
, "Error while showing about dialog", e
);
506 case DIALOG_CREATE_DIR
: {
507 builder
= new Builder(this);
508 final EditText dirNameInput
= new EditText(getBaseContext());
509 builder
.setView(dirNameInput
);
510 builder
.setTitle(R
.string
.uploader_info_dirname
);
511 int typed_color
= getResources().getColor(R
.color
.setup_text_typed
);
512 dirNameInput
.setTextColor(typed_color
);
513 builder
.setPositiveButton(android
.R
.string
.ok
,
514 new OnClickListener() {
515 public void onClick(DialogInterface dialog
, int which
) {
516 String directoryName
= dirNameInput
.getText().toString();
517 if (directoryName
.trim().length() == 0) {
522 // Figure out the path where the dir needs to be created
524 if (mCurrentDir
== null
) {
525 // this is just a patch; we should ensure that mCurrentDir never is null
526 if (!mStorageManager
.fileExists(OCFile
.PATH_SEPARATOR
)) {
527 OCFile file
= new OCFile(OCFile
.PATH_SEPARATOR
);
528 mStorageManager
.saveFile(file
);
530 mCurrentDir
= mStorageManager
.getFileByPath(OCFile
.PATH_SEPARATOR
);
532 path
= FileDisplayActivity
.this.mCurrentDir
.getRemotePath();
535 path
+= directoryName
+ OCFile
.PATH_SEPARATOR
;
536 Thread thread
= new Thread(new DirectoryCreator(path
, AccountUtils
.getCurrentOwnCloudAccount(FileDisplayActivity
.this), new Handler()));
541 showDialog(DIALOG_SHORT_WAIT
);
544 builder
.setNegativeButton(R
.string
.common_cancel
,
545 new OnClickListener() {
546 public void onClick(DialogInterface dialog
, int which
) {
550 dialog
= builder
.create();
553 case DIALOG_SHORT_WAIT
: {
554 ProgressDialog working_dialog
= new ProgressDialog(this);
555 working_dialog
.setMessage(getResources().getString(
556 R
.string
.wait_a_moment
));
557 working_dialog
.setIndeterminate(true
);
558 working_dialog
.setCancelable(false
);
559 dialog
= working_dialog
;
562 case DIALOG_CHOOSE_UPLOAD_SOURCE
: {
563 final String
[] items
= { getString(R
.string
.actionbar_upload_files
),
564 getString(R
.string
.actionbar_upload_from_apps
) };
565 builder
= new AlertDialog
.Builder(this);
566 builder
.setTitle(R
.string
.actionbar_upload
);
567 builder
.setItems(items
, new DialogInterface
.OnClickListener() {
568 public void onClick(DialogInterface dialog
, int item
) {
571 Intent action
= new Intent(FileDisplayActivity
.this, UploadFilesActivity
.class);
572 startActivityForResult(action
, ACTION_SELECT_MULTIPLE_FILES
);
574 // TODO create and handle new fragment LocalFileListFragment
576 } else if (item
== 1) {
577 Intent action
= new Intent(Intent
.ACTION_GET_CONTENT
);
578 action
= action
.setType("*/*")
579 .addCategory(Intent
.CATEGORY_OPENABLE
);
580 startActivityForResult(
581 Intent
.createChooser(action
, getString(R
.string
.upload_chooser_title
)),
582 ACTION_SELECT_CONTENT_FROM_APPS
);
586 dialog
= builder
.create();
598 * Translates a content URI of an image to a physical path
600 * @param uri The URI to resolve
601 * @return The path to the image or null if it could not be found
603 public String
getPath(Uri uri
) {
604 String
[] projection
= { MediaStore
.Images
.Media
.DATA
};
605 Cursor cursor
= managedQuery(uri
, projection
, null
, null
, null
);
606 if (cursor
!= null
) {
607 int column_index
= cursor
608 .getColumnIndexOrThrow(MediaStore
.Images
.Media
.DATA
);
609 cursor
.moveToFirst();
610 return cursor
.getString(column_index
);
616 * Pushes a directory to the drop down list
617 * @param directory to push
618 * @throws IllegalArgumentException If the {@link OCFile#isDirectory()} returns false.
620 public void pushDirname(OCFile directory
) {
621 if(!directory
.isDirectory()){
622 throw new IllegalArgumentException("Only directories may be pushed!");
624 mDirectories
.insert(directory
.getFileName(), 0);
625 mCurrentDir
= directory
;
629 * Pops a directory name from the drop down list
630 * @return True, unless the stack is empty
632 public boolean popDirname() {
633 mDirectories
.remove(mDirectories
.getItem(0));
634 return !mDirectories
.isEmpty();
637 private class DirectoryCreator
implements Runnable
{
638 private String mTargetPath
;
639 private Account mAccount
;
640 private AccountManager mAm
;
641 private Handler mHandler
;
643 public DirectoryCreator(String targetPath
, Account account
, Handler handler
) {
644 mTargetPath
= targetPath
;
646 mAm
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
652 WebdavClient wdc
= OwnCloudClientUtils
.createOwnCloudClient(mAccount
, getApplicationContext());
653 boolean created
= wdc
.createDirectory(mTargetPath
);
655 mHandler
.post(new Runnable() {
658 dismissDialog(DIALOG_SHORT_WAIT
);
660 // Save new directory in local database
661 OCFile newDir
= new OCFile(mTargetPath
);
662 newDir
.setMimetype("DIR");
663 newDir
.setParentId(mCurrentDir
.getFileId());
664 mStorageManager
.saveFile(newDir
);
666 // Display the new folder right away
667 mFileList
.listDirectory(mCurrentDir
);
672 mHandler
.post(new Runnable() {
675 dismissDialog(DIALOG_SHORT_WAIT
);
677 Toast msg
= Toast
.makeText(FileDisplayActivity
.this, R
.string
.create_dir_fail_msg
, Toast
.LENGTH_LONG
);
680 } catch (NotFoundException e
) {
681 Log
.e(TAG
, "Error while trying to show fail message " , e
);
690 // Custom array adapter to override text colors
691 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
693 public CustomArrayAdapter(FileDisplayActivity ctx
, int view
) {
697 public View
getView(int position
, View convertView
, ViewGroup parent
) {
698 View v
= super.getView(position
, convertView
, parent
);
700 ((TextView
) v
).setTextColor(getResources().getColorStateList(
701 android
.R
.color
.white
));
705 public View
getDropDownView(int position
, View convertView
,
707 View v
= super.getDropDownView(position
, convertView
, parent
);
709 ((TextView
) v
).setTextColor(getResources().getColorStateList(
710 android
.R
.color
.white
));
717 private class SyncBroadcastReceiver
extends BroadcastReceiver
{
719 * {@link BroadcastReceiver} to enable syncing feedback in UI
722 public void onReceive(Context context
, Intent intent
) {
723 boolean inProgress
= intent
.getBooleanExtra(
724 FileSyncService
.IN_PROGRESS
, false
);
725 String accountName
= intent
726 .getStringExtra(FileSyncService
.ACCOUNT_NAME
);
728 Log
.d("FileDisplay", "sync of account " + accountName
729 + " is in_progress: " + inProgress
);
731 if (accountName
.equals(AccountUtils
.getCurrentOwnCloudAccount(context
).name
)) {
733 String synchFolderRemotePath
= intent
.getStringExtra(FileSyncService
.SYNC_FOLDER_REMOTE_PATH
);
735 boolean fillBlankRoot
= false
;
736 if (mCurrentDir
== null
) {
737 mCurrentDir
= mStorageManager
.getFileByPath("/");
738 fillBlankRoot
= (mCurrentDir
!= null
);
741 if ((synchFolderRemotePath
!= null
&& mCurrentDir
!= null
&& (mCurrentDir
.getRemotePath().equals(synchFolderRemotePath
)))
744 mCurrentDir
= getStorageManager().getFileByPath(synchFolderRemotePath
);
745 OCFileListFragment fileListFragment
= (OCFileListFragment
) getSupportFragmentManager()
746 .findFragmentById(R
.id
.fileList
);
747 if (fileListFragment
!= null
) {
748 fileListFragment
.listDirectory(mCurrentDir
);
752 setSupportProgressBarIndeterminateVisibility(inProgress
);
759 private class UploadFinishReceiver
extends BroadcastReceiver
{
761 * Once the file upload has finished -> update view
762 * @author David A. Velasco
763 * {@link BroadcastReceiver} to enable upload feedback in UI
766 public void onReceive(Context context
, Intent intent
) {
767 long parentDirId
= intent
.getLongExtra(FileUploader
.EXTRA_PARENT_DIR_ID
, -1);
768 OCFile parentDir
= mStorageManager
.getFileById(parentDirId
);
769 String accountName
= intent
.getStringExtra(FileUploader
.ACCOUNT_NAME
);
771 if (accountName
.equals(AccountUtils
.getCurrentOwnCloudAccount(context
).name
) &&
773 ( (mCurrentDir
== null
&& parentDir
.getFileName().equals("/")) ||
774 parentDir
.equals(mCurrentDir
)
777 OCFileListFragment fileListFragment
= (OCFileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
778 if (fileListFragment
!= null
) {
779 fileListFragment
.listDirectory();
788 * Once the file download has finished -> update view
790 private class DownloadFinishReceiver
extends BroadcastReceiver
{
792 public void onReceive(Context context
, Intent intent
) {
793 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
794 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
796 if (accountName
.equals(AccountUtils
.getCurrentOwnCloudAccount(context
).name
) &&
797 mCurrentDir
!= null
&& mCurrentDir
.getFileId() == mStorageManager
.getFileByPath(downloadedRemotePath
).getParentId()) {
798 OCFileListFragment fileListFragment
= (OCFileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
799 if (fileListFragment
!= null
) {
800 fileListFragment
.listDirectory();
813 public DataStorageManager
getStorageManager() {
814 return mStorageManager
;
822 public void onDirectoryClick(OCFile directory
) {
823 pushDirname(directory
);
824 ActionBar actionBar
= getSupportActionBar();
825 actionBar
.setDisplayHomeAsUpEnabled(true
);
828 // Resets the FileDetailsFragment on Tablets so that it always displays
829 FileDetailFragment fileDetails
= (FileDetailFragment
) getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
830 if (fileDetails
!= null
&& !fileDetails
.isEmpty()) {
831 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
832 transaction
.remove(fileDetails
);
833 transaction
.add(R
.id
.file_details_container
, new FileDetailFragment(null
, null
));
834 transaction
.commit();
844 public void onFileClick(OCFile file
) {
846 // If we are on a large device -> update fragment
848 // buttons in the details view are problematic when trying to reuse an existing fragment; create always a new one solves some of them, BUT no all; downloads are 'dangerous'
849 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
850 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(file
, AccountUtils
.getCurrentOwnCloudAccount(this)), FileDetailFragment
.FTAG
);
851 transaction
.setTransition(FragmentTransaction
.TRANSIT_FRAGMENT_FADE
);
852 transaction
.commit();
854 } else { // small or medium screen device -> new Activity
855 Intent showDetailsIntent
= new Intent(this, FileDetailActivity
.class);
856 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
857 showDetailsIntent
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
858 startActivity(showDetailsIntent
);
867 public void onFileStateChanged() {
868 OCFileListFragment fileListFragment
= (OCFileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
869 if (fileListFragment
!= null
) {
870 fileListFragment
.listDirectory();
875 * Launch an intent to request the PIN code to the user before letting him use the app
877 private void requestPinCode() {
878 boolean pinStart
= false
;
879 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
880 pinStart
= appPrefs
.getBoolean("set_pincode", false
);
882 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
883 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "FileDisplayActivity");