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
);
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);
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);
106 Log
.d(TAG
, "onCreate() end");
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
){
121 retval
= onOptionsItemSelected(item
);
128 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
129 int i
= itemPosition
;
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);
143 public void onBackPressed() {
144 if (mDirectories
.getCount() <= 1) {
149 mFileListFragment
.onNavigateUp();
150 mCurrentDir
= mFileListFragment
.getCurrentDirectory();
152 if(mCurrentDir
.getParentFile() == null
){
153 ActionBar actionBar
= getSupportActionBar();
154 actionBar
.setDisplayHomeAsUpEnabled(false
);
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");
170 * Pushes a directory to the drop down list
171 * @param directory to push
172 * @throws IllegalArgumentException If the {@link File#isDirectory()} returns false.
174 public void pushDirname(File directory
) {
175 if(!directory
.isDirectory()){
176 throw new IllegalArgumentException("Only directories may be pushed!");
178 mDirectories
.insert(directory
.getName(), 0);
179 mCurrentDir
= directory
;
183 * Pops a directory name from the drop down list
184 * @return True, unless the stack is empty
186 public boolean popDirname() {
187 mDirectories
.remove(mDirectories
.getItem(0));
188 return !mDirectories
.isEmpty();
192 // Custom array adapter to override text colors
193 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
195 public CustomArrayAdapter(UploadFilesActivity ctx
, int view
) {
199 public View
getView(int position
, View convertView
, ViewGroup parent
) {
200 View v
= super.getView(position
, convertView
, parent
);
202 ((TextView
) v
).setTextColor(getResources().getColorStateList(
203 android
.R
.color
.white
));
207 public View
getDropDownView(int position
, View convertView
,
209 View v
= super.getDropDownView(position
, convertView
, parent
);
211 ((TextView
) v
).setTextColor(getResources().getColorStateList(
212 android
.R
.color
.white
));
223 public void onDirectoryClick(File directory
) {
224 pushDirname(directory
);
225 ActionBar actionBar
= getSupportActionBar();
226 actionBar
.setDisplayHomeAsUpEnabled(true
);
234 public void onFileClick(File file
) {
242 public File
getInitialDirectory() {
248 * Performs corresponding action when user presses 'Cancel' or 'Upload' button
251 public void onClick(View v
) {
252 if (v
.getId() == R
.id
.upload_files_btn_cancel
) {
253 setResult(RESULT_CANCELED
);
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
);