Browse Source

New Layout for FileDetailsFragment (needs to be redone with
RelativeLayout for better performance), Fix AppCrashes on rotation
change

Lennart Rosam 13 years ago
parent
commit
cb8761f66f

+ 2 - 8
res/layout-large-land/files.xml

@@ -19,6 +19,7 @@
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
+    android:background="@color/owncloud_white"
     android:orientation="horizontal" >
 
     <LinearLayout
@@ -42,14 +43,7 @@
         android:layout_height="fill_parent"
         android:layout_weight="2" >
 
-        <fragment
-            android:id="@+id/fileDetail"
-            android:layout_width="fill_parent"
-            android:layout_height="fill_parent"
-            class="eu.alefzero.owncloud.ui.fragment.FileDetailFragment" >
-
-            <!-- Preview: layout=@layout/file_details_empty -->
-        </fragment>
+        <!-- Preview: layout=@layout/file_details_empty -->
     </LinearLayout>
 
 </LinearLayout>

+ 9 - 8
res/layout/file_activity_details.xml

@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<!-- 
+<!--
   ownCloud Android client application
 
   Copyright (C) 2012  Bartek Przybylski
@@ -14,17 +14,18 @@
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
- -->
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.

+-->
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="vertical" >
 
-<fragment
-    android:id="@+id/fileDetail"
-    android:layout_width="fill_parent"
-    android:layout_height="fill_parent"
-    class="eu.alefzero.owncloud.ui.fragment.FileDetailFragment" />
+    <LinearLayout
+        android:id="@+id/fragment"
+            android:layout_width="fill_parent"
+            android:layout_height="fill_parent" >
+            <!-- Preview: layout=@layout/file_details_fragment -->
+    </LinearLayout>
 
 </LinearLayout>

+ 84 - 77
res/layout/file_details_fragment.xml

@@ -28,89 +28,48 @@
         android:layout_height="fill_parent" >
 
         <LinearLayout
-            android:layout_width="fill_parent"
+            android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:orientation="vertical" >
 
-            <ImageView
-                android:id="@+id/fdIcon"
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:layout_alignParentLeft="true"
-                android:layout_marginLeft="16dp"
-                android:src="@drawable/file" />
-
-            <TextView
-                android:id="@+id/fdFilename"
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:layout_alignBottom="@+id/fdIcon"
-                android:layout_centerHorizontal="true"
-                android:text="file.name"
-                android:textAppearance="?android:attr/textAppearanceLarge" />
-
             <LinearLayout
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content" >
-
-                <TextView
-                    android:id="@+id/fdType"
-                    android:layout_width="wrap_content"
-                    android:layout_height="wrap_content"
-                    android:layout_above="@+id/fdCreatedLabel"
-                    android:layout_alignLeft="@+id/fdFilename"
-                    android:text="JPG Image"
-                    android:textAppearance="?android:attr/textAppearanceMedium" />
-
-                <TextView
-                    android:id="@+id/fdSize"
-                    android:layout_width="wrap_content"
-                    android:layout_height="wrap_content"
-                    android:layout_above="@+id/fdSizeLabel"
-                    android:layout_alignLeft="@+id/fdFilename"
-                    android:text="389 KB"
-                    android:textAppearance="?android:attr/textAppearanceMedium" />
+                android:layout_width="fill_parent"
+                android:layout_height="wrap_content"
+                android:orientation="horizontal" >
 
-                <TextView
-                    android:id="@+id/fdCreated"
+                <ImageView
+                    android:id="@+id/fdIcon"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
-                    android:layout_above="@+id/fdModifiedLabel"
-                    android:layout_alignLeft="@+id/fdFilename"
-                    android:text="2012/05/18 12:23 PM"
-                    android:textAppearance="?android:attr/textAppearanceMedium" />
+                    android:layout_marginLeft="16dp"
+                    android:layout_marginTop="4dp"
+                    android:src="@drawable/file" />
 
                 <TextView
-                    android:id="@+id/fdModified"
+                    android:id="@+id/fdFilename"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
-                    android:layout_alignBaseline="@+id/fdModifiedLabel"
-                    android:layout_alignBottom="@+id/fdModifiedLabel"
-                    android:layout_alignLeft="@+id/fdCreated"
-                    android:text="2012/05/19 02:56 PM"
-                    android:textAppearance="?android:attr/textAppearanceMedium" />
+                    android:layout_marginLeft="4dp"
+                    android:text="file.name"
+                    android:textAppearance="?android:attr/textAppearanceLarge" />
+            </LinearLayout>
 
