Migration to ActionBarSherlock 4.0.1 / Custom Adapter to override text
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / activity / FileDisplayActivity.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 eu.alefzero.owncloud.ui.activity;
20
21 import android.accounts.Account;
22 import android.accounts.AccountManager;
23 import android.app.AlertDialog;
24 import android.app.AlertDialog.Builder;
25 import android.app.Dialog;
26 import android.content.DialogInterface;
27 import android.content.DialogInterface.OnCancelListener;
28 import android.content.DialogInterface.OnClickListener;
29 import android.content.Intent;
30 import android.net.Uri;
31 import android.os.Bundle;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.widget.ArrayAdapter;
35 import android.widget.EditText;
36 import android.widget.TextView;
37
38 import com.actionbarsherlock.app.ActionBar;
39 import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
40 import com.actionbarsherlock.app.SherlockFragmentActivity;
41 import com.actionbarsherlock.view.Menu;
42 import com.actionbarsherlock.view.MenuInflater;
43 import com.actionbarsherlock.view.MenuItem;
44
45 import eu.alefzero.owncloud.R;
46 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
47 import eu.alefzero.owncloud.authenticator.AuthUtils;
48 import eu.alefzero.owncloud.datamodel.OCFile;
49 import eu.alefzero.owncloud.ui.fragment.FileList;
50 import eu.alefzero.webdav.WebdavClient;
51
52 /**
53 * Displays, what files the user has available in his ownCloud.
54 *
55 * @author Bartek Przybylski
56 *
57 */
58
59 public class FileDisplayActivity extends SherlockFragmentActivity implements
60 OnNavigationListener {
61 private ArrayAdapter<String> mDirectories;
62
63 private static final int DIALOG_CHOOSE_ACCOUNT = 0;
64
65 public void pushPath(String path) {
66 mDirectories.insert(path, 0);
67 }
68
69 public boolean popPath() {
70 mDirectories.remove(mDirectories.getItem(0));
71 return !mDirectories.isEmpty();
72 }
73
74 @Override
75 protected Dialog onCreateDialog(int id, Bundle args) {
76 final AlertDialog.Builder builder = new Builder(this);
77 final EditText dirName = new EditText(getBaseContext());
78 final Account a = AuthUtils.getCurrentOwnCloudAccount(this);
79 builder.setView(dirName);
80 builder.setTitle(R.string.uploader_info_dirname);
81
82 builder.setPositiveButton(R.string.common_ok, new OnClickListener() {
83 public void onClick(DialogInterface dialog, int which) {
84 String s = dirName.getText().toString();
85 if (s.trim().isEmpty()) {
86 dialog.cancel();
87 return;
88 }
89
90 String path = "";
91 for (int i = mDirectories.getCount() - 2; i >= 0; --i) {
92 path += "/" + mDirectories.getItem(i);
93 }
94 OCFile parent = new OCFile(getContentResolver(), a, path + "/");
95 path += "/" + s + "/";
96 Thread thread = new Thread(new DirectoryCreator(path, a));
97 thread.start();
98 OCFile.createNewFile(getContentResolver(), a, path, 0, 0, 0,
99 "DIR", parent.getFileId()).save();
100
101 dialog.dismiss();
102 }
103 });
104 builder.setNegativeButton(R.string.common_cancel,
105 new OnClickListener() {
106 public void onClick(DialogInterface dialog, int which) {
107 dialog.cancel();
108 }
109 });
110 return builder.create();
111 }
112
113 @Override
114 public void onCreate(Bundle savedInstanceState) {
115 super.onCreate(savedInstanceState);
116 mDirectories = new CustomArrayAdapter<String>(this,
117 R.layout.sherlock_spinner_dropdown_item);
118 mDirectories.add("/");
119 setContentView(R.layout.files);
120 ActionBar action_bar = getSupportActionBar();
121 action_bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
122 action_bar.setDisplayShowTitleEnabled(false);
123 action_bar.setListNavigationCallbacks(mDirectories, this);
124 action_bar.setDisplayHomeAsUpEnabled(true);
125 }
126
127 @Override
128 public boolean onOptionsItemSelected(MenuItem item) {
129 switch (item.getItemId()) {
130 case R.id.settingsItem: {
131 Intent i = new Intent(this, Preferences.class);
132 startActivity(i);
133 break;
134 }
135 case R.id.createDirectoryItem: {
136 showDialog(0);
137 break;
138 }
139 }
140 return true;
141 }
142
143 @Override
144 protected Dialog onCreateDialog(int id) {
145 switch (id) {
146 case DIALOG_CHOOSE_ACCOUNT:
147 return createChooseAccountDialog();
148 default:
149 throw new IllegalArgumentException("Unknown dialog id: " + id);
150 }
151 }
152
153 @Override
154 public boolean onCreateOptionsMenu(Menu menu) {
155 MenuInflater inflater = getSherlock().getMenuInflater();
156 inflater.inflate(R.menu.menu, menu);
157 return true;
158 }
159
160 private Dialog createChooseAccountDialog() {
161 final AccountManager accMan = AccountManager.get(this);
162 CharSequence[] items = new CharSequence[accMan
163 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE).length];
164 int i = 0;
165 for (Account a : accMan
166 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)) {
167 items[i++] = a.name;
168 }
169
170 AlertDialog.Builder builder = new AlertDialog.Builder(this);
171 builder.setTitle(R.string.common_choose_account);
172 builder.setCancelable(true);
173 builder.setItems(items, new DialogInterface.OnClickListener() {
174 public void onClick(DialogInterface dialog, int item) {
175 // mAccount =
176 // accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[item];
177 dialog.dismiss();
178 }
179 });
180 builder.setOnCancelListener(new OnCancelListener() {
181 public void onCancel(DialogInterface dialog) {
182 FileDisplayActivity.this.finish();
183 }
184 });
185 AlertDialog alert = builder.create();
186 return alert;
187 }
188
189 @Override
190 public boolean onNavigationItemSelected(int itemPosition, long itemId) {
191 int i = itemPosition;
192 while (i-- != 0) {
193 onBackPressed();
194 }
195 return true;
196 }
197
198 @Override
199 public void onBackPressed() {
200 popPath();
201 if (mDirectories.getCount() == 0) {
202 super.onBackPressed();
203 return;
204 }
205 ((FileList) getSupportFragmentManager().findFragmentById(R.id.fileList))
206 .onBackPressed();
207 }
208
209 private class DirectoryCreator implements Runnable {
210 private String mTargetPath;
211 private Account mAccount;
212 private AccountManager mAm;
213
214 public DirectoryCreator(String targetPath, Account account) {
215 mTargetPath = targetPath;
216 mAccount = account;
217 mAm = (AccountManager) getSystemService(ACCOUNT_SERVICE);
218 }
219
220 @Override
221 public void run() {
222 WebdavClient wdc = new WebdavClient(Uri.parse(mAm.getUserData(
223 mAccount, AccountAuthenticator.KEY_OC_URL)));
224
225 String username = mAccount.name.substring(0,
226 mAccount.name.lastIndexOf('@'));
227 String password = mAm.getPassword(mAccount);
228
229 wdc.setCredentials(username, password);
230 wdc.allowUnsignedCertificates();
231 wdc.createDirectory(mTargetPath);
232 }
233
234 }
235
236 // Custom array adapter to override text colors
237 private class CustomArrayAdapter<T> extends ArrayAdapter<T> {
238
239 public CustomArrayAdapter(FileDisplayActivity ctx,
240 int view) {
241 super(ctx, view);
242 }
243
244 public View getView(int position, View convertView,
245 ViewGroup parent) {
246 View v = super.getView(position, convertView, parent);
247
248 ((TextView) v).setTextColor(
249 getResources()
250 .getColorStateList(android.R.color.white));
251 return v;
252 }
253
254 public View getDropDownView(int position, View convertView,
255 ViewGroup parent) {
256 View v = super.getDropDownView(position, convertView,
257 parent);
258
259 ((TextView) v).setTextColor(getResources().getColorStateList(
260 android.R.color.white));
261
262 return v;
263 }
264
265
266
267 }
268 }