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