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