Better synchronization for current account between FileDisplayActivity and FileListFr...
[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; // dvelasco : the fragment is not recreated when other account is selected; keep as an attribute is dangerous
54 private Vector<OCFile> mFiles;
55 //private DataStorageManager mStorageManager; // dvelasco : just the same; it depends upon the current account ; it's updated in FileDisplayActivity!!
56 private OCFile mFile;
57 private boolean mIsLargeDevice = false;
58
59 @Override
60 public void onCreate(Bundle savedInstanceState) {
61 super.onCreate(savedInstanceState);
62
63 Intent intent = getActivity().getIntent();
64 OCFile directory = intent.getParcelableExtra(FileDetailFragment.EXTRA_FILE);
65 mFile = directory;
66
67 }
68
69 @Override
70 public View onCreateView(LayoutInflater inflater, ViewGroup container,
71 Bundle savedInstanceState) {
72 super.onCreateView(inflater, container, savedInstanceState);
73 getListView().setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
74 getListView().setDividerHeight(1);
75
76 //listDirectory(mFile);
77
78 return getListView();
79 }
80
81 @Override
82 public void onStart() {
83 // Create a placeholder upon launch
84 View fragmentContainer = getActivity().findViewById(R.id.file_details_container);
85 if (fragmentContainer != null) {
86 mIsLargeDevice = true;
87 FragmentTransaction transaction = getFragmentManager().beginTransaction();
88 transaction.replace(R.id.file_details_container, new FileDetailFragment(true));
89 transaction.commit();
90 }
91 super.onStart();
92 }
93
94 @Override
95 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
96 if (mFiles.size() <= position) {
97 throw new IndexOutOfBoundsException("Incorrect item selected");
98 }
99 OCFile file = mFiles.get(position);
100
101 // Update ActionBarPath
102 if (file.getMimetype().equals("DIR")) {
103 mFile = file;
104 ((FileDisplayActivity) getActivity()).pushDirname(file);
105 ActionBar actionBar = ((FileDisplayActivity) getActivity()).getSupportActionBar();
106 actionBar.setDisplayHomeAsUpEnabled(true);
107 listDirectory(file);
108 resetFileFragment();
109 return;
110 }
111
112 Intent showDetailsIntent = new Intent(getActivity(), FileDetailActivity.class);
113 showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, file);
114 showDetailsIntent.putExtra(FileDownloader.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(getActivity()));
115
116 // If we are on a large device -> update fragment
117 if (mIsLargeDevice) {
118 FileDetailFragment fileDetails = (FileDetailFragment) getFragmentManager().findFragmentByTag("FileDetails");
119 if (fileDetails == null) {
120 FragmentTransaction transaction = getFragmentManager().beginTransaction();
121 transaction.replace(R.id.file_details_container, new FileDetailFragment(showDetailsIntent), "FileDetails");
122 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
123 transaction.commit();
124 } else {
125 fileDetails.updateFileDetails(showDetailsIntent);
126 }
127 } else {
128 startActivity(showDetailsIntent);
129 }
130 }
131
132 /**
133 * Resets the FileDetailsFragment on Tablets so that it always displays
134 * "Tab on a file to display it's details"
135 */
136 private void resetFileFragment() {
137 FileDetailFragment fileDetails = (FileDetailFragment) getFragmentManager().findFragmentByTag("FileDetails");
138 if (fileDetails != null) {
139 FragmentTransaction transaction = getFragmentManager().beginTransaction();
140 transaction.remove(fileDetails);
141 transaction.add(R.id.file_details_container, new FileDetailFragment(true));
142 transaction.commit();
143 }
144 }
145
146 /**
147 * Call this, when the user presses the up button
148 */
149 public void onNavigateUp() {
150 OCFile parentDir = null;
151
152 if(mFile != null){
153 DataStorageManager storageManager = ((FileDisplayActivity)getActivity()).getStorageManager();
154 parentDir = storageManager.getFileById(mFile.getParentId());
155 mFile = parentDir;
156 }
157
158 listDirectory(parentDir);
159 resetFileFragment();
160 }
161
162 /**
163 * Use this to query the {@link OCFile} that is currently
164 * being displayed by this fragment
165 * @return The currently viewed OCFile
166 */
167 public OCFile getCurrentFile(){
168 return mFile;
169 }
170
171 /**
172 * Calls {@link FileListFragment#listDirectory(OCFile)} with a null parameter
173 */
174 public void listDirectory(){
175 listDirectory(null);
176 }
177
178 /**
179 * Lists the given directory on the view. When the input parameter is null,
180 * it will either refresh the last known directory, or list the root
181 * if there never was a directory.
182 *
183 * @param directory File to be listed
184 */
185 public void listDirectory(OCFile directory) {
186
187 DataStorageManager storageManager = ((FileDisplayActivity)getActivity()).getStorageManager();
188
189 // Check input parameters for null
190 if(directory == null){
191 if(mFile != null){
192 directory = mFile;
193 } else {
194 directory = storageManager.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 = storageManager.getFileById(directory.getParentId());
204 }
205
206 mFile = directory;
207
208 mFiles = storageManager.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, storageManager, getActivity()));
213 }
214
215 @Override
216 public void onSaveInstanceState(Bundle outState) {
217 super.onSaveInstanceState(outState);
218 outState.putParcelable("ACCOUNT", AccountUtils.getCurrentOwnCloudAccount(getActivity()));
219 }
220
221 }