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