-                <ImageView
-                    android:id="@+id/fdPreview"
-                    android:layout_width="wrap_content"
-                    android:layout_height="wrap_content"
-                    android:layout_below="@+id/fdModified"
-                    android:layout_centerHorizontal="true"
-                    android:layout_marginTop="16dp"
-                    android:src="@drawable/owncloud_logo" />
+            <LinearLayout
+                android:layout_width="wrap_content"
+                android:layout_height="match_parent"
+                android:orientation="horizontal" >
 
                 <LinearLayout
-                    android:layout_width="wrap_content"
+                    android:layout_width="match_parent"
                     android:layout_height="wrap_content"
-                    android:layout_alignParentLeft="true"
-                    android:layout_alignParentTop="true" >
+                    android:layout_weight="1"
+                    android:orientation="vertical"
+                    android:paddingLeft="16dp" >
 
                     <TextView
                         android:id="@+id/fdTypeLabel"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
-                        android:layout_alignLeft="@+id/fdIcon"
-                        android:layout_below="@+id/fdIcon"
                         android:layout_marginTop="24dp"
                         android:text="@string/filedetails_type"
                         android:textAppearance="?android:attr/textAppearanceMedium" />
@@ -119,8 +78,6 @@
                         android:id="@+id/fdSizeLabel"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
-                        android:layout_alignLeft="@+id/fdIcon"
-                        android:layout_below="@+id/fdTypeLabel"
                         android:layout_marginTop="12dp"
                         android:text="@string/filedetails_size"
                         android:textAppearance="?android:attr/textAppearanceMedium" />
@@ -129,8 +86,6 @@
                         android:id="@+id/fdCreatedLabel"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
-                        android:layout_alignLeft="@+id/fdIcon"
-                        android:layout_below="@+id/fdSizeLabel"
                         android:layout_marginTop="12dp"
                         android:text="@string/filedetails_created"
                         android:textAppearance="?android:attr/textAppearanceMedium" />
@@ -139,22 +94,74 @@
                         android:id="@+id/fdModifiedLabel"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
-                        android:layout_alignLeft="@+id/fdIcon"
-                        android:layout_below="@+id/fdCreatedLabel"
                         android:layout_marginTop="12dp"
                         android:text="@string/filedetails_modified"
                         android:textAppearance="?android:attr/textAppearanceMedium" />
                 </LinearLayout>
+
+                <LinearLayout
+                    android:layout_width="match_parent"
+                    android:layout_height="wrap_content"
+                    android:layout_weight="2"
+                    android:orientation="vertical"
+                    android:paddingLeft="4dp" >
+
+                    <TextView
+                        android:id="@+id/fdType"
+                        android:layout_width="wrap_content"
+                        android:layout_height="wrap_content"
+                        android:layout_marginTop="24dp"
+                        android:text="JPG Image"
+                        android:textAppearance="?android:attr/textAppearanceMedium" />
+
+                    <TextView
+                        android:id="@+id/fdSize"
+                        android:layout_width="wrap_content"
+                        android:layout_height="wrap_content"
+                        android:layout_marginTop="12dp"
+                        android:text="389 KB"
+                        android:textAppearance="?android:attr/textAppearanceMedium" />
+
+                    <TextView
+                        android:id="@+id/fdCreated"
+                        android:layout_width="wrap_content"
+                        android:layout_height="wrap_content"
+                        android:layout_marginTop="12dp"
+                        android:text="2012/05/18 12:23 PM"
+                        android:textAppearance="?android:attr/textAppearanceMedium" />
+
+                    <TextView
+                        android:id="@+id/fdModified"
+                        android:layout_width="wrap_content"
+                        android:layout_height="wrap_content"
+                        android:layout_marginTop="12dp"
+                        android:text="2012/05/19 02:56 PM"
+                        android:textAppearance="?android:attr/textAppearanceMedium" />
+                </LinearLayout>
+            </LinearLayout>
+
+            <LinearLayout
+                android:layout_width="match_parent"
+                android:layout_height="match_parent"
+                android:orientation="vertical" >
+
+                <ImageView
+                    android:id="@+id/fdPreview"
+                    android:layout_width="wrap_content"
+                    android:layout_height="wrap_content"
+                    android:layout_marginTop="16dp"
+                    android:layout_gravity="center_horizontal"
+                    android:src="@drawable/owncloud_logo" />
+
+                <Button
+                    android:id="@+id/fdDownloadBtn"
+                    android:layout_width="wrap_content"
+                    android:layout_height="wrap_content"
+                    android:layout_marginTop="12dp"
+                    android:layout_gravity="center_horizontal"
+                    android:text="@string/filedetails_download" />
             </LinearLayout>
         </LinearLayout>
     </ScrollView>
 
