Filelisting using intents and OCFile.getId() instead of string matching
[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 @Override
148 public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
149 ClipData.Item item = new ClipData.Item("ASD");
150 ClipDescription cd = new ClipDescription("ASD", new String[] { ClipDescription.MIMETYPE_TEXT_PLAIN });
151 ClipData dragData = new ClipData(cd, item);
152 arg1.startDrag(dragData, new View.DragShadowBuilder(arg0.getChildAt(arg2)), null, 0);
153 return true;
154 }
155
156 /**
157 * Call this, when the user presses the up button
158 */
159 public void onNavigateUp() {
160 mDirNames.pop();
161 OCFile parentDir = null;
162
163 if(mFile != null){
164 parentDir = mStorageManager.getFileById(mFile.getParentId());
165 mFile = parentDir;
166 }
167
168 listDirectory(parentDir);
169 resetFileFragment();
170 }
171
172 /**
173 * Calls {@link FileListFragment#listDirectory(OCFile)} with a null parameter
174 */
175 public void listDirectory(){
176 listDirectory(null);
177 }
178
179 /**
180 * Lists the given directory on the view. When the input parameter is null,
181 * it will either refresh the last known directory, or list the root
182 * if there never was a directory.
183 *
184 * @param directory File to be listed
185 */
186 public void listDirectory(OCFile directory) {
187
188 // Check input parameters for null
189 if(directory == null){
190 if(mFile != null){
191 directory = mFile;
192 } else {
193 directory = mStorageManager.getFileByPath("/");
194 }
195 }
196
197 // If that's not a directory -> List its parent
198 if(!directory.isDirectory()){
199 Log.w(TAG, "You see, that is not a directory -> " + directory.toString());
200 directory = mStorageManager.getFileById(directory.getParentId());
201 }
202
203 mFiles = mStorageManager.getDirectoryContent(directory);
204 if (mFiles == null || mFiles.size() == 0) {
205 Toast.makeText(getActivity(), "There are no files here", Toast.LENGTH_LONG).show();
206 }
207 setListAdapter(new FileListListAdapter(directory, mStorageManager, getActivity()));
208 }
209
210 @Override
211 public void onSaveInstanceState(Bundle outState) {
212 super.onSaveInstanceState(outState);
213 outState.putParcelable("ACCOUNT", mAccount);
214 }
215
216 }