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