UploadFilesActivity.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 com.owncloud.android.ui.activity;
  19. import java.io.File;
  20. import android.content.Intent;
  21. import android.os.Bundle;
  22. import android.os.Environment;
  23. import android.util.Log;
  24. import android.view.View;
  25. import android.view.View.OnClickListener;
  26. import android.view.ViewGroup;
  27. import android.widget.ArrayAdapter;
  28. import android.widget.Button;
  29. import android.widget.TextView;
  30. import com.actionbarsherlock.app.ActionBar;
  31. import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
  32. import com.actionbarsherlock.app.SherlockFragmentActivity;
  33. import com.actionbarsherlock.view.MenuItem;
  34. import com.owncloud.android.ui.fragment.LocalFileListFragment;
  35. import com.owncloud.android.R;
  36. /**
  37. * Displays local files and let the user choose what of them wants to upload
  38. * to the current ownCloud account
  39. *
  40. * @author David A. Velasco
  41. *
  42. */
  43. public class UploadFilesActivity extends SherlockFragmentActivity implements
  44. LocalFileListFragment.ContainerActivity, OnNavigationListener, OnClickListener {
  45. private ArrayAdapter<String> mDirectories;
  46. private File mCurrentDir = null;
  47. private LocalFileListFragment mFileListFragment;
  48. private Button mCancelBtn;
  49. private Button mUploadBtn;
  50. public static final String EXTRA_DIRECTORY_PATH = "com.owncloud.android.Directory";
  51. public static final String EXTRA_CHOSEN_FILES = "com.owncloud.android.ChosenFiles";
  52. private static final String TAG = "UploadFilesActivity";
  53. @Override
  54. public void onCreate(Bundle savedInstanceState) {
  55. Log.d(TAG, "onCreate() start");
  56. super.onCreate(savedInstanceState);
  57. if(savedInstanceState != null) {
  58. mCurrentDir = new File(savedInstanceState.getString(UploadFilesActivity.EXTRA_DIRECTORY_PATH));
  59. } else {
  60. mCurrentDir = Environment.getExternalStorageDirectory();
  61. }
  62. /// USER INTERFACE
  63. // Drop-down navigation
  64. mDirectories = new CustomArrayAdapter<String>(this, R.layout.sherlock_spinner_dropdown_item);
  65. File currDir = mCurrentDir;
  66. while(currDir != null && currDir.getParentFile() != null) {
  67. mDirectories.add(currDir.getName());
  68. currDir = currDir.getParentFile();
  69. }
  70. mDirectories.add(File.separator);
  71. // Inflate and set the layout view
  72. setContentView(R.layout.upload_files_layout);
  73. mFileListFragment = (LocalFileListFragment) getSupportFragmentManager().findFragmentById(R.id.local_files_list);
  74. // Set input controllers
  75. mCancelBtn = (Button) findViewById(R.id.upload_files_btn_cancel);
  76. mCancelBtn.setOnClickListener(this);
  77. mUploadBtn = (Button) findViewById(R.id.upload_files_btn_upload);
  78. mUploadBtn.setOnClickListener(this);
  79. // Action bar setup
  80. ActionBar actionBar = getSupportActionBar();
  81. actionBar.setHomeButtonEnabled(true); // mandatory since Android ICS, according to the official documentation
  82. actionBar.setDisplayHomeAsUpEnabled(mCurrentDir != null && mCurrentDir.getName() != null);
  83. actionBar.setDisplayShowTitleEnabled(false);
  84. actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
  85. actionBar.setListNavigationCallbacks(mDirectories, this);
  86. Log.d(TAG, "onCreate() end");
  87. }
  88. @Override
  89. public boolean onOptionsItemSelected(MenuItem item) {
  90. boolean retval = true;
  91. switch (item.getItemId()) {
  92. case android.R.id.home: {
  93. if(mCurrentDir != null && mCurrentDir.getParentFile() != null){
  94. onBackPressed();
  95. }
  96. break;
  97. }
  98. default:
  99. retval = onOptionsItemSelected(item);
  100. }
  101. return retval;
  102. }
  103. @Override
  104. public boolean onNavigationItemSelected(int itemPosition, long itemId) {
  105. int i = itemPosition;
  106. while (i-- != 0) {
  107. onBackPressed();
  108. }
  109. // the next operation triggers a new call to this method, but it's necessary to
  110. // ensure that the name exposed in the action bar is the current directory when the
  111. // user selected it in the navigation list
  112. if (itemPosition != 0)
  113. getSupportActionBar().setSelectedNavigationItem(0);
  114. return true;
  115. }
  116. @Override
  117. public void onBackPressed() {
  118. if (mDirectories.getCount() <= 1) {
  119. finish();
  120. return;
  121. }
  122. popDirname();
  123. mFileListFragment.onNavigateUp();
  124. mCurrentDir = mFileListFragment.getCurrentDirectory();
  125. if(mCurrentDir.getParentFile() == null){
  126. ActionBar actionBar = getSupportActionBar();
  127. actionBar.setDisplayHomeAsUpEnabled(false);
  128. }
  129. }
  130. @Override
  131. protected void onSaveInstanceState(Bundle outState) {
  132. // responsibility of restore is preferred in onCreate() before than in onRestoreInstanceState when there are Fragments involved
  133. Log.d(TAG, "onSaveInstanceState() start");
  134. super.onSaveInstanceState(outState);
  135. outState.putString(UploadFilesActivity.EXTRA_DIRECTORY_PATH, mCurrentDir.getAbsolutePath());
  136. Log.d(TAG, "onSaveInstanceState() end");
  137. }
  138. /**
  139. * Pushes a directory to the drop down list
  140. * @param directory to push
  141. * @throws IllegalArgumentException If the {@link File#isDirectory()} returns false.
  142. */
  143. public void pushDirname(File directory) {
  144. if(!directory.isDirectory()){
  145. throw new IllegalArgumentException("Only directories may be pushed!");
  146. }
  147. mDirectories.insert(directory.getName(), 0);
  148. mCurrentDir = directory;
  149. }
  150. /**
  151. * Pops a directory name from the drop down list
  152. * @return True, unless the stack is empty
  153. */
  154. public boolean popDirname() {
  155. mDirectories.remove(mDirectories.getItem(0));
  156. return !mDirectories.isEmpty();
  157. }
  158. // Custom array adapter to override text colors
  159. private class CustomArrayAdapter<T> extends ArrayAdapter<T> {
  160. public CustomArrayAdapter(UploadFilesActivity ctx, int view) {
  161. super(ctx, view);
  162. }
  163. public View getView(int position, View convertView, ViewGroup parent) {
  164. View v = super.getView(position, convertView, parent);
  165. ((TextView) v).setTextColor(getResources().getColorStateList(
  166. android.R.color.white));
  167. return v;
  168. }
  169. public View getDropDownView(int position, View convertView,
  170. ViewGroup parent) {
  171. View v = super.getDropDownView(position, convertView, parent);
  172. ((TextView) v).setTextColor(getResources().getColorStateList(
  173. android.R.color.white));
  174. return v;
  175. }
  176. }
  177. /**
  178. * {@inheritDoc}
  179. */
  180. @Override
  181. public void onDirectoryClick(File directory) {
  182. pushDirname(directory);
  183. ActionBar actionBar = getSupportActionBar();
  184. actionBar.setDisplayHomeAsUpEnabled(true);
  185. }
  186. /**
  187. * {@inheritDoc}
  188. */
  189. @Override
  190. public void onFileClick(File file) {
  191. // nothing to do
  192. }
  193. /**
  194. * {@inheritDoc}
  195. */
  196. @Override
  197. public File getInitialDirectory() {
  198. return mCurrentDir;
  199. }
  200. /**
  201. * Performs corresponding action when user presses 'Cancel' or 'Upload' button
  202. */
  203. @Override
  204. public void onClick(View v) {
  205. if (v.getId() == R.id.upload_files_btn_cancel) {
  206. setResult(RESULT_CANCELED);
  207. finish();
  208. } else if (v.getId() == R.id.upload_files_btn_upload) {
  209. Intent data = new Intent();
  210. data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
  211. setResult(RESULT_OK, data);
  212. finish();
  213. }
  214. }
  215. }