-    <Button
-        android:id="@+id/fdDownloadBtn"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_below="@+id/fdScrollView"
-        android:layout_centerHorizontal="true"
-        android:text="@string/filedetails_download" />
-
 </LinearLayout>

+ 10 - 2
src/eu/alefzero/owncloud/ui/activity/FileDetailActivity.java

@@ -43,11 +43,19 @@ public class FileDetailActivity extends SherlockFragmentActivity {
         getWindow().requestFeature(Window.FEATURE_NO_TITLE);
         setContentView(R.layout.file_activity_details);
 
-        mFileDetail = new FileDetailFragment(getIntent());
         FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
-        ft.add(R.id.fileDetail, mFileDetail);
+        mFileDetail = new FileDetailFragment();
+        ft.add(R.id.fragment, mFileDetail, "FileDetails");
         ft.commit();
 
     }
 
+    @Override
+    protected void onResume() {
+        super.onResume();
+        mFileDetail.updateFileDetails(getIntent());
+    }
+    
+    
+
 }

+ 37 - 59
src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java

@@ -17,7 +17,6 @@
  */
 package eu.alefzero.owncloud.ui.fragment;
 
-import android.app.FragmentTransaction;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -52,35 +51,33 @@ public class FileDetailFragment extends SherlockFragment implements
 
     public static final String FILE = "FILE";
 
-    private Intent mIntent;
-    //private View mView;
     private DownloadFinishReceiver mDownloadFinishReceiver;
-    private OCFile mFile;
-
+    private Intent mIntent;
     private int mLayout;
-    private boolean mEmptyLayout;
+    private View mView;
+    private OCFile mFile;
 
     /**
-     * Default constructor. When inflated by android -> display empty layout
+     * Default constructor - contains real layout
      */
-    public FileDetailFragment() {
-        mLayout = R.layout.file_details_empty;
-        mEmptyLayout = true;
+    public FileDetailFragment(){
+        mLayout = R.layout.file_details_fragment;
     }
-
+    
     /**
-     * Custom construtor. Use with a {@link FragmentTransaction}. The intent has
-     * to contain {@link FileDetailFragment#FILE} with an OCFile and also
-     * {@link FileDownloader#EXTRA_ACCOUNT} with the account.
+     * Creates a dummy layout. For use if the user never has
+     * tapped on a file before
      * 
-     * @param intent Intent with an account and a file in it for rendering
+     * @param useEmptyView If true, use empty layout
      */
-    public FileDetailFragment(Intent intent) {
-        mLayout = R.layout.file_details_fragment;
-        mIntent = intent;
-        mEmptyLayout = false;
+    public FileDetailFragment(boolean useEmptyView){
+        if(useEmptyView){
+            mLayout = R.layout.file_details_empty;
+        } else {
+            mLayout = R.layout.file_details_fragment;
+        }
     }
-
+    
     @Override
     public void onResume() {
         super.onResume();
@@ -138,31 +135,7 @@ public class FileDetailFragment extends SherlockFragment implements
             downloadButton.setOnClickListener(this);
         }
     }
-
-    @Override
-    public View onCreateView(LayoutInflater inflater, ViewGroup container,
-            Bundle savedInstanceState) {
-        View view = null;
-        view = inflater.inflate(mLayout, container, false);
-        return view;
-    }
-
-    @Override
-    public void onStart() {
-        super.onStart();
-        
-        // Fill in required information about file displaying
-        if(mIntent == null){
-            mIntent = getActivity().getIntent();
-        }
-        
-        // Fill in the details if the layout is not empty
-        if(!mEmptyLayout){
-            updateFileDetails();
-        }
-        
-    }
-
+    
     private void setFilename(String filename) {
         TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
         if (tv != null)
@@ -181,20 +154,25 @@ public class FileDetailFragment extends SherlockFragment implements
             tv.setText(DisplayUtils.bitsToHumanReadable(filesize));
     }
 
-    /**
-     * Use this to check if the correct layout is loaded. When android
-     * instanciates this class using the default constructor, the layout will be
-     * empty.
-     * 
-     * Once a user touches a file for the first time, you must instanciate a new
-     * Fragment with the new FileDetailFragment(true) to inflate the actual
-     * details
-     * 
-     * @return If the layout is empty, this method will return true, otherwise
-     *         false
-     */
-    public boolean isEmptyLayout() {
-        return mEmptyLayout;
+    @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){
+            mIntent = getActivity().getIntent();
+            updateFileDetails();
+        }
+        
+        return view;
+    }
+    
+    
+
+    @Override
+    public View getView() {
+        return super.getView() == null ? mView : super.getView();
     }
 
     @Override

+ 38 - 42
src/eu/alefzero/owncloud/ui/fragment/FileListFragment.java

