Merge branch 'long_press'
[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
92 // Set input controllers
93 mCancelBtn = (Button) findViewById(R.id.upload_files_btn_cancel);
94 mCancelBtn.setOnClickListener(this);
95 mUploadBtn = (Button) findViewById(R.id.upload_files_btn_upload);
96 mUploadBtn.setOnClickListener(this);
97
98 // Action bar setup
99 ActionBar actionBar = getSupportActionBar();
100 actionBar.setHomeButtonEnabled(true); // mandatory since Android ICS, according to the official documentation
101 actionBar.setDisplayHomeAsUpEnabled(mCurrentDir != null && mCurrentDir.getName() != null);
102 actionBar.setDisplayShowTitleEnabled(false);
103 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
104 actionBar.setListNavigationCallbacks(mDirectories, this);
105
106 Log.d(TAG, "onCreate() end");
107 }
108
109
110 @Override
111 public boolean onOptionsItemSelected(MenuItem item) {
112 boolean retval = true;
113 switch (item.getItemId()) {
114 case android.R.id.home: {
115 if(mCurrentDir != null && mCurrentDir.getParentFile() != null){
116 onBackPressed();
117 }
118 break;
119 }
120 default:
121 retval = onOptionsItemSelected(item);
122 }
123 return retval;
124 }
125
126
127 @Override
128 public boolean onNavigationItemSelected(int itemPosition, long itemId) {
129 int i = itemPosition;
130 while (i-- != 0) {
131 onBackPressed();
132 }
133 // the next operation triggers a new call to this method, but it's necessary to
134 // ensure that the name exposed in the action bar is the current directory when the
135 // user selected it in the navigation list
136 if (itemPosition != 0)
137 getSupportActionBar().setSelectedNavigationItem(0);
138 return true;
139 }
140
141
142 @Override
143 public void onBackPressed() {
144 if (mDirectories.getCount() <= 1) {
145 finish();
146 return;
147 }
148 popDirname();
149 mFileListFragment.onNavigateUp();
150 mCurrentDir = mFileListFragment.getCurrentDirectory();
151
152 if(mCurrentDir.getParentFile() == null){
153 ActionBar actionBar = getSupportActionBar();
154 actionBar.setDisplayHomeAsUpEnabled(false);
155 }
156 }
157
158
159 @Override
160 protected void onSaveInstanceState(Bundle outState) {
161 // responsibility of restore is preferred in onCreate() before than in onRestoreInstanceState when there are Fragments involved
162 Log.d(TAG, "onSaveInstanceState() start");
163 super.onSaveInstanceState(outState);
164 outState.putString(UploadFilesActivity.EXTRA_DIRECTORY_PATH, mCurrentDir.getAbsolutePath());
165 Log.d(TAG, "onSaveInstanceState() end");
166 }
167
168
169 /**
170 * Pushes a directory to the drop down list
171 * @param directory to push
172 * @throws IllegalArgumentException If the {@link File#isDirectory()} returns false.
173 */
174 public void pushDirname(File directory) {
175 if(!directory.isDirectory()){
176 throw new IllegalArgumentException("Only directories may be pushed!");
177 }
178 mDirectories.insert(directory.getName(), 0);
179 mCurrentDir = directory;
180 }
181
182 /**
183 * Pops a directory name from the drop down list
184 * @return True, unless the stack is empty
185 */
186 public boolean popDirname() {
187 mDirectories.remove(mDirectories.getItem(0));
188 return !mDirectories.isEmpty();
189 }
190
191
192 // Custom array adapter to override text colors
193 private class CustomArrayAdapter<T> extends ArrayAdapter<T> {
194
195 public CustomArrayAdapter(UploadFilesActivity ctx, int view) {
196 super(ctx, view);
197 }
198
199 public View getView(int position, View convertView, ViewGroup parent) {
200 View v = super.getView(position, convertView, parent);
201
202 ((TextView) v).setTextColor(getResources().getColorStateList(
203 android.R.color.white));
204 return v;
205 }
206
207 public View getDropDownView(int position, View convertView,
208 ViewGroup parent) {
209 View v = super.getDropDownView(position, convertView, parent);
210
211 ((TextView) v).setTextColor(getResources().getColorStateList(
212 android.R.color.white));
213
214 return v;
215 }
216
217 }
218
219 /**
220 * {@inheritDoc}
221 */
222 @Override
223 public void onDirectoryClick(File directory) {
224 pushDirname(directory);
225 ActionBar actionBar = getSupportActionBar();
226 actionBar.setDisplayHomeAsUpEnabled(true);
227 }
228
229
230 /**
231 * {@inheritDoc}
232 */
233 @Override
234 public void onFileClick(File file) {
235 // nothing to do
236 }
237
238 /**
239 * {@inheritDoc}
240 */
241 @Override
242 public File getInitialDirectory() {
243 return mCurrentDir;
244 }
245
246
247 /**
248 * Performs corresponding action when user presses 'Cancel' or 'Upload' button
249 */
250 @Override
251 public void onClick(View v) {
252 if (v.getId() == R.id.upload_files_btn_cancel) {
253 setResult(RESULT_CANCELED);
254 finish();
255
256 } else if (v.getId() == R.id.upload_files_btn_upload) {
257 Intent data = new Intent();
258 data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
259 setResult(RESULT_OK, data);
260 finish();
261 }
262 }
263
264 }