From: David A. Velasco Date: Wed, 4 Jul 2012 12:38:52 +0000 (+0200) Subject: Refactoring of main Activities and Fragments to avoid inconsistent states leading... X-Git-Tag: oc-android-1.4.3~313 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/7c2174d09e376ecc5ebd9a510a66dc7582b173f9 Refactoring of main Activities and Fragments to avoid inconsistent states leading to crashes, specially in tablet devices --- diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 16c44724..d2d03e62 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -18,7 +18,7 @@ --> + android:versionName="0.1.143B" xmlns:android="http://schemas.android.com/apk/res/android"> diff --git a/src/eu/alefzero/owncloud/ui/activity/FileDetailActivity.java b/src/eu/alefzero/owncloud/ui/activity/FileDetailActivity.java index 00a1f838..737c00f2 100644 --- a/src/eu/alefzero/owncloud/ui/activity/FileDetailActivity.java +++ b/src/eu/alefzero/owncloud/ui/activity/FileDetailActivity.java @@ -17,15 +17,20 @@ */ package eu.alefzero.owncloud.ui.activity; +import android.accounts.Account; import android.content.Intent; +import android.content.res.Configuration; import android.os.Bundle; import android.support.v4.app.FragmentTransaction; +import android.util.Log; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.view.MenuItem; import eu.alefzero.owncloud.R; +import eu.alefzero.owncloud.datamodel.OCFile; +import eu.alefzero.owncloud.files.services.FileDownloader; import eu.alefzero.owncloud.ui.fragment.FileDetailFragment; /** @@ -36,21 +41,38 @@ import eu.alefzero.owncloud.ui.fragment.FileDetailFragment; * */ public class FileDetailActivity extends SherlockFragmentActivity { - private FileDetailFragment mFileDetail; + + private boolean mConfigurationChangedToLandscape = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.file_activity_details); - ActionBar actionBar = getSupportActionBar(); - actionBar.setDisplayHomeAsUpEnabled(true); - - FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); - mFileDetail = new FileDetailFragment(); - ft.replace(R.id.fragment, mFileDetail, "FileDetails"); - ft.commit(); + // check if configuration changed to large-land ; for a tablet being changed from portrait to landscape when in FileDetailActivity + Configuration conf = getResources().getConfiguration(); + mConfigurationChangedToLandscape = (conf.orientation == Configuration.ORIENTATION_LANDSCAPE && + (conf.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE + ); + if (!mConfigurationChangedToLandscape) { + setContentView(R.layout.file_activity_details); + + ActionBar actionBar = getSupportActionBar(); + actionBar.setDisplayHomeAsUpEnabled(true); + + OCFile file = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE); + Account account = getIntent().getParcelableExtra(FileDownloader.EXTRA_ACCOUNT); + FileDetailFragment mFileDetail = new FileDetailFragment(file, account); + + FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); + ft.replace(R.id.fragment, mFileDetail, FileDetailFragment.FTAG); + ft.commit(); + + } else { + backToDisplayActivity(); // the 'back' won't be effective until this.onStart() and this.onResume() are completed; + } + + } @Override @@ -59,11 +81,7 @@ public class FileDetailActivity extends SherlockFragmentActivity { switch(item.getItemId()){ case android.R.id.home: - Intent intent = new Intent(this, FileDisplayActivity.class); - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - intent.putExtra(FileDetailFragment.EXTRA_FILE, mFileDetail.getDisplayedFile()); - startActivity(intent); - finish(); + backToDisplayActivity(); returnValue = true; } @@ -74,10 +92,21 @@ public class FileDetailActivity extends SherlockFragmentActivity { @Override protected void onResume() { + super.onResume(); - mFileDetail.updateFileDetails(getIntent()); + if (!mConfigurationChangedToLandscape) { + FileDetailFragment fragment = (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG); + fragment.updateFileDetails(); + } } - + + private void backToDisplayActivity() { + Intent intent = new Intent(this, FileDisplayActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + intent.putExtra(FileDetailFragment.EXTRA_FILE, getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE)); + startActivity(intent); + finish(); + } } diff --git a/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java b/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java index 98c64bf1..2c4ae382 100644 --- a/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java +++ b/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java @@ -39,13 +39,13 @@ import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; +import android.support.v4.app.FragmentTransaction; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.TextView; -import android.widget.Toast; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.ActionBar.OnNavigationListener; @@ -77,7 +77,7 @@ import eu.alefzero.webdav.WebdavClient; */ public class FileDisplayActivity extends SherlockFragmentActivity implements - OnNavigationListener, OnClickListener, android.view.View.OnClickListener { + FileListFragment.ContainerActivity, OnNavigationListener, OnClickListener, android.view.View.OnClickListener { private ArrayAdapter mDirectories; private OCFile mCurrentDir; @@ -90,6 +90,10 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements private View mLayoutView = null; private FileListFragment mFileList; + private boolean mDualPane; + + private boolean mForcedLoginToCreateFirstAccount = false; + private static final String KEY_DIR_ARRAY = "DIR_ARRAY"; private static final String KEY_CURRENT_DIR = "DIR"; @@ -98,8 +102,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements private static final int DIALOG_ABOUT_APP = 2; private static final int ACTION_SELECT_FILE = 1; - //private static final int ACTION_CREATE_FIRST_ACCOUNT = 2; dvelasco: WIP - + @Override public void onCreate(Bundle savedInstanceState) { Log.i(getClass().toString(), "onCreate() start"); @@ -110,20 +113,32 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(getApplicationContext())); - if(savedInstanceState != null){ - mCurrentDir = (OCFile) savedInstanceState.getParcelable(KEY_CURRENT_DIR); // this is never saved with this key :S + if(savedInstanceState != null) { + mDirs = savedInstanceState.getStringArray(KEY_DIR_ARRAY); + mDirectories = new CustomArrayAdapter(this, R.layout.sherlock_spinner_dropdown_item); + mDirectories.add("/"); + if (mDirs != null) + for (String s : mDirs) + mDirectories.insert(s, 0); + mCurrentDir = savedInstanceState.getParcelable(FileDetailFragment.EXTRA_FILE); } mLayoutView = getLayoutInflater().inflate(R.layout.files, null); // always inflate this at onCreate() ; just once! if (AccountUtils.accountsAreSetup(this)) { - setContentView(mLayoutView); - } else { + initDelayedTilAccountAvailabe(); + + } else { + setContentView(R.layout.no_account_available); - setProgressBarIndeterminateVisibility(false); getSupportActionBar().setNavigationMode(ActionBar.DISPLAY_SHOW_TITLE); findViewById(R.id.setup_account).setOnClickListener(this); + + Intent intent = new Intent(android.provider.Settings.ACTION_ADD_ACCOUNT); + intent.putExtra(android.provider.Settings.EXTRA_AUTHORITIES, new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE }); + startActivity(intent); // although the code is here, the activity won't be created until this.onStart() and this.onResume() are finished; + mForcedLoginToCreateFirstAccount = true; } Log.i(getClass().toString(), "onCreate() end"); @@ -256,21 +271,8 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements } @Override - protected void onRestoreInstanceState(Bundle savedInstanceState) { - Log.i(getClass().toString(), "onRestoreInstanceState() start"); - super.onRestoreInstanceState(savedInstanceState); - mDirs = savedInstanceState.getStringArray(KEY_DIR_ARRAY); - mDirectories = new CustomArrayAdapter(this, R.layout.sherlock_spinner_dropdown_item); - mDirectories.add("/"); - if (mDirs != null) - for (String s : mDirs) - mDirectories.insert(s, 0); - mCurrentDir = savedInstanceState.getParcelable(FileDetailFragment.EXTRA_FILE); - Log.i(getClass().toString(), "onRestoreInstanceState() end"); - } - - @Override protected void onSaveInstanceState(Bundle outState) { + // responsability of restore is prefered in onCreate() before than in onRestoreInstanceState when there are Fragments involved Log.i(getClass().toString(), "onSaveInstanceState() start"); super.onSaveInstanceState(outState); if(mDirectories != null && mDirectories.getCount() != 0){ @@ -289,17 +291,14 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements Log.i(getClass().toString(), "onResume() start"); super.onResume(); - if (!AccountUtils.accountsAreSetup(this)) { - /*Intent intent = new Intent(android.provider.Settings.ACTION_ADD_ACCOUNT); - intent.putExtra(android.provider.Settings.EXTRA_AUTHORITIES, new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE }); - //startActivity(intent); - startActivityForResult(intent, ACTION_CREATE_FIRST_ACCOUNT);*/ + if (AccountUtils.accountsAreSetup(this)) { + // at least an account exist: normal operation - } else { // at least an account exist: normal operation - // set the layout only if it couldn't be set in onCreate - if (findViewById(R.id.file_list_view) == null) - setContentView(mLayoutView); + if (mForcedLoginToCreateFirstAccount) { + initDelayedTilAccountAvailabe(); + mForcedLoginToCreateFirstAccount = false; + } // Listen for sync messages IntentFilter syncIntentFilter = new IntentFilter(FileSyncService.SYNC_MESSAGE); @@ -316,9 +315,10 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements AccountUtils.getCurrentOwnCloudAccount(this), getContentResolver()); - // File list + // File list fragments mFileList = (FileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList); + // Figure out what directory to list. // Priority: Intent (here), savedInstanceState (onCreate), root dir (dir is null) if(getIntent().hasExtra(FileDetailFragment.EXTRA_FILE)){ @@ -561,7 +561,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements @Override public void run() { WebdavClient wdc = new WebdavClient(mAccount, getApplicationContext()); - + String username = mAccount.name.substring(0, mAccount.name.lastIndexOf('@')); String password = mAm.getPassword(mAccount); @@ -667,13 +667,97 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements @Override public void onClick(View v) { if (v.getId() == R.id.setup_account) { - Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS"); - intent.putExtra("authorities", new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE }); - startActivity(intent); + Intent intent = new Intent(android.provider.Settings.ACTION_ADD_ACCOUNT); + intent.putExtra(android.provider.Settings.EXTRA_AUTHORITIES, new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE }); + startActivity(intent); + mForcedLoginToCreateFirstAccount = true; } } + + + + + /** + * {@inheritDoc} + */ + @Override public DataStorageManager getStorageManager() { return mStorageManager; - } + } + + + /** + * {@inheritDoc} + */ + @Override + public void onDirectoryClick(OCFile directory) { + pushDirname(directory); + ActionBar actionBar = getSupportActionBar(); + actionBar.setDisplayHomeAsUpEnabled(true); + + if (mDualPane) { + // Resets the FileDetailsFragment on Tablets so that it always displays + FileDetailFragment fileDetails = (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG); + if (fileDetails != null) { + FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); + transaction.remove(fileDetails); + transaction.add(R.id.file_details_container, new FileDetailFragment(null, null)); + transaction.commit(); + } + } + } + + + /** + * {@inheritDoc} + */ + @Override + public void onFileClick(OCFile file) { + + // If we are on a large device -> update fragment + if (mDualPane) { + FileDetailFragment fileDetails = (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG); + if (fileDetails == null) { + // first selected file since the current directory was listed + FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); + transaction.replace(R.id.file_details_container, new FileDetailFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG); + transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); + transaction.commit(); + } else { + // another select file + fileDetails.updateFileDetails(file, AccountUtils.getCurrentOwnCloudAccount(this)); + } + + } else { // small or medium screen device -> new Activity + Intent showDetailsIntent = new Intent(this, FileDetailActivity.class); + showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, file); + showDetailsIntent.putExtra(FileDownloader.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(this)); + startActivity(showDetailsIntent); + } + } + + /** + * Operations in this method should be preferably performed in onCreate to have a lighter onResume method. + * + * 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 + * put instead the ugly view that shows the 'Setup' button to restart the login activity. + * + * 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 + * FragmentList view empty). + * + * 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) + */ + private void initDelayedTilAccountAvailabe() { + setContentView(mLayoutView); + mDualPane = (findViewById(R.id.file_details_container) != null); + if (mDualPane && getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG) == null) { + FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); + transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null)); // empty FileDetailFragment + transaction.commit(); + } + + } + + } diff --git a/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java b/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java index 9bcf9dee..627c26ab 100644 --- a/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java +++ b/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java @@ -17,21 +17,16 @@ */ package eu.alefzero.owncloud.ui.fragment; -import java.util.List; - import android.accounts.Account; import android.accounts.AccountManager; -import android.app.ActionBar.LayoutParams; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; -import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; -import android.graphics.Path.FillType; import android.net.Uri; import android.os.Bundle; import android.util.Log; @@ -64,47 +59,79 @@ public class FileDetailFragment extends SherlockFragment implements OnClickListener { public static final String EXTRA_FILE = "FILE"; + public static final String EXTRA_ACCOUNT = "ACCOUNT"; - private DownloadFinishReceiver mDownloadFinishReceiver; - private Intent mIntent; private int mLayout; private View mView; private OCFile mFile; + private Account mAccount; + + private DownloadFinishReceiver mDownloadFinishReceiver; + private static final String TAG = "FileDetailFragment"; + public static final String FTAG = "FileDetails"; + /** - * Default constructor - contains real layout + * Creates an empty details fragment. + * + * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically. */ - public FileDetailFragment(){ - mLayout = R.layout.file_details_fragment; + public FileDetailFragment() { + mFile = null; + mAccount = null; + mLayout = R.layout.file_details_empty; } + /** - * Creates a dummy layout. For use if the user never has - * tapped on a file before + * Creates a details fragment. + * + * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before). * - * @param useEmptyView If true, use empty layout + * @param fileToDetail An {@link OCFile} to show in the fragment + * @param ocAccount An ownCloud account; needed to start downloads */ - public FileDetailFragment(boolean useEmptyView){ - if(useEmptyView){ - mLayout = R.layout.file_details_empty; - } else { + public FileDetailFragment(OCFile fileToDetail, Account ocAccount){ + mFile = fileToDetail; + mAccount = ocAccount; + mLayout = R.layout.file_details_empty; + + if(fileToDetail != null && ocAccount != null) { mLayout = R.layout.file_details_fragment; } } - /** - * Use this when creating the fragment and display - * a file at the same time - * - * @param showDetailsIntent The Intent with the required parameters - * @see FileDetailFragment#updateFileDetails(Intent) - */ - public FileDetailFragment(Intent showDetailsIntent) { - mIntent = showDetailsIntent; - mLayout = R.layout.file_details_fragment; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + super.onCreateView(inflater, container, savedInstanceState); + + if (savedInstanceState != null) { + mFile = savedInstanceState.getParcelable(FileDetailFragment.EXTRA_FILE); + mAccount = savedInstanceState.getParcelable(FileDetailFragment.EXTRA_ACCOUNT); + } + + View view = null; + view = inflater.inflate(mLayout, container, false); + mView = view; + + updateFileDetails(); + return view; + } + + + @Override + public void onSaveInstanceState(Bundle outState) { + Log.i(getClass().toString(), "onSaveInstanceState() start"); + super.onSaveInstanceState(outState); + outState.putParcelable(FileDetailFragment.EXTRA_FILE, mFile); + outState.putParcelable(FileDetailFragment.EXTRA_ACCOUNT, mAccount); + Log.i(getClass().toString(), "onSaveInstanceState() end"); } + @Override public void onResume() { super.onResume(); @@ -122,23 +149,6 @@ public class FileDetailFragment extends SherlockFragment implements } @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View view = null; - view = inflater.inflate(mLayout, container, false); - mView = view; - if(mLayout == R.layout.file_details_fragment){ - // Phones will launch an activity with this intent - if(mIntent == null){ - mIntent = getActivity().getIntent(); - } - updateFileDetails(); - } - - return view; - } - - @Override public View getView() { return super.getView() == null ? mView : super.getView(); } @@ -147,8 +157,7 @@ public class FileDetailFragment extends SherlockFragment implements public void onClick(View v) { Toast.makeText(getActivity(), "Downloading", Toast.LENGTH_LONG).show(); Intent i = new Intent(getActivity(), FileDownloader.class); - i.putExtra(FileDownloader.EXTRA_ACCOUNT, - mIntent.getParcelableExtra(FileDownloader.EXTRA_ACCOUNT)); + i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount); i.putExtra(FileDownloader.EXTRA_REMOTE_PATH, mFile.getRemotePath()); i.putExtra(FileDownloader.EXTRA_FILE_PATH, mFile.getURLDecodedRemotePath()); i.putExtra(FileDownloader.EXTRA_FILE_SIZE, mFile.getFileLength()); @@ -167,27 +176,22 @@ public class FileDetailFragment extends SherlockFragment implements /** * Use this method to signal this Activity that it shall update its view. * - * @param intent The {@link Intent} that contains extra information about - * this file The intent needs to have these extras: - *