@@ -52,6 +52,8 @@ public class FileListFragment extends FragmentListView {
     private Stack<String> mDirNames;
     private Vector<OCFile> mFiles;
     private DataStorageManager mStorageManager;
+    private FileDetailFragment mFileDetailsDetailFragment;
+    private boolean mIsLargeDevice = false;
 
     public FileListFragment() {
         mDirNames = new Stack<String>();
@@ -62,13 +64,28 @@ public class FileListFragment extends FragmentListView {
         super.onCreate(savedInstanceState);
 
         mAccount = AccountUtils.getCurrentOwnCloudAccount(getActivity());
-        getListView().setDivider(
-                getResources().getDrawable(R.drawable.uploader_list_separator));
+        getListView().setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
         getListView().setDividerHeight(1);
 
         populateFileList();
     }
 
+    @Override
+    public void onStart() {
+        // Inflate the original fragment for better performance
+        mFileDetailsDetailFragment = new FileDetailFragment();
+        
+        // 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();
+        }
+        super.onStart();
+    }
+
     @Override
     public void onItemClick(AdapterView<?> l, View v, int position, long id) {
         if (mFiles.size() <= position) {
@@ -85,68 +102,50 @@ public class FileListFragment extends FragmentListView {
 
             populateFileList();
             resetFileFragment();
-            
+
             return;
         }
 
-        Intent showDetailsIntent = new Intent(getActivity(),
-                FileDetailActivity.class);
+        Intent showDetailsIntent = new Intent(getActivity(), FileDetailActivity.class);
         showDetailsIntent.putExtra(FileDetailFragment.FILE, file);
         showDetailsIntent.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
 
-        // Try to find by tag first
-        FileDetailFragment fd = (FileDetailFragment) getFragmentManager()
-                .findFragmentByTag("FileDetails");
-
-        // Could be the first time the user has touched a file. find by id
-        if (fd == null) {
-            fd = (FileDetailFragment) getFragmentManager().findFragmentById(
-                    R.id.fileDetail);
-        }
-
-        // Tablets will have this fragment, phones not. Could still be null
-        if (fd != null) {
-
-            if (fd.isEmptyLayout()) {
-                // True, if this is the first time a user taps on a file
-                FragmentTransaction transaction = getFragmentManager()
-                        .beginTransaction();
-                fd = new FileDetailFragment(showDetailsIntent);
-                transaction.replace(R.id.fileDetail, fd, "FileDetails");
+        // 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, mFileDetailsDetailFragment, "FileDetails");
                 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                 transaction.commit();
             } else {
-                fd.updateFileDetails(showDetailsIntent);
+                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" 
+     * 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){
+        if (fileDetails != null) {
             FragmentTransaction transaction = getFragmentManager().beginTransaction();
             transaction.remove(fileDetails);
-            transaction.add(R.id.file_details_container, new FileDetailFragment());
+            transaction.add(R.id.file_details_container, new FileDetailFragment(true));
             transaction.commit();
         }
     }
 
     @Override
-    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
-            long arg3) {
+    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
         ClipData.Item item = new ClipData.Item("ASD");
-        ClipDescription cd = new ClipDescription("ASD",
-                new String[] { ClipDescription.MIMETYPE_TEXT_PLAIN });
+        ClipDescription cd = new ClipDescription("ASD", new String[] { ClipDescription.MIMETYPE_TEXT_PLAIN });
         ClipData dragData = new ClipData(cd, item);
-        arg1.startDrag(dragData,
-                new View.DragShadowBuilder(arg0.getChildAt(arg2)), null, 0);
+        arg1.startDrag(dragData, new View.DragShadowBuilder(arg0.getChildAt(arg2)), null, 0);
         return true;
     }
 
@@ -168,16 +167,13 @@ public class FileListFragment extends FragmentListView {
             s += a + "/";
         Log.e("ASD", s);
 
-        mStorageManager = new FileDataStorageManager(mAccount, getActivity()
-                .getContentResolver());
+        mStorageManager = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
         OCFile file = mStorageManager.getFileByPath(s);
         mFiles = mStorageManager.getDirectoryContent(file);
         if (mFiles == null || mFiles.size() == 0) {
-            Toast.makeText(getActivity(), "There are no files here",
-                    Toast.LENGTH_LONG).show();
+            Toast.makeText(getActivity(), "There are no files here", Toast.LENGTH_LONG).show();
         }
-        setListAdapter(new FileListListAdapter(file, mStorageManager,
-                getActivity()));
+        setListAdapter(new FileListListAdapter(file, mStorageManager, getActivity()));
     }
 
     // TODO: Delete this testing stuff.