FileListFragment.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 java.util.Vector;
  21. import android.accounts.Account;
  22. import android.content.Intent;
  23. import android.os.Bundle;
  24. import android.util.Log;
  25. import android.view.View;
  26. import android.widget.AdapterView;
  27. import eu.alefzero.owncloud.R;
  28. import eu.alefzero.owncloud.authenticator.AuthUtils;
  29. import eu.alefzero.owncloud.datamodel.DataStorageManager;
  30. import eu.alefzero.owncloud.datamodel.FileDataStorageManager;
  31. import eu.alefzero.owncloud.datamodel.OCFile;
  32. import eu.alefzero.owncloud.ui.FragmentListView;
  33. import eu.alefzero.owncloud.ui.activity.FileDetailActivity;
  34. import eu.alefzero.owncloud.ui.activity.FileDisplayActivity;
  35. import eu.alefzero.owncloud.ui.adapter.FileListListAdapter;
  36. /**
  37. * A Fragment that lists all files and folders in a given path.
  38. * @author Bartek Przybylski
  39. *
  40. */
  41. public class FileListFragment extends FragmentListView {
  42. private Account mAccount;
  43. private Stack<String> mDirNames;
  44. private Vector<OCFile> mFiles;
  45. private DataStorageManager mStorageManager;
  46. public FileListFragment() {
  47. mDirNames = new Stack<String>();
  48. }
  49. @Override
  50. public void onCreate(Bundle savedInstanceState) {
  51. super.onCreate(savedInstanceState);
  52. mAccount = AuthUtils.getCurrentOwnCloudAccount(getActivity());
  53. populateFileList();
  54. }
  55. @Override
  56. public void onItemClick(AdapterView<?> l, View v, int position, long id) {
  57. if (mFiles.size() <= position) {
  58. throw new IndexOutOfBoundsException("Incorrect item selected");
  59. }
  60. OCFile file = mFiles.get(position);
  61. String id_ = String.valueOf(file.getFileId());
  62. if (file.getMimetype().equals("DIR")) {
  63. String dirname = file.getFileName();
  64. mDirNames.push(dirname);
  65. ((FileDisplayActivity)getActivity()).pushPath(dirname);
  66. populateFileList();
  67. return;
  68. }
  69. Intent i = new Intent(getActivity(), FileDetailActivity.class);
  70. i.putExtra("FILE_NAME", file.getFileName());
  71. i.putExtra("FULL_PATH", file.getPath());
  72. i.putExtra("FILE_ID", id_);
  73. Log.e("ASD", mAccount+"");
  74. i.putExtra("ACCOUNT", mAccount);
  75. FileDetailFragment fd = (FileDetailFragment) getFragmentManager().findFragmentById(R.id.fileDetail);
  76. if (fd != null) {
  77. fd.setStuff(i);
  78. } else {
  79. startActivity(i);
  80. }
  81. }
  82. /**
  83. * Call this, when the user presses the up button
  84. */
  85. public void onNavigateUp() {
  86. mDirNames.pop();
  87. populateFileList();
  88. }
  89. /**
  90. * Lists the directory
  91. */
  92. private void populateFileList() {
  93. String s = "/";
  94. for (String a : mDirNames)
  95. s+= a + "/";
  96. Log.e("ASD", s);
  97. mStorageManager = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
  98. OCFile file = mStorageManager.getFileByPath(s);
  99. mFiles = mStorageManager.getDirectoryContent(file);
  100. setListAdapter(new FileListListAdapter(file, mStorageManager, getActivity()));
  101. }
  102. //TODO: Delete this testing stuff.
  103. /*private void addContact(Account account, String name, String username) {
  104. Log.i("ASD", "Adding contact: " + name);
  105. ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
  106. //Create our RawContact
  107. ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
  108. builder.withValue(RawContacts.ACCOUNT_NAME, account.name);
  109. builder.withValue(RawContacts.ACCOUNT_TYPE, account.type);
  110. builder.withValue(RawContacts.SYNC1, username);
  111. operationList.add(builder.build());
  112. //Create a Data record of common type 'StructuredName' for our RawContact
  113. builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
  114. builder.withValueBackReference(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID, 0);
  115. builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
  116. builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name);
  117. operationList.add(builder.build());
  118. //Create a Data record of custom type "vnd.android.cursor.item/vnd.fm.last.android.profile" to display a link to the Last.fm profile
  119. builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
  120. builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
  121. builder.withValue(ContactsContract.Data.MIMETYPE, "vnd.android.cursor.item/vnd.owncloud.contact.profile");
  122. builder.withValue(ContactsContract.Data.DATA1, username);
  123. builder.withValue(ContactsContract.Data.DATA2, "Last.fm Profile");
  124. builder.withValue(ContactsContract.Data.DATA3, "View profile");
  125. operationList.add(builder.build());
  126. try {
  127. getActivity().getContentResolver().applyBatch(ContactsContract.AUTHORITY, operationList);
  128. } catch (Exception e) {
  129. Log.e("ASD", "Something went wrong during creation! " + e);
  130. e.printStackTrace();
  131. }
  132. }*/
  133. }