2016-02-22 13:33:55 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright © 2013 – 2016 Ricki Hirner (bitfire web engineering).
|
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
|
* are made available under the terms of the GNU Public License v3.0
|
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
|
* http://www.gnu.org/licenses/gpl.html
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package at.bitfire.davdroid.ui;
|
|
|
|
|
|
|
|
|
|
import android.accounts.Account;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.support.v4.app.LoaderManager;
|
2016-02-23 17:42:50 +00:00
|
|
|
|
import android.support.v4.app.NavUtils;
|
2016-02-22 13:33:55 +00:00
|
|
|
|
import android.support.v4.content.AsyncTaskLoader;
|
|
|
|
|
import android.support.v4.content.Loader;
|
|
|
|
|
import android.support.v7.app.AppCompatActivity;
|
2016-02-23 17:42:50 +00:00
|
|
|
|
import android.text.TextUtils;
|
2016-02-22 13:33:55 +00:00
|
|
|
|
import android.view.Menu;
|
|
|
|
|
import android.view.MenuItem;
|
2016-03-18 14:40:05 +00:00
|
|
|
|
import android.widget.ArrayAdapter;
|
2016-02-23 17:42:50 +00:00
|
|
|
|
import android.widget.EditText;
|
2016-02-22 13:33:55 +00:00
|
|
|
|
import android.widget.Spinner;
|
|
|
|
|
|
2016-03-18 14:40:05 +00:00
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2016-02-22 13:33:55 +00:00
|
|
|
|
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
import java.util.List;
|
2016-03-18 14:40:05 +00:00
|
|
|
|
import java.util.UUID;
|
2016-02-22 13:33:55 +00:00
|
|
|
|
|
|
|
|
|
import at.bitfire.davdroid.R;
|
|
|
|
|
import at.bitfire.davdroid.model.CollectionInfo;
|
|
|
|
|
import at.bitfire.davdroid.model.ServiceDB;
|
|
|
|
|
import lombok.Cleanup;
|
|
|
|
|
import okhttp3.HttpUrl;
|
|
|
|
|
|
|
|
|
|
public class CreateAddressBookActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<CreateAddressBookActivity.AccountInfo> {
|
2016-03-18 14:40:05 +00:00
|
|
|
|
public static final String EXTRA_ACCOUNT = "account";
|
2016-02-22 13:33:55 +00:00
|
|
|
|
|
2016-03-18 14:40:05 +00:00
|
|
|
|
protected Account account;
|
2016-02-22 13:33:55 +00:00
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
|
|
account = getIntent().getParcelableExtra(EXTRA_ACCOUNT);
|
2016-03-18 14:40:05 +00:00
|
|
|
|
|
2016-02-22 13:33:55 +00:00
|
|
|
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
2016-03-18 14:40:05 +00:00
|
|
|
|
setContentView(R.layout.activity_create_address_book);
|
2016-02-22 13:33:55 +00:00
|
|
|
|
|
|
|
|
|
getSupportLoaderManager().initLoader(0, getIntent().getExtras(), this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
2016-03-18 14:40:05 +00:00
|
|
|
|
getMenuInflater().inflate(R.menu.activity_create_collection, menu);
|
2016-02-22 13:33:55 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
2016-03-18 14:40:05 +00:00
|
|
|
|
if (item.getItemId() == android.R.id.home) {
|
|
|
|
|
Intent intent = new Intent(this, AccountActivity.class);
|
|
|
|
|
intent.putExtra(AccountActivity.EXTRA_ACCOUNT, account);
|
|
|
|
|
NavUtils.navigateUpTo(this, intent);
|
|
|
|
|
return true;
|
2016-02-22 13:33:55 +00:00
|
|
|
|
}
|
2016-03-18 14:40:05 +00:00
|
|
|
|
return false;
|
2016-02-22 13:33:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-18 14:40:05 +00:00
|
|
|
|
public void onCreateCollection(MenuItem item) {
|
2016-02-23 17:42:50 +00:00
|
|
|
|
boolean ok = true;
|
2016-03-18 14:40:05 +00:00
|
|
|
|
CollectionInfo info = new CollectionInfo();
|
2016-02-23 17:42:50 +00:00
|
|
|
|
|
2016-03-18 14:40:05 +00:00
|
|
|
|
Spinner spinner = (Spinner)findViewById(R.id.home_sets);
|
|
|
|
|
String homeSet = (String)spinner.getSelectedItem();
|
2016-02-23 17:42:50 +00:00
|
|
|
|
|
2016-03-18 14:40:05 +00:00
|
|
|
|
EditText edit = (EditText)findViewById(R.id.display_name);
|
|
|
|
|
info.displayName = edit.getText().toString();
|
|
|
|
|
if (TextUtils.isEmpty(info.displayName)) {
|
|
|
|
|
edit.setError(getString(R.string.create_collection_display_name_required));
|
2016-02-23 17:42:50 +00:00
|
|
|
|
ok = false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-18 14:40:05 +00:00
|
|
|
|
edit = (EditText)findViewById(R.id.description);
|
|
|
|
|
info.description = StringUtils.trimToNull(edit.getText().toString());
|
2016-02-23 17:42:50 +00:00
|
|
|
|
|
2016-03-18 14:40:05 +00:00
|
|
|
|
if (ok) {
|
|
|
|
|
info.type = CollectionInfo.Type.ADDRESS_BOOK;
|
|
|
|
|
info.url = HttpUrl.parse(homeSet).resolve(UUID.randomUUID().toString() + "/").toString();
|
|
|
|
|
CreateCollectionFragment.newInstance(account, info).show(getSupportFragmentManager(), null);
|
2016-02-22 13:33:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Loader<AccountInfo> onCreateLoader(int id, Bundle args) {
|
2016-03-18 14:40:05 +00:00
|
|
|
|
return new AccountInfoLoader(this, account);
|
2016-02-22 13:33:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2016-03-18 14:40:05 +00:00
|
|
|
|
public void onLoadFinished(Loader<AccountInfo> loader, AccountInfo info) {
|
|
|
|
|
if (info != null) {
|
|
|
|
|
Spinner spinner = (Spinner)findViewById(R.id.home_sets);
|
|
|
|
|
spinner.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, info.homeSets));
|
2016-02-22 13:33:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onLoaderReset(Loader<AccountInfo> loader) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static class AccountInfo {
|
2016-03-18 14:40:05 +00:00
|
|
|
|
List<String> homeSets = new LinkedList<>();
|
2016-02-22 13:33:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-18 14:40:05 +00:00
|
|
|
|
protected static class AccountInfoLoader extends AsyncTaskLoader<AccountInfo> {
|
2016-02-22 13:33:55 +00:00
|
|
|
|
private final Account account;
|
2016-02-23 17:42:50 +00:00
|
|
|
|
private final ServiceDB.OpenHelper dbHelper;
|
2016-02-22 13:33:55 +00:00
|
|
|
|
|
2016-03-18 14:40:05 +00:00
|
|
|
|
public AccountInfoLoader(Context context, Account account) {
|
2016-02-22 13:33:55 +00:00
|
|
|
|
super(context);
|
|
|
|
|
this.account = account;
|
|
|
|
|
dbHelper = new ServiceDB.OpenHelper(context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onStartLoading() {
|
|
|
|
|
forceLoad();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public AccountInfo loadInBackground() {
|
2016-03-18 14:40:05 +00:00
|
|
|
|
final AccountInfo info = new AccountInfo();
|
|
|
|
|
|
|
|
|
|
// find DAV service and home sets
|
2016-02-22 13:33:55 +00:00
|
|
|
|
SQLiteDatabase db = dbHelper.getReadableDatabase();
|
|
|
|
|
try {
|
2016-03-18 14:40:05 +00:00
|
|
|
|
@Cleanup Cursor cursorService = db.query(ServiceDB.Services._TABLE, new String[] { ServiceDB.Services.ID },
|
|
|
|
|
ServiceDB.Services.ACCOUNT_NAME + "=? AND " + ServiceDB.Services.SERVICE + "=?",
|
2016-02-22 13:33:55 +00:00
|
|
|
|
new String[] { account.name, ServiceDB.Services.SERVICE_CARDDAV }, null, null, null);
|
|
|
|
|
if (!cursorService.moveToNext())
|
|
|
|
|
return null;
|
2016-03-18 14:40:05 +00:00
|
|
|
|
String strServiceID = cursorService.getString(0);
|
2016-02-22 13:33:55 +00:00
|
|
|
|
|
2016-03-18 14:40:05 +00:00
|
|
|
|
@Cleanup Cursor cursorHomeSets = db.query(ServiceDB.HomeSets._TABLE, new String[] { ServiceDB.HomeSets.URL },
|
|
|
|
|
ServiceDB.HomeSets.SERVICE_ID + "=?", new String[] { strServiceID }, null, null, null);
|
|
|
|
|
while (cursorHomeSets.moveToNext())
|
|
|
|
|
info.homeSets.add(cursorHomeSets.getString(0));
|
2016-02-22 13:33:55 +00:00
|
|
|
|
} finally {
|
|
|
|
|
dbHelper.close();
|
|
|
|
|
}
|
2016-03-18 14:40:05 +00:00
|
|
|
|
|
|
|
|
|
return info;
|
2016-02-22 13:33:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|