1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
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.
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.
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/>.
19 package com
.owncloud
.android
.ui
.activity
;
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
;
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
;
40 import com
.owncloud
.android
.R
;
43 * Displays local files and let the user choose what of them wants to upload
44 * to the current ownCloud account
46 * @author David A. Velasco
50 public class UploadFilesActivity
extends SherlockFragmentActivity
implements
51 LocalFileListFragment
.ContainerActivity
, OnNavigationListener
, OnClickListener
{
53 private ArrayAdapter
<String
> mDirectories
;
54 private File mCurrentDir
= null
;
55 private LocalFileListFragment mFileListFragment
;
56 private Button mCancelBtn
;
57 private Button mUploadBtn
;
59 public static final String EXTRA_DIRECTORY_PATH
= "com.owncloud.android.Directory";
60 public static final String EXTRA_CHOSEN_FILES
= "com.owncloud.android.ChosenFiles";
62 private static final String TAG
= "UploadFilesActivity";
66 public void onCreate(Bundle savedInstanceState
) {
67 Log
.d(TAG
, "onCreate() start");
68 super.onCreate(savedInstanceState
);
70 if(savedInstanceState
!= null
) {
71 mCurrentDir
= new File(savedInstanceState
.getString(UploadFilesActivity
.EXTRA_DIRECTORY_PATH
));
73 mCurrentDir
= Environment
.getExternalStorageDirectory();
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();
85 mDirectories
.add(File
.separator
);
87 // Inflate and set the layout view
88 setContentView(R
.layout
.upload_files_layout
);
89 mFileListFragment
= (LocalFileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.local_files_list
);
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);
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);
105 Log
.d(TAG
, "onCreate() end");
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
){
127 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
128 int i
= itemPosition
;
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);
142 public void onBackPressed() {
143 if (mDirectories
.getCount() <= 1) {
148 mFileListFragment
.onNavigateUp();
149 mCurrentDir
= mFileListFragment
.getCurrentDirectory();
151 if(mCurrentDir
.getParentFile() == null
){
152 ActionBar actionBar
= getSupportActionBar();
153 actionBar
.setDisplayHomeAsUpEnabled(false
);
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");
168 protected void onResume() {
169 Log
.d(TAG
, "onResume() start");
172 // List current directory
173 mFileListFragment
.listDirectory(mCurrentDir
);
175 Log
.d(TAG
, "onResume() end");
180 * Pushes a directory to the drop down list
181 * @param directory to push
182 * @throws IllegalArgumentException If the {@link File#isDirectory()} returns false.
184 public void pushDirname(File directory
) {
185 if(!directory
.isDirectory()){
186 throw new IllegalArgumentException("Only directories may be pushed!");
188 mDirectories
.insert(directory
.getName(), 0);
189 mCurrentDir
= directory
;
193 * Pops a directory name from the drop down list
194 * @return True, unless the stack is empty
196 public boolean popDirname() {
197 mDirectories
.remove(mDirectories
.getItem(0));
198 return !mDirectories
.isEmpty();
202 // Custom array adapter to override text colors
203 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
205 public CustomArrayAdapter(UploadFilesActivity ctx
, int view
) {
209 public View
getView(int position
, View convertView
, ViewGroup parent
) {
210 View v
= super.getView(position
, convertView
, parent
);
212 ((TextView
) v
).setTextColor(getResources().getColorStateList(
213 android
.R
.color
.white
));
217 public View
getDropDownView(int position
, View convertView
,
219 View v
= super.getDropDownView(position
, convertView
, parent
);
221 ((TextView
) v
).setTextColor(getResources().getColorStateList(
222 android
.R
.color
.white
));
233 public void onDirectoryClick(File directory
) {
234 pushDirname(directory
);
235 ActionBar actionBar
= getSupportActionBar();
236 actionBar
.setDisplayHomeAsUpEnabled(true
);
244 public void onFileClick(File file
) {
250 * Performs corresponding action when user presses 'Cancel' or 'Upload' button
253 public void onClick(View v
) {
254 if (v
.getId() == R
.id
.upload_files_btn_cancel
) {
255 setResult(RESULT_CANCELED
);
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
);