Make account selectable from preferences
[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.Dialog;
25 import android.content.DialogInterface;
26 import android.content.DialogInterface.OnCancelListener;
27 import android.content.Intent;
28 import android.os.Bundle;
29 import android.support.v4.app.ActionBar;
30 import android.support.v4.app.ActionBar.OnNavigationListener;
31 import android.support.v4.view.Menu;
32 import android.support.v4.view.MenuItem;
33 import android.view.MenuInflater;
34 import android.widget.ArrayAdapter;
35 import eu.alefzero.owncloud.R;
36 import eu.alefzero.owncloud.R.id;
37 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
38 import eu.alefzero.owncloud.ui.fragment.FileList;
39
40 /**
41 * Displays, what files the user has available in his ownCloud.
42 * @author Bartek Przybylski
43 *
44 */
45
46 public class FileDisplayActivity extends android.support.v4.app.FragmentActivity implements OnNavigationListener {
47 private ArrayAdapter<String> mDirectories;
48
49 private static final int DIALOG_CHOOSE_ACCOUNT = 0;
50
51 public void pushPath(String path) {
52 mDirectories.insert(path, 0);
53 }
54
55 public boolean popPath() {
56 mDirectories.remove(mDirectories.getItem(0));
57 return !mDirectories.isEmpty();
58 }
59
60 @Override
61 public void onCreate(Bundle savedInstanceState) {
62 super.onCreate(savedInstanceState);
63 mDirectories = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item);
64 mDirectories.add("/");
65 setContentView(R.layout.files);
66 ActionBar action_bar = getSupportActionBar();
67 action_bar.setNavigationMode(android.support.v4.app.ActionBar.NAVIGATION_MODE_LIST);
68 action_bar.setDisplayShowTitleEnabled(false);
69 action_bar.setListNavigationCallbacks(mDirectories, this);
70 }
71
72 @Override
73 public boolean onOptionsItemSelected(MenuItem item) {
74 switch (item.getItemId()) {
75 case R.id.settingsItem :
76 Intent i = new Intent(this, Preferences.class);
77 startActivity(i);
78 break;
79 }
80 return true;
81 }
82
83 @Override
84 protected Dialog onCreateDialog(int id) {
85 switch (id) {
86 case DIALOG_CHOOSE_ACCOUNT:
87 return createChooseAccountDialog();
88 default:
89 throw new IllegalArgumentException("Unknown dialog id: " + id);
90 }
91 }
92
93 @Override
94 public boolean onCreateOptionsMenu(Menu menu) {
95 MenuInflater inflater = getMenuInflater();
96 inflater.inflate(R.menu.menu, menu);
97 return true;
98 }
99
100 private Dialog createChooseAccountDialog() {
101 final AccountManager accMan = AccountManager.get(this);
102 CharSequence[] items = new CharSequence[accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE).length];
103 int i = 0;
104 for (Account a : accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)) {
105 items[i++] = a.name;
106 }
107
108 AlertDialog.Builder builder = new AlertDialog.Builder(this);
109 builder.setTitle(R.string.common_choose_account);
110 builder.setCancelable(true);
111 builder.setItems(items, new DialogInterface.OnClickListener() {
112 public void onClick(DialogInterface dialog, int item) {
113 //mAccount = accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[item];
114 dialog.dismiss();
115 }
116 });
117 builder.setOnCancelListener(new OnCancelListener() {
118 public void onCancel(DialogInterface dialog) {
119 FileDisplayActivity.this.finish();
120 }
121 });
122 AlertDialog alert = builder.create();
123 return alert;
124 }
125
126 @Override
127 public boolean onNavigationItemSelected(int itemPosition, long itemId) {
128 int i = itemPosition;
129 while (i-- != 0) {
130 onBackPressed();
131 }
132 return true;
133 }
134
135 @Override
136 public void onBackPressed() {
137 popPath();
138 if (mDirectories.getCount() == 0)
139 {
140 super.onBackPressed();
141 return;
142 }
143 ((FileList)getSupportFragmentManager().findFragmentById(id.file_list_container)).onBackPressed();
144 }
145 }