Variable nameing / imports improved, fixed nullpointer on account
[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.BroadcastReceiver;
27 import android.content.ContentResolver;
28 import android.content.Context;
29 import android.content.DialogInterface;
30 import android.content.DialogInterface.OnClickListener;
31 import android.content.Intent;
32 import android.content.IntentFilter;
33 import android.net.Uri;
34 import android.os.Bundle;
35 import android.util.Log;
36 import android.view.View;
37 import android.view.ViewGroup;
38 import android.widget.ArrayAdapter;
39 import android.widget.EditText;
40 import android.widget.TextView;
41
42 import com.actionbarsherlock.app.ActionBar;
43 import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
44 import com.actionbarsherlock.app.SherlockFragmentActivity;
45 import com.actionbarsherlock.view.Menu;
46 import com.actionbarsherlock.view.MenuInflater;
47 import com.actionbarsherlock.view.MenuItem;
48 import com.actionbarsherlock.view.Window;
49
50 import eu.alefzero.owncloud.AccountUtils;
51 import eu.alefzero.owncloud.R;
52 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
53 import eu.alefzero.owncloud.datamodel.DataStorageManager;
54 import eu.alefzero.owncloud.datamodel.FileDataStorageManager;
55 import eu.alefzero.owncloud.datamodel.OCFile;
56 import eu.alefzero.owncloud.syncadapter.FileSyncService;
57 import eu.alefzero.owncloud.ui.fragment.FileListFragment;
58 import eu.alefzero.webdav.WebdavClient;
59
60 /**
61 * Displays, what files the user has available in his ownCloud.
62 *
63 * @author Bartek Przybylski
64 *
65 */
66
67 public class FileDisplayActivity extends SherlockFragmentActivity implements
68 OnNavigationListener, OnClickListener {
69 private ArrayAdapter<String> mDirectories;
70 private DataStorageManager mStorageManager;
71
72 private SyncBroadcastReceiver syncBroadcastRevceiver;
73
74 private static final int DIALOG_SETUP_ACCOUNT = 0;
75 private static final int DIALOG_CREATE_DIR = 1;
76
77 public void pushPath(String path) {
78 mDirectories.insert(path, 0);
79 }
80
81 public boolean popPath() {
82 mDirectories.remove(mDirectories.getItem(0));
83 return !mDirectories.isEmpty();
84 }
85
86 @Override
87 protected Dialog onCreateDialog(int id) {
88 Dialog dialog;
89 AlertDialog.Builder builder;
90 switch(id){
91 case DIALOG_SETUP_ACCOUNT:
92 builder = new AlertDialog.Builder(this);
93 builder.setTitle(R.string.main_tit_accsetup);
94 builder.setMessage(R.string.main_wrn_accsetup);
95 builder.setCancelable(false);
96 builder.setPositiveButton(R.string.common_ok, this);
97 builder.setNegativeButton(R.string.common_cancel, this);
98 dialog = builder.create();
99 break;
100 case DIALOG_CREATE_DIR:
101 {
102 builder = new Builder(this);
103 final EditText dirName = new EditText(getBaseContext());
104 final Account a = AccountUtils.getCurrentOwnCloudAccount(this);
105 builder.setView(dirName);
106 builder.setTitle(R.string.uploader_info_dirname);
107 dirName.setTextColor(R.color.setup_text_typed);
108
109 builder.setPositiveButton(R.string.common_ok, new OnClickListener() {
110 public void onClick(DialogInterface dialog, int which) {
111 String s = dirName.getText().toString();
112 if (s.trim().isEmpty()) {
113 dialog.cancel();
114 return;
115 }
116
117 String path = "";
118 for (int i = mDirectories.getCount() - 2; i >= 0; --i) {
119 path += "/" + mDirectories.getItem(i);
120 }
121 OCFile parent = mStorageManager.getFileByPath(path + "/");
122 path += s + "/";
123 Thread thread = new Thread(new DirectoryCreator(path, a));
124 thread.start();
125
126 OCFile new_file = new OCFile(path);
127 new_file.setMimetype("DIR");
128 new_file.setParentId(parent.getParentId());
129 mStorageManager.saveFile(new_file);
130
131 dialog.dismiss();
132 }
133 });
134 builder.setNegativeButton(R.string.common_cancel,
135 new OnClickListener() {
136 public void onClick(DialogInterface dialog, int which) {
137 dialog.cancel();
138 }
139 });
140 }
141 default:
142 dialog = null;
143 }
144
145 return dialog;
146 }
147
148 @Override
149 public void onCreate(Bundle savedInstanceState) {
150 super.onCreate(savedInstanceState);
151 if(!accountsAreSetup()){
152 showDialog(DIALOG_SETUP_ACCOUNT);
153 return;
154 }
155
156 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
157
158 mDirectories = new CustomArrayAdapter<String>(this,
159 R.layout.sherlock_spinner_dropdown_item);
160 mDirectories.add("/");
161 setContentView(R.layout.files);
162 mStorageManager = new FileDataStorageManager(AccountUtils.getCurrentOwnCloudAccount(this), getContentResolver());
163 ActionBar action_bar = getSupportActionBar();
164 action_bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
165 action_bar.setDisplayShowTitleEnabled(false);
166 action_bar.setListNavigationCallbacks(mDirectories, this);
167 action_bar.setDisplayHomeAsUpEnabled(true);
168 }
169
170 @Override
171 public boolean onOptionsItemSelected(MenuItem item) {
172 switch (item.getItemId()) {
173 case R.id.settingsItem: {
174 Intent i = new Intent(this, Preferences.class);
175 startActivity(i);
176 break;
177 }
178 case R.id.createDirectoryItem: {
179 showDialog(DIALOG_CREATE_DIR);
180 break;
181 }
182 case R.id.startSync: {
183 Bundle bundle = new Bundle();
184 bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
185 ContentResolver.requestSync(AccountUtils.getCurrentOwnCloudAccount(this),
186 "org.owncloud",
187 bundle);
188 break;
189 }
190 case android.R.id.home: {
191 onBackPressed();
192 break;
193 }
194
195 }
196 return true;
197 }
198
199 @Override
200 public void onBackPressed(){
201 if(mDirectories.getCount() == 1) {
202 finish();
203 return;
204 }
205 popPath();
206 ((FileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList))
207 .onNavigateUp();
208 }
209
210 @Override
211 public boolean onCreateOptionsMenu(Menu menu) {
212 MenuInflater inflater = getSherlock().getMenuInflater();
213 inflater.inflate(R.menu.menu, menu);
214 return true;
215 }
216
217 @Override
218 protected void onRestoreInstanceState(Bundle savedInstanceState) {
219 super.onRestoreInstanceState(savedInstanceState);
220 // Check, if there are ownCloud accounts
221 if(!accountsAreSetup()){
222 showDialog(DIALOG_SETUP_ACCOUNT);
223 }
224 }
225
226 @Override
227 protected void onStart() {
228 super.onStart();
229 // Check, if there are ownCloud accounts
230 if(!accountsAreSetup()){
231 showDialog(DIALOG_SETUP_ACCOUNT);
232 }
233 }
234
235 @Override
236 protected void onResume() {
237 super.onResume();
238 if(!accountsAreSetup()){
239 showDialog(DIALOG_SETUP_ACCOUNT);
240 return;
241 }
242 IntentFilter syncMessageIntentFilter = new IntentFilter(FileSyncService.SYNC_MESSAGE);
243 syncBroadcastRevceiver = new SyncBroadcastReceiver();
244 registerReceiver(syncBroadcastRevceiver, syncMessageIntentFilter);
245 setProgressBarIndeterminateVisibility(false);
246 }
247
248 @Override
249 protected void onPause() {
250 super.onPause();
251 if(syncBroadcastRevceiver != null){
252 unregisterReceiver(syncBroadcastRevceiver);
253 }
254 }
255
256 @Override
257 public boolean onNavigationItemSelected(int itemPosition, long itemId) {
258 int i = itemPosition;
259 while (i-- != 0) {
260 onBackPressed();
261 }
262 return true;
263 }
264
265 private class DirectoryCreator implements Runnable {
266 private String mTargetPath;
267 private Account mAccount;
268 private AccountManager mAm;
269
270 public DirectoryCreator(String targetPath, Account account) {
271 mTargetPath = targetPath;
272 mAccount = account;
273 mAm = (AccountManager) getSystemService(ACCOUNT_SERVICE);
274 }
275
276 @Override
277 public void run() {
278 WebdavClient wdc = new WebdavClient(Uri.parse(mAm.getUserData(
279 mAccount, AccountAuthenticator.KEY_OC_URL)));
280
281 String username = mAccount.name.substring(0,
282 mAccount.name.lastIndexOf('@'));
283 String password = mAm.getPassword(mAccount);
284
285 wdc.setCredentials(username, password);
286 wdc.allowUnsignedCertificates();
287 wdc.createDirectory(mTargetPath);
288 }
289
290 }
291
292 // Custom array adapter to override text colors
293 private class CustomArrayAdapter<T> extends ArrayAdapter<T> {
294
295 public CustomArrayAdapter(FileDisplayActivity ctx,
296 int view) {
297 super(ctx, view);
298 }
299
300 public View getView(int position, View convertView,
301 ViewGroup parent) {
302 View v = super.getView(position, convertView, parent);
303
304 ((TextView) v).setTextColor(
305 getResources()
306 .getColorStateList(android.R.color.white));
307 return v;
308 }
309
310 public View getDropDownView(int position, View convertView,
311 ViewGroup parent) {
312 View v = super.getDropDownView(position, convertView,
313 parent);
314
315 ((TextView) v).setTextColor(getResources().getColorStateList(
316 android.R.color.white));
317
318 return v;
319 }
320
321
322
323 }
324
325 public void onClick(DialogInterface dialog, int which) {
326 // In any case - we won't need it anymore
327 dialog.dismiss();
328 switch(which){
329 case DialogInterface.BUTTON_POSITIVE:
330 Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
331 intent.putExtra("authorities",
332 new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });
333 startActivity(intent);
334 break;
335 case DialogInterface.BUTTON_NEGATIVE:
336 finish();
337 }
338
339 }
340
341 /**
342 * Checks, whether or not there are any ownCloud accounts
343 * setup.
344 *
345 * @return true, if there is at least one account.
346 */
347 private boolean accountsAreSetup() {
348 AccountManager accMan = AccountManager.get(this);
349 Account[] accounts = accMan
350 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
351 return accounts.length > 0;
352 }
353
354 private class SyncBroadcastReceiver extends BroadcastReceiver {
355 /**
356 * {@link BroadcastReceiver} to enable syncing feedback in UI
357 */
358 @Override
359 public void onReceive(Context context, Intent intent) {
360 boolean inProgress = intent.getBooleanExtra(FileSyncService.IN_PROGRESS, false);
361 String account_name = intent.getStringExtra(FileSyncService.ACCOUNT_NAME);
362 Log.d("FileDisplay", "sync of account " + account_name + " is in_progress: " + inProgress);
363 setProgressBarIndeterminateVisibility(inProgress);
364 if (!inProgress) {
365 FileListFragment fileListFramgent = (FileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);
366 if (fileListFramgent != null)
367 fileListFramgent.populateFileList();
368 }
369 }
370
371 }
372
373 }