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