- * - * {@link FileDetailFragment#EXTRA_FILE}: An {@link OCFile} - * {@link FileDownloader#EXTRA_ACCOUNT}: The Account that file - * belongs to (required for downloading) + * @param file : An {@link OCFile} */ - public void updateFileDetails(Intent intent) { - mIntent = intent; + public void updateFileDetails(OCFile file, Account ocAccount) { + mFile = file; + mAccount = ocAccount; updateFileDetails(); } + /** * Updates the view with all relevant details about that file. */ - private void updateFileDetails() { - mFile = mIntent.getParcelableExtra(EXTRA_FILE); - Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn); + public void updateFileDetails() { - if (mFile != null) { + if (mFile != null && mLayout == R.layout.file_details_fragment) { + Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn); // set file details setFilename(mFile.getFileName()); setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mFile @@ -350,18 +354,15 @@ public class FileDetailFragment extends SherlockFragment implements * the time that the file was created. There is a chance that this will * be fixed in future versions. Use this method to check if this version of * ownCloud has this fix. - * @return True, if ownCloud the ownCloud version is supporting creationg time + * @return True, if ownCloud the ownCloud version is supporting creation time */ private boolean ocVersionSupportsTimeCreated(){ - /* if(mIntent != null){ - Account ocAccount = mIntent.getParcelableExtra(FileDownloader.EXTRA_ACCOUNT); - if(ocAccount != null){ - AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE); - OwnCloudVersion ocVersion = new OwnCloudVersion(accManager - .getUserData(ocAccount, AccountAuthenticator.KEY_OC_VERSION)); - if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) { - return true; - } + /*if(mAccount != null){ + AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE); + OwnCloudVersion ocVersion = new OwnCloudVersion(accManager + .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION)); + if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) { + return true; } }*/ return false; @@ -379,11 +380,12 @@ public class FileDetailFragment extends SherlockFragment implements Toast.makeText(context, R.string.downloader_download_failed , Toast.LENGTH_SHORT).show(); } else if (intent.getAction().equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE)) { - ((OCFile)mIntent.getParcelableExtra(EXTRA_FILE)).setStoragePath(intent.getStringExtra(FileDownloader.EXTRA_FILE_PATH)); - updateFileDetails(mIntent); + mFile.setStoragePath(intent.getStringExtra(FileDownloader.EXTRA_FILE_PATH)); + updateFileDetails(); } } } + } diff --git a/src/eu/alefzero/owncloud/ui/fragment/FileListFragment.java b/src/eu/alefzero/owncloud/ui/fragment/FileListFragment.java index 895b3a28..069a24d6 100644 --- a/src/eu/alefzero/owncloud/ui/fragment/FileListFragment.java +++ b/src/eu/alefzero/owncloud/ui/fragment/FileListFragment.java @@ -19,27 +19,17 @@ package eu.alefzero.owncloud.ui.fragment; import java.util.Vector; -import com.actionbarsherlock.app.ActionBar; - -import android.accounts.Account; -import android.content.Intent; +import android.app.Activity; import android.os.Bundle; -import android.support.v4.app.FragmentTransaction; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; -import android.widget.Toast; -import eu.alefzero.owncloud.AccountUtils; import eu.alefzero.owncloud.R; import eu.alefzero.owncloud.datamodel.DataStorageManager; -import eu.alefzero.owncloud.datamodel.FileDataStorageManager; import eu.alefzero.owncloud.datamodel.OCFile; -import eu.alefzero.owncloud.files.services.FileDownloader; import eu.alefzero.owncloud.ui.FragmentListView; -import eu.alefzero.owncloud.ui.activity.FileDetailActivity; -import eu.alefzero.owncloud.ui.activity.FileDisplayActivity; import eu.alefzero.owncloud.ui.adapter.FileListListAdapter; /** @@ -51,23 +41,26 @@ import eu.alefzero.owncloud.ui.adapter.FileListListAdapter; public class FileListFragment extends FragmentListView { private static final String TAG = "FileListFragment"; + private FileListFragment.ContainerActivity mContainerActivity; + private Vector mFiles; - private OCFile mFile; - private boolean mIsLargeDevice; + private OCFile mFile = null; + + /** + * {@inheritDoc} + */ @Override - public void onCreate(Bundle savedInstanceState) { - Log.i(getClass().toString(), "onCreate() start"); - super.onCreate(savedInstanceState); - - Intent intent = getActivity().getIntent(); - OCFile directory = intent.getParcelableExtra(FileDetailFragment.EXTRA_FILE); - mFile = directory; - mIsLargeDevice = false; - - Log.i(getClass().toString(), "onCreate() stop"); + public void onAttach(Activity activity) { + super.onAttach(activity); + try { + mContainerActivity = (ContainerActivity) activity; + } catch (ClassCastException e) { + throw new ClassCastException(activity.toString() + " must implement FileListFragment.ContainerActivity"); + } } + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -80,21 +73,17 @@ public class FileListFragment extends FragmentListView { return getListView(); } + @Override - public void onStart() { - Log.i(getClass().toString(), "onStart() start"); - super.onStart(); - // Create a placeholder upon launch - View fragmentContainer = getActivity().findViewById(R.id.file_details_container); - if (fragmentContainer != null) { - mIsLargeDevice = true; - FragmentTransaction transaction = getFragmentManager().beginTransaction(); - transaction.replace(R.id.file_details_container, new FileDetailFragment(true)); - transaction.commit(); - } - Log.i(getClass().toString(), "onStart() end"); + public void onActivityCreated(Bundle savedInstanceState) { + Log.i(getClass().toString(), "onActivityCreated() start"); + + super.onCreate(savedInstanceState); + + Log.i(getClass().toString(), "onActivityCreated() stop"); } - + + @Override public void onItemClick(AdapterView l, View v, int position, long id) { if (mFiles.size() <= position) { @@ -102,49 +91,18 @@ public class FileListFragment extends FragmentListView { } OCFile file = mFiles.get(position); - // Update ActionBarPath + /// Click on a directory if (file.getMimetype().equals("DIR")) { + // just local updates mFile = file; - ((FileDisplayActivity) getActivity()).pushDirname(file); - ActionBar actionBar = ((FileDisplayActivity) getActivity()).getSupportActionBar(); - actionBar.setDisplayHomeAsUpEnabled(true); listDirectory(file); - resetFileFragment(); - return; - } - - Intent showDetailsIntent = new Intent(getActivity(), FileDetailActivity.class); - showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, file); - showDetailsIntent.putExtra(FileDownloader.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(getActivity())); - - // If we are on a large device -> update fragment - if (mIsLargeDevice) { - FileDetailFragment fileDetails = (FileDetailFragment) getFragmentManager().findFragmentByTag("FileDetails"); - if (fileDetails == null) { - FragmentTransaction transaction = getFragmentManager().beginTransaction(); - transaction.replace(R.id.file_details_container, new FileDetailFragment(showDetailsIntent), "FileDetails"); - transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); - transaction.commit(); - } else { - fileDetails.updateFileDetails(showDetailsIntent); - } - } else { - startActivity(showDetailsIntent); - } - } - - /** - * Resets the FileDetailsFragment on Tablets so that it always displays - * "Tab on a file to display it's details" - */ - private void resetFileFragment() { - FileDetailFragment fileDetails = (FileDetailFragment) getFragmentManager().findFragmentByTag("FileDetails"); - if (fileDetails != null) { - FragmentTransaction transaction = getFragmentManager().beginTransaction(); - transaction.remove(fileDetails); - transaction.add(R.id.file_details_container, new FileDetailFragment(true)); - transaction.commit(); + // any other updates are let to the container Activity + mContainerActivity.onDirectoryClick(file); + + } else { /// Click on a file + mContainerActivity.onFileClick(file); } + } /** @@ -154,13 +112,11 @@ public class FileListFragment extends FragmentListView { OCFile parentDir = null; if(mFile != null){ - DataStorageManager storageManager = ((FileDisplayActivity)getActivity()).getStorageManager(); + DataStorageManager storageManager = mContainerActivity.getStorageManager(); parentDir = storageManager.getFileById(mFile.getParentId()); mFile = parentDir; } - listDirectory(parentDir); - resetFileFragment(); } /** @@ -188,7 +144,7 @@ public class FileListFragment extends FragmentListView { */ public void listDirectory(OCFile directory) { - DataStorageManager storageManager = ((FileDisplayActivity)getActivity()).getStorageManager(); + DataStorageManager storageManager = mContainerActivity.getStorageManager(); // Check input parameters for null if(directory == null){ @@ -208,12 +164,42 @@ public class FileListFragment extends FragmentListView { } mFile = directory; - mFiles = storageManager.getDirectoryContent(directory); + /*if (mFiles == null || mFiles.size() == 0) { Toast.makeText(getActivity(), "There are no files here", Toast.LENGTH_LONG).show(); }*/ setListAdapter(new FileListListAdapter(directory, storageManager, getActivity())); } + + + + /** + * Interface to implement by any Activity that includes some instance of FileListFragment + * + * @author David A. Velasco + */ + public interface ContainerActivity { + + /** + * Callback method invoked when a directory is clicked by the user on the files list + * + * @param file + */ + public void onDirectoryClick(OCFile file); + + /** + * Callback method invoked when a file (non directory) is clicked by the user on the files list + * + * @param file + */ + public void onFileClick(OCFile file); + + /** + * Getter for the current DataStorageManager in the container activity + */ + public DataStorageManager getStorageManager(); + + } }