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