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