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
.app
.AlertDialog
;
25 import android
.app
.ProgressDialog
;
26 import android
.app
.AlertDialog
.Builder
;
27 import android
.app
.Dialog
;
28 import android
.content
.BroadcastReceiver
;
29 import android
.content
.ComponentName
;
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
.ServiceConnection
;
37 import android
.content
.SharedPreferences
;
38 import android
.content
.pm
.PackageInfo
;
39 import android
.content
.pm
.PackageManager
.NameNotFoundException
;
40 import android
.content
.res
.Resources
.NotFoundException
;
41 import android
.database
.Cursor
;
42 import android
.net
.Uri
;
43 import android
.os
.Bundle
;
44 import android
.os
.Handler
;
45 import android
.os
.IBinder
;
46 import android
.preference
.PreferenceManager
;
47 import android
.provider
.MediaStore
;
48 import android
.support
.v4
.app
.FragmentTransaction
;
49 import android
.util
.Log
;
50 import android
.view
.View
;
51 import android
.view
.ViewGroup
;
52 import android
.widget
.ArrayAdapter
;
53 import android
.widget
.EditText
;
54 import android
.widget
.TextView
;
55 import android
.widget
.Toast
;
57 import com
.actionbarsherlock
.app
.ActionBar
;
58 import com
.actionbarsherlock
.app
.ActionBar
.OnNavigationListener
;
59 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
60 import com
.actionbarsherlock
.view
.Menu
;
61 import com
.actionbarsherlock
.view
.MenuInflater
;
62 import com
.actionbarsherlock
.view
.MenuItem
;
63 import com
.actionbarsherlock
.view
.Window
;
64 import com
.owncloud
.android
.AccountUtils
;
65 import com
.owncloud
.android
.authenticator
.AccountAuthenticator
;
66 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
67 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
68 import com
.owncloud
.android
.datamodel
.OCFile
;
69 import com
.owncloud
.android
.files
.services
.FileDownloader
;
70 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
71 import com
.owncloud
.android
.files
.services
.FileUploader
;
72 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
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
= null
;
93 private OCFile mCurrentFile
= null
;
95 private DataStorageManager mStorageManager
;
96 private SyncBroadcastReceiver mSyncBroadcastReceiver
;
97 private UploadFinishReceiver mUploadFinishReceiver
;
98 private DownloadFinishReceiver mDownloadFinishReceiver
;
99 private FileDownloaderBinder mDownloaderBinder
= null
;
100 private FileUploaderBinder mUploaderBinder
= null
;
101 private ServiceConnection mDownloadConnection
= null
, mUploadConnection
= null
;
103 private OCFileListFragment mFileList
;
105 private boolean mDualPane
;
107 private static final int DIALOG_SETUP_ACCOUNT
= 0;
108 private static final int DIALOG_CREATE_DIR
= 1;
109 private static final int DIALOG_ABOUT_APP
= 2;
110 public static final int DIALOG_SHORT_WAIT
= 3;
111 private static final int DIALOG_CHOOSE_UPLOAD_SOURCE
= 4;
113 private static final int ACTION_SELECT_CONTENT_FROM_APPS
= 1;
114 private static final int ACTION_SELECT_MULTIPLE_FILES
= 2;
116 private static final String TAG
= "FileDisplayActivity";
119 public void onCreate(Bundle savedInstanceState
) {
120 Log
.d(getClass().toString(), "onCreate() start");
121 super.onCreate(savedInstanceState
);
123 /// Load of parameters from received intent
124 mCurrentDir
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
); // no check necessary, mCurrenDir == null if the parameter is not in the intent
125 Account account
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
);
127 AccountUtils
.setCurrentOwnCloudAccount(this, account
.name
);
129 /// Load of saved instance state: keep this always before initDataFromCurrentAccount()
130 if(savedInstanceState
!= null
) {
131 // TODO - test if savedInstanceState should take precedence over file in the intent ALWAYS (now), NEVER, or SOME TIMES
132 mCurrentDir
= savedInstanceState
.getParcelable(FileDetailFragment
.EXTRA_FILE
);
135 if (!AccountUtils
.accountsAreSetup(this)) {
136 /// no account available: FORCE ACCOUNT CREATION
137 mStorageManager
= null
;
138 createFirstAccount();
140 } else { /// at least an account is available
142 initDataFromCurrentAccount(); // it checks mCurrentDir and mCurrentFile with the current account
146 mUploadConnection
= new ListServiceConnection();
147 mDownloadConnection
= new ListServiceConnection();
148 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
149 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
151 // PIN CODE request ; best location is to decide, let's try this first
152 if (getIntent().getAction() != null
&& getIntent().getAction().equals(Intent
.ACTION_MAIN
) && savedInstanceState
== null
) {
157 /*Intent observer_intent = new Intent(this, FileObserverService.class);
158 observer_intent.putExtra(FileObserverService.KEY_FILE_CMD, FileObserverService.CMD_INIT_OBSERVED_LIST);
159 startService(observer_intent);
163 requestWindowFeature(Window
.FEATURE_INDETERMINATE_PROGRESS
);
165 // Drop-down navigation
166 mDirectories
= new CustomArrayAdapter
<String
>(this, R
.layout
.sherlock_spinner_dropdown_item
);
167 OCFile currFile
= mCurrentDir
;
168 while(currFile
!= null
&& currFile
.getFileName() != OCFile
.PATH_SEPARATOR
) {
169 mDirectories
.add(currFile
.getFileName());
170 currFile
= mStorageManager
.getFileById(currFile
.getParentId());
172 mDirectories
.add(OCFile
.PATH_SEPARATOR
);
174 // Inflate and set the layout view
175 setContentView(R
.layout
.files
);
176 mFileList
= (OCFileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
177 mDualPane
= (findViewById(R
.id
.file_details_container
) != null
);
179 initFileDetailsInDualPane();
183 ActionBar actionBar
= getSupportActionBar();
184 actionBar
.setHomeButtonEnabled(true
); // mandatory since Android ICS, according to the official documentation
185 actionBar
.setDisplayHomeAsUpEnabled(mCurrentDir
!= null
&& mCurrentDir
.getParentId() != 0);
186 actionBar
.setDisplayShowTitleEnabled(false
);
187 actionBar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_LIST
);
188 actionBar
.setListNavigationCallbacks(mDirectories
, this);
189 setSupportProgressBarIndeterminateVisibility(false
); // always AFTER setContentView(...) ; to workaround bug in its implementation
191 Log
.d(getClass().toString(), "onCreate() end");
196 * Launches the account creation activity. To use when no ownCloud account is available
198 private void createFirstAccount() {
199 Intent intent
= new Intent(android
.provider
.Settings
.ACTION_ADD_ACCOUNT
);
200 intent
.putExtra(android
.provider
.Settings
.EXTRA_AUTHORITIES
, new String
[] { AccountAuthenticator
.AUTH_TOKEN_TYPE
});
201 startActivity(intent
); // the new activity won't be created until this.onStart() and this.onResume() are finished;
206 * Load of state dependent of the existence of an ownCloud account
208 private void initDataFromCurrentAccount() {
209 /// Storage manager initialization - access to local database
210 mStorageManager
= new FileDataStorageManager(
211 AccountUtils
.getCurrentOwnCloudAccount(this),
212 getContentResolver());
214 /// Check if mCurrentDir is a directory
215 if(mCurrentDir
!= null
&& !mCurrentDir
.isDirectory()) {
216 mCurrentFile
= mCurrentDir
;
217 mCurrentDir
= mStorageManager
.getFileById(mCurrentDir
.getParentId());
220 /// Check if mCurrentDir and mCurrentFile are in the current account, and update them
221 if (mCurrentDir
!= null
) {
222 mCurrentDir
= mStorageManager
.getFileByPath(mCurrentDir
.getRemotePath()); // mCurrentDir == null if it is not in the current account
224 if (mCurrentFile
!= null
) {
225 mCurrentFile
= mStorageManager
.getFileByPath(mCurrentFile
.getRemotePath()); // mCurrentFile == null if it is not in the current account
228 /// Default to root if mCurrentDir was not found
229 if (mCurrentDir
== null
) {
230 mCurrentDir
= mStorageManager
.getFileByPath("/"); // will be NULL if the database was never synchronized
235 private void initFileDetailsInDualPane() {
236 if (mDualPane
&& getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
) == null
) {
237 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
238 if (mCurrentFile
!= null
) {
239 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(mCurrentFile
, AccountUtils
.getCurrentOwnCloudAccount(this)), FileDetailFragment
.FTAG
); // empty FileDetailFragment
242 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(null
, null
), FileDetailFragment
.FTAG
); // empty FileDetailFragment
244 transaction
.commit();
250 public void onDestroy() {
252 if (mDownloadConnection
!= null
)
253 unbindService(mDownloadConnection
);
254 if (mUploadConnection
!= null
)
255 unbindService(mUploadConnection
);
260 public boolean onCreateOptionsMenu(Menu menu
) {
261 MenuInflater inflater
= getSherlock().getMenuInflater();
262 inflater
.inflate(R
.menu
.menu
, menu
);
267 public boolean onOptionsItemSelected(MenuItem item
) {
268 boolean retval
= true
;
269 switch (item
.getItemId()) {
270 case R
.id
.createDirectoryItem
: {
271 showDialog(DIALOG_CREATE_DIR
);
274 case R
.id
.startSync
: {
275 ContentResolver
.cancelSync(null
, AccountAuthenticator
.AUTH_TOKEN_TYPE
); // cancel the current synchronizations of any ownCloud account
276 Bundle bundle
= new Bundle();
277 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
278 ContentResolver
.requestSync(
279 AccountUtils
.getCurrentOwnCloudAccount(this),
280 AccountAuthenticator
.AUTH_TOKEN_TYPE
, bundle
);
283 case R
.id
.action_upload
: {
284 showDialog(DIALOG_CHOOSE_UPLOAD_SOURCE
);
287 case R
.id
.action_settings
: {
288 Intent settingsIntent
= new Intent(this, Preferences
.class);
289 startActivity(settingsIntent
);
292 case R
.id
.about_app
: {
293 showDialog(DIALOG_ABOUT_APP
);
296 case android
.R
.id
.home
: {
297 if(mCurrentDir
!= null
&& mCurrentDir
.getParentId() != 0){
309 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
310 int i
= itemPosition
;
314 // the next operation triggers a new call to this method, but it's necessary to
315 // ensure that the name exposed in the action bar is the current directory when the
316 // user selected it in the navigation list
317 if (itemPosition
!= 0)
318 getSupportActionBar().setSelectedNavigationItem(0);
323 * Called, when the user selected something for uploading
325 public void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
327 if (requestCode
== ACTION_SELECT_CONTENT_FROM_APPS
&& resultCode
== RESULT_OK
) {
328 requestSimpleUpload(data
);
330 } else if (requestCode
== ACTION_SELECT_MULTIPLE_FILES
&& resultCode
== RESULT_OK
) {
331 requestMultipleUpload(data
);
336 private void requestMultipleUpload(Intent data
) {
337 String
[] filePaths
= data
.getStringArrayExtra(UploadFilesActivity
.EXTRA_CHOSEN_FILES
);
338 if (filePaths
!= null
) {
339 String
[] remotePaths
= new String
[filePaths
.length
];
340 String remotePathBase
= "";
341 for (int j
= mDirectories
.getCount() - 2; j
>= 0; --j
) {
342 remotePathBase
+= OCFile
.PATH_SEPARATOR
+ mDirectories
.getItem(j
);
344 if (!remotePathBase
.endsWith(OCFile
.PATH_SEPARATOR
))
345 remotePathBase
+= OCFile
.PATH_SEPARATOR
;
346 for (int j
= 0; j
< remotePaths
.length
; j
++) {
347 remotePaths
[j
] = remotePathBase
+ (new File(filePaths
[j
])).getName();
350 Intent i
= new Intent(this, FileUploader
.class);
351 i
.putExtra(FileUploader
.KEY_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
352 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filePaths
);
353 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotePaths
);
354 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_MULTIPLE_FILES
);
358 Log
.d("FileDisplay", "User clicked on 'Update' with no selection");
359 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_no_file_selected
), Toast
.LENGTH_LONG
);
366 private void requestSimpleUpload(Intent data
) {
367 String filepath
= null
;
369 Uri selectedImageUri
= data
.getData();
371 String filemanagerstring
= selectedImageUri
.getPath();
372 String selectedImagePath
= getPath(selectedImageUri
);
374 if (selectedImagePath
!= null
)
375 filepath
= selectedImagePath
;
377 filepath
= filemanagerstring
;
379 } catch (Exception e
) {
380 Log
.e("FileDisplay", "Unexpected exception when trying to read the result of Intent.ACTION_GET_CONTENT", e
);
384 if (filepath
== null
) {
385 Log
.e("FileDisplay", "Couldnt resolve path to file");
386 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_unexpected_bad_get_content
), Toast
.LENGTH_LONG
);
392 Intent i
= new Intent(this, FileUploader
.class);
393 i
.putExtra(FileUploader
.KEY_ACCOUNT
,
394 AccountUtils
.getCurrentOwnCloudAccount(this));
395 String remotepath
= new String();
396 for (int j
= mDirectories
.getCount() - 2; j
>= 0; --j
) {
397 remotepath
+= OCFile
.PATH_SEPARATOR
+ mDirectories
.getItem(j
);
399 if (!remotepath
.endsWith(OCFile
.PATH_SEPARATOR
))
400 remotepath
+= OCFile
.PATH_SEPARATOR
;
401 remotepath
+= new File(filepath
).getName();
403 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filepath
);
404 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotepath
);
405 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
411 public void onBackPressed() {
412 if (mDirectories
.getCount() <= 1) {
417 mFileList
.onNavigateUp();
418 mCurrentDir
= mFileList
.getCurrentFile();
421 // Resets the FileDetailsFragment on Tablets so that it always displays
422 FileDetailFragment fileDetails
= (FileDetailFragment
) getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
423 if (fileDetails
!= null
&& !fileDetails
.isEmpty()) {
424 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
425 transaction
.remove(fileDetails
);
426 transaction
.add(R
.id
.file_details_container
, new FileDetailFragment(null
, null
), FileDetailFragment
.FTAG
);
427 transaction
.commit();
431 if(mCurrentDir
.getParentId() == 0){
432 ActionBar actionBar
= getSupportActionBar();
433 actionBar
.setDisplayHomeAsUpEnabled(false
);
438 protected void onSaveInstanceState(Bundle outState
) {
439 // responsibility of restore is preferred in onCreate() before than in onRestoreInstanceState when there are Fragments involved
440 Log
.d(getClass().toString(), "onSaveInstanceState() start");
441 super.onSaveInstanceState(outState
);
442 outState
.putParcelable(FileDetailFragment
.EXTRA_FILE
, mCurrentDir
);
444 FileDetailFragment fragment
= (FileDetailFragment
) getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
445 OCFile file
= fragment
.getDisplayedFile();
447 outState
.putParcelable(FileDetailFragment
.EXTRA_FILE
, file
);
450 Log
.d(getClass().toString(), "onSaveInstanceState() end");
454 protected void onResume() {
455 Log
.d(getClass().toString(), "onResume() start");
458 if (AccountUtils
.accountsAreSetup(this)) {
460 if (mStorageManager
== null
) {
461 // this is necessary for handling the come back to FileDisplayActivity when the first ownCloud account is created
462 initDataFromCurrentAccount();
464 initFileDetailsInDualPane();
468 // Listen for sync messages
469 IntentFilter syncIntentFilter
= new IntentFilter(FileSyncService
.SYNC_MESSAGE
);
470 mSyncBroadcastReceiver
= new SyncBroadcastReceiver();
471 registerReceiver(mSyncBroadcastReceiver
, syncIntentFilter
);
473 // Listen for upload messages
474 IntentFilter uploadIntentFilter
= new IntentFilter(FileUploader
.UPLOAD_FINISH_MESSAGE
);
475 mUploadFinishReceiver
= new UploadFinishReceiver();
476 registerReceiver(mUploadFinishReceiver
, uploadIntentFilter
);
478 // Listen for download messages
479 IntentFilter downloadIntentFilter
= new IntentFilter(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
480 mDownloadFinishReceiver
= new DownloadFinishReceiver();
481 registerReceiver(mDownloadFinishReceiver
, downloadIntentFilter
);
483 // List current directory
484 mFileList
.listDirectory(mCurrentDir
); // we should find the way to avoid the need of this
488 mStorageManager
= null
; // an invalid object will be there if all the ownCloud accounts are removed
489 showDialog(DIALOG_SETUP_ACCOUNT
);
492 Log
.d(getClass().toString(), "onResume() end");
497 protected void onPause() {
498 Log
.d(getClass().toString(), "onPause() start");
500 if (mSyncBroadcastReceiver
!= null
) {
501 unregisterReceiver(mSyncBroadcastReceiver
);
502 mSyncBroadcastReceiver
= null
;
504 if (mUploadFinishReceiver
!= null
) {
505 unregisterReceiver(mUploadFinishReceiver
);
506 mUploadFinishReceiver
= null
;
508 if (mDownloadFinishReceiver
!= null
) {
509 unregisterReceiver(mDownloadFinishReceiver
);
510 mDownloadFinishReceiver
= null
;
512 if (!AccountUtils
.accountsAreSetup(this)) {
513 dismissDialog(DIALOG_SETUP_ACCOUNT
);
516 Log
.d(getClass().toString(), "onPause() end");
520 protected Dialog
onCreateDialog(int id
) {
521 Dialog dialog
= null
;
522 AlertDialog
.Builder builder
;
524 case DIALOG_SETUP_ACCOUNT
: {
525 builder
= new AlertDialog
.Builder(this);
526 builder
.setTitle(R
.string
.main_tit_accsetup
);
527 builder
.setMessage(R
.string
.main_wrn_accsetup
);
528 builder
.setCancelable(false
);
529 builder
.setPositiveButton(android
.R
.string
.ok
, new OnClickListener() {
530 public void onClick(DialogInterface dialog
, int which
) {
531 createFirstAccount();
535 builder
.setNegativeButton(R
.string
.common_exit
, new OnClickListener() {
536 public void onClick(DialogInterface dialog
, int which
) {
541 //builder.setNegativeButton(android.R.string.cancel, this);
542 dialog
= builder
.create();
545 case DIALOG_ABOUT_APP
: {
546 builder
= new AlertDialog
.Builder(this);
547 builder
.setTitle(getString(R
.string
.about_title
));
550 pkg
= getPackageManager().getPackageInfo(getPackageName(), 0);
551 builder
.setMessage("ownCloud android client\n\nversion: " + pkg
.versionName
);
552 builder
.setIcon(android
.R
.drawable
.ic_menu_info_details
);
553 dialog
= builder
.create();
554 } catch (NameNotFoundException e
) {
557 Log
.e(TAG
, "Error while showing about dialog", e
);
561 case DIALOG_CREATE_DIR
: {
562 builder
= new Builder(this);
563 final EditText dirNameInput
= new EditText(getBaseContext());
564 builder
.setView(dirNameInput
);
565 builder
.setTitle(R
.string
.uploader_info_dirname
);
566 int typed_color
= getResources().getColor(R
.color
.setup_text_typed
);
567 dirNameInput
.setTextColor(typed_color
);
568 builder
.setPositiveButton(android
.R
.string
.ok
,
569 new OnClickListener() {
570 public void onClick(DialogInterface dialog
, int which
) {
571 String directoryName
= dirNameInput
.getText().toString();
572 if (directoryName
.trim().length() == 0) {
577 // Figure out the path where the dir needs to be created
579 if (mCurrentDir
== null
) {
580 // this is just a patch; we should ensure that mCurrentDir never is null
581 if (!mStorageManager
.fileExists(OCFile
.PATH_SEPARATOR
)) {
582 OCFile file
= new OCFile(OCFile
.PATH_SEPARATOR
);
583 mStorageManager
.saveFile(file
);
585 mCurrentDir
= mStorageManager
.getFileByPath(OCFile
.PATH_SEPARATOR
);
587 path
= FileDisplayActivity
.this.mCurrentDir
.getRemotePath();
590 path
+= directoryName
+ OCFile
.PATH_SEPARATOR
;
591 Thread thread
= new Thread(new DirectoryCreator(path
, AccountUtils
.getCurrentOwnCloudAccount(FileDisplayActivity
.this), new Handler()));
596 showDialog(DIALOG_SHORT_WAIT
);
599 builder
.setNegativeButton(R
.string
.common_cancel
,
600 new OnClickListener() {
601 public void onClick(DialogInterface dialog
, int which
) {
605 dialog
= builder
.create();
608 case DIALOG_SHORT_WAIT
: {
609 ProgressDialog working_dialog
= new ProgressDialog(this);
610 working_dialog
.setMessage(getResources().getString(
611 R
.string
.wait_a_moment
));
612 working_dialog
.setIndeterminate(true
);
613 working_dialog
.setCancelable(false
);
614 dialog
= working_dialog
;
617 case DIALOG_CHOOSE_UPLOAD_SOURCE
: {
618 final String
[] items
= { getString(R
.string
.actionbar_upload_files
),
619 getString(R
.string
.actionbar_upload_from_apps
) };
620 builder
= new AlertDialog
.Builder(this);
621 builder
.setTitle(R
.string
.actionbar_upload
);
622 builder
.setItems(items
, new DialogInterface
.OnClickListener() {
623 public void onClick(DialogInterface dialog
, int item
) {
626 Intent action
= new Intent(FileDisplayActivity
.this, UploadFilesActivity
.class);
627 startActivityForResult(action
, ACTION_SELECT_MULTIPLE_FILES
);
629 // TODO create and handle new fragment LocalFileListFragment
631 } else if (item
== 1) {
632 Intent action
= new Intent(Intent
.ACTION_GET_CONTENT
);
633 action
= action
.setType("*/*")
634 .addCategory(Intent
.CATEGORY_OPENABLE
);
635 startActivityForResult(
636 Intent
.createChooser(action
, getString(R
.string
.upload_chooser_title
)),
637 ACTION_SELECT_CONTENT_FROM_APPS
);
641 dialog
= builder
.create();
653 * Translates a content URI of an image to a physical path
655 * @param uri The URI to resolve
656 * @return The path to the image or null if it could not be found
658 public String
getPath(Uri uri
) {
659 String
[] projection
= { MediaStore
.Images
.Media
.DATA
};
660 Cursor cursor
= managedQuery(uri
, projection
, null
, null
, null
);
661 if (cursor
!= null
) {
662 int column_index
= cursor
663 .getColumnIndexOrThrow(MediaStore
.Images
.Media
.DATA
);
664 cursor
.moveToFirst();
665 return cursor
.getString(column_index
);
671 * Pushes a directory to the drop down list
672 * @param directory to push
673 * @throws IllegalArgumentException If the {@link OCFile#isDirectory()} returns false.
675 public void pushDirname(OCFile directory
) {
676 if(!directory
.isDirectory()){
677 throw new IllegalArgumentException("Only directories may be pushed!");
679 mDirectories
.insert(directory
.getFileName(), 0);
680 mCurrentDir
= directory
;
684 * Pops a directory name from the drop down list
685 * @return True, unless the stack is empty
687 public boolean popDirname() {
688 mDirectories
.remove(mDirectories
.getItem(0));
689 return !mDirectories
.isEmpty();
692 private class DirectoryCreator
implements Runnable
{
693 private String mTargetPath
;
694 private Account mAccount
;
695 private Handler mHandler
;
697 public DirectoryCreator(String targetPath
, Account account
, Handler handler
) {
698 mTargetPath
= targetPath
;
705 WebdavClient wdc
= OwnCloudClientUtils
.createOwnCloudClient(mAccount
, getApplicationContext());
706 boolean created
= wdc
.createDirectory(mTargetPath
);
708 mHandler
.post(new Runnable() {
711 dismissDialog(DIALOG_SHORT_WAIT
);
713 // Save new directory in local database
714 OCFile newDir
= new OCFile(mTargetPath
);
715 newDir
.setMimetype("DIR");
716 newDir
.setParentId(mCurrentDir
.getFileId());
717 mStorageManager
.saveFile(newDir
);
719 // Display the new folder right away
720 mFileList
.listDirectory(mCurrentDir
);
725 mHandler
.post(new Runnable() {
728 dismissDialog(DIALOG_SHORT_WAIT
);
730 Toast msg
= Toast
.makeText(FileDisplayActivity
.this, R
.string
.create_dir_fail_msg
, Toast
.LENGTH_LONG
);
733 } catch (NotFoundException e
) {
734 Log
.e(TAG
, "Error while trying to show fail message " , e
);
743 // Custom array adapter to override text colors
744 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
746 public CustomArrayAdapter(FileDisplayActivity ctx
, int view
) {
750 public View
getView(int position
, View convertView
, ViewGroup parent
) {
751 View v
= super.getView(position
, convertView
, parent
);
753 ((TextView
) v
).setTextColor(getResources().getColorStateList(
754 android
.R
.color
.white
));
758 public View
getDropDownView(int position
, View convertView
,
760 View v
= super.getDropDownView(position
, convertView
, parent
);
762 ((TextView
) v
).setTextColor(getResources().getColorStateList(
763 android
.R
.color
.white
));
770 private class SyncBroadcastReceiver
extends BroadcastReceiver
{
772 * {@link BroadcastReceiver} to enable syncing feedback in UI
775 public void onReceive(Context context
, Intent intent
) {
776 boolean inProgress
= intent
.getBooleanExtra(
777 FileSyncService
.IN_PROGRESS
, false
);
778 String accountName
= intent
779 .getStringExtra(FileSyncService
.ACCOUNT_NAME
);
781 Log
.d("FileDisplay", "sync of account " + accountName
782 + " is in_progress: " + inProgress
);
784 if (accountName
.equals(AccountUtils
.getCurrentOwnCloudAccount(context
).name
)) {
786 String synchFolderRemotePath
= intent
.getStringExtra(FileSyncService
.SYNC_FOLDER_REMOTE_PATH
);
788 boolean fillBlankRoot
= false
;
789 if (mCurrentDir
== null
) {
790 mCurrentDir
= mStorageManager
.getFileByPath("/");
791 fillBlankRoot
= (mCurrentDir
!= null
);
794 if ((synchFolderRemotePath
!= null
&& mCurrentDir
!= null
&& (mCurrentDir
.getRemotePath().equals(synchFolderRemotePath
)))
797 mCurrentDir
= getStorageManager().getFileByPath(synchFolderRemotePath
);
798 OCFileListFragment fileListFragment
= (OCFileListFragment
) getSupportFragmentManager()
799 .findFragmentById(R
.id
.fileList
);
800 if (fileListFragment
!= null
) {
801 fileListFragment
.listDirectory(mCurrentDir
);
805 setSupportProgressBarIndeterminateVisibility(inProgress
);
812 private class UploadFinishReceiver
extends BroadcastReceiver
{
814 * Once the file upload has finished -> update view
815 * @author David A. Velasco
816 * {@link BroadcastReceiver} to enable upload feedback in UI
819 public void onReceive(Context context
, Intent intent
) {
820 long parentDirId
= intent
.getLongExtra(FileUploader
.EXTRA_PARENT_DIR_ID
, -1);
821 OCFile parentDir
= mStorageManager
.getFileById(parentDirId
);
822 String accountName
= intent
.getStringExtra(FileUploader
.ACCOUNT_NAME
);
824 if (accountName
.equals(AccountUtils
.getCurrentOwnCloudAccount(context
).name
) &&
826 ( (mCurrentDir
== null
&& parentDir
.getFileName().equals("/")) ||
827 parentDir
.equals(mCurrentDir
)
830 OCFileListFragment fileListFragment
= (OCFileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
831 if (fileListFragment
!= null
) {
832 fileListFragment
.listDirectory();
841 * Once the file download has finished -> update view
843 private class DownloadFinishReceiver
extends BroadcastReceiver
{
845 public void onReceive(Context context
, Intent intent
) {
846 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
847 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
849 if (accountName
.equals(AccountUtils
.getCurrentOwnCloudAccount(context
).name
) &&
850 mCurrentDir
!= null
&& mCurrentDir
.getFileId() == mStorageManager
.getFileByPath(downloadedRemotePath
).getParentId()) {
851 OCFileListFragment fileListFragment
= (OCFileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
852 if (fileListFragment
!= null
) {
853 fileListFragment
.listDirectory();
866 public DataStorageManager
getStorageManager() {
867 return mStorageManager
;
875 public void onDirectoryClick(OCFile directory
) {
876 pushDirname(directory
);
877 ActionBar actionBar
= getSupportActionBar();
878 actionBar
.setDisplayHomeAsUpEnabled(true
);
881 // Resets the FileDetailsFragment on Tablets so that it always displays
882 FileDetailFragment fileDetails
= (FileDetailFragment
) getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
883 if (fileDetails
!= null
&& !fileDetails
.isEmpty()) {
884 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
885 transaction
.remove(fileDetails
);
886 transaction
.add(R
.id
.file_details_container
, new FileDetailFragment(null
, null
), FileDetailFragment
.FTAG
);
887 transaction
.commit();
897 public void onFileClick(OCFile file
) {
899 // If we are on a large device -> update fragment
901 // 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'
902 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
903 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(file
, AccountUtils
.getCurrentOwnCloudAccount(this)), FileDetailFragment
.FTAG
);
904 transaction
.setTransition(FragmentTransaction
.TRANSIT_FRAGMENT_FADE
);
905 transaction
.commit();
907 } else { // small or medium screen device -> new Activity
908 Intent showDetailsIntent
= new Intent(this, FileDetailActivity
.class);
909 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
910 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
911 startActivity(showDetailsIntent
);
920 public OCFile
getInitialDirectory() {
929 public void onFileStateChanged() {
930 OCFileListFragment fileListFragment
= (OCFileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
931 if (fileListFragment
!= null
) {
932 fileListFragment
.listDirectory();
941 public FileDownloaderBinder
getFileDownloaderBinder() {
942 return mDownloaderBinder
;
950 public FileUploaderBinder
getFileUploaderBinder() {
951 return mUploaderBinder
;
955 /** Defines callbacks for service binding, passed to bindService() */
956 private class ListServiceConnection
implements ServiceConnection
{
959 public void onServiceConnected(ComponentName component
, IBinder service
) {
960 if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileDownloader
.class))) {
961 Log
.d(TAG
, "Download service connected");
962 mDownloaderBinder
= (FileDownloaderBinder
) service
;
963 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileUploader
.class))) {
964 Log
.d(TAG
, "Upload service connected");
965 mUploaderBinder
= (FileUploaderBinder
) service
;
969 // a new chance to get the mDownloadBinder through getFileDownloadBinder() - THIS IS A MESS
970 mFileList
.listDirectory();
972 FileDetailFragment fragment
= (FileDetailFragment
) getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
973 if (fragment
!= null
)
974 fragment
.updateFileDetails();
980 public void onServiceDisconnected(ComponentName component
) {
981 if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileDownloader
.class))) {
982 Log
.d(TAG
, "Download service disconnected");
983 mDownloaderBinder
= null
;
984 } else if (component
.equals(new ComponentName(FileDisplayActivity
.this, FileUploader
.class))) {
985 Log
.d(TAG
, "Upload service disconnected");
986 mUploaderBinder
= null
;
994 * Launch an intent to request the PIN code to the user before letting him use the app
996 private void requestPinCode() {
997 boolean pinStart
= false
;
998 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
999 pinStart
= appPrefs
.getBoolean("set_pincode", false
);
1001 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
1002 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "FileDisplayActivity");