77cd78a13ce97085e10cb710967fee807af9855a
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / OCFileListFragment.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18 package com.owncloud.android.ui.fragment;
19
20 import com.owncloud.android.datamodel.DataStorageManager;
21 import com.owncloud.android.datamodel.OCFile;
22 import com.owncloud.android.ui.FragmentListView;
23 import com.owncloud.android.ui.adapter.FileListListAdapter;
24
25 import android.app.Activity;
26 import android.os.Bundle;
27 import android.util.Log;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.widget.AdapterView;
32 import com.owncloud.android.R;
33
34 /**
35 * A Fragment that lists all files and folders in a given path.
36 *
37 * @author Bartek Przybylski
38 *
39 */
40 public class OCFileListFragment extends FragmentListView {
41 private static final String TAG = "FileListFragment";
42
43 private OCFileListFragment.ContainerActivity mContainerActivity;
44
45 private OCFile mFile = null;
46 private FileListListAdapter mAdapter;
47
48
49 /**
50 * {@inheritDoc}
51 */
52 @Override
53 public void onAttach(Activity activity) {
54 super.onAttach(activity);
55 try {
56 mContainerActivity = (ContainerActivity) activity;
57 } catch (ClassCastException e) {
58 throw new ClassCastException(activity.toString() + " must implement " + OCFileListFragment.ContainerActivity.class.getCanonicalName());
59 }
60 }
61
62
63 /**
64 * {@inheritDoc}
65 */
66 @Override
67 public View onCreateView(LayoutInflater inflater, ViewGroup container,
68 Bundle savedInstanceState) {
69 Log.i(TAG, "onCreateView() start");
70 super.onCreateView(inflater, container, savedInstanceState);
71 getListView().setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
72 getListView().setDividerHeight(1);
73
74 Log.i(TAG, "onCreateView() end");
75 return getListView();
76 }
77
78
79 @Override
80 public void onActivityCreated(Bundle savedInstanceState) {
81 Log.i(TAG, "onActivityCreated() start");
82
83 super.onCreate(savedInstanceState);
84 mAdapter = new FileListListAdapter(mContainerActivity.getInitialDirectory(), mContainerActivity.getStorageManager(), getActivity());
85 setListAdapter(mAdapter);
86
87 if (savedInstanceState != null) {
88 Log.i(TAG, "savedInstanceState is not null");
89 int position = savedInstanceState.getInt("LIST_POSITION");
90 getListView().setSelectionFromTop(position, 0);
91 }
92 //mAdapter = new FileListListAdapter();
93
94 Log.i(TAG, "onActivityCreated() stop");
95 }
96
97
98 @Override
99 public void onSaveInstanceState(Bundle savedInstanceState) {
100 Log.i(TAG, "onSaveInstanceState() start");
101
102 savedInstanceState.putInt("LIST_POSITION", getListView().getFirstVisiblePosition());
103
104 Log.i(TAG, "onSaveInstanceState() stop");
105 }
106
107
108 @Override
109 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
110 OCFile file = (OCFile) mAdapter.getItem(position);
111 if (file != null) {
112 /// Click on a directory
113 if (file.getMimetype().equals("DIR")) {
114 // just local updates
115 mFile = file;
116 listDirectory(file);
117 // any other updates are let to the container Activity
118 mContainerActivity.onDirectoryClick(file);
119
120 } else { /// Click on a file
121 mContainerActivity.onFileClick(file);
122 }
123
124 } else {
125 Log.d(TAG, "Null object in ListAdapter!!");
126 }
127
128 }
129
130 /**
131 * Call this, when the user presses the up button
132 */
133 public void onNavigateUp() {
134 OCFile parentDir = null;
135
136 if(mFile != null){
137 DataStorageManager storageManager = mContainerActivity.getStorageManager();
138 parentDir = storageManager.getFileById(mFile.getParentId());
139 mFile = parentDir;
140 }
141 listDirectory(parentDir);
142 }
143
144 /**
145 * Use this to query the {@link OCFile} that is currently
146 * being displayed by this fragment
147 * @return The currently viewed OCFile
148 */
149 public OCFile getCurrentFile(){
150 return mFile;
151 }
152
153 /**
154 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
155 */
156 public void listDirectory(){
157 listDirectory(null);
158 }
159
160 /**
161 * Lists the given directory on the view. When the input parameter is null,
162 * it will either refresh the last known directory, or list the root
163 * if there never was a directory.
164 *
165 * @param directory File to be listed
166 */
167 public void listDirectory(OCFile directory) {
168 DataStorageManager storageManager = mContainerActivity.getStorageManager();
169
170 // Check input parameters for null
171 if(directory == null){
172 if(mFile != null){
173 directory = mFile;
174 } else {
175 directory = storageManager.getFileByPath("/");
176 if (directory == null) return; // no files, wait for sync
177 }
178 }
179
180
181 // If that's not a directory -> List its parent
182 if(!directory.isDirectory()){
183 Log.w(TAG, "You see, that is not a directory -> " + directory.toString());
184 directory = storageManager.getFileById(directory.getParentId());
185 }
186
187 mFile = directory;
188 mAdapter.swapDirectory(mFile);
189 mList.setSelectionFromTop(0, 0);
190 }
191
192
193
194 /**
195 * Interface to implement by any Activity that includes some instance of FileListFragment
196 *
197 * @author David A. Velasco
198 */
199 public interface ContainerActivity {
200
201 /**
202 * Callback method invoked when a directory is clicked by the user on the files list
203 *
204 * @param file
205 */
206 public void onDirectoryClick(OCFile file);
207
208 /**
209 * Callback method invoked when a file (non directory) is clicked by the user on the files list
210 *
211 * @param file
212 */
213 public void onFileClick(OCFile file);
214
215 /**
216 * Getter for the current DataStorageManager in the container activity
217 */
218 public DataStorageManager getStorageManager();
219
220
221 /**
222 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
223 *
224 * @return Directory to list firstly. Can be NULL.
225 */
226 public OCFile getInitialDirectory();
227
228
229 }
230
231 }