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