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