moving from eu.alefzero.eu to com.owncloud.android
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / FileListFragment.java
diff --git a/src/com/owncloud/android/ui/fragment/FileListFragment.java b/src/com/owncloud/android/ui/fragment/FileListFragment.java
new file mode 100644 (file)
index 0000000..5d1a8d1
--- /dev/null
@@ -0,0 +1,205 @@
+/* ownCloud Android client application\r
+ *   Copyright (C) 2011  Bartek Przybylski\r
+ *\r
+ *   This program is free software: you can redistribute it and/or modify\r
+ *   it under the terms of the GNU General Public License as published by\r
+ *   the Free Software Foundation, either version 3 of the License, or\r
+ *   (at your option) any later version.\r
+ *\r
+ *   This program is distributed in the hope that it will be useful,\r
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ *   GNU General Public License for more details.\r
+ *\r
+ *   You should have received a copy of the GNU General Public License\r
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
+ *\r
+ */\r
+package com.owncloud.android.ui.fragment;\r
+\r
+import java.util.Vector;\r
+\r
+import com.owncloud.android.datamodel.DataStorageManager;\r
+import com.owncloud.android.datamodel.OCFile;\r
+import com.owncloud.android.ui.FragmentListView;\r
+import com.owncloud.android.ui.adapter.FileListListAdapter;\r
+\r
+import android.app.Activity;\r
+import android.os.Bundle;\r
+import android.util.Log;\r
+import android.view.LayoutInflater;\r
+import android.view.View;\r
+import android.view.ViewGroup;\r
+import android.widget.AdapterView;\r
+import com.owncloud.android.R;\r
+\r
+/**\r
+ * A Fragment that lists all files and folders in a given path.\r
+ * \r
+ * @author Bartek Przybylski\r
+ * \r
+ */\r
+public class FileListFragment extends FragmentListView {\r
+    private static final String TAG = "FileListFragment";\r
+    \r
+    private FileListFragment.ContainerActivity mContainerActivity;\r
+    \r
+    private OCFile mFile = null;\r
+    private FileListListAdapter mAdapter;\r
+\r
+    \r
+    /**\r
+     * {@inheritDoc}\r
+     */\r
+    @Override\r
+    public void onAttach(Activity activity) {\r
+        super.onAttach(activity);\r
+        try {\r
+            mContainerActivity = (ContainerActivity) activity;\r
+        } catch (ClassCastException e) {\r
+            throw new ClassCastException(activity.toString() + " must implement FileListFragment.ContainerActivity");\r
+        }\r
+    }\r
+    \r
+    \r
+    @Override\r
+    public View onCreateView(LayoutInflater inflater, ViewGroup container,\r
+            Bundle savedInstanceState) {\r
+        Log.i(getClass().toString(), "onCreateView() start");\r
+        super.onCreateView(inflater, container, savedInstanceState);\r
+        getListView().setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));\r
+        getListView().setDividerHeight(1);\r
+        \r
+        Log.i(getClass().toString(), "onCreateView() end");\r
+        return getListView();\r
+    }    \r
+\r
+\r
+    @Override\r
+    public void onActivityCreated(Bundle savedInstanceState) {\r
+        Log.i(getClass().toString(), "onActivityCreated() start");\r
+        \r
+        super.onCreate(savedInstanceState);\r
+        //mAdapter = new FileListListAdapter();\r
+        \r
+        Log.i(getClass().toString(), "onActivityCreated() stop");\r
+    }\r
+    \r
+    \r
+    @Override\r
+    public void onItemClick(AdapterView<?> l, View v, int position, long id) {\r
+        OCFile file = (OCFile) mAdapter.getItem(position);\r
+        if (file != null) {\r
+            /// Click on a directory\r
+            if (file.getMimetype().equals("DIR")) {\r
+                // just local updates\r
+                mFile = file;\r
+                listDirectory(file);\r
+                // any other updates are let to the container Activity\r
+                mContainerActivity.onDirectoryClick(file);\r
+            \r
+            } else {    /// Click on a file\r
+                mContainerActivity.onFileClick(file);\r
+            }\r
+            \r
+        } else {\r
+            Log.d(TAG, "Null object in ListAdapter!!");\r
+        }\r
+        \r
+    }\r
+\r
+    /**\r
+     * Call this, when the user presses the up button\r
+     */\r
+    public void onNavigateUp() {\r
+        OCFile parentDir = null;\r
+        \r
+        if(mFile != null){\r
+            DataStorageManager storageManager = mContainerActivity.getStorageManager();\r
+            parentDir = storageManager.getFileById(mFile.getParentId());\r
+            mFile = parentDir;\r
+        }\r
+        listDirectory(parentDir);\r
+    }\r
+\r
+    /**\r
+     * Use this to query the {@link OCFile} that is currently\r
+     * being displayed by this fragment\r
+     * @return The currently viewed OCFile\r
+     */\r
+    public OCFile getCurrentFile(){\r
+        return mFile;\r
+    }\r
+    \r
+    /**\r
+     * Calls {@link FileListFragment#listDirectory(OCFile)} with a null parameter\r
+     */\r
+    public void listDirectory(){\r
+        listDirectory(null);\r
+    }\r
+    \r
+    /**\r
+     * Lists the given directory on the view. When the input parameter is null,\r
+     * it will either refresh the last known directory, or list the root\r
+     * if there never was a directory.\r
+     * \r
+     * @param directory File to be listed\r
+     */\r
+    public void listDirectory(OCFile directory) {\r
+        \r
+        DataStorageManager storageManager = mContainerActivity.getStorageManager();\r
+\r
+        // Check input parameters for null\r
+        if(directory == null){\r
+            if(mFile != null){\r
+                directory = mFile;\r
+            } else {\r
+                directory = storageManager.getFileByPath("/");\r
+                if (directory == null) return; // no files, wait for sync\r
+            }\r
+        }\r
+        \r
+        \r
+        // If that's not a directory -> List its parent\r
+        if(!directory.isDirectory()){\r
+            Log.w(TAG, "You see, that is not a directory -> " + directory.toString());\r
+            directory = storageManager.getFileById(directory.getParentId());\r
+        }\r
+\r
+        mFile = directory;\r
+        \r
+        mAdapter = new FileListListAdapter(directory, storageManager, getActivity());\r
+        setListAdapter(mAdapter);\r
+    }\r
+    \r
+    \r
+    \r
+    /**\r
+     * Interface to implement by any Activity that includes some instance of FileListFragment\r
+     * \r
+     * @author David A. Velasco\r
+     */\r
+    public interface ContainerActivity {\r
+\r
+        /**\r
+         * Callback method invoked when a directory is clicked by the user on the files list\r
+         *  \r
+         * @param file\r
+         */\r
+        public void onDirectoryClick(OCFile file);\r
+        \r
+        /**\r
+         * Callback method invoked when a file (non directory) is clicked by the user on the files list\r
+         *  \r
+         * @param file\r
+         */\r
+        public void onFileClick(OCFile file);\r
+\r
+        /**\r
+         * Getter for the current DataStorageManager in the container activity\r
+         */\r
+        public DataStorageManager getStorageManager();\r
+        \r
+    }\r
+\r
+}\r