Synchronize kept-in-sync files during account synchronization, in both directions...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / OCFileListFragment.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 com.owncloud.android.datamodel.DataStorageManager;
21 import com.owncloud.android.datamodel.OCFile;
22 import com.owncloud.android.ui.FragmentListView;
23 import com.owncloud.android.ui.activity.TransferServiceGetter;
24 import com.owncloud.android.ui.adapter.FileListListAdapter;
25
26 import android.app.Activity;
27 import android.os.Bundle;
28 import android.util.Log;
29 import android.view.View;
30 import android.widget.AdapterView;
31
32 /**
33 * A Fragment that lists all files and folders in a given path.
34 *
35 * @author Bartek Przybylski
36 *
37 */
38 public class OCFileListFragment extends FragmentListView {
39 private static final String TAG = "FileListFragment";
40 private static final String SAVED_LIST_POSITION = "LIST_POSITION";
41
42 private OCFileListFragment.ContainerActivity mContainerActivity;
43
44 private OCFile mFile = null;
45 private FileListListAdapter mAdapter;
46
47
48 /**
49 * {@inheritDoc}
50 */
51 @Override
52 public void onAttach(Activity activity) {
53 super.onAttach(activity);
54 try {
55 mContainerActivity = (ContainerActivity) activity;
56 } catch (ClassCastException e) {
57 throw new ClassCastException(activity.toString() + " must implement " + OCFileListFragment.ContainerActivity.class.getSimpleName());
58 }
59 }
60
61
62 /**
63 * {@inheritDoc}
64 */
65 @Override
66 public void onActivityCreated(Bundle savedInstanceState) {
67 Log.i(TAG, "onActivityCreated() start");
68
69 super.onActivityCreated(savedInstanceState);
70 mAdapter = new FileListListAdapter(mContainerActivity.getInitialDirectory(), mContainerActivity.getStorageManager(), getActivity(), mContainerActivity);
71 setListAdapter(mAdapter);
72
73 if (savedInstanceState != null) {
74 Log.i(TAG, "savedInstanceState is not null");
75 int position = savedInstanceState.getInt(SAVED_LIST_POSITION);
76 setReferencePosition(position);
77 }
78
79 Log.i(TAG, "onActivityCreated() stop");
80 }
81
82
83
84 @Override
85 public void onSaveInstanceState(Bundle savedInstanceState) {
86 Log.i(TAG, "onSaveInstanceState() start");
87
88 savedInstanceState.putInt(SAVED_LIST_POSITION, getReferencePosition());
89
90 Log.i(TAG, "onSaveInstanceState() stop");
91 }
92
93
94 @Override
95 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
96 OCFile file = (OCFile) mAdapter.getItem(position);
97 if (file != null) {
98 /// Click on a directory
99 if (file.getMimetype().equals("DIR")) {
100 // just local updates
101 mFile = file;
102 listDirectory(file);
103 // any other updates are let to the container Activity
104 mContainerActivity.onDirectoryClick(file);
105
106 } else { /// Click on a file
107 mContainerActivity.onFileClick(file);
108 }
109
110 } else {
111 Log.d(TAG, "Null object in ListAdapter!!");
112 }
113
114 }
115
116 /**
117 * Call this, when the user presses the up button
118 */
119 public void onNavigateUp() {
120 OCFile parentDir = null;
121
122 if(mFile != null){
123 DataStorageManager storageManager = mContainerActivity.getStorageManager();
124 parentDir = storageManager.getFileById(mFile.getParentId());
125 mFile = parentDir;
126 }
127 listDirectory(parentDir);
128 }
129
130 /**
131 * Use this to query the {@link OCFile} that is currently
132 * being displayed by this fragment
133 * @return The currently viewed OCFile
134 */
135 public OCFile getCurrentFile(){
136 return mFile;
137 }
138
139 /**
140 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
141 */
142 public void listDirectory(){
143 listDirectory(null);
144 }
145
146 /**
147 * Lists the given directory on the view. When the input parameter is null,
148 * it will either refresh the last known directory, or list the root
149 * if there never was a directory.
150 *
151 * @param directory File to be listed
152 */
153 public void listDirectory(OCFile directory) {
154 DataStorageManager storageManager = mContainerActivity.getStorageManager();
155 if (storageManager != null) {
156
157 // Check input parameters for null
158 if(directory == null){
159 if(mFile != null){
160 directory = mFile;
161 } else {
162 directory = storageManager.getFileByPath("/");
163 if (directory == null) return; // no files, wait for sync
164 }
165 }
166
167
168 // If that's not a directory -> List its parent
169 if(!directory.isDirectory()){
170 Log.w(TAG, "You see, that is not a directory -> " + directory.toString());
171 directory = storageManager.getFileById(directory.getParentId());
172 }
173
174 mAdapter.swapDirectory(directory, storageManager);
175 if (mFile == null || !mFile.equals(directory)) {
176 mList.setSelectionFromTop(0, 0);
177 }
178 mFile = directory;
179 }
180 }
181
182
183
184 /**
185 * Interface to implement by any Activity that includes some instance of FileListFragment
186 *
187 * @author David A. Velasco
188 */
189 public interface ContainerActivity extends TransferServiceGetter {
190
191 /**
192 * Callback method invoked when a directory is clicked by the user on the files list
193 *
194 * @param file
195 */
196 public void onDirectoryClick(OCFile file);
197
198 /**
199 * Callback method invoked when a file (non directory) is clicked by the user on the files list
200 *
201 * @param file
202 */
203 public void onFileClick(OCFile file);
204
205 /**
206 * Getter for the current DataStorageManager in the container activity
207 */
208 public DataStorageManager getStorageManager();
209
210
211 /**
212 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
213 *
214 * @return Directory to list firstly. Can be NULL.
215 */
216 public OCFile getInitialDirectory();
217
218
219 }
220
221 }