2015-03-08 22:30:03 +00:00
|
|
|
|
/*
|
2015-05-27 08:48:27 +00:00
|
|
|
|
* Copyright © 2013 – 2015 Ricki Hirner (bitfire web engineering).
|
2014-12-20 19:21:46 +00:00
|
|
|
|
* 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
|
2015-03-08 22:30:03 +00:00
|
|
|
|
*/
|
2016-02-24 14:56:30 +00:00
|
|
|
|
package at.bitfire.davdroid;
|
2014-12-20 19:21:46 +00:00
|
|
|
|
|
|
|
|
|
import android.accounts.Account;
|
|
|
|
|
import android.accounts.AccountManager;
|
2015-10-16 01:27:56 +00:00
|
|
|
|
import android.annotation.TargetApi;
|
2015-10-15 13:36:55 +00:00
|
|
|
|
import android.app.Notification;
|
|
|
|
|
import android.app.NotificationManager;
|
|
|
|
|
import android.content.ContentProviderClient;
|
2014-12-20 19:21:46 +00:00
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
|
import android.content.ContentUris;
|
|
|
|
|
import android.content.ContentValues;
|
|
|
|
|
import android.content.Context;
|
2015-03-08 22:30:03 +00:00
|
|
|
|
import android.content.PeriodicSync;
|
2014-12-20 19:21:46 +00:00
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.net.Uri;
|
2015-10-15 13:36:55 +00:00
|
|
|
|
import android.os.Build;
|
2014-12-20 19:21:46 +00:00
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.provider.CalendarContract;
|
|
|
|
|
import android.provider.CalendarContract.Calendars;
|
2015-10-15 13:36:55 +00:00
|
|
|
|
import android.provider.ContactsContract;
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.math.NumberUtils;
|
2014-12-21 01:19:06 +00:00
|
|
|
|
|
|
|
|
|
import java.net.URI;
|
|
|
|
|
import java.net.URISyntaxException;
|
2015-03-08 22:30:03 +00:00
|
|
|
|
import java.util.List;
|
2014-12-21 01:19:06 +00:00
|
|
|
|
|
2015-10-15 13:36:55 +00:00
|
|
|
|
import at.bitfire.davdroid.resource.LocalAddressBook;
|
|
|
|
|
import at.bitfire.vcard4android.ContactsStorageException;
|
2014-12-21 01:19:06 +00:00
|
|
|
|
import lombok.Cleanup;
|
2014-12-20 19:21:46 +00:00
|
|
|
|
|
|
|
|
|
public class AccountSettings {
|
2015-10-15 13:36:55 +00:00
|
|
|
|
private final static int CURRENT_VERSION = 2;
|
2014-12-20 19:21:46 +00:00
|
|
|
|
private final static String
|
|
|
|
|
KEY_SETTINGS_VERSION = "version",
|
2015-10-15 13:36:55 +00:00
|
|
|
|
|
2014-12-20 19:21:46 +00:00
|
|
|
|
KEY_USERNAME = "user_name",
|
|
|
|
|
KEY_AUTH_PREEMPTIVE = "auth_preemptive",
|
2015-10-15 13:36:55 +00:00
|
|
|
|
KEY_LAST_ANDROID_VERSION = "last_android_version";
|
2015-03-08 22:30:03 +00:00
|
|
|
|
|
|
|
|
|
public final static long SYNC_INTERVAL_MANUALLY = -1;
|
|
|
|
|
|
2015-06-14 17:24:40 +00:00
|
|
|
|
final Context context;
|
|
|
|
|
final AccountManager accountManager;
|
|
|
|
|
final Account account;
|
2014-12-20 19:21:46 +00:00
|
|
|
|
|
|
|
|
|
|
2015-10-15 13:36:55 +00:00
|
|
|
|
public AccountSettings(Context context, Account account) {
|
2014-12-20 19:21:46 +00:00
|
|
|
|
this.context = context;
|
|
|
|
|
this.account = account;
|
|
|
|
|
|
|
|
|
|
accountManager = AccountManager.get(context);
|
|
|
|
|
|
|
|
|
|
synchronized(AccountSettings.class) {
|
|
|
|
|
int version = 0;
|
|
|
|
|
try {
|
|
|
|
|
version = Integer.parseInt(accountManager.getUserData(account, KEY_SETTINGS_VERSION));
|
|
|
|
|
} catch(NumberFormatException e) {
|
|
|
|
|
}
|
2015-10-15 13:36:55 +00:00
|
|
|
|
Constants.log.info("AccountSettings version: v" + version + ", should be: " + version);
|
|
|
|
|
|
|
|
|
|
if (version < CURRENT_VERSION) {
|
|
|
|
|
showNotification(Constants.NOTIFICATION_ACCOUNT_SETTINGS_UPDATED,
|
|
|
|
|
context.getString(R.string.settings_version_update_title),
|
|
|
|
|
context.getString(R.string.settings_version_update_description));
|
|
|
|
|
update(version);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check whether Android version has changed
|
2015-10-16 01:27:56 +00:00
|
|
|
|
String lastAndroidVersionInt = accountManager.getUserData(account, KEY_LAST_ANDROID_VERSION);
|
|
|
|
|
if (lastAndroidVersionInt != null && NumberUtils.toInt(lastAndroidVersionInt) < Build.VERSION.SDK_INT) {
|
2015-10-15 13:36:55 +00:00
|
|
|
|
// notify user
|
|
|
|
|
showNotification(Constants.NOTIFICATION_ANDROID_VERSION_UPDATED,
|
|
|
|
|
context.getString(R.string.settings_android_update_title),
|
|
|
|
|
context.getString(R.string.settings_android_update_description));
|
|
|
|
|
}
|
2015-10-18 14:20:26 +00:00
|
|
|
|
accountManager.setUserData(account, KEY_LAST_ANDROID_VERSION, String.valueOf(Build.VERSION.SDK_INT));
|
2014-12-20 19:21:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-15 13:36:55 +00:00
|
|
|
|
|
2015-10-18 22:04:58 +00:00
|
|
|
|
@TargetApi(21)
|
2015-10-15 13:36:55 +00:00
|
|
|
|
protected void showNotification(int id, String title, String message) {
|
|
|
|
|
NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
Notification.Builder n = new Notification.Builder(context);
|
|
|
|
|
if (Build.VERSION.SDK_INT >= 16) {
|
|
|
|
|
n.setPriority(Notification.PRIORITY_HIGH);
|
|
|
|
|
n.setStyle(new Notification.BigTextStyle().bigText(message));
|
|
|
|
|
} if (Build.VERSION.SDK_INT >= 20)
|
|
|
|
|
n.setLocalOnly(true);
|
|
|
|
|
if (Build.VERSION.SDK_INT >= 21)
|
|
|
|
|
n.setCategory(Notification.CATEGORY_SYSTEM);
|
|
|
|
|
n.setSmallIcon(R.drawable.ic_launcher);
|
|
|
|
|
n.setContentTitle(title);
|
|
|
|
|
n.setContentText(message);
|
|
|
|
|
nm.notify(id, Build.VERSION.SDK_INT >= 16 ? n.build() : n.getNotification());
|
|
|
|
|
}
|
2014-12-20 19:21:46 +00:00
|
|
|
|
|
|
|
|
|
|
2016-01-17 16:10:30 +00:00
|
|
|
|
public static Bundle initialUserData(String userName, boolean preemptive) {
|
2014-12-20 19:21:46 +00:00
|
|
|
|
Bundle bundle = new Bundle();
|
|
|
|
|
bundle.putString(KEY_SETTINGS_VERSION, String.valueOf(CURRENT_VERSION));
|
2016-01-17 16:10:30 +00:00
|
|
|
|
bundle.putString(KEY_USERNAME, userName);
|
|
|
|
|
bundle.putString(KEY_AUTH_PREEMPTIVE, Boolean.toString(preemptive));
|
2014-12-20 19:21:46 +00:00
|
|
|
|
return bundle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-03-08 22:30:03 +00:00
|
|
|
|
// authentication settings
|
|
|
|
|
|
2015-10-18 14:20:26 +00:00
|
|
|
|
public String username() { return accountManager.getUserData(account, KEY_USERNAME); }
|
|
|
|
|
public void username(String userName) { accountManager.setUserData(account, KEY_USERNAME, userName); }
|
2014-12-20 19:21:46 +00:00
|
|
|
|
|
2015-10-18 14:20:26 +00:00
|
|
|
|
public String password() { return accountManager.getPassword(account); }
|
|
|
|
|
public void password(String password) { accountManager.setPassword(account, password); }
|
2014-12-20 19:21:46 +00:00
|
|
|
|
|
2015-10-18 14:20:26 +00:00
|
|
|
|
public boolean preemptiveAuth() { return Boolean.parseBoolean(accountManager.getUserData(account, KEY_AUTH_PREEMPTIVE)); }
|
|
|
|
|
public void preemptiveAuth(boolean preemptive) { accountManager.setUserData(account, KEY_AUTH_PREEMPTIVE, Boolean.toString(preemptive)); }
|
|
|
|
|
|
|
|
|
|
|
2015-03-08 22:30:03 +00:00
|
|
|
|
// sync. settings
|
|
|
|
|
|
2015-05-25 17:54:16 +00:00
|
|
|
|
public Long getSyncInterval(String authority) {
|
|
|
|
|
if (ContentResolver.getIsSyncable(account, authority) <= 0)
|
2015-03-08 22:30:03 +00:00
|
|
|
|
return null;
|
|
|
|
|
|
2015-05-25 17:54:16 +00:00
|
|
|
|
if (ContentResolver.getSyncAutomatically(account, authority)) {
|
|
|
|
|
List<PeriodicSync> syncs = ContentResolver.getPeriodicSyncs(account, authority);
|
2015-03-08 22:30:03 +00:00
|
|
|
|
if (syncs.isEmpty())
|
|
|
|
|
return SYNC_INTERVAL_MANUALLY;
|
|
|
|
|
else
|
|
|
|
|
return syncs.get(0).period;
|
|
|
|
|
} else
|
|
|
|
|
return SYNC_INTERVAL_MANUALLY;
|
2014-12-20 19:21:46 +00:00
|
|
|
|
}
|
2015-03-08 22:30:03 +00:00
|
|
|
|
|
2015-05-25 17:54:16 +00:00
|
|
|
|
public void setSyncInterval(String authority, long seconds) {
|
2015-03-08 22:30:03 +00:00
|
|
|
|
if (seconds == SYNC_INTERVAL_MANUALLY) {
|
2015-05-25 17:54:16 +00:00
|
|
|
|
ContentResolver.setSyncAutomatically(account, authority, false);
|
2015-03-08 22:30:03 +00:00
|
|
|
|
} else {
|
2015-05-25 17:54:16 +00:00
|
|
|
|
ContentResolver.setSyncAutomatically(account, authority, true);
|
|
|
|
|
ContentResolver.addPeriodicSync(account, authority, new Bundle(), seconds);
|
2015-03-08 22:30:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-20 19:21:46 +00:00
|
|
|
|
|
|
|
|
|
// update from previous account settings
|
|
|
|
|
|
|
|
|
|
private void update(int fromVersion) {
|
2015-10-15 13:36:55 +00:00
|
|
|
|
for (int toVersion = fromVersion + 1; toVersion <= CURRENT_VERSION; toVersion++)
|
|
|
|
|
updateTo(toVersion);
|
2014-12-20 19:21:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-15 13:36:55 +00:00
|
|
|
|
private void updateTo(int toVersion) {
|
|
|
|
|
final int fromVersion = toVersion - 1;
|
|
|
|
|
Constants.log.info("Updating account settings from v" + fromVersion + " to " + toVersion);
|
2014-12-20 19:21:46 +00:00
|
|
|
|
try {
|
2015-10-15 13:36:55 +00:00
|
|
|
|
switch (toVersion) {
|
|
|
|
|
case 1:
|
|
|
|
|
update_0_1();
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
update_1_2();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
Constants.log.error("Don't know how to update settings from v" + fromVersion + " to v" + toVersion);
|
|
|
|
|
}
|
2014-12-20 19:21:46 +00:00
|
|
|
|
} catch(Exception e) {
|
2015-10-15 13:36:55 +00:00
|
|
|
|
Constants.log.error("Couldn't update account settings (DAVdroid will probably crash)!", e);
|
2014-12-20 19:21:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-18 22:04:58 +00:00
|
|
|
|
|
|
|
|
|
@SuppressWarnings("Recycle")
|
2014-12-20 19:21:46 +00:00
|
|
|
|
private void update_0_1() throws URISyntaxException {
|
|
|
|
|
String v0_principalURL = accountManager.getUserData(account, "principal_url"),
|
|
|
|
|
v0_addressBookPath = accountManager.getUserData(account, "addressbook_path");
|
2015-10-15 13:36:55 +00:00
|
|
|
|
Constants.log.debug("Old principal URL = " + v0_principalURL);
|
|
|
|
|
Constants.log.debug("Old address book path = " + v0_addressBookPath);
|
2014-12-20 19:21:46 +00:00
|
|
|
|
|
|
|
|
|
URI principalURI = new URI(v0_principalURL);
|
|
|
|
|
|
|
|
|
|
// update address book
|
|
|
|
|
if (v0_addressBookPath != null) {
|
|
|
|
|
String addressBookURL = principalURI.resolve(v0_addressBookPath).toASCIIString();
|
2015-10-15 13:36:55 +00:00
|
|
|
|
Constants.log.debug("New address book URL = " + addressBookURL);
|
2014-12-20 19:21:46 +00:00
|
|
|
|
accountManager.setUserData(account, "addressbook_url", addressBookURL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// update calendars
|
|
|
|
|
ContentResolver resolver = context.getContentResolver();
|
|
|
|
|
Uri calendars = Calendars.CONTENT_URI.buildUpon()
|
|
|
|
|
.appendQueryParameter(Calendars.ACCOUNT_NAME, account.name)
|
|
|
|
|
.appendQueryParameter(Calendars.ACCOUNT_TYPE, account.type)
|
|
|
|
|
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true").build();
|
|
|
|
|
@Cleanup Cursor cursor = resolver.query(calendars, new String[] { Calendars._ID, Calendars.NAME }, null, null, null);
|
|
|
|
|
while (cursor != null && cursor.moveToNext()) {
|
|
|
|
|
int id = cursor.getInt(0);
|
|
|
|
|
String v0_path = cursor.getString(1),
|
|
|
|
|
v1_url = principalURI.resolve(v0_path).toASCIIString();
|
2015-10-15 13:36:55 +00:00
|
|
|
|
Constants.log.debug("Updating calendar #" + id + " name: " + v0_path + " -> " + v1_url);
|
2014-12-20 19:21:46 +00:00
|
|
|
|
Uri calendar = ContentUris.appendId(Calendars.CONTENT_URI.buildUpon()
|
|
|
|
|
.appendQueryParameter(Calendars.ACCOUNT_NAME, account.name)
|
|
|
|
|
.appendQueryParameter(Calendars.ACCOUNT_TYPE, account.type)
|
|
|
|
|
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true"), id).build();
|
|
|
|
|
ContentValues newValues = new ContentValues(1);
|
|
|
|
|
newValues.put(Calendars.NAME, v1_url);
|
|
|
|
|
if (resolver.update(calendar, newValues, null, null) != 1)
|
2015-10-15 13:36:55 +00:00
|
|
|
|
Constants.log.debug("Number of modified calendars != 1");
|
2014-12-20 19:21:46 +00:00
|
|
|
|
}
|
2015-10-15 13:36:55 +00:00
|
|
|
|
|
2014-12-20 19:21:46 +00:00
|
|
|
|
accountManager.setUserData(account, "principal_url", null);
|
|
|
|
|
accountManager.setUserData(account, "addressbook_path", null);
|
2015-10-15 13:36:55 +00:00
|
|
|
|
|
2014-12-20 19:21:46 +00:00
|
|
|
|
accountManager.setUserData(account, KEY_SETTINGS_VERSION, "1");
|
|
|
|
|
}
|
2015-10-15 13:36:55 +00:00
|
|
|
|
|
2015-10-18 22:04:58 +00:00
|
|
|
|
@SuppressWarnings("Recycle")
|
2015-10-15 13:36:55 +00:00
|
|
|
|
private void update_1_2() throws ContactsStorageException {
|
2015-10-18 14:20:26 +00:00
|
|
|
|
/* - KEY_ADDRESSBOOK_URL ("addressbook_url"),
|
2015-10-15 13:36:55 +00:00
|
|
|
|
- KEY_ADDRESSBOOK_CTAG ("addressbook_ctag"),
|
|
|
|
|
- KEY_ADDRESSBOOK_VCARD_VERSION ("addressbook_vcard_version") are not used anymore (now stored in ContactsContract.SyncState)
|
|
|
|
|
- KEY_LAST_ANDROID_VERSION ("last_android_version") has been added
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// move previous address book info to ContactsContract.SyncState
|
|
|
|
|
@Cleanup("release") ContentProviderClient provider = context.getContentResolver().acquireContentProviderClient(ContactsContract.AUTHORITY);
|
2015-10-18 14:20:26 +00:00
|
|
|
|
if (provider == null)
|
|
|
|
|
throw new ContactsStorageException("Couldn't access Contacts provider");
|
|
|
|
|
|
|
|
|
|
LocalAddressBook addr = new LocalAddressBook(account, provider);
|
|
|
|
|
|
|
|
|
|
// until now, ContactsContract.Settings.UNGROUPED_VISIBLE was not set explicitly
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
values.put(ContactsContract.Settings.UNGROUPED_VISIBLE, 1);
|
|
|
|
|
addr.updateSettings(values);
|
|
|
|
|
|
|
|
|
|
String url = accountManager.getUserData(account, "addressbook_url");
|
|
|
|
|
if (!TextUtils.isEmpty(url))
|
|
|
|
|
addr.setURL(url);
|
|
|
|
|
accountManager.setUserData(account, "addressbook_url", null);
|
|
|
|
|
|
|
|
|
|
String cTag = accountManager.getUserData(account, "addressbook_ctag");
|
|
|
|
|
if (!TextUtils.isEmpty(cTag))
|
|
|
|
|
addr.setCTag(cTag);
|
|
|
|
|
accountManager.setUserData(account, "addressbook_ctag", null);
|
2015-10-15 13:36:55 +00:00
|
|
|
|
|
|
|
|
|
accountManager.setUserData(account, KEY_SETTINGS_VERSION, "2");
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-20 19:21:46 +00:00
|
|
|
|
}
|