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