sync progress indicator
[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.Context;
27 import android.content.DialogInterface;
28 import android.content.DialogInterface.OnCancelListener;
29 import android.content.DialogInterface.OnClickListener;
30 import android.content.BroadcastReceiver;
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.FileSyncAdapter;
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 BR b;
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 android.R.id.home: {
183 onBackPressed();
184 break;
185 }
186
187 }
188 return true;
189 }
190
191 @Override
192 public void onBackPressed(){
193 if(mDirectories.getCount() == 1) {
194 finish();
195 return;
196 }
197 popPath();
198 ((FileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList))
199 .onNavigateUp();
200 }
201
202 @Override
203 public boolean onCreateOptionsMenu(Menu menu) {
204 MenuInflater inflater = getSherlock().getMenuInflater();
205 inflater.inflate(R.menu.menu, menu);
206 return true;
207 }
208
209 @Override
210 protected void onRestoreInstanceState(Bundle savedInstanceState) {
211 super.onRestoreInstanceState(savedInstanceState);
212 // Check, if there are ownCloud accounts
213 if(!accountsAreSetup()){
214 showDialog(DIALOG_SETUP_ACCOUNT);
215 }
216 }
217
218 @Override
219 protected void onStart() {
220 super.onStart();
221 // Check, if there are ownCloud accounts
222 if(!accountsAreSetup()){
223 showDialog(DIALOG_SETUP_ACCOUNT);
224 }
225 }
226
227 @Override
228 protected void onResume() {
229 super.onResume();
230 if(!accountsAreSetup()){
231 showDialog(DIALOG_SETUP_ACCOUNT);
232 return;
233 }
234 IntentFilter f = new IntentFilter(FileSyncAdapter.SYNC_MESSAGE);
235 b = new BR();
236 registerReceiver(b, f);
237 setProgressBarIndeterminateVisibility(false);
238 }
239
240 @Override
241 protected void onPause() {
242 super.onPause();
243 unregisterReceiver(b);
244 }
245
246 @Override
247 public boolean onNavigationItemSelected(int itemPosition, long itemId) {
248 int i = itemPosition;
249 while (i-- != 0) {
250 onBackPressed();
251 }
252 return true;
253 }
254
255 private class DirectoryCreator implements Runnable {
256 private String mTargetPath;
257 private Account mAccount;
258 private AccountManager mAm;
259
260 public DirectoryCreator(String targetPath, Account account) {
261 mTargetPath = targetPath;
262 mAccount = account;
263 mAm = (AccountManager) getSystemService(ACCOUNT_SERVICE);
264 }
265
266 @Override
267 public void run() {
268 WebdavClient wdc = new WebdavClient(Uri.parse(mAm.getUserData(
269 mAccount, AccountAuthenticator.KEY_OC_URL)));
270
271 String username = mAccount.name.substring(0,
272 mAccount.name.lastIndexOf('@'));
273 String password = mAm.getPassword(mAccount);
274
275 wdc.setCredentials(username, password);
276 wdc.allowUnsignedCertificates();
277 wdc.createDirectory(mTargetPath);
278 }
279
280 }
281
282 // Custom array adapter to override text colors
283 private class CustomArrayAdapter<T> extends ArrayAdapter<T> {
284
285 public CustomArrayAdapter(FileDisplayActivity ctx,
286 int view) {
287 super(ctx, view);
288 }
289
290 public View getView(int position, View convertView,
291 ViewGroup parent) {
292 View v = super.getView(position, convertView, parent);
293
294 ((TextView) v).setTextColor(
295 getResources()
296 .getColorStateList(android.R.color.white));
297 return v;
298 }
299
300 public View getDropDownView(int position, View convertView,
301 ViewGroup parent) {
302 View v = super.getDropDownView(position, convertView,
303 parent);
304
305 ((TextView) v).setTextColor(getResources().getColorStateList(
306 android.R.color.white));
307
308 return v;
309 }
310
311
312
313 }
314
315 public void onClick(DialogInterface dialog, int which) {
316 // In any case - we won't need it anymore
317 dialog.dismiss();
318 switch(which){
319 case DialogInterface.BUTTON_POSITIVE:
320 Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
321 intent.putExtra("authorities",
322 new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });
323 startActivity(intent);
324 break;
325 case DialogInterface.BUTTON_NEGATIVE:
326 finish();
327 }
328
329 }
330
331 /**
332 * Checks, whether or not there are any ownCloud accounts
333 * setup.
334 *
335 * @return true, if there is at least one account.
336 */
337 private boolean accountsAreSetup() {
338 AccountManager accMan = AccountManager.get(this);
339 Account[] accounts = accMan
340 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
341 return accounts.length > 0;
342 }
343
344 private class BR extends BroadcastReceiver {
345 @Override
346 public void onReceive(Context context, Intent intent) {
347 boolean in_progress = intent.getBooleanExtra(FileSyncAdapter.IN_PROGRESS, false);
348 String account_name = intent.getStringExtra(FileSyncAdapter.ACCOUNT_NAME);
349 Log.d("FileDisplay", "sync of account " + account_name + " is in_progress: " + in_progress);
350 setProgressBarIndeterminateVisibility(in_progress);
351 if (!in_progress) {
352 FileListFragment f = (FileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);
353 if (f != null)
354 f.populateFileList();
355 }
356 }
357
358 }
359
360 }