FileListFragment.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.Vector;
  20. import com.actionbarsherlock.app.ActionBar;
  21. import android.accounts.Account;
  22. import android.content.Intent;
  23. import android.os.Bundle;
  24. import android.support.v4.app.FragmentTransaction;
  25. import android.util.Log;
  26. import android.view.LayoutInflater;
  27. import android.view.View;
  28. import android.view.ViewGroup;
  29. import android.widget.AdapterView;
  30. import android.widget.Toast;
  31. import eu.alefzero.owncloud.AccountUtils;
  32. import eu.alefzero.owncloud.R;
  33. import eu.alefzero.owncloud.datamodel.DataStorageManager;
  34. import eu.alefzero.owncloud.datamodel.FileDataStorageManager;
  35. import eu.alefzero.owncloud.datamodel.OCFile;
  36. import eu.alefzero.owncloud.files.services.FileDownloader;
  37. import eu.alefzero.owncloud.ui.FragmentListView;
  38. import eu.alefzero.owncloud.ui.activity.FileDetailActivity;
  39. import eu.alefzero.owncloud.ui.activity.FileDisplayActivity;
  40. import eu.alefzero.owncloud.ui.adapter.FileListListAdapter;
  41. /**
  42. * A Fragment that lists all files and folders in a given path.
  43. *
  44. * @author Bartek Przybylski
  45. *
  46. */
  47. public class FileListFragment extends FragmentListView {
  48. private static final String TAG = "FileListFragment";
  49. private Vector<OCFile> mFiles;
  50. private OCFile mFile;
  51. private boolean mIsLargeDevice;
  52. @Override
  53. public void onCreate(Bundle savedInstanceState) {
  54. Log.i(getClass().toString(), "onCreate() start");
  55. super.onCreate(savedInstanceState);
  56. Intent intent = getActivity().getIntent();
  57. OCFile directory = intent.getParcelableExtra(FileDetailFragment.EXTRA_FILE);
  58. mFile = directory;
  59. mIsLargeDevice = false;
  60. Log.i(getClass().toString(), "onCreate() stop");
  61. }
  62. @Override
  63. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  64. Bundle savedInstanceState) {
  65. Log.i(getClass().toString(), "onCreateView() start");
  66. super.onCreateView(inflater, container, savedInstanceState);
  67. getListView().setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
  68. getListView().setDividerHeight(1);
  69. Log.i(getClass().toString(), "onCreateView() end");
  70. return getListView();
  71. }
  72. @Override
  73. public void onStart() {
  74. Log.i(getClass().toString(), "onStart() start");
  75. super.onStart();
  76. // Create a placeholder upon launch
  77. View fragmentContainer = getActivity().findViewById(R.id.file_details_container);
  78. if (fragmentContainer != null) {
  79. mIsLargeDevice = true;
  80. FragmentTransaction transaction = getFragmentManager().beginTransaction();
  81. transaction.replace(R.id.file_details_container, new FileDetailFragment(true));
  82. transaction.commit();
  83. }
  84. Log.i(getClass().toString(), "onStart() end");
  85. }
  86. @Override
  87. public void onItemClick(AdapterView<?> l, View v, int position, long id) {
  88. if (mFiles.size() <= position) {
  89. throw new IndexOutOfBoundsException("Incorrect item selected");
  90. }
  91. OCFile file = mFiles.get(position);
  92. // Update ActionBarPath
  93. if (file.getMimetype().equals("DIR")) {
  94. mFile = file;
  95. ((FileDisplayActivity) getActivity()).pushDirname(file);
  96. ActionBar actionBar = ((FileDisplayActivity) getActivity()).getSupportActionBar();
  97. actionBar.setDisplayHomeAsUpEnabled(true);
  98. listDirectory(file);
  99. resetFileFragment();
  100. return;
  101. }
  102. Intent showDetailsIntent = new Intent(getActivity(), FileDetailActivity.class);
  103. showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, file);
  104. showDetailsIntent.putExtra(FileDownloader.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(getActivity()));
  105. // If we are on a large device -> update fragment
  106. if (mIsLargeDevice) {
  107. FileDetailFragment fileDetails = (FileDetailFragment) getFragmentManager().findFragmentByTag("FileDetails");
  108. if (fileDetails == null) {
  109. FragmentTransaction transaction = getFragmentManager().beginTransaction();
  110. transaction.replace(R.id.file_details_container, new FileDetailFragment(showDetailsIntent), "FileDetails");
  111. transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
  112. transaction.commit();
  113. } else {
  114. fileDetails.updateFileDetails(showDetailsIntent);
  115. }
  116. } else {
  117. startActivity(showDetailsIntent);
  118. }
  119. }
  120. /**
  121. * Resets the FileDetailsFragment on Tablets so that it always displays
  122. * "Tab on a file to display it's details"
  123. */
  124. private void resetFileFragment() {
  125. FileDetailFragment fileDetails = (FileDetailFragment) getFragmentManager().findFragmentByTag("FileDetails");
  126. if (fileDetails != null) {
  127. FragmentTransaction transaction = getFragmentManager().beginTransaction();
  128. transaction.remove(fileDetails);
  129. transaction.add(R.id.file_details_container, new FileDetailFragment(true));
  130. transaction.commit();
  131. }
  132. }
  133. /**
  134. * Call this, when the user presses the up button
  135. */
  136. public void onNavigateUp() {
  137. OCFile parentDir = null;
  138. if(mFile != null){
  139. DataStorageManager storageManager = ((FileDisplayActivity)getActivity()).getStorageManager();
  140. parentDir = storageManager.getFileById(mFile.getParentId());
  141. mFile = parentDir;
  142. }
  143. listDirectory(parentDir);
  144. resetFileFragment();
  145. }
  146. /**
  147. * Use this to query the {@link OCFile} that is currently
  148. * being displayed by this fragment
  149. * @return The currently viewed OCFile
  150. */
  151. public OCFile getCurrentFile(){
  152. return mFile;
  153. }
  154. /**
  155. * Calls {@link FileListFragment#listDirectory(OCFile)} with a null parameter
  156. */
  157. public void listDirectory(){
  158. listDirectory(null);
  159. }
  160. /**
  161. * Lists the given directory on the view. When the input parameter is null,
  162. * it will either refresh the last known directory, or list the root
  163. * if there never was a directory.
  164. *
  165. * @param directory File to be listed
  166. */
  167. public void listDirectory(OCFile directory) {
  168. DataStorageManager storageManager = ((FileDisplayActivity)getActivity()).getStorageManager();
  169. // Check input parameters for null
  170. if(directory == null){
  171. if(mFile != null){
  172. directory = mFile;
  173. } else {
  174. directory = storageManager.getFileByPath("/");
  175. if (directory == null) return; // no files, wait for sync
  176. }
  177. }
  178. // If that's not a directory -> List its parent
  179. if(!directory.isDirectory()){
  180. Log.w(TAG, "You see, that is not a directory -> " + directory.toString());
  181. directory = storageManager.getFileById(directory.getParentId());
  182. }
  183. mFile = directory;
  184. mFiles = storageManager.getDirectoryContent(directory);
  185. if (mFiles == null || mFiles.size() == 0) {
  186. Toast.makeText(getActivity(), "There are no files here", Toast.LENGTH_LONG).show();
  187. }
  188. setListAdapter(new FileListListAdapter(directory, storageManager, getActivity()));
  189. }
  190. }