Added notification for failures in manual synchronizations; some improvements in...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / LocalFileListFragment.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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.FragmentListView;
23 import com.owncloud.android.ui.adapter.LocalFileListAdapter;
24
25 import android.app.Activity;
26 import android.os.Bundle;
27 import android.os.Environment;
28 import android.util.Log;
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
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 FragmentListView {
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.getCanonicalName());
68 }
69 }
70
71
72 /**
73 * {@inheritDoc}
74 */
75 @Override
76 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
77 Log.i(getClass().toString(), "onCreateView() start");
78 super.onCreateView(inflater, container, savedInstanceState);
79 getListView().setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
80 getListView().setDividerHeight(1);
81 getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
82
83 if (savedInstanceState != null) {
84 // TODO recover previous state; or maybe in onActivityCreated
85 }
86
87 Log.i(getClass().toString(), "onCreateView() end");
88 return getListView();
89 }
90
91
92 /**
93 * {@inheritDoc}
94 */
95 @Override
96 public void onActivityCreated(Bundle savedInstanceState) {
97 Log.i(getClass().toString(), "onActivityCreated() start");
98
99 super.onCreate(savedInstanceState);
100
101 Log.i(getClass().toString(), "onActivityCreated() stop");
102 }
103
104
105 /**
106 * Checks the file clicked over. Browses inside if it is a directory. Notifies the container activity in any case.
107 */
108 @Override
109 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
110 File file = (File) mAdapter.getItem(position);
111 if (file != null) {
112 /// Click on a directory
113 if (file.isDirectory()) {
114 // just local updates
115 listDirectory(file);
116 // notify the click to container Activity
117 mContainerActivity.onDirectoryClick(file);
118
119 } else { /// Click on a file
120 ImageView checkBoxV = (ImageView) v.findViewById(R.id.custom_checkbox);
121 if (checkBoxV != null) {
122 if (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.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
149
150 /**
151 * Use this to query the {@link File} object for the directory
152 * that is currently being displayed by this fragment
153 *
154 * @return File The currently displayed directory
155 */
156 public File getCurrentDirectory(){
157 return mDirectory;
158 }
159
160
161 /**
162 * Calls {@link LocalFileListFragment#listDirectory(File)} with a null parameter
163 * to refresh the current directory.
164 */
165 public void listDirectory(){
166 listDirectory(null);
167 }
168
169
170 /**
171 * Lists the given directory on the view. When the input parameter is null,
172 * it will either refresh the last known directory, or list the root
173 * if there never was a directory.
174 *
175 * @param directory Directory to be listed
176 */
177 public void listDirectory(File directory) {
178
179 // Check input parameters for null
180 if(directory == null) {
181 if(mDirectory != null){
182 directory = mDirectory;
183 } else {
184 directory = Environment.getExternalStorageDirectory(); // TODO be careful with the state of the storage; could not be available
185 if (directory == null) return; // no files, wait for sync
186 }
187 }
188
189
190 // if that's not a directory -> List its parent
191 if(!directory.isDirectory()){
192 Log.w(TAG, "You see, that is not a directory -> " + directory.toString());
193 directory = directory.getParentFile();
194 }
195
196 mDirectory = directory;
197 if (mAdapter == null) {
198 mAdapter = new LocalFileListAdapter(mDirectory, getActivity());
199 setListAdapter(mAdapter);
200 } else {
201 mList.clearChoices(); // by now, only files in the same directory will be kept as selected
202 mAdapter.swapDirectory(mDirectory);
203 }
204
205 }
206
207
208 /**
209 * Returns the fule paths to the files checked by the user
210 *
211 * @return File paths to the files checked by the user.
212 */
213 public String[] getCheckedFilePaths() {
214 String [] result = null;
215 SparseBooleanArray positions = mList.getCheckedItemPositions();
216 if (positions.size() > 0) {
217 Log.d(TAG, "Returning " + positions.size() + " selected files");
218 result = new String[positions.size()];
219 for (int i=0; i<positions.size(); i++) {
220 result[i] = ((File) mList.getItemAtPosition(positions.keyAt(i))).getAbsolutePath();
221 }
222 }
223 return result;
224 }
225
226
227 /**
228 * Interface to implement by any Activity that includes some instance of LocalFileListFragment
229 *
230 * @author David A. Velasco
231 */
232 public interface ContainerActivity {
233
234 /**
235 * Callback method invoked when a directory is clicked by the user on the files list
236 *
237 * @param file
238 */
239 public void onDirectoryClick(File directory);
240
241 /**
242 * Callback method invoked when a file (non directory) is clicked by the user on the files list
243 *
244 * @param file
245 */
246 public void onFileClick(File file);
247
248 }
249
250
251 }