fix invalid list get in filelist
[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
22 import android.accounts.Account;
23 import android.accounts.AccountManager;
24 import android.app.Service;
25 import android.app.DownloadManager.Query;
26 import android.content.Intent;
27 import android.database.Cursor;
28 import android.net.Uri;
29 import android.os.Bundle;
30 import android.util.Log;
31 import android.view.View;
32 import android.widget.AdapterView;
33 import android.widget.TextView;
34 import eu.alefzero.owncloud.R;
35 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
36 import eu.alefzero.owncloud.datamodel.OCFile;
37 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
38 import eu.alefzero.owncloud.ui.FragmentListView;
39 import eu.alefzero.owncloud.ui.activity.FileDetailActivity;
40 import eu.alefzero.owncloud.ui.activity.FileDisplayActivity;
41 import eu.alefzero.owncloud.ui.adapter.FileListListAdapter;
42
43 /**
44 * A Fragment that lists all files and folders in a given path.
45 * @author Bartek Przybylski
46 *
47 */
48 public class FileList extends FragmentListView {
49 private Cursor mCursor;
50 private Account mAccount;
51 private AccountManager mAccountManager;
52 private Stack<String> mDirNames;
53 private Stack<String> mParentsIds;
54
55 public FileList() {
56 mDirNames = new Stack<String>();
57 mParentsIds = new Stack<String>();
58 }
59
60 @Override
61 public void onCreate(Bundle savedInstanceState) {
62 super.onCreate(savedInstanceState);
63
64 mAccountManager = (AccountManager)getActivity().getSystemService(Service.ACCOUNT_SERVICE);
65 mAccount = mAccountManager.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[0];
66 populateFileList();
67 }
68
69 @Override
70 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
71 if (!mCursor.moveToPosition(position)) {
72 throw new IndexOutOfBoundsException("Incorrect item selected");
73 }
74 String id_ = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta._ID));
75 if (mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)).equals("DIR")) {
76 String dirname = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_NAME));
77
78 mDirNames.push(dirname);
79 mParentsIds.push(id_);
80 ((FileDisplayActivity)getActivity()).pushPath(dirname);
81
82 populateFileList();
83 return;
84 }
85 Intent i = new Intent(getActivity(), FileDetailActivity.class);
86 String filename = ((TextView)v.findViewById(R.id.Filename)).getText().toString();
87 i.putExtra("FILE_NAME", filename);
88 i.putExtra("FULL_PATH", "/" + filename);
89 i.putExtra("FILE_ID", id_);
90 i.putExtra("ACCOUNT", mAccount);
91 FileDetail fd = (FileDetail) getSupportFragmentManager().findFragmentById(R.id.fileDetail);
92 if (fd != null) {
93 fd.setStuff(i);
94 } else {
95 startActivity(i);
96 }
97 }
98
99 public void onBackPressed() {
100 mParentsIds.pop();
101 mDirNames.pop();
102 populateFileList();
103 }
104
105 private void populateFileList() {
106 if (mParentsIds.empty()) {
107 OCFile file = new OCFile(getActivity().getContentResolver(), mAccount, "/");
108 Log.d("ASD", file.getFileName()+"");
109 Log.d("ASD", file.getFileId()+"");
110 if (file.getDirectoryContent() != null)
111 Log.d("ASD", file.getDirectoryContent().size()+"");
112
113 mCursor = getActivity().getContentResolver().query(ProviderTableMeta.CONTENT_URI,
114 null,
115 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
116 new String[]{mAccount.name},
117 null);
118 } else {
119 mCursor = getActivity().managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParentsIds.peek()),
120 null,
121 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
122 new String[]{mAccount.name}, null);
123 }
124 setListAdapter(new FileListListAdapter(mCursor, getActivity()));
125 }
126 }