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
.SharedPreferences
;
37 import android
.content
.pm
.PackageInfo
;
38 import android
.content
.pm
.PackageManager
.NameNotFoundException
;
39 import android
.database
.Cursor
;
40 import android
.net
.Uri
;
41 import android
.os
.Bundle
;
42 import android
.preference
.PreferenceManager
;
43 import android
.provider
.MediaStore
;
44 import android
.support
.v4
.app
.FragmentTransaction
;
45 import android
.util
.Log
;
46 import android
.view
.View
;
47 import android
.view
.ViewGroup
;
48 import android
.widget
.ArrayAdapter
;
49 import android
.widget
.EditText
;
50 import android
.widget
.TextView
;
51 import android
.widget
.Toast
;
53 import com
.actionbarsherlock
.app
.ActionBar
;
54 import com
.actionbarsherlock
.app
.ActionBar
.OnNavigationListener
;
55 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
56 import com
.actionbarsherlock
.view
.Menu
;
57 import com
.actionbarsherlock
.view
.MenuInflater
;
58 import com
.actionbarsherlock
.view
.MenuItem
;
59 import com
.actionbarsherlock
.view
.Window
;
61 import eu
.alefzero
.owncloud
.AccountUtils
;
62 import eu
.alefzero
.owncloud
.CrashHandler
;
63 import eu
.alefzero
.owncloud
.R
;
64 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
65 import eu
.alefzero
.owncloud
.datamodel
.DataStorageManager
;
66 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
;
67 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
68 import eu
.alefzero
.owncloud
.files
.services
.FileDownloader
;
69 import eu
.alefzero
.owncloud
.files
.services
.FileUploader
;
70 import eu
.alefzero
.owncloud
.syncadapter
.FileSyncService
;
71 import eu
.alefzero
.owncloud
.ui
.fragment
.FileDetailFragment
;
72 import eu
.alefzero
.owncloud
.ui
.fragment
.FileListFragment
;
73 import eu
.alefzero
.webdav
.WebdavClient
;
76 * Displays, what files the user has available in his ownCloud.
78 * @author Bartek Przybylski
82 public class FileDisplayActivity
extends SherlockFragmentActivity
implements
83 FileListFragment
.ContainerActivity
, FileDetailFragment
.ContainerActivity
, OnNavigationListener
, OnClickListener
, android
.view
.View
.OnClickListener
{
85 private ArrayAdapter
<String
> mDirectories
;
86 private OCFile mCurrentDir
;
87 private String
[] mDirs
= null
;
89 private DataStorageManager mStorageManager
;
90 private SyncBroadcastReceiver mSyncBroadcastReceiver
;
91 private UploadFinishReceiver mUploadFinishReceiver
;
92 private DownloadFinishReceiver mDownloadFinishReceiver
;
94 private View mLayoutView
= null
;
95 private FileListFragment mFileList
;
97 private boolean mDualPane
;
99 private boolean mForcedLoginToCreateFirstAccount
= false
;
101 private static final String KEY_DIR_ARRAY
= "DIR_ARRAY";
102 private static final String KEY_CURRENT_DIR
= "DIR";
104 private static final int DIALOG_SETUP_ACCOUNT
= 0;
105 private static final int DIALOG_CREATE_DIR
= 1;
106 private static final int DIALOG_ABOUT_APP
= 2;
108 private static final int ACTION_SELECT_FILE
= 1;
111 public void onCreate(Bundle savedInstanceState
) {
112 Log
.i(getClass().toString(), "onCreate() start");
113 super.onCreate(savedInstanceState
);
115 requestWindowFeature(Window
.FEATURE_INDETERMINATE_PROGRESS
);
117 Thread
.setDefaultUncaughtExceptionHandler(new CrashHandler(getApplicationContext()));
119 if(savedInstanceState
!= null
) {
120 mDirs
= savedInstanceState
.getStringArray(KEY_DIR_ARRAY
);
121 mDirectories
= new CustomArrayAdapter
<String
>(this, R
.layout
.sherlock_spinner_dropdown_item
);
122 mDirectories
.add(OCFile
.PATH_SEPARATOR
);
124 for (String s
: mDirs
)
125 mDirectories
.insert(s
, 0);
126 mCurrentDir
= savedInstanceState
.getParcelable(FileDetailFragment
.EXTRA_FILE
);
129 mLayoutView
= getLayoutInflater().inflate(R
.layout
.files
, null
); // always inflate this at onCreate() ; just once!
131 if (AccountUtils
.accountsAreSetup(this)) {
133 initDelayedTilAccountAvailabe();
135 // PIN CODE request ; best location is to decide, let's try this first
136 //if (savedInstanceState == null) {
137 if (getIntent().getAction() != null
&& getIntent().getAction().equals(Intent
.ACTION_MAIN
) && savedInstanceState
== null
) {
144 setContentView(R
.layout
.no_account_available
);
145 getSupportActionBar().setNavigationMode(ActionBar
.DISPLAY_SHOW_TITLE
);
146 findViewById(R
.id
.setup_account
).setOnClickListener(this);
148 setSupportProgressBarIndeterminateVisibility(false
);
150 Intent intent
= new Intent(android
.provider
.Settings
.ACTION_ADD_ACCOUNT
);
151 intent
.putExtra(android
.provider
.Settings
.EXTRA_AUTHORITIES
, new String
[] { AccountAuthenticator
.AUTH_TOKEN_TYPE
});
152 startActivity(intent
); // although the code is here, the activity won't be created until this.onStart() and this.onResume() are finished;
153 mForcedLoginToCreateFirstAccount
= true
;
156 Log
.i(getClass().toString(), "onCreate() end");
160 public boolean onCreateOptionsMenu(Menu menu
) {
161 MenuInflater inflater
= getSherlock().getMenuInflater();
162 inflater
.inflate(R
.menu
.menu
, menu
);
167 public boolean onOptionsItemSelected(MenuItem item
) {
168 boolean retval
= true
;
169 switch (item
.getItemId()) {
170 case R
.id
.createDirectoryItem
: {
171 showDialog(DIALOG_CREATE_DIR
);
174 case R
.id
.startSync
: {
175 ContentResolver
.cancelSync(null
, "org.owncloud"); // cancel the current synchronizations of any ownCloud account
176 Bundle bundle
= new Bundle();
177 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
178 ContentResolver
.requestSync(
179 AccountUtils
.getCurrentOwnCloudAccount(this),
180 "org.owncloud", bundle
);
183 case R
.id
.action_upload
: {
184 Intent action
= new Intent(Intent
.ACTION_GET_CONTENT
);
185 action
= action
.setType("*/*")
186 .addCategory(Intent
.CATEGORY_OPENABLE
);
187 startActivityForResult(
188 Intent
.createChooser(action
, "Upload file from..."),
192 case R
.id
.action_settings
: {
193 Intent settingsIntent
= new Intent(this, Preferences
.class);
194 startActivity(settingsIntent
);
197 case R
.id
.about_app
: {
198 showDialog(DIALOG_ABOUT_APP
);
201 case android
.R
.id
.home
: {
202 if(mCurrentDir
!= null
&& mCurrentDir
.getParentId() != 0){
214 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
215 int i
= itemPosition
;
223 * Called, when the user selected something for uploading
225 public void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
226 if (requestCode
== ACTION_SELECT_FILE
) {
227 if (resultCode
== RESULT_OK
) {
228 String filepath
= null
;
230 Uri selectedImageUri
= data
.getData();
232 String filemanagerstring
= selectedImageUri
.getPath();
233 String selectedImagePath
= getPath(selectedImageUri
);
235 if (selectedImagePath
!= null
)
236 filepath
= selectedImagePath
;
238 filepath
= filemanagerstring
;
240 } catch (Exception e
) {
241 Log
.e("FileDisplay", "Unexpected exception when trying to read the result of Intent.ACTION_GET_CONTENT", e
);
245 if (filepath
== null
) {
246 Log
.e("FileDisplay", "Couldnt resolve path to file");
247 Toast t
= Toast
.makeText(this, getString(R
.string
.filedisplay_unexpected_bad_get_content
), Toast
.LENGTH_LONG
);
253 Intent i
= new Intent(this, FileUploader
.class);
254 i
.putExtra(FileUploader
.KEY_ACCOUNT
,
255 AccountUtils
.getCurrentOwnCloudAccount(this));
256 String remotepath
= new String();
257 for (int j
= mDirectories
.getCount() - 2; j
>= 0; --j
) {
258 remotepath
+= OCFile
.PATH_SEPARATOR
+ mDirectories
.getItem(j
);
260 if (!remotepath
.endsWith(OCFile
.PATH_SEPARATOR
))
261 remotepath
+= OCFile
.PATH_SEPARATOR
;
262 remotepath
+= new File(filepath
).getName();
264 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filepath
);
265 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotepath
);
266 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
270 }/* dvelasco: WIP - not working as expected ... yet :)
271 else if (requestCode == ACTION_CREATE_FIRST_ACCOUNT) {
272 if (resultCode != RESULT_OK) {
273 finish(); // the user cancelled the AuthenticatorActivity
279 public void onBackPressed() {
280 if (mDirectories
== null
|| mDirectories
.getCount() <= 1) {
285 mFileList
.onNavigateUp();
286 mCurrentDir
= mFileList
.getCurrentFile();
288 if(mCurrentDir
.getParentId() == 0){
289 ActionBar actionBar
= getSupportActionBar();
290 actionBar
.setDisplayHomeAsUpEnabled(false
);
295 protected void onSaveInstanceState(Bundle outState
) {
296 // responsability of restore is prefered in onCreate() before than in onRestoreInstanceState when there are Fragments involved
297 Log
.i(getClass().toString(), "onSaveInstanceState() start");
298 super.onSaveInstanceState(outState
);
299 if(mDirectories
!= null
&& mDirectories
.getCount() != 0){
300 mDirs
= new String
[mDirectories
.getCount()-1];
301 for (int j
= mDirectories
.getCount() - 2, i
= 0; j
>= 0; --j
, ++i
) {
302 mDirs
[i
] = mDirectories
.getItem(j
);
305 outState
.putStringArray(KEY_DIR_ARRAY
, mDirs
);
306 outState
.putParcelable(FileDetailFragment
.EXTRA_FILE
, mCurrentDir
);
307 Log
.i(getClass().toString(), "onSaveInstanceState() end");
311 protected void onResume() {
312 Log
.i(getClass().toString(), "onResume() start");
315 if (AccountUtils
.accountsAreSetup(this)) {
316 // at least an account exist: normal operation
318 // set the layout only if it couldn't be set in onCreate
319 if (mForcedLoginToCreateFirstAccount
) {
320 initDelayedTilAccountAvailabe();
321 mForcedLoginToCreateFirstAccount
= false
;
324 // Listen for sync messages
325 IntentFilter syncIntentFilter
= new IntentFilter(FileSyncService
.SYNC_MESSAGE
);
326 mSyncBroadcastReceiver
= new SyncBroadcastReceiver();
327 registerReceiver(mSyncBroadcastReceiver
, syncIntentFilter
);
329 // Listen for upload messages
330 IntentFilter uploadIntentFilter
= new IntentFilter(FileUploader
.UPLOAD_FINISH_MESSAGE
);
331 mUploadFinishReceiver
= new UploadFinishReceiver();
332 registerReceiver(mUploadFinishReceiver
, uploadIntentFilter
);
334 // Listen for download messages
335 IntentFilter downloadIntentFilter
= new IntentFilter(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
336 mDownloadFinishReceiver
= new DownloadFinishReceiver();
337 registerReceiver(mDownloadFinishReceiver
, downloadIntentFilter
);
339 // Storage manager initialization
340 mStorageManager
= new FileDataStorageManager(
341 AccountUtils
.getCurrentOwnCloudAccount(this),
342 getContentResolver());
344 // File list fragments
345 mFileList
= (FileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
348 // Figure out what directory to list.
349 // Priority: Intent (here), savedInstanceState (onCreate), root dir (dir is null)
350 if(getIntent().hasExtra(FileDetailFragment
.EXTRA_FILE
)){
351 mCurrentDir
= (OCFile
) getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
352 if(mCurrentDir
!= null
&& !mCurrentDir
.isDirectory()){
353 mCurrentDir
= mStorageManager
.getFileById(mCurrentDir
.getParentId());
356 // Clear intent extra, so rotating the screen will not return us to this directory
357 getIntent().removeExtra(FileDetailFragment
.EXTRA_FILE
);
360 if (mCurrentDir
== null
)
361 mCurrentDir
= mStorageManager
.getFileByPath("/");
363 // Drop-Down navigation and file list restore
364 mDirectories
= new CustomArrayAdapter
<String
>(this, R
.layout
.sherlock_spinner_dropdown_item
);
367 // Given the case we have a file to display:
368 if(mCurrentDir
!= null
){
369 ArrayList
<OCFile
> files
= new ArrayList
<OCFile
>();
370 OCFile currFile
= mCurrentDir
;
371 while(currFile
!= null
){
373 currFile
= mStorageManager
.getFileById(currFile
.getParentId());
377 mDirs
= new String
[files
.size()];
378 for(int i
= files
.size() - 1; i
>= 0; i
--){
379 mDirs
[i
] = files
.get(i
).getFileName();
384 for (String s
: mDirs
)
387 mDirectories
.add(OCFile
.PATH_SEPARATOR
);
391 ActionBar action_bar
= getSupportActionBar();
392 action_bar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_LIST
);
393 action_bar
.setDisplayShowTitleEnabled(false
);
394 action_bar
.setListNavigationCallbacks(mDirectories
, this);
395 if(mCurrentDir
!= null
&& mCurrentDir
.getParentId() != 0){
396 action_bar
.setDisplayHomeAsUpEnabled(true
);
398 action_bar
.setDisplayHomeAsUpEnabled(false
);
402 mFileList
.listDirectory(mCurrentDir
);
404 Log
.i(getClass().toString(), "onResume() end");
408 protected void onPause() {
409 Log
.i(getClass().toString(), "onPause() start");
411 if (mSyncBroadcastReceiver
!= null
) {
412 unregisterReceiver(mSyncBroadcastReceiver
);
413 mSyncBroadcastReceiver
= null
;
415 if (mUploadFinishReceiver
!= null
) {
416 unregisterReceiver(mUploadFinishReceiver
);
417 mUploadFinishReceiver
= null
;
419 if (mDownloadFinishReceiver
!= null
) {
420 unregisterReceiver(mDownloadFinishReceiver
);
421 mDownloadFinishReceiver
= null
;
424 getIntent().putExtra(FileDetailFragment
.EXTRA_FILE
, mCurrentDir
);
425 Log
.i(getClass().toString(), "onPause() end");
429 protected Dialog
onCreateDialog(int id
) {
430 Dialog dialog
= null
;
431 AlertDialog
.Builder builder
;
433 case DIALOG_SETUP_ACCOUNT
:
434 builder
= new AlertDialog
.Builder(this);
435 builder
.setTitle(R
.string
.main_tit_accsetup
);
436 builder
.setMessage(R
.string
.main_wrn_accsetup
);
437 builder
.setCancelable(false
);
438 builder
.setPositiveButton(android
.R
.string
.ok
, this);
439 builder
.setNegativeButton(android
.R
.string
.cancel
, this);
440 dialog
= builder
.create();
442 case DIALOG_ABOUT_APP
: {
443 builder
= new AlertDialog
.Builder(this);
444 builder
.setTitle("About");
447 pkg
= getPackageManager().getPackageInfo(getPackageName(), 0);
448 builder
.setMessage("ownCloud android client\n\nversion: " + pkg
.versionName
);
449 builder
.setIcon(android
.R
.drawable
.ic_menu_info_details
);
450 dialog
= builder
.create();
451 } catch (NameNotFoundException e
) {
458 case DIALOG_CREATE_DIR
: {
459 builder
= new Builder(this);
460 final EditText dirNameInput
= new EditText(getBaseContext());
461 final Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
462 builder
.setView(dirNameInput
);
463 builder
.setTitle(R
.string
.uploader_info_dirname
);
464 int typed_color
= getResources().getColor(R
.color
.setup_text_typed
);
465 dirNameInput
.setTextColor(typed_color
);
466 builder
.setPositiveButton(android
.R
.string
.ok
,
467 new OnClickListener() {
468 public void onClick(DialogInterface dialog
, int which
) {
469 String directoryName
= dirNameInput
.getText().toString();
470 if (directoryName
.trim().length() == 0) {
475 // Figure out the path where the dir needs to be created
477 if (mCurrentDir
== null
) {
478 // this is just a patch; we should ensure that mCurrentDir never is null
479 if (!mStorageManager
.fileExists(OCFile
.PATH_SEPARATOR
)) {
480 OCFile file
= new OCFile(OCFile
.PATH_SEPARATOR
);
481 mStorageManager
.saveFile(file
);
483 mCurrentDir
= mStorageManager
.getFileByPath(OCFile
.PATH_SEPARATOR
);
485 path
= FileDisplayActivity
.this.mCurrentDir
.getRemotePath();
488 path
+= directoryName
+ OCFile
.PATH_SEPARATOR
;
489 Thread thread
= new Thread(new DirectoryCreator(path
, a
));
492 // Save new directory in local database
493 OCFile newDir
= new OCFile(path
);
494 newDir
.setMimetype("DIR");
495 newDir
.setParentId(mCurrentDir
.getFileId());
496 mStorageManager
.saveFile(newDir
);
498 // Display the new folder right away
500 mFileList
.listDirectory(mCurrentDir
);
503 builder
.setNegativeButton(R
.string
.common_cancel
,
504 new OnClickListener() {
505 public void onClick(DialogInterface dialog
, int which
) {
509 dialog
= builder
.create();
521 * Responds to the "There are no ownCloud Accounts setup" dialog
522 * TODO: Dialog is 100% useless -> Remove
525 public void onClick(DialogInterface dialog
, int which
) {
526 // In any case - we won't need it anymore
529 case DialogInterface
.BUTTON_POSITIVE
:
530 Intent intent
= new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
531 intent
.putExtra("authorities",
532 new String
[] { AccountAuthenticator
.AUTH_TOKEN_TYPE
});
533 startActivity(intent
);
535 case DialogInterface
.BUTTON_NEGATIVE
:
542 * Translates a content URI of an image to a physical path
544 * @param uri The URI to resolve
545 * @return The path to the image or null if it could not be found
547 public String
getPath(Uri uri
) {
548 String
[] projection
= { MediaStore
.Images
.Media
.DATA
};
549 Cursor cursor
= managedQuery(uri
, projection
, null
, null
, null
);
550 if (cursor
!= null
) {
551 int column_index
= cursor
552 .getColumnIndexOrThrow(MediaStore
.Images
.Media
.DATA
);
553 cursor
.moveToFirst();
554 return cursor
.getString(column_index
);
560 * Pushes a directory to the drop down list
561 * @param directory to push
562 * @throws IllegalArgumentException If the {@link OCFile#isDirectory()} returns false.
564 public void pushDirname(OCFile directory
) {
565 if(!directory
.isDirectory()){
566 throw new IllegalArgumentException("Only directories may be pushed!");
568 mDirectories
.insert(directory
.getFileName(), 0);
569 mCurrentDir
= directory
;
573 * Pops a directory name from the drop down list
574 * @return True, unless the stack is empty
576 public boolean popDirname() {
577 mDirectories
.remove(mDirectories
.getItem(0));
578 return !mDirectories
.isEmpty();
581 private class DirectoryCreator
implements Runnable
{
582 private String mTargetPath
;
583 private Account mAccount
;
584 private AccountManager mAm
;
586 public DirectoryCreator(String targetPath
, Account account
) {
587 mTargetPath
= targetPath
;
589 mAm
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
594 WebdavClient wdc
= new WebdavClient(mAccount
, getApplicationContext());
596 String username
= mAccount
.name
.substring(0,
597 mAccount
.name
.lastIndexOf('@'));
598 String password
= mAm
.getPassword(mAccount
);
600 wdc
.setCredentials(username
, password
);
601 wdc
.allowSelfsignedCertificates();
602 wdc
.createDirectory(mTargetPath
);
607 // Custom array adapter to override text colors
608 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
610 public CustomArrayAdapter(FileDisplayActivity ctx
, int view
) {
614 public View
getView(int position
, View convertView
, ViewGroup parent
) {
615 View v
= super.getView(position
, convertView
, parent
);
617 ((TextView
) v
).setTextColor(getResources().getColorStateList(
618 android
.R
.color
.white
));
622 public View
getDropDownView(int position
, View convertView
,
624 View v
= super.getDropDownView(position
, convertView
, parent
);
626 ((TextView
) v
).setTextColor(getResources().getColorStateList(
627 android
.R
.color
.white
));
634 private class SyncBroadcastReceiver
extends BroadcastReceiver
{
636 * {@link BroadcastReceiver} to enable syncing feedback in UI
639 public void onReceive(Context context
, Intent intent
) {
640 boolean inProgress
= intent
.getBooleanExtra(
641 FileSyncService
.IN_PROGRESS
, false
);
642 String accountName
= intent
643 .getStringExtra(FileSyncService
.ACCOUNT_NAME
);
645 Log
.d("FileDisplay", "sync of account " + accountName
646 + " is in_progress: " + inProgress
);
648 if (accountName
.equals(AccountUtils
.getCurrentOwnCloudAccount(context
).name
)) {
650 String synchFolderRemotePath
= intent
.getStringExtra(FileSyncService
.SYNC_FOLDER_REMOTE_PATH
);
652 boolean fillBlankRoot
= false
;
653 if (mCurrentDir
== null
) {
654 mCurrentDir
= mStorageManager
.getFileByPath("/");
655 fillBlankRoot
= (mCurrentDir
!= null
);
658 if ((synchFolderRemotePath
!= null
&& mCurrentDir
!= null
&& (mCurrentDir
.getRemotePath().equals(synchFolderRemotePath
)))
661 mCurrentDir
= getStorageManager().getFileByPath(synchFolderRemotePath
);
662 FileListFragment fileListFragment
= (FileListFragment
) getSupportFragmentManager()
663 .findFragmentById(R
.id
.fileList
);
664 if (fileListFragment
!= null
) {
665 fileListFragment
.listDirectory(mCurrentDir
);
669 setSupportProgressBarIndeterminateVisibility(inProgress
);
676 private class UploadFinishReceiver
extends BroadcastReceiver
{
678 * Once the file upload has finished -> update view
679 * @author David A. Velasco
680 * {@link BroadcastReceiver} to enable upload feedback in UI
683 public void onReceive(Context context
, Intent intent
) {
684 long parentDirId
= intent
.getLongExtra(FileUploader
.EXTRA_PARENT_DIR_ID
, -1);
685 OCFile parentDir
= mStorageManager
.getFileById(parentDirId
);
686 String accountName
= intent
.getStringExtra(FileUploader
.ACCOUNT_NAME
);
688 if (accountName
.equals(AccountUtils
.getCurrentOwnCloudAccount(context
).name
) &&
690 ( (mCurrentDir
== null
&& parentDir
.getFileName().equals("/")) ||
691 parentDir
.equals(mCurrentDir
)
694 FileListFragment fileListFragment
= (FileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
695 if (fileListFragment
!= null
) {
696 fileListFragment
.listDirectory();
705 * Once the file download has finished -> update view
707 private class DownloadFinishReceiver
extends BroadcastReceiver
{
709 public void onReceive(Context context
, Intent intent
) {
710 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
711 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
713 if (accountName
.equals(AccountUtils
.getCurrentOwnCloudAccount(context
).name
) &&
714 mCurrentDir
!= null
&& mCurrentDir
.getFileId() == mStorageManager
.getFileByPath(downloadedRemotePath
).getParentId()) {
715 FileListFragment fileListFragment
= (FileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
716 if (fileListFragment
!= null
) {
717 fileListFragment
.listDirectory();
725 public void onClick(View v
) {
726 if (v
.getId() == R
.id
.setup_account
) {
727 Intent intent
= new Intent(android
.provider
.Settings
.ACTION_ADD_ACCOUNT
);
728 intent
.putExtra(android
.provider
.Settings
.EXTRA_AUTHORITIES
, new String
[] { AccountAuthenticator
.AUTH_TOKEN_TYPE
});
729 startActivity(intent
);
730 mForcedLoginToCreateFirstAccount
= true
;
742 public DataStorageManager
getStorageManager() {
743 return mStorageManager
;
751 public void onDirectoryClick(OCFile directory
) {
752 pushDirname(directory
);
753 ActionBar actionBar
= getSupportActionBar();
754 actionBar
.setDisplayHomeAsUpEnabled(true
);
757 // Resets the FileDetailsFragment on Tablets so that it always displays
758 FileDetailFragment fileDetails
= (FileDetailFragment
) getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
759 if (fileDetails
!= null
) {
760 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
761 transaction
.remove(fileDetails
);
762 transaction
.add(R
.id
.file_details_container
, new FileDetailFragment(null
, null
));
763 transaction
.commit();
773 public void onFileClick(OCFile file
) {
775 // If we are on a large device -> update fragment
777 // 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'
778 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
779 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(file
, AccountUtils
.getCurrentOwnCloudAccount(this)), FileDetailFragment
.FTAG
);
780 transaction
.setTransition(FragmentTransaction
.TRANSIT_FRAGMENT_FADE
);
781 transaction
.commit();
783 } else { // small or medium screen device -> new Activity
784 Intent showDetailsIntent
= new Intent(this, FileDetailActivity
.class);
785 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
786 showDetailsIntent
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
787 startActivity(showDetailsIntent
);
796 public void onFileStateChanged() {
797 FileListFragment fileListFragment
= (FileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
798 if (fileListFragment
!= null
) {
799 fileListFragment
.listDirectory();
805 * Operations in this method should be preferably performed in onCreate to have a lighter onResume method.
807 * But we need to delay them to onResume for the first start of the application, when no account exists and the login activity must be shown; and
808 * put instead the ugly view that shows the 'Setup' button to restart the login activity.
810 * In other way, if the users cancels or presses BACK in the login page that first time (users can be cruel sometimes) would show a blank view (the
811 * FragmentList view empty).
813 * This is temporal, until we found out how to get a result in this activity after launching the ADD_ACCOUNT Intent with startActivityForResult (not trivial)
815 private void initDelayedTilAccountAvailabe() {
816 setContentView(mLayoutView
);
817 mDualPane
= (findViewById(R
.id
.file_details_container
) != null
);
818 if (mDualPane
&& getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
) == null
) {
819 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
820 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(null
, null
)); // empty FileDetailFragment
821 transaction
.commit();
823 setSupportProgressBarIndeterminateVisibility(false
);
828 * Launch an intent to request the PIN code to the user before letting him use the app
830 private void requestPinCode() {
831 boolean pinStart
= false
;
832 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
833 pinStart
= appPrefs
.getBoolean("set_pincode", false
);
835 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
836 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "FileDisplayActivity");