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