86c77c7372348abd3f898e9d673aa5dc584f3d55
[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 String dirname = file.getFileName();
94 mFile = file;
95
96 ((FileDisplayActivity) getActivity()).pushPath(dirname);
97
98 listDirectory(file);
99 resetFileFragment();
100
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 * Calls {@link FileListFragment#listDirectory(OCFile)} with a null parameter
155 */
156 public void listDirectory(){
157 listDirectory(null);
158 }
159
160 /**
161 * Lists the given directory on the view. When the input parameter is null,
162 * it will either refresh the last known directory, or list the root
163 * if there never was a directory.
164 *
165 * @param directory File to be listed
166 */
167 public void listDirectory(OCFile directory) {
168
169 // Check input parameters for null
170 if(directory == null){
171 if(mFile != null){
172 directory = mFile;
173 } else {
174 directory = mStorageManager.getFileByPath("/");
175 }
176 }
177
178 // If that's not a directory -> List its parent
179 if(!directory.isDirectory()){
180 Log.w(TAG, "You see, that is not a directory -> " + directory.toString());
181 directory = mStorageManager.getFileById(directory.getParentId());
182 }
183
184 mFiles = mStorageManager.getDirectoryContent(directory);
185 if (mFiles == null || mFiles.size() == 0) {
186 Toast.makeText(getActivity(), "There are no files here", Toast.LENGTH_LONG).show();
187 }
188 setListAdapter(new FileListListAdapter(directory, mStorageManager, getActivity()));
189 }
190
191 @Override
192 public void onSaveInstanceState(Bundle outState) {
193 super.onSaveInstanceState(outState);
194 outState.putParcelable("ACCOUNT", mAccount);
195 }
196
197 }