Merge branch 'text_file_preview_pr_707_with_develop' of github.com:owncloud/android...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / LocalFileListFragment.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author David A. Velasco
5 * Copyright (C) 2011 Bartek Przybylski
6 * Copyright (C) 2015 ownCloud Inc.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21 package com.owncloud.android.ui.fragment;
22
23 import java.io.File;
24 import java.util.ArrayList;
25
26 import android.app.Activity;
27 import android.os.Bundle;
28 import android.os.Environment;
29 import android.util.SparseBooleanArray;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.widget.AdapterView;
34 import android.widget.ImageView;
35 import android.widget.ListView;
36
37 import com.owncloud.android.R;
38 import com.owncloud.android.lib.common.utils.Log_OC;
39 import com.owncloud.android.ui.adapter.LocalFileListAdapter;
40
41
42 /**
43 * A Fragment that lists all files and folders in a given LOCAL path.
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 " +
68 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 setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
81 setSwipeEnabled(false); // Disable pull-to-refresh
82 setMessageForEmptyList(getString(R.string.local_file_list_empty));
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.onActivityCreated(savedInstanceState);
96 mAdapter = new LocalFileListAdapter(mContainerActivity.getInitialDirectory(), getActivity());
97 setListAdapter(mAdapter);
98
99 Log_OC.i(TAG, "onActivityCreated() stop");
100 }
101
102 /**
103 * Checks the file clicked over. Browses inside if it is a directory.
104 * Notifies the container activity in any case.
105 */
106 @Override
107 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
108 File file = (File) mAdapter.getItem(position);
109 if (file != null) {
110 /// Click on a directory
111 if (file.isDirectory()) {
112 // just local updates
113 listDirectory(file);
114 // notify the click to container Activity
115 mContainerActivity.onDirectoryClick(file);
116 // save index and top position
117 saveIndexAndTopPosition(position);
118
119 } else { /// Click on a file
120 ImageView checkBoxV = (ImageView) v.findViewById(R.id.custom_checkbox);
121 if (checkBoxV != null) {
122 if (((ListView)getListView()).isItemChecked(position)) {
123 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
124 } else {
125 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
126 }
127 }
128 // notify the change to the container Activity
129 mContainerActivity.onFileClick(file);
130 }
131
132 } else {
133 Log_OC.w(TAG, "Null object in ListAdapter!!");
134 }
135 }
136
137
138 /**
139 * Call this, when the user presses the up button
140 */
141 public void onNavigateUp() {
142 File parentDir = null;
143 if(mDirectory != null) {
144 parentDir = mDirectory.getParentFile(); // can be null
145 }
146 listDirectory(parentDir);
147
148 // restore index and top position
149 restoreIndexAndTopPosition();
150 }
151
152
153 /**
154 * Use this to query the {@link File} object for the directory
155 * that is currently being displayed by this fragment
156 *
157 * @return File The currently displayed directory
158 */
159 public File getCurrentDirectory(){
160 return mDirectory;
161 }
162
163
164 /**
165 * Calls {@link LocalFileListFragment#listDirectory(File)} with a null parameter
166 * to refresh the current directory.
167 */
168 public void listDirectory(){
169 listDirectory(null);
170 }
171
172
173 /**
174 * Lists the given directory on the view. When the input parameter is null,
175 * it will either refresh the last known directory. list the root
176 * if there never was a directory.
177 *
178 * @param directory Directory to be listed
179 */
180 public void listDirectory(File directory) {
181
182 // Check input parameters for null
183 if(directory == null) {
184 if(mDirectory != null){
185 directory = mDirectory;
186 } else {
187 directory = Environment.getExternalStorageDirectory();
188 // TODO be careful with the state of the storage; could not be available
189 if (directory == null) return; // no files to show
190 }
191 }
192
193
194 // if that's not a directory -> List its parent
195 if(!directory.isDirectory()){
196 Log_OC.w(TAG, "You see, that is not a directory -> " + directory.toString());
197 directory = directory.getParentFile();
198 }
199
200 // by now, only files in the same directory will be kept as selected
201 ((ListView)mCurrentListView).clearChoices();
202 mAdapter.swapDirectory(directory);
203 if (mDirectory == null || !mDirectory.equals(directory)) {
204 mCurrentListView.setSelection(0);
205 }
206 mDirectory = directory;
207 }
208
209
210 /**
211 * Returns the fule paths to the files checked by the user
212 *
213 * @return File paths to the files checked by the user.
214 */
215 public String[] getCheckedFilePaths() {
216 ArrayList<String> result = new ArrayList<String>();
217 SparseBooleanArray positions = ((ListView)mCurrentListView).getCheckedItemPositions();
218 if (positions.size() > 0) {
219 for (int i = 0; i < positions.size(); i++) {
220 if (positions.get(positions.keyAt(i)) == true) {
221 result.add(((File) mCurrentListView.getItemAtPosition(
222 positions.keyAt(i))).getAbsolutePath());
223 }
224 }
225
226 Log_OC.d(TAG, "Returning " + result.size() + " selected files");
227 }
228 return result.toArray(new String[result.size()]);
229 }
230
231
232 /**
233 * Interface to implement by any Activity that includes some instance of LocalFileListFragment
234 */
235 public interface ContainerActivity {
236
237 /**
238 * Callback method invoked when a directory is clicked by the user on the files list
239 *
240 * @param directory
241 */
242 public void onDirectoryClick(File directory);
243
244 /**
245 * Callback method invoked when a file (non directory)
246 * is clicked by the user on the files list
247 *
248 * @param file
249 */
250 public void onFileClick(File file);
251
252
253 /**
254 * Callback method invoked when the parent activity
255 * is fully created to get the directory to list firstly.
256 *
257 * @return Directory to list firstly. Can be NULL.
258 */
259 public File getInitialDirectory();
260
261 }
262
263
264 }