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