895b3a2813d05fd7ffd8d2e8d491f8a462e79f8d
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / fragment / FileListFragment.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 eu.alefzero.owncloud.ui.fragment;
19
20 import java.util.Vector;
21
22 import com.actionbarsherlock.app.ActionBar;
23
24 import android.accounts.Account;
25 import android.content.Intent;
26 import android.os.Bundle;
27 import android.support.v4.app.FragmentTransaction;
28 import android.util.Log;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.AdapterView;
33 import android.widget.Toast;
34 import eu.alefzero.owncloud.AccountUtils;
35 import eu.alefzero.owncloud.R;
36 import eu.alefzero.owncloud.datamodel.DataStorageManager;
37 import eu.alefzero.owncloud.datamodel.FileDataStorageManager;
38 import eu.alefzero.owncloud.datamodel.OCFile;
39 import eu.alefzero.owncloud.files.services.FileDownloader;
40 import eu.alefzero.owncloud.ui.FragmentListView;
41 import eu.alefzero.owncloud.ui.activity.FileDetailActivity;
42 import eu.alefzero.owncloud.ui.activity.FileDisplayActivity;
43 import eu.alefzero.owncloud.ui.adapter.FileListListAdapter;
44
45 /**
46 * A Fragment that lists all files and folders in a given path.
47 *
48 * @author Bartek Przybylski
49 *
50 */
51 public class FileListFragment extends FragmentListView {
52 private static final String TAG = "FileListFragment";
53
54 private Vector<OCFile> mFiles;
55 private OCFile mFile;
56 private boolean mIsLargeDevice;
57
58 @Override
59 public void onCreate(Bundle savedInstanceState) {
60 Log.i(getClass().toString(), "onCreate() start");
61 super.onCreate(savedInstanceState);
62
63 Intent intent = getActivity().getIntent();
64 OCFile directory = intent.getParcelableExtra(FileDetailFragment.EXTRA_FILE);
65 mFile = directory;
66 mIsLargeDevice = false;
67
68 Log.i(getClass().toString(), "onCreate() stop");
69 }
70
71 @Override
72 public View onCreateView(LayoutInflater inflater, ViewGroup container,
73 Bundle savedInstanceState) {
74 Log.i(getClass().toString(), "onCreateView() start");
75 super.onCreateView(inflater, container, savedInstanceState);
76 getListView().setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
77 getListView().setDividerHeight(1);
78
79 Log.i(getClass().toString(), "onCreateView() end");
80 return getListView();
81 }
82
83 @Override
84 public void onStart() {
85 Log.i(getClass().toString(), "onStart() start");
86 super.onStart();
87 // Create a placeholder upon launch
88 View fragmentContainer = getActivity().findViewById(R.id.file_details_container);
89 if (fragmentContainer != null) {
90 mIsLargeDevice = true;
91 FragmentTransaction transaction = getFragmentManager().beginTransaction();
92 transaction.replace(R.id.file_details_container, new FileDetailFragment(true));
93 transaction.commit();
94 }
95 Log.i(getClass().toString(), "onStart() end");
96 }
97
98 @Override
99 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
100 if (mFiles.size() <= position) {
101 throw new IndexOutOfBoundsException("Incorrect item selected");
102 }
103 OCFile file = mFiles.get(position);
104
105 // Update ActionBarPath
106 if (file.getMimetype().equals("DIR")) {
107 mFile = file;
108 ((FileDisplayActivity) getActivity()).pushDirname(file);
109 ActionBar actionBar = ((FileDisplayActivity) getActivity()).getSupportActionBar();
110 actionBar.setDisplayHomeAsUpEnabled(true);
111 listDirectory(file);
112 resetFileFragment();
113 return;
114 }
115
116 Intent showDetailsIntent = new Intent(getActivity(), FileDetailActivity.class);
117 showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, file);
118 showDetailsIntent.putExtra(FileDownloader.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(getActivity()));
119
120 // If we are on a large device -> update fragment
121 if (mIsLargeDevice) {
122 FileDetailFragment fileDetails = (FileDetailFragment) getFragmentManager().findFragmentByTag("FileDetails");
123 if (fileDetails == null) {
124 FragmentTransaction transaction = getFragmentManager().beginTransaction();
125 transaction.replace(R.id.file_details_container, new FileDetailFragment(showDetailsIntent), "FileDetails");
126 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
127 transaction.commit();
128 } else {
129 fileDetails.updateFileDetails(showDetailsIntent);
130 }
131 } else {
132 startActivity(showDetailsIntent);
133 }
134 }
135
136 /**
137 * Resets the FileDetailsFragment on Tablets so that it always displays
138 * "Tab on a file to display it's details"
139 */
140 private void resetFileFragment() {
141 FileDetailFragment fileDetails = (FileDetailFragment) getFragmentManager().findFragmentByTag("FileDetails");
142 if (fileDetails != null) {
143 FragmentTransaction transaction = getFragmentManager().beginTransaction();
144 transaction.remove(fileDetails);
145 transaction.add(R.id.file_details_container, new FileDetailFragment(true));
146 transaction.commit();
147 }
148 }
149
150 /**
151 * Call this, when the user presses the up button
152 */
153 public void onNavigateUp() {
154 OCFile parentDir = null;
155
156 if(mFile != null){
157 DataStorageManager storageManager = ((FileDisplayActivity)getActivity()).getStorageManager();
158 parentDir = storageManager.getFileById(mFile.getParentId());
159 mFile = parentDir;
160 }
161
162 listDirectory(parentDir);
163 resetFileFragment();
164 }
165
166 /**
167 * Use this to query the {@link OCFile} that is currently
168 * being displayed by this fragment
169 * @return The currently viewed OCFile
170 */
171 public OCFile getCurrentFile(){
172 return mFile;
173 }
174
175 /**
176 * Calls {@link FileListFragment#listDirectory(OCFile)} with a null parameter
177 */
178 public void listDirectory(){
179 listDirectory(null);
180 }
181
182 /**
183 * Lists the given directory on the view. When the input parameter is null,
184 * it will either refresh the last known directory, or list the root
185 * if there never was a directory.
186 *
187 * @param directory File to be listed
188 */
189 public void listDirectory(OCFile directory) {
190
191 DataStorageManager storageManager = ((FileDisplayActivity)getActivity()).getStorageManager();
192
193 // Check input parameters for null
194 if(directory == null){
195 if(mFile != null){
196 directory = mFile;
197 } else {
198 directory = storageManager.getFileByPath("/");
199 if (directory == null) return; // no files, wait for sync
200 }
201 }
202
203
204 // If that's not a directory -> List its parent
205 if(!directory.isDirectory()){
206 Log.w(TAG, "You see, that is not a directory -> " + directory.toString());
207 directory = storageManager.getFileById(directory.getParentId());
208 }
209
210 mFile = directory;
211
212 mFiles = storageManager.getDirectoryContent(directory);
213 /*if (mFiles == null || mFiles.size() == 0) {
214 Toast.makeText(getActivity(), "There are no files here", Toast.LENGTH_LONG).show();
215 }*/
216 setListAdapter(new FileListListAdapter(directory, storageManager, getActivity()));
217 }
218
219 }