46775b9815938c42a074f4c3655f37061c4926bf
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / LocalFileListFragment.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 java.io.File;
21
22 import com.owncloud.android.ui.ExtendedListView;
23 import com.owncloud.android.ui.FragmentListView;
24 import com.owncloud.android.ui.adapter.LocalFileListAdapter;
25
26 import android.app.Activity;
27 import android.os.Bundle;
28 import android.os.Environment;
29 import android.util.Log;
30 import android.util.SparseBooleanArray;
31 import android.view.LayoutInflater;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.widget.AdapterView;
35 import android.widget.ImageView;
36 import android.widget.ListView;
37
38 import com.owncloud.android.R;
39
40 /**
41 * A Fragment that lists all files and folders in a given LOCAL path.
42 *
43 * @author David A. Velasco
44 *
45 */
46 public class LocalFileListFragment extends FragmentListView {
47 private static final String TAG = "LocalFileListFragment";
48 private static final String SAVED_LIST_POSITION = "LIST_POSITION";
49
50 /** Reference to the Activity which this fragment is attached to. For callbacks */
51 private LocalFileListFragment.ContainerActivity mContainerActivity;
52
53 /** Directory to show */
54 private File mDirectory = null;
55
56 /** Adapter to connect the data from the directory with the View object */
57 private LocalFileListAdapter mAdapter = null;
58
59
60 /**
61 * {@inheritDoc}
62 */
63 @Override
64 public void onAttach(Activity activity) {
65 super.onAttach(activity);
66 try {
67 mContainerActivity = (ContainerActivity) activity;
68 } catch (ClassCastException e) {
69 throw new ClassCastException(activity.toString() + " must implement " + LocalFileListFragment.ContainerActivity.class.getCanonicalName());
70 }
71 }
72
73
74 /**
75 * {@inheritDoc}
76 */
77 @Override
78 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
79 Log.i(TAG, "onCreateView() start");
80 super.onCreateView(inflater, container, savedInstanceState);
81 getListView().setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
82 getListView().setDividerHeight(1);
83 getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
84
85 Log.i(TAG, "onCreateView() end");
86 return getListView();
87 }
88
89
90 /**
91 * {@inheritDoc}
92 */
93 @Override
94 public void onActivityCreated(Bundle savedInstanceState) {
95 Log.i(TAG, "onActivityCreated() start");
96
97 super.onCreate(savedInstanceState);
98 mAdapter = new LocalFileListAdapter(mContainerActivity.getInitialDirectory(), getActivity());
99 setListAdapter(mAdapter);
100
101 if (savedInstanceState != null) {
102 Log.i(TAG, "savedInstanceState is not null");
103 int position = savedInstanceState.getInt(SAVED_LIST_POSITION);
104 setReferencePosition(position);
105 }
106
107 Log.i(TAG, "onActivityCreated() stop");
108 }
109
110
111 @Override
112 public void onSaveInstanceState(Bundle savedInstanceState) {
113 Log.i(TAG, "onSaveInstanceState() start");
114
115 savedInstanceState.putInt(SAVED_LIST_POSITION, getReferencePosition());
116
117
118 Log.i(TAG, "onSaveInstanceState() stop");
119 }
120
121
122 /**
123 * Calculates the position of the item that will be used as a reference to reposition the visible items in the list when
124 * the device is turned to other position.
125 *
126 * THe current policy is take as a reference the visible item in the center of the screen.
127 *
128 * @return The position in the list of the visible item in the center of the screen.
129 */
130 private int getReferencePosition() {
131 return (getListView().getFirstVisiblePosition() + getListView().getLastVisiblePosition()) / 2;
132 }
133
134
135 /**
136 * Sets the visible part of the list from the reference position.
137 *
138 * @param position Reference position previously returned by {@link LocalFileListFragment#getReferencePosition()}
139 */
140 private void setReferencePosition(int position) {
141 ((ExtendedListView)getListView()).setAndCenterSelection(position);
142 }
143
144
145 /**
146 * Checks the file clicked over. Browses inside if it is a directory. Notifies the container activity in any case.
147 */
148 @Override
149 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
150 File file = (File) mAdapter.getItem(position);
151 if (file != null) {
152 /// Click on a directory
153 if (file.isDirectory()) {
154 // just local updates
155 listDirectory(file);
156 // notify the click to container Activity
157 mContainerActivity.onDirectoryClick(file);
158
159 } else { /// Click on a file
160 ImageView checkBoxV = (ImageView) v.findViewById(R.id.custom_checkbox);
161 if (checkBoxV != null) {
162 if (getListView().isItemChecked(position)) {
163 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
164 } else {
165 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
166 }
167 }
168 // notify the change to the container Activity
169 mContainerActivity.onFileClick(file);
170 }
171
172 } else {
173 Log.w(TAG, "Null object in ListAdapter!!");
174 }
175 }
176
177
178 /**
179 * Call this, when the user presses the up button
180 */
181 public void onNavigateUp() {
182 File parentDir = null;
183 if(mDirectory != null) {
184 parentDir = mDirectory.getParentFile(); // can be null
185 }
186 listDirectory(parentDir);
187 }
188
189
190 /**
191 * Use this to query the {@link File} object for the directory
192 * that is currently being displayed by this fragment
193 *
194 * @return File The currently displayed directory
195 */
196 public File getCurrentDirectory(){
197 return mDirectory;
198 }
199
200
201 /**
202 * Calls {@link LocalFileListFragment#listDirectory(File)} with a null parameter
203 * to refresh the current directory.
204 */
205 public void listDirectory(){
206 listDirectory(null);
207 }
208
209
210 /**
211 * Lists the given directory on the view. When the input parameter is null,
212 * it will either refresh the last known directory, or list the root
213 * if there never was a directory.
214 *
215 * @param directory Directory to be listed
216 */
217 public void listDirectory(File directory) {
218
219 // Check input parameters for null
220 if(directory == null) {
221 if(mDirectory != null){
222 directory = mDirectory;
223 } else {
224 directory = Environment.getExternalStorageDirectory(); // TODO be careful with the state of the storage; could not be available
225 if (directory == null) return; // no files to show
226 }
227 }
228
229
230 // if that's not a directory -> List its parent
231 if(!directory.isDirectory()){
232 Log.w(TAG, "You see, that is not a directory -> " + directory.toString());
233 directory = directory.getParentFile();
234 }
235
236 mList.clearChoices(); // by now, only files in the same directory will be kept as selected
237 mAdapter.swapDirectory(directory);
238 if (mDirectory == null || !mDirectory.equals(directory)) {
239 mList.setSelectionFromTop(0, 0);
240 }
241 mDirectory = directory;
242 }
243
244
245 /**
246 * Returns the fule paths to the files checked by the user
247 *
248 * @return File paths to the files checked by the user.
249 */
250 public String[] getCheckedFilePaths() {
251 String [] result = null;
252 SparseBooleanArray positions = mList.getCheckedItemPositions();
253 if (positions.size() > 0) {
254 Log.d(TAG, "Returning " + positions.size() + " selected files");
255 result = new String[positions.size()];
256 for (int i=0; i<positions.size(); i++) {
257 result[i] = ((File) mList.getItemAtPosition(positions.keyAt(i))).getAbsolutePath();
258 }
259 }
260 return result;
261 }
262
263
264 /**
265 * Interface to implement by any Activity that includes some instance of LocalFileListFragment
266 *
267 * @author David A. Velasco
268 */
269 public interface ContainerActivity {
270
271 /**
272 * Callback method invoked when a directory is clicked by the user on the files list
273 *
274 * @param file
275 */
276 public void onDirectoryClick(File directory);
277
278 /**
279 * Callback method invoked when a file (non directory) is clicked by the user on the files list
280 *
281 * @param file
282 */
283 public void onFileClick(File file);
284
285
286 /**
287 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
288 *
289 * @return Directory to list firstly. Can be NULL.
290 */
291 public File getInitialDirectory();
292
293 }
294
295
296 }