Cleaned stuff , add a history delete button and renamed
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / LocalFileListFragment.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19 package com.owncloud.android.ui.fragment;
20
21 import java.io.File;
22
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.Log_OC;
39 import com.owncloud.android.R;
40
41 /**
42 * A Fragment that lists all files and folders in a given LOCAL path.
43 *
44 * @author David A. Velasco
45 *
46 */
47 public class LocalFileListFragment extends FragmentListView {
48 private static final String TAG = "LocalFileListFragment";
49 private static final String SAVED_LIST_POSITION = "LIST_POSITION";
50
51 /** Reference to the Activity which this fragment is attached to. For callbacks */
52 private LocalFileListFragment.ContainerActivity mContainerActivity;
53
54 /** Directory to show */
55 private File mDirectory = null;
56
57 /** Adapter to connect the data from the directory with the View object */
58 private LocalFileListAdapter mAdapter = null;
59
60
61 /**
62 * {@inheritDoc}
63 */
64 @Override
65 public void onAttach(Activity activity) {
66 super.onAttach(activity);
67 try {
68 mContainerActivity = (ContainerActivity) activity;
69 } catch (ClassCastException e) {
70 throw new ClassCastException(activity.toString() + " must implement " + LocalFileListFragment.ContainerActivity.class.getSimpleName());
71 }
72 }
73
74
75 /**
76 * {@inheritDoc}
77 */
78 @Override
79 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
80 Log_OC.i(TAG, "onCreateView() start");
81 View v = super.onCreateView(inflater, container, savedInstanceState);
82 getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
83 Log_OC.i(TAG, "onCreateView() end");
84 return v;
85 }
86
87
88 /**
89 * {@inheritDoc}
90 */
91 @Override
92 public void onActivityCreated(Bundle savedInstanceState) {
93 Log_OC.i(TAG, "onActivityCreated() start");
94
95 super.onCreate(savedInstanceState);
96 mAdapter = new LocalFileListAdapter(mContainerActivity.getInitialDirectory(), getActivity());
97 setListAdapter(mAdapter);
98
99 if (savedInstanceState != null) {
100 Log_OC.i(TAG, "savedInstanceState is not null");
101 int position = savedInstanceState.getInt(SAVED_LIST_POSITION);
102 setReferencePosition(position);
103 }
104
105 Log_OC.i(TAG, "onActivityCreated() stop");
106 }
107
108
109 @Override
110 public void onSaveInstanceState(Bundle savedInstanceState) {
111 Log_OC.i(TAG, "onSaveInstanceState() start");
112
113 savedInstanceState.putInt(SAVED_LIST_POSITION, getReferencePosition());
114
115
116 Log_OC.i(TAG, "onSaveInstanceState() stop");
117 }
118
119
120 /**
121 * Checks the file clicked over. Browses inside if it is a directory. Notifies the container activity in any case.
122 */
123 @Override
124 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
125 File file = (File) mAdapter.getItem(position);
126 if (file != null) {
127 /// Click on a directory
128 if (file.isDirectory()) {
129 // just local updates
130 listDirectory(file);
131 // notify the click to container Activity
132 mContainerActivity.onDirectoryClick(file);
133
134 } else { /// Click on a file
135 ImageView checkBoxV = (ImageView) v.findViewById(R.id.custom_checkbox);
136 if (checkBoxV != null) {
137 if (getListView().isItemChecked(position)) {
138 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
139 } else {
140 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
141 }
142 }
143 // notify the change to the container Activity
144 mContainerActivity.onFileClick(file);
145 }
146
147 } else {
148 Log_OC.w(TAG, "Null object in ListAdapter!!");
149 }
150 }
151
152
153 /**
154 * Call this, when the user presses the up button
155 */
156 public void onNavigateUp() {
157 File parentDir = null;
158 if(mDirectory != null) {
159 parentDir = mDirectory.getParentFile(); // can be null
160 }
161 listDirectory(parentDir);
162 }
163
164
165 /**
166 * Use this to query the {@link File} object for the directory
167 * that is currently being displayed by this fragment
168 *
169 * @return File The currently displayed directory
170 */
171 public File getCurrentDirectory(){
172 return mDirectory;
173 }
174
175
176 /**
177 * Calls {@link LocalFileListFragment#listDirectory(File)} with a null parameter
178 * to refresh the current directory.
179 */
180 public void listDirectory(){
181 listDirectory(null);
182 }
183
184
185 /**
186 * Lists the given directory on the view. When the input parameter is null,
187 * it will either refresh the last known directory, or list the root
188 * if there never was a directory.
189 *
190 * @param directory Directory to be listed
191 */
192 public void listDirectory(File directory) {
193
194 // Check input parameters for null
195 if(directory == null) {
196 if(mDirectory != null){
197 directory = mDirectory;
198 } else {
199 directory = Environment.getExternalStorageDirectory(); // TODO be careful with the state of the storage; could not be available
200 if (directory == null) return; // no files to show
201 }
202 }
203
204
205 // if that's not a directory -> List its parent
206 if(!directory.isDirectory()){
207 Log_OC.w(TAG, "You see, that is not a directory -> " + directory.toString());
208 directory = directory.getParentFile();
209 }
210
211 mList.clearChoices(); // by now, only files in the same directory will be kept as selected
212 mAdapter.swapDirectory(directory);
213 if (mDirectory == null || !mDirectory.equals(directory)) {
214 mList.setSelectionFromTop(0, 0);
215 }
216 mDirectory = directory;
217 }
218
219
220 /**
221 * Returns the fule paths to the files checked by the user
222 *
223 * @return File paths to the files checked by the user.
224 */
225 public String[] getCheckedFilePaths() {
226 String [] result = null;
227 SparseBooleanArray positions = mList.getCheckedItemPositions();
228 if (positions.size() > 0) {
229 Log_OC.d(TAG, "Returning " + positions.size() + " selected files");
230 result = new String[positions.size()];
231 for (int i=0; i<positions.size(); i++) {
232 result[i] = ((File) mList.getItemAtPosition(positions.keyAt(i))).getAbsolutePath();
233 }
234 }
235 return result;
236 }
237
238
239 /**
240 * Interface to implement by any Activity that includes some instance of LocalFileListFragment
241 *
242 * @author David A. Velasco
243 */
244 public interface ContainerActivity {
245
246 /**
247 * Callback method invoked when a directory is clicked by the user on the files list
248 *
249 * @param file
250 */
251 public void onDirectoryClick(File directory);
252
253 /**
254 * Callback method invoked when a file (non directory) is clicked by the user on the files list
255 *
256 * @param file
257 */
258 public void onFileClick(File file);
259
260
261 /**
262 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
263 *
264 * @return Directory to list firstly. Can be NULL.
265 */
266 public File getInitialDirectory();
267
268 }
269
270
271 }