FileList.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. import java.util.Stack;
  20. import android.accounts.Account;
  21. import android.accounts.AccountManager;
  22. import android.app.Service;
  23. import android.app.DownloadManager.Query;
  24. import android.content.Intent;
  25. import android.database.Cursor;
  26. import android.net.Uri;
  27. import android.os.Bundle;
  28. import android.util.Log;
  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.datamodel.OCFile;
  35. import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
  36. import eu.alefzero.owncloud.ui.FragmentListView;
  37. import eu.alefzero.owncloud.ui.activity.FileDetailActivity;
  38. import eu.alefzero.owncloud.ui.activity.FileDisplayActivity;
  39. import eu.alefzero.owncloud.ui.adapter.FileListListAdapter;
  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. public FileList() {
  52. mDirNames = new Stack<String>();
  53. mParentsIds = new Stack<String>();
  54. }
  55. @Override
  56. public void onCreate(Bundle savedInstanceState) {
  57. super.onCreate(savedInstanceState);
  58. mAccountManager = (AccountManager)getActivity().getSystemService(Service.ACCOUNT_SERVICE);
  59. mAccount = mAccountManager.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[0];
  60. populateFileList();
  61. }
  62. @Override
  63. public void onItemClick(AdapterView<?> l, View v, int position, long id) {
  64. if (!mCursor.moveToPosition(position)) {
  65. throw new IndexOutOfBoundsException("Incorrect item selected");
  66. }
  67. String id_ = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta._ID));
  68. if (mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)).equals("DIR")) {
  69. String dirname = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_NAME));
  70. mDirNames.push(dirname);
  71. mParentsIds.push(id_);
  72. ((FileDisplayActivity)getActivity()).pushPath(dirname);
  73. populateFileList();
  74. return;
  75. }
  76. Intent i = new Intent(getActivity(), FileDetailActivity.class);
  77. String filename = ((TextView)v.findViewById(R.id.Filename)).getText().toString();
  78. i.putExtra("FILE_NAME", filename);
  79. i.putExtra("FULL_PATH", "/" + filename);
  80. i.putExtra("FILE_ID", id_);
  81. i.putExtra("ACCOUNT", mAccount);
  82. FileDetail fd = (FileDetail) getSupportFragmentManager().findFragmentById(R.id.fileDetail);
  83. if (fd != null) {
  84. fd.setStuff(i);
  85. } else {
  86. startActivity(i);
  87. }
  88. }
  89. public void onBackPressed() {
  90. mParentsIds.pop();
  91. mDirNames.pop();
  92. populateFileList();
  93. }
  94. private void populateFileList() {
  95. if (mParentsIds.empty()) {
  96. OCFile file = new OCFile(getActivity().getContentResolver(), mAccount, "/");
  97. Log.d("ASD", file.getFileName()+"");
  98. Log.d("ASD", file.getFileId()+"");
  99. if (file.getDirectoryContent() != null)
  100. Log.d("ASD", file.getDirectoryContent().size()+"");
  101. mCursor = getActivity().getContentResolver().query(ProviderTableMeta.CONTENT_URI,
  102. null,
  103. ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
  104. new String[]{mAccount.name},
  105. null);
  106. } else {
  107. mCursor = getActivity().managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParentsIds.peek()),
  108. null,
  109. ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
  110. new String[]{mAccount.name}, null);
  111. }
  112. setListAdapter(new FileListListAdapter(mCursor, getActivity()));
  113. }
  114. }