some cleanup, closing cursors
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / fragment / FileList.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.accounts.AccountManager;
25 import android.app.Service;
26 import android.content.Intent;
27 import android.os.Bundle;
28 import android.view.View;
29 import android.widget.AdapterView;
30 import eu.alefzero.owncloud.R;
31 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
32 import eu.alefzero.owncloud.datamodel.OCFile;
33 import eu.alefzero.owncloud.ui.FragmentListView;
34 import eu.alefzero.owncloud.ui.activity.FileDetailActivity;
35 import eu.alefzero.owncloud.ui.activity.FileDisplayActivity;
36 import eu.alefzero.owncloud.ui.adapter.FileListListAdapter;
37
38 /**
39 * A Fragment that lists all files and folders in a given path.
40 * @author Bartek Przybylski
41 *
42 */
43 public class FileList extends FragmentListView {
44 private Account mAccount;
45 private AccountManager mAccountManager;
46 private Stack<String> mDirNames;
47 private Vector<OCFile> mFiles;
48
49 public FileList() {
50 mDirNames = new Stack<String>();
51 }
52
53 @Override
54 public void onCreate(Bundle savedInstanceState) {
55 super.onCreate(savedInstanceState);
56
57 mAccountManager = (AccountManager)getActivity().getSystemService(Service.ACCOUNT_SERVICE);
58 mAccount = mAccountManager.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[0];
59 populateFileList();
60 }
61
62 @Override
63 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
64 if (mFiles.size() <= position) {
65 throw new IndexOutOfBoundsException("Incorrect item selected");
66 }
67 OCFile file = mFiles.get(position);
68 String id_ = String.valueOf(file.getFileId());
69 if (file.getMimetype().equals("DIR")) {
70 String dirname = file.getFileName();
71
72 mDirNames.push(dirname);
73 ((FileDisplayActivity)getActivity()).pushPath(dirname);
74
75 populateFileList();
76 return;
77 }
78 Intent i = new Intent(getActivity(), FileDetailActivity.class);
79 i.putExtra("FILE_NAME", file.getFileName());
80 i.putExtra("FULL_PATH", file.getPath());
81 i.putExtra("FILE_ID", id_);
82 i.putExtra("ACCOUNT", mAccount);
83 FileDetail fd = (FileDetail) getSupportFragmentManager().findFragmentById(R.id.fileDetail);
84 if (fd != null) {
85 fd.setStuff(i);
86 } else {
87 startActivity(i);
88 }
89 }
90
91 public void onBackPressed() {
92 mDirNames.pop();
93 populateFileList();
94 }
95
96 private void populateFileList() {
97 String s = "/";
98 for (String a : mDirNames)
99 s+= a+"/";
100
101 OCFile file = new OCFile(getActivity().getContentResolver(), mAccount, s);
102 mFiles = file.getDirectoryContent();
103 setListAdapter(new FileListListAdapter(file, getActivity()));
104 }
105 }