Kotlin: more kotlin migration.

tmp
Tom Hacohen 5 years ago
parent df47f7bea6
commit 093eee6409

@ -228,7 +228,7 @@ constructor(internal val context: Context, internal val account: Account) {
if (!accountManager.addAccountExplicitly(addressBookAccount, null, null))
throw ContactsStorageException("Couldn't create address book account")
LocalAddressBook.setUserData(accountManager, addressBookAccount, account, info.uid)
LocalAddressBook.setUserData(accountManager, addressBookAccount, account, info.uid!!)
val newAddressBook = LocalAddressBook(context, addressBookAccount, provider)
// move contacts to new address book

@ -1,116 +0,0 @@
/*
* 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 com.etesync.syncadapter.model;
import android.content.ContentValues;
import com.etesync.syncadapter.journalmanager.Constants;
import com.etesync.syncadapter.journalmanager.JournalManager;
import com.etesync.syncadapter.model.ServiceDB.Collections;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;
import java.io.Serializable;
import io.requery.Persistable;
import io.requery.sql.EntityDataStore;
public class CollectionInfo implements Serializable {
@Deprecated
public long id;
public int serviceID;
public enum Type {
ADDRESS_BOOK,
CALENDAR
}
// FIXME: Shouldn't be exposed, as it's already saved in the journal. We just expose it for when we save for db.
@Expose
public int version = -1;
@Expose
public Type type;
public String uid;
@Expose
public String displayName, description;
@Expose
public Integer color;
@Expose
public String timeZone;
@Expose
public boolean selected;
public CollectionInfo() {
version = Constants.CURRENT_VERSION;
}
public static CollectionInfo defaultForServiceType(Type service) {
CollectionInfo info = new CollectionInfo();
info.displayName = "Default";
info.selected = true;
info.type = service;
return info;
}
public void updateFromJournal(JournalManager.Journal journal) {
uid = journal.getUid();
version = journal.getVersion();
}
public boolean isOfTypeService(String service) {
return service.equals(type.toString());
}
public static CollectionInfo fromDB(ContentValues values) {
CollectionInfo info = new CollectionInfo();
info.id = values.getAsLong(Collections.ID);
info.serviceID = values.getAsInteger(Collections.SERVICE_ID);
info.uid = values.getAsString(Collections.URL);
info.displayName = values.getAsString(Collections.DISPLAY_NAME);
info.description = values.getAsString(Collections.DESCRIPTION);
info.color = values.getAsInteger(Collections.COLOR);
info.timeZone = values.getAsString(Collections.TIME_ZONE);
info.selected = values.getAsInteger(Collections.SYNC) != 0;
return info;
}
public ServiceEntity getServiceEntity(EntityDataStore<Persistable> data) {
return data.findByKey(ServiceEntity.class, serviceID);
}
public static CollectionInfo fromJson(String json) {
return new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().fromJson(json, CollectionInfo.class);
}
public String toJson() {
return new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().toJson(this, CollectionInfo.class);
}
private static Boolean getAsBooleanOrNull(ContentValues values, String field) {
Integer i = values.getAsInteger(field);
return (i == null) ? null : (i != 0);
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public java.lang.String toString() {
return "CollectionInfo(serviceID=" + this.serviceID + ", version=" + this.version + ", type=" + this.type + ", uid=" + this.uid + ", displayName=" + this.displayName + ", description=" + this.description + ", color=" + this.color + ", timeZone=" + this.timeZone + ", selected=" + this.selected + ")";
}
}

@ -0,0 +1,119 @@
/*
* 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 com.etesync.syncadapter.model
import android.content.ContentValues
import com.etesync.syncadapter.journalmanager.Constants
import com.etesync.syncadapter.journalmanager.JournalManager
import com.etesync.syncadapter.model.ServiceDB.Collections
import com.google.gson.GsonBuilder
import com.google.gson.annotations.Expose
import java.io.Serializable
import io.requery.Persistable
import io.requery.sql.EntityDataStore
class CollectionInfo : Serializable {
@Deprecated("")
var id: Long = 0
var serviceID: Int = 0
// FIXME: Shouldn't be exposed, as it's already saved in the journal. We just expose it for when we save for db.
@Expose
var version = -1
@Expose
var type: Type? = null
var uid: String? = null
@Expose
var displayName: String? = null
@Expose
var description: String? = null
@Expose
var color: Int? = null
@Expose
var timeZone: String? = null
@Expose
var selected: Boolean = false
enum class Type {
ADDRESS_BOOK,
CALENDAR
}
init {
version = Constants.CURRENT_VERSION
}
fun updateFromJournal(journal: JournalManager.Journal) {
uid = journal.uid!!
version = journal.version
}
fun isOfTypeService(service: String): Boolean {
return service == type.toString()
}
fun getServiceEntity(data: EntityDataStore<Persistable>): ServiceEntity {
return data.findByKey(ServiceEntity::class.java, serviceID)
}
fun toJson(): String {
return GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().toJson(this, CollectionInfo::class.java)
}
override fun toString(): String {
return "CollectionInfo(serviceID=" + this.serviceID + ", version=" + this.version + ", type=" + this.type + ", uid=" + this.uid + ", displayName=" + this.displayName + ", description=" + this.description + ", color=" + this.color + ", timeZone=" + this.timeZone + ", selected=" + this.selected + ")"
}
companion object {
fun defaultForServiceType(service: Type): CollectionInfo {
val info = CollectionInfo()
info.displayName = "Default"
info.selected = true
info.type = service
return info
}
fun fromDB(values: ContentValues): CollectionInfo {
val info = CollectionInfo()
info.id = values.getAsLong(Collections.ID)!!
info.serviceID = values.getAsInteger(Collections.SERVICE_ID)!!
info.uid = values.getAsString(Collections.URL)
info.displayName = values.getAsString(Collections.DISPLAY_NAME)
info.description = values.getAsString(Collections.DESCRIPTION)
info.color = values.getAsInteger(Collections.COLOR)
info.timeZone = values.getAsString(Collections.TIME_ZONE)
info.selected = values.getAsInteger(Collections.SYNC) != 0
return info
}
fun fromJson(json: String): CollectionInfo {
return GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().fromJson(json, CollectionInfo::class.java)
}
private fun getAsBooleanOrNull(values: ContentValues, field: String): Boolean? {
val i = values.getAsInteger(field)
return if (i == null) null else i != 0
}
}
}

@ -53,8 +53,8 @@ public class JournalModel {
@PostLoad
void afterLoad() {
this.info.serviceID = this.serviceModel.id;
this.info.uid = uid;
this.info.setServiceID(this.serviceModel.id);
this.info.setUid(uid);
}
public Journal() {
@ -64,7 +64,7 @@ public class JournalModel {
public Journal(EntityDataStore<Persistable> data, CollectionInfo info) {
this();
this.info = info;
this.uid = info.uid;
this.uid = info.getUid();
this.serviceModel = info.getServiceEntity(data);
}
@ -87,7 +87,7 @@ public class JournalModel {
}
public static JournalEntity fetchOrCreate(EntityDataStore<Persistable> data, CollectionInfo collection) {
JournalEntity journalEntity = fetch(data, collection.getServiceEntity(data), collection.uid);
JournalEntity journalEntity = fetch(data, collection.getServiceEntity(data), collection.getUid());
if (journalEntity == null) {
journalEntity = new JournalEntity(data, collection);
} else {
@ -173,7 +173,7 @@ public class JournalModel {
@Override
public CollectionInfo convertToMapped(Class<? extends CollectionInfo> type, String value) {
return value == null ? null : CollectionInfo.fromJson(value);
return value == null ? null : CollectionInfo.Companion.fromJson(value);
}
}

@ -102,10 +102,10 @@ public class LocalAddressBook extends AndroidAddressBook implements LocalCollect
if (!accountManager.addAccountExplicitly(account, null, null))
throw new ContactsStorageException("Couldn't create address book account");
setUserData(accountManager, account, mainAccount, info.uid);
setUserData(accountManager, account, mainAccount, info.getUid());
LocalAddressBook addressBook = new LocalAddressBook(context, account, provider);
addressBook.setMainAccount(mainAccount);
addressBook.setURL(info.uid);
addressBook.setURL(info.getUid());
ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true);
@ -403,12 +403,12 @@ public class LocalAddressBook extends AndroidAddressBook implements LocalCollect
// HELPERS
public static String accountName(@NonNull Account mainAccount, @NonNull CollectionInfo info) {
String displayName = (info.displayName != null) ? info.displayName : info.uid;
String displayName = (info.getDisplayName() != null) ? info.getDisplayName() : info.getUid();
StringBuilder sb = new StringBuilder(displayName);
sb .append(" (")
.append(mainAccount.name)
.append(" ")
.append(info.uid.substring(0, 4))
.append(info.getUid().substring(0, 4))
.append(")");
return sb.toString();
}

@ -95,11 +95,11 @@ public class LocalCalendar extends AndroidCalendar implements LocalCollection {
private static ContentValues valuesFromCollectionInfo(JournalEntity journalEntity, boolean withColor) {
CollectionInfo info = journalEntity.getInfo();
ContentValues values = new ContentValues();
values.put(Calendars.NAME, info.uid);
values.put(Calendars.CALENDAR_DISPLAY_NAME, info.displayName);
values.put(Calendars.NAME, info.getUid());
values.put(Calendars.CALENDAR_DISPLAY_NAME, info.getDisplayName());
if (withColor)
values.put(Calendars.CALENDAR_COLOR, info.color != null ? info.color : defaultColor);
values.put(Calendars.CALENDAR_COLOR, info.getColor() != null ? info.getColor() : defaultColor);
if (journalEntity.isReadOnly())
values.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_READ);
@ -109,8 +109,8 @@ public class LocalCalendar extends AndroidCalendar implements LocalCollection {
values.put(Calendars.CAN_ORGANIZER_RESPOND, 1);
}
if (!TextUtils.isEmpty(info.timeZone)) {
VTimeZone timeZone = DateUtils.parseVTimeZone(info.timeZone);
if (!TextUtils.isEmpty(info.getTimeZone())) {
VTimeZone timeZone = DateUtils.parseVTimeZone(info.getTimeZone());
if (timeZone != null && timeZone.getTimeZoneId() != null)
values.put(Calendars.CALENDAR_TIME_ZONE, DateUtils.findAndroidTimezoneID(timeZone.getTimeZoneId().getValue()));
}

@ -68,11 +68,11 @@ public class LocalTaskList extends AndroidTaskList implements LocalCollection {
private static ContentValues valuesFromCollectionInfo(CollectionInfo info, boolean withColor) {
ContentValues values = new ContentValues();
values.put(TaskLists._SYNC_ID, info.uid);
values.put(TaskLists.LIST_NAME, info.displayName);
values.put(TaskLists._SYNC_ID, info.getUid());
values.put(TaskLists.LIST_NAME, info.getDisplayName());
if (withColor)
values.put(TaskLists.LIST_COLOR, info.color != null ? info.color : defaultColor);
values.put(TaskLists.LIST_COLOR, info.getColor() != null ? info.getColor() : defaultColor);
return values;
}

@ -155,9 +155,10 @@ abstract class SyncAdapterService : Service() {
if (journals.isEmpty()) {
val info = CollectionInfo.defaultForServiceType(serviceType)
info.uid = JournalManager.Journal.genUid()
val crypto = Crypto.CryptoManager(info.version, settings.password(), info.uid)
val journal = JournalManager.Journal(crypto, info.toJson(), info.uid)
val uid = JournalManager.Journal.genUid()
info.uid = uid
val crypto = Crypto.CryptoManager(info.version, settings.password(), uid)
val journal = JournalManager.Journal(crypto, info.toJson(), uid)
journalsManager.create(journal)
journals.add(Pair(journal, info))
}

@ -117,7 +117,7 @@ constructor(protected val context: Context, protected val account: Account, prot
if (journalEntity.encryptedKey != null) {
crypto = Crypto.CryptoManager(info.version, settings.keyPair!!, journalEntity.encryptedKey)
} else {
crypto = Crypto.CryptoManager(info.version, settings.password(), info.uid)
crypto = Crypto.CryptoManager(info.version, settings.password(), info.uid!!)
}
}
@ -191,7 +191,7 @@ constructor(protected val context: Context, protected val account: Account, prot
notifyUserOnSync()
App.log.info("Finished sync with CTag=" + remoteCTag!!)
App.log.info("Finished sync with CTag=$remoteCTag")
} catch (e: IOException) {
App.log.log(Level.WARNING, "I/O exception during sync, trying again later", e)
syncResult.stats.numIoExceptions++

@ -335,11 +335,7 @@ class AccountActivity : BaseActivity(), Toolbar.OnMenuItemClickListener, PopupMe
if (info.type == CollectionInfo.Type.ADDRESS_BOOK) {
vColor.visibility = View.GONE
} else {
if (info.color != null) {
vColor.setBackgroundColor(info.color)
} else {
vColor.setBackgroundColor(LocalCalendar.defaultColor)
}
vColor.setBackgroundColor(info.color ?: LocalCalendar.defaultColor)
}
val readOnly = v.findViewById<View>(R.id.read_only)

@ -101,8 +101,8 @@ class AddMemberFragment : DialogFragment() {
val httpClient = HttpClient.create(ctx!!, settings!!)
val journalsManager = JournalManager(httpClient, remote!!)
val journal = JournalManager.Journal.fakeWithUid(info!!.uid)
val crypto = Crypto.CryptoManager(info!!.version, settings!!.password(), info!!.uid)
val journal = JournalManager.Journal.fakeWithUid(info!!.uid!!)
val crypto = Crypto.CryptoManager(info!!.version, settings!!.password(), info!!.uid!!)
val encryptedKey = crypto.getEncryptedKey(settings!!.keyPair!!, memberPubKey!!)
val member = JournalManager.Member(memberEmail!!, encryptedKey!!)

@ -37,11 +37,7 @@ class CollectionMembersActivity : BaseActivity(), Refreshable {
val colorSquare = findViewById<View>(R.id.color)
if (info.type == CollectionInfo.Type.CALENDAR) {
if (info.color != null) {
colorSquare.setBackgroundColor(info.color)
} else {
colorSquare.setBackgroundColor(LocalCalendar.defaultColor)
}
colorSquare.setBackgroundColor(info.color ?: LocalCalendar.defaultColor)
} else {
colorSquare.visibility = View.GONE
}

@ -102,14 +102,16 @@ class CreateCollectionFragment : DialogFragment(), LoaderManager.LoaderCallbacks
val principal = HttpUrl.get(settings.uri!!)
val journalManager = JournalManager(HttpClient.create(context, settings), principal!!)
if (info.uid == null) {
info.uid = JournalManager.Journal.genUid()
val crypto = Crypto.CryptoManager(info.version, settings.password(), info.uid)
val journal = JournalManager.Journal(crypto, info.toJson(), info.uid)
var uid = info.uid
if (uid == null) {
uid = JournalManager.Journal.genUid()
info.uid = uid
val crypto = Crypto.CryptoManager(info.version, settings.password(), uid)
val journal = JournalManager.Journal(crypto, info.toJson(), uid)
journalManager.create(journal)
} else {
val crypto = Crypto.CryptoManager(info.version, settings.password(), info.uid)
val journal = JournalManager.Journal(crypto, info.toJson(), info.uid)
val crypto = Crypto.CryptoManager(info.version, settings.password(), uid)
val journal = JournalManager.Journal(crypto, info.toJson(), uid)
journalManager.update(journal)
}

@ -89,9 +89,9 @@ class DeleteCollectionFragment : DialogFragment(), LoaderManager.LoaderCallbacks
val principal = HttpUrl.get(settings.uri!!)
val journalManager = JournalManager(HttpClient.create(context, settings), principal!!)
val crypto = Crypto.CryptoManager(collectionInfo.version, settings.password(), collectionInfo.uid)
val crypto = Crypto.CryptoManager(collectionInfo.version, settings.password(), collectionInfo.uid!!)
journalManager.delete(JournalManager.Journal(crypto, collectionInfo.toJson(), collectionInfo.uid))
journalManager.delete(JournalManager.Journal(crypto, collectionInfo.toJson(), collectionInfo.uid!!))
val journalEntity = JournalEntity.fetch(data, collectionInfo.getServiceEntity(data), collectionInfo.uid)
journalEntity!!.isDeleted = true
data.update(journalEntity)

@ -32,11 +32,7 @@ class EditCollectionActivity : CreateCollectionActivity() {
if (info!!.type == CollectionInfo.Type.CALENDAR) {
val colorSquare = findViewById<View>(R.id.color)
if (info!!.color != null) {
colorSquare.setBackgroundColor(info!!.color)
} else {
colorSquare.setBackgroundColor(LocalCalendar.defaultColor)
}
colorSquare.setBackgroundColor(info.color ?: LocalCalendar.defaultColor)
}
val edit = findViewById<View>(R.id.display_name) as EditText

@ -52,7 +52,7 @@ class RemoveMemberFragment : DialogFragment() {
override fun doInBackground(vararg voids: Void): RemoveResult {
try {
val journalsManager = JournalManager(httpClient!!, remote!!)
val journal = JournalManager.Journal.fakeWithUid(info!!.uid)
val journal = JournalManager.Journal.fakeWithUid(info!!.uid!!)
val member = JournalManager.Member(memberEmail!!, "placeholder".toByteArray(Charsets.UTF_8))
journalsManager.deleteMember(journal, member)

@ -59,11 +59,7 @@ class ViewCollectionActivity : BaseActivity(), Refreshable {
val colorSquare = findViewById<View>(R.id.color)
if (info.type == CollectionInfo.Type.CALENDAR) {
if (info.color != null) {
colorSquare.setBackgroundColor(info.color)
} else {
colorSquare.setBackgroundColor(LocalCalendar.defaultColor)
}
colorSquare.setBackgroundColor(info.color ?: LocalCalendar.defaultColor)
} else {
colorSquare.visibility = View.GONE
}

@ -28,7 +28,7 @@ import io.requery.sql.EntityDataStore
class ListEntriesFragment : ListFragment(), AdapterView.OnItemClickListener {
private var data: EntityDataStore<Persistable>? = null
private lateinit var data: EntityDataStore<Persistable>
private lateinit var info: CollectionInfo
private var journalEntity: JournalEntity? = null
private var asyncTask: AsyncTask<*, *, *>? = null
@ -92,8 +92,8 @@ class ListEntriesFragment : ListFragment(), AdapterView.OnItemClickListener {
private inner class JournalFetch : AsyncTask<Void, Void, List<EntryEntity>>() {
override fun doInBackground(vararg voids: Void): List<EntryEntity> {
journalEntity = JournalModel.Journal.fetch(data!!, info!!.getServiceEntity(data), info!!.uid)
return data!!.select(EntryEntity::class.java).where(EntryEntity.JOURNAL.eq(journalEntity)).orderBy(EntryEntity.ID.desc()).get().toList()
journalEntity = JournalModel.Journal.fetch(data, info.getServiceEntity(data), info.uid)
return data.select(EntryEntity::class.java).where(EntryEntity.JOURNAL.eq(journalEntity)).orderBy(EntryEntity.ID.desc()).get().toList()
}
override fun onPostExecute(result: List<EntryEntity>) {

Loading…
Cancel
Save