8a5b379227914d693940759342e9d965411220aa
[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 private Account mAccount;
54 private Vector<OCFile> mFiles;
55 private DataStorageManager mStorageManager;
56 private OCFile mFile;
57 private boolean mIsLargeDevice = false;
58
59 @Override
60 public void onCreate(Bundle savedInstanceState) {
61 super.onCreate(savedInstanceState);
62
63 mAccount = AccountUtils.getCurrentOwnCloudAccount(getActivity());
64 mStorageManager = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
65
66 Intent intent = getActivity().getIntent();
67 OCFile directory = intent.getParcelableExtra(FileDetailFragment.EXTRA_FILE);
68 mFile = directory;
69
70 }
71
72 @Override
73 public View onCreateView(LayoutInflater inflater, ViewGroup container,
74 Bundle savedInstanceState) {
75 super.onCreateView(inflater, container, savedInstanceState);
76 getListView().setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
77 getListView().setDividerHeight(1);
78
79 //listDirectory(mFile);
80
81 return getListView();
82 }
83
84 @Override
85 public void onStart() {
86 // Create a placeholder upon launch
87 View fragmentContainer = getActivity().findViewById(R.id.file_details_container);
88 if (fragmentContainer != null) {
89 mIsLargeDevice = true;
90 FragmentTransaction transaction = getFragmentManager().beginTransaction();
91 transaction.replace(R.id.file_details_container, new FileDetailFragment(true));
92 transaction.commit();
93 }
94 super.onStart();
95 }
96
97 @Override
98 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
99 if (mFiles.size() <= position) {
100 throw new IndexOutOfBoundsException("Incorrect item selected");
101 }
102 OCFile file = mFiles.get(position);
103
104 // Update ActionBarPath
105 if (file.getMimetype().equals("DIR")) {
106 mFile = file;
107 ((FileDisplayActivity) getActivity()).pushDirname(file);
108 ActionBar actionBar = ((FileDisplayActivity) getActivity()).getSupportActionBar();
109 actionBar.setDisplayHomeAsUpEnabled(true);
110 listDirectory(file);
111 resetFileFragment();
112 return;
113 }
114
115 Intent showDetailsIntent = new Intent(getActivity(), FileDetailActivity.class);
116 showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, file);
117 showDetailsIntent.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
118
119 // If we are on a large device -> update fragment
120 if (mIsLargeDevice) {
121 FileDetailFragment fileDetails = (FileDetailFragment) getFragmentManager().findFragmentByTag("FileDetails");
122 if (fileDetails == null) {
123 FragmentTransaction transaction = getFragmentManager().beginTransaction();
124 transaction.replace(R.id.file_details_container, new FileDetailFragment(showDetailsIntent), "FileDetails");
125 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
126 transaction.commit();
127 } else {
128 fileDetails.updateFileDetails(showDetailsIntent);
129 }
130 } else {
131 startActivity(showDetailsIntent);
132 }
133 }
134
135 /**
136 * Resets the FileDetailsFragment on Tablets so that it always displays
137 * "Tab on a file to display it's details"
138 */
139 private void resetFileFragment() {
140 FileDetailFragment fileDetails = (FileDetailFragment) getFragmentManager().findFragmentByTag("FileDetails");
141 if (fileDetails != null) {
142 FragmentTransaction transaction = getFragmentManager().beginTransaction();
143 transaction.remove(fileDetails);
144 transaction.add(R.id.file_details_container, new FileDetailFragment(true));
145 transaction.commit();
146 }
147 }
148
149 /**
150 * Call this, when the user presses the up button
151 */
152 public void onNavigateUp() {
153 OCFile parentDir = null;
154
155 if(mFile != null){
156 parentDir = mStorageManager.getFileById(mFile.getParentId());
157 mFile = parentDir;
158 }
159
160 listDirectory(parentDir);
161 resetFileFragment();
162 }
163
164 /**
165 * Use this to query the {@link OCFile} that is currently
166 * being displayed by this fragment
167 * @return The currently viewed OCFile
168 */
169 public OCFile getCurrentFile(){
170 return mFile;
171 }
172
173 /**
174 * Calls {@link FileListFragment#listDirectory(OCFile)} with a null parameter
175 */
176 public void listDirectory(){
177 listDirectory(null);
178 }
179
180 /**
181 * Lists the given directory on the view. When the input parameter is null,
182 * it will either refresh the last known directory, or list the root
183 * if there never was a directory.
184 *
185 * @param directory File to be listed
186 */
187 public void listDirectory(OCFile directory) {
188
189 // Check input parameters for null
190 if(directory == null){
191 if(mFile != null){
192 directory = mFile;
193 } else {
194 directory = mStorageManager.getFileByPath("/");
195 if (directory == null) return; // no files, wait for sync
196 }
197 }
198
199
200 // If that's not a directory -> List its parent
201 if(!directory.isDirectory()){
202 Log.w(TAG, "You see, that is not a directory -> " + directory.toString());
203 directory = mStorageManager.getFileById(directory.getParentId());
204 }
205
206 mFile = directory;
207
208 mFiles = mStorageManager.getDirectoryContent(directory);
209 if (mFiles == null || mFiles.size() == 0) {
210 Toast.makeText(getActivity(), "There are no files here", Toast.LENGTH_LONG).show();
211 }
212 setListAdapter(new FileListListAdapter(directory, mStorageManager, getActivity()));
213 }
214
215 @Override
216 public void onSaveInstanceState(Bundle outState) {
217 super.onSaveInstanceState(outState);
218 outState.putParcelable("ACCOUNT", mAccount);
219 }
220
221 /**
222 * This should be called every time the current account changes, in order to synchronize mStorageManager without create a new FileListFragment
223 */
224 public void updateAccount() {
225 Account old = mAccount;
226 mAccount = AccountUtils.getCurrentOwnCloudAccount(getActivity());
227 if (old != mAccount)
228 mStorageManager = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
229 // dvelasco : a better solution can be provided change the flow between states "wiht account" and "without account", in terms of interactions between AuthenticatorActivity and FileDisplayActivity
230 }
231
232 }