newmodel of file in action
[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.app.DownloadManager.Query;
27 import android.content.Intent;
28 import android.database.Cursor;
29 import android.net.Uri;
30 import android.os.Bundle;
31 import android.util.Log;
32 import android.view.View;
33 import android.widget.AdapterView;
34 import android.widget.TextView;
35 import eu.alefzero.owncloud.R;
36 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
37 import eu.alefzero.owncloud.datamodel.OCFile;
38 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
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 * @author Bartek Przybylski
47 *
48 */
49 public class FileList extends FragmentListView {
50 private Account mAccount;
51 private AccountManager mAccountManager;
52 private Stack<String> mDirNames;
53 private Stack<String> mParentsIds;
54 private Vector<OCFile> mFiles;
55
56 public FileList() {
57 mDirNames = new Stack<String>();
58 mParentsIds = new Stack<String>();
59 }
60
61 @Override
62 public void onCreate(Bundle savedInstanceState) {
63 super.onCreate(savedInstanceState);
64
65 mAccountManager = (AccountManager)getActivity().getSystemService(Service.ACCOUNT_SERVICE);
66 mAccount = mAccountManager.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[0];
67 populateFileList();
68 }
69
70 @Override
71 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
72 if (mFiles.size() <= position) {
73 throw new IndexOutOfBoundsException("Incorrect item selected");
74 }
75 OCFile file = mFiles.get(position);
76 String id_ = String.valueOf(file.getFileId());
77 if (file.getMimetype().equals("DIR")) {
78 String dirname = file.getFileName();
79
80 mDirNames.push(dirname);
81 mParentsIds.push(id_);
82 ((FileDisplayActivity)getActivity()).pushPath(dirname);
83
84 populateFileList();
85 return;
86 }
87 Intent i = new Intent(getActivity(), FileDetailActivity.class);
88 String filename = ((TextView)v.findViewById(R.id.Filename)).getText().toString();
89 i.putExtra("FILE_NAME", filename);
90 i.putExtra("FULL_PATH", "/" + filename);
91 i.putExtra("FILE_ID", id_);
92 i.putExtra("ACCOUNT", mAccount);
93 FileDetail fd = (FileDetail) getSupportFragmentManager().findFragmentById(R.id.fileDetail);
94 if (fd != null) {
95 fd.setStuff(i);
96 } else {
97 startActivity(i);
98 }
99 }
100
101 public void onBackPressed() {
102 mParentsIds.pop();
103 mDirNames.pop();
104 populateFileList();
105 }
106
107 private void populateFileList() {
108 String s = "/";
109 for (String a : mDirNames)
110 s+= a+"/";
111
112 OCFile file = new OCFile(getActivity().getContentResolver(), mAccount, s);
113 mFiles = file.getDirectoryContent();
114 setListAdapter(new FileListListAdapter(file, getActivity()));
115 }
116 }