51f78b706db287f2f49e8f81fd4ff09f86427dab
[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.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.getSimpleName());
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 View v = super.onCreateView(inflater, container, savedInstanceState);
81 getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
82 Log.i(TAG, "onCreateView() end");
83 return v;
84 }
85
86
87 /**
88 * {@inheritDoc}
89 */
90 @Override
91 public void onActivityCreated(Bundle savedInstanceState) {
92 Log.i(TAG, "onActivityCreated() start");
93
94 super.onCreate(savedInstanceState);
95 mAdapter = new LocalFileListAdapter(mContainerActivity.getInitialDirectory(), getActivity());
96 setListAdapter(mAdapter);
97
98 if (savedInstanceState != null) {
99 Log.i(TAG, "savedInstanceState is not null");
100 int position = savedInstanceState.getInt(SAVED_LIST_POSITION);
101 setReferencePosition(position);
102 }
103
104 Log.i(TAG, "onActivityCreated() stop");
105 }
106
107
108 @Override
109 public void onSaveInstanceState(Bundle savedInstanceState) {
110 Log.i(TAG, "onSaveInstanceState() start");
111
112 savedInstanceState.putInt(SAVED_LIST_POSITION, getReferencePosition());
113
114
115 Log.i(TAG, "onSaveInstanceState() stop");
116 }
117
118
119 /**
120 * Checks the file clicked over. Browses inside if it is a directory. Notifies the container activity in any case.
121 */
122 @Override
123 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
124 File file = (File) mAdapter.getItem(position);
125 if (file != null) {
126 /// Click on a directory
127 if (file.isDirectory()) {
128 // just local updates
129 listDirectory(file);
130 // notify the click to container Activity
131 mContainerActivity.onDirectoryClick(file);
132
133 } else { /// Click on a file
134 ImageView checkBoxV = (ImageView) v.findViewById(R.id.custom_checkbox);
135 if (checkBoxV != null) {
136 if (getListView().isItemChecked(position)) {
137 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
138 } else {
139 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
140 }
141 }
142 // notify the change to the container Activity
143 mContainerActivity.onFileClick(file);
144 }
145
146 } else {
147 Log.w(TAG, "Null object in ListAdapter!!");
148 }
149 }
150
151
152 /**
153 * Call this, when the user presses the up button
154 */
155 public void onNavigateUp() {
156 File parentDir = null;
157 if(mDirectory != null) {
158 parentDir = mDirectory.getParentFile(); // can be null
159 }
160 listDirectory(parentDir);
161 }
162
163
164 /**
165 * Use this to query the {@link File} object for the directory
166 * that is currently being displayed by this fragment
167 *
168 * @return File The currently displayed directory
169 */
170 public File getCurrentDirectory(){
171 return mDirectory;
172 }
173
174
175 /**
176 * Calls {@link LocalFileListFragment#listDirectory(File)} with a null parameter
177 * to refresh the current directory.
178 */
179 public void listDirectory(){
180 listDirectory(null);
181 }
182
183
184 /**
185 * Lists the given directory on the view. When the input parameter is null,
186 * it will either refresh the last known directory, or list the root
187 * if there never was a directory.
188 *
189 * @param directory Directory to be listed
190 */
191 public void listDirectory(File directory) {
192
193 // Check input parameters for null
194 if(directory == null) {
195 if(mDirectory != null){
196 directory = mDirectory;
197 } else {
198 directory = Environment.getExternalStorageDirectory(); // TODO be careful with the state of the storage; could not be available
199 if (directory == null) return; // no files to show
200 }
201 }
202
203
204 // if that's not a directory -> List its parent
205 if(!directory.isDirectory()){
206 Log.w(TAG, "You see, that is not a directory -> " + directory.toString());
207 directory = directory.getParentFile();
208 }
209
210 mList.clearChoices(); // by now, only files in the same directory will be kept as selected
211 mAdapter.swapDirectory(directory);
212 if (mDirectory == null || !mDirectory.equals(directory)) {
213 mList.setSelectionFromTop(0, 0);
214 }
215 mDirectory = directory;
216 }
217
218
219 /**
220 * Returns the fule paths to the files checked by the user
221 *
222 * @return File paths to the files checked by the user.
223 */
224 public String[] getCheckedFilePaths() {
225 String [] result = null;
226 SparseBooleanArray positions = mList.getCheckedItemPositions();
227 if (positions.size() > 0) {
228 Log.d(TAG, "Returning " + positions.size() + " selected files");
229 result = new String[positions.size()];
230 for (int i=0; i<positions.size(); i++) {
231 result[i] = ((File) mList.getItemAtPosition(positions.keyAt(i))).getAbsolutePath();
232 }
233 }
234 return result;
235 }
236
237
238 /**
239 * Interface to implement by any Activity that includes some instance of LocalFileListFragment
240 *
241 * @author David A. Velasco
242 */
243 public interface ContainerActivity {
244
245 /**
246 * Callback method invoked when a directory is clicked by the user on the files list
247 *
248 * @param file
249 */
250 public void onDirectoryClick(File directory);
251
252 /**
253 * Callback method invoked when a file (non directory) is clicked by the user on the files list
254 *
255 * @param file
256 */
257 public void onFileClick(File file);
258
259
260 /**
261 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
262 *
263 * @return Directory to list firstly. Can be NULL.
264 */
265 public File getInitialDirectory();
266
267 }
268
269
270 }