From: David A. Velasco Date: Thu, 14 May 2015 16:05:12 +0000 (+0200) Subject: Removed extra public constructor from Fragments handled by PreviewImageActivity X-Git-Tag: test~6^2^2~1 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/c0ef93882e77fd31bbe3327ed641e438f4ed24a5?ds=inline Removed extra public constructor from Fragments handled by PreviewImageActivity --- diff --git a/src/com/owncloud/android/ui/preview/FileDownloadFragment.java b/src/com/owncloud/android/ui/preview/FileDownloadFragment.java index 99a4d444..631a8367 100644 --- a/src/com/owncloud/android/ui/preview/FileDownloadFragment.java +++ b/src/com/owncloud/android/ui/preview/FileDownloadFragment.java @@ -23,18 +23,16 @@ import java.lang.ref.WeakReference; import com.owncloud.android.R; import com.owncloud.android.datamodel.OCFile; -import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder; import com.owncloud.android.ui.fragment.FileFragment; import android.accounts.Account; import android.os.Bundle; +import android.support.v4.app.Fragment; import android.support.v4.app.FragmentStatePagerAdapter; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; -import android.widget.ImageButton; -import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.TextView; @@ -50,10 +48,14 @@ public class FileDownloadFragment extends FileFragment implements OnClickListene public static final String EXTRA_FILE = "FILE"; public static final String EXTRA_ACCOUNT = "ACCOUNT"; private static final String EXTRA_ERROR = "ERROR"; + + private static final String ARG_FILE = "FILE"; + private static final String ARG_IGNORE_FIRST = "IGNORE_FIRST"; + private static final String ARG_ACCOUNT = "ACCOUNT" ; private View mView; private Account mAccount; - + public ProgressListener mProgressListener; private boolean mListening; @@ -61,12 +63,39 @@ public class FileDownloadFragment extends FileFragment implements OnClickListene private boolean mIgnoreFirstSavedState; private boolean mError; - + + + /** + * Public factory method to create a new fragment that shows the progress of a file download. + * + * Android strongly recommends keep the empty constructor of fragments as the only public constructor, and + * use {@link #setArguments(Bundle)} to set the needed arguments. + * + * This method hides to client objects the need of doing the construction in two steps. + * + * When 'file' is null creates a dummy layout (useful when a file wasn't tapped before). + * + * @param file An {@link OCFile} to show in the fragment + * @param account An OC account; needed to start downloads + * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter} + * TODO better solution + */ + public static Fragment newInstance(OCFile file, Account account, boolean ignoreFirstSavedState) { + FileDownloadFragment frag = new FileDownloadFragment(); + Bundle args = new Bundle(); + args.putParcelable(ARG_FILE, file); + args.putParcelable(ARG_ACCOUNT, account); + args.putBoolean(ARG_IGNORE_FIRST, ignoreFirstSavedState); + frag.setArguments(args); + return frag; + } + /** * 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. + * It's necessary to keep a public constructor without parameters; the system uses it when tries to + * reinstantiate a fragment automatically. */ public FileDownloadFragment() { super(); @@ -76,30 +105,17 @@ public class FileDownloadFragment extends FileFragment implements OnClickListene mIgnoreFirstSavedState = false; mError = false; } - - - /** - * Creates a details fragment. - * - * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before). - * - * @param fileToDetail An {@link OCFile} to show in the fragment - * @param ocAccount An ownCloud account; needed to start downloads - * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}; TODO better solution - */ - public FileDownloadFragment(OCFile fileToDetail, Account ocAccount, boolean ignoreFirstSavedState) { - super(fileToDetail); - mAccount = ocAccount; - mProgressListener = null; - mListening = false; - mIgnoreFirstSavedState = ignoreFirstSavedState; - mError = false; - } - - + + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + Bundle args = getArguments(); + setFile((OCFile)args.getParcelable(ARG_FILE)); + // TODO better in super, but needs to check ALL the class extending FileFragment; not right now + + mIgnoreFirstSavedState = args.getBoolean(ARG_IGNORE_FIRST); + mAccount = args.getParcelable(ARG_ACCOUNT); } @@ -125,9 +141,9 @@ public class FileDownloadFragment extends FileFragment implements OnClickListene ProgressBar progressBar = (ProgressBar)mView.findViewById(R.id.progressBar); mProgressListener = new ProgressListener(progressBar); - ((ImageButton)mView.findViewById(R.id.cancelBtn)).setOnClickListener(this); + (mView.findViewById(R.id.cancelBtn)).setOnClickListener(this); - ((LinearLayout)mView.findViewById(R.id.fileDownloadLL)).setOnClickListener(new OnClickListener() { + (mView.findViewById(R.id.fileDownloadLL)).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ((PreviewImageActivity) getActivity()).toggleFullScreen(); @@ -206,31 +222,6 @@ public class FileDownloadFragment extends FileFragment implements OnClickListene /** - * Updates the view depending upon the state of the downloading file. - * - * @param transferring When true, the view must be updated assuming that the holded file is - * downloading, no matter what the downloaderBinder says. - */ - /* - public void updateView(boolean transferring) { - // configure UI for depending upon local state of the file - // TODO remove - if (transferring || getFile().isDownloading()) { - setButtonsForTransferring(); - - } else if (getFile().isDown()) { - - setButtonsForDown(); - - } else { - setButtonsForRemote(); - } - getView().invalidate(); - - } - */ - - /** * Enables or disables buttons for a file being downloaded */ private void setButtonsForTransferring() { @@ -289,7 +280,9 @@ public class FileDownloadFragment extends FileFragment implements OnClickListene public void listenForTransferProgress() { if (mProgressListener != null && !mListening) { if (mContainerActivity.getFileDownloaderBinder() != null) { - mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, getFile()); + mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener( + mProgressListener, mAccount, getFile() + ); mListening = true; setButtonsForTransferring(); } @@ -300,13 +293,15 @@ public class FileDownloadFragment extends FileFragment implements OnClickListene public void leaveTransferProgress() { if (mProgressListener != null) { if (mContainerActivity.getFileDownloaderBinder() != null) { - mContainerActivity.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, getFile()); + mContainerActivity.getFileDownloaderBinder().removeDatatransferProgressListener( + mProgressListener, mAccount, getFile() + ); mListening = false; } } } - + /** * Helper class responsible for updating the progress bar shown for file uploading or downloading */ @@ -319,7 +314,9 @@ public class FileDownloadFragment extends FileFragment implements OnClickListene } @Override - public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String filename) { + public void onTransferProgress( + long progressRate, long totalTransferredSoFar, long totalToTransfer, String filename + ) { int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer)); if (percent != mLastPercent) { ProgressBar pb = mProgressBar.get(); diff --git a/src/com/owncloud/android/ui/preview/PreviewImageFragment.java b/src/com/owncloud/android/ui/preview/PreviewImageFragment.java index 938abf7e..699d9f85 100644 --- a/src/com/owncloud/android/ui/preview/PreviewImageFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewImageFragment.java @@ -64,17 +64,18 @@ import third_parties.michaelOrtiz.TouchImageViewCustom; /** * This fragment shows a preview of a downloaded image. * - * Trying to get an instance with NULL {@link OCFile} or ownCloud {@link Account} values will produce an {@link IllegalStateException}. + * Trying to get an instance with a NULL {@link OCFile} will produce an {@link IllegalStateException}. * * If the {@link OCFile} passed is not downloaded, an {@link IllegalStateException} is generated on instantiation too. */ public class PreviewImageFragment extends FileFragment { public static final String EXTRA_FILE = "FILE"; - public static final String EXTRA_ACCOUNT = "ACCOUNT"; + + private static final String ARG_FILE = "FILE"; + private static final String ARG_IGNORE_FIRST = "IGNORE_FIRST"; private View mView; - private Account mAccount; private TouchImageViewCustom mImageView; private TextView mMessageView; private ProgressBar mProgressWheel; @@ -87,33 +88,39 @@ public class PreviewImageFragment extends FileFragment { private LoadBitmapTask mLoadBitmapTask = null; - + /** - * Creates a fragment to preview an image. - * - * When 'imageFile' or 'ocAccount' are null - * + * Public factory method to create a new fragment that previews an image. + * + * Android strongly recommends keep the empty constructor of fragments as the only public constructor, and + * use {@link #setArguments(Bundle)} to set the needed arguments. + * + * This method hides to client objects the need of doing the construction in two steps. + * * @param imageFile An {@link OCFile} to preview as an image in the fragment - * @param ocAccount An ownCloud account; needed to start downloads - * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}; TODO better solution + * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter} + * ; TODO better solution */ - public PreviewImageFragment(OCFile fileToDetail, Account ocAccount, boolean ignoreFirstSavedState) { - super(fileToDetail); - mAccount = ocAccount; - mIgnoreFirstSavedState = ignoreFirstSavedState; + public static PreviewImageFragment newInstance(OCFile imageFile, boolean ignoreFirstSavedState) { + PreviewImageFragment frag = new PreviewImageFragment(); + Bundle args = new Bundle(); + args.putParcelable(ARG_FILE, imageFile); + args.putBoolean(ARG_IGNORE_FIRST, ignoreFirstSavedState); + frag.setArguments(args); + return frag; } + /** * Creates an empty fragment for image previews. * - * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically (for instance, when the device is turned a aside). + * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically + * (for instance, when the device is turned a aside). * * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful construction */ public PreviewImageFragment() { - super(); - mAccount = null; mIgnoreFirstSavedState = false; } @@ -124,6 +131,11 @@ public class PreviewImageFragment extends FileFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + Bundle args = getArguments(); + setFile((OCFile)args.getParcelable(ARG_FILE)); + // TODO better in super, but needs to check ALL the class extending FileFragment; not right now + + mIgnoreFirstSavedState = args.getBoolean(ARG_IGNORE_FIRST); setHasOptionsMenu(true); } @@ -162,7 +174,6 @@ public class PreviewImageFragment extends FileFragment { if (!mIgnoreFirstSavedState) { OCFile file = (OCFile)savedInstanceState.getParcelable(PreviewImageFragment.EXTRA_FILE); setFile(file); - mAccount = savedInstanceState.getParcelable(PreviewImageFragment.EXTRA_ACCOUNT); } else { mIgnoreFirstSavedState = false; } @@ -170,9 +181,6 @@ public class PreviewImageFragment extends FileFragment { if (getFile() == null) { throw new IllegalStateException("Instanced with a NULL OCFile"); } - if (mAccount == null) { - throw new IllegalStateException("Instanced with a NULL ownCloud Account"); - } if (!getFile().isDown()) { throw new IllegalStateException("There is no local file to preview"); } @@ -186,7 +194,6 @@ public class PreviewImageFragment extends FileFragment { public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putParcelable(PreviewImageFragment.EXTRA_FILE, getFile()); - outState.putParcelable(PreviewImageFragment.EXTRA_ACCOUNT, mAccount); } @@ -329,8 +336,9 @@ public class PreviewImageFragment extends FileFragment { if (mBitmap != null) { mBitmap.recycle(); System.gc(); - // putting this in onStop() is just the same; the fragment is always destroyed by the ViewPager - // when swipes further than the valid offset, and onStop() is never called before than that + // putting this in onStop() is just the same; the fragment is always destroyed by + // {@link FragmentStatePagerAdapter} when the fragment in swiped further than the valid offscreen + // distance, and onStop() is never called before than that } super.onDestroy(); } @@ -350,20 +358,22 @@ public class PreviewImageFragment extends FileFragment { /** * Weak reference to the target {@link ImageView} where the bitmap will be loaded into. * - * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the load finishes. + * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before + * the load finishes. */ private final WeakReference mImageViewRef; /** * Weak reference to the target {@link TextView} where error messages will be written. * - * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the load finishes. + * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the + * load finishes. */ private final WeakReference mMessageViewRef; /** - * Weak reference to the target {@link Progressbar} shown while the load is in progress. + * Weak reference to the target {@link ProgressBar} shown while the load is in progress. * * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the load finishes. */ diff --git a/src/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.java b/src/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.java index 14ae34fa..6e7f19d4 100644 --- a/src/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.java +++ b/src/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.java @@ -102,15 +102,17 @@ public class PreviewImagePagerAdapter extends FragmentStatePagerAdapter { OCFile file = mImageFiles.get(i); Fragment fragment = null; if (file.isDown()) { - fragment = new PreviewImageFragment(file, mAccount, mObsoletePositions.contains(Integer.valueOf(i))); + fragment = PreviewImageFragment.newInstance(file, mObsoletePositions.contains(Integer.valueOf(i))); } else if (mDownloadErrors.contains(Integer.valueOf(i))) { - fragment = new FileDownloadFragment(file, mAccount, true); + fragment = FileDownloadFragment.newInstance(file, mAccount, true); ((FileDownloadFragment)fragment).setError(true); mDownloadErrors.remove(Integer.valueOf(i)); } else { - fragment = new FileDownloadFragment(file, mAccount, mObsoletePositions.contains(Integer.valueOf(i))); + fragment = FileDownloadFragment.newInstance( + file, mAccount, mObsoletePositions.contains(Integer.valueOf(i)) + ); } mObsoletePositions.remove(Integer.valueOf(i)); return fragment;