Code style improvements (thanks to codebeat.co)

+ bumped Jabit to the feature/refactoring branch
This commit is contained in:
Christian Basler 2017-11-23 20:33:00 +01:00
parent dd22caaa50
commit f45f6c3919
4 changed files with 123 additions and 125 deletions

View File

@ -50,7 +50,7 @@ android {
}
//ext.jabitVersion = '2.0.4'
ext.jabitVersion = 'development-SNAPSHOT'
ext.jabitVersion = 'feature-refactoring-SNAPSHOT'
ext.supportVersion = '27.0.1'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

View File

@ -47,7 +47,15 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.preferences)
findPreference("about")?.onPreferenceClickListener = Preference.OnPreferenceClickListener {
findPreference("about")?.onPreferenceClickListener = aboutClickListener()
val cleanup = findPreference("cleanup")
cleanup?.onPreferenceClickListener = cleanupClickListener(cleanup)
findPreference("export")?.onPreferenceClickListener = exportClickListener()
findPreference("import")?.onPreferenceClickListener = importClickListener()
findPreference("status").onPreferenceClickListener = statusClickListener()
}
private fun aboutClickListener() = Preference.OnPreferenceClickListener {
(activity as? MainActivity)?.let { activity ->
val libsBuilder = LibsBuilder()
.withActivityTitle(activity.getString(R.string.about))
@ -63,8 +71,8 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
}
return@OnPreferenceClickListener true
}
val cleanup = findPreference("cleanup")
cleanup?.onPreferenceClickListener = Preference.OnPreferenceClickListener {
private fun cleanupClickListener(cleanup: Preference) = Preference.OnPreferenceClickListener {
val ctx = activity?.applicationContext ?: throw IllegalStateException("Context not available")
cleanup.isEnabled = false
Toast.makeText(ctx, R.string.cleanup_notification_start, Toast.LENGTH_SHORT).show()
@ -87,7 +95,7 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
return@OnPreferenceClickListener true
}
findPreference("export")?.onPreferenceClickListener = Preference.OnPreferenceClickListener {
private fun exportClickListener() = Preference.OnPreferenceClickListener {
val ctx = context ?: throw IllegalStateException("No context available")
indeterminateProgressDialog(R.string.export_data_summary, R.string.export_data).apply {
@ -109,7 +117,7 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
return@OnPreferenceClickListener true
}
findPreference("import")?.onPreferenceClickListener = Preference.OnPreferenceClickListener {
private fun importClickListener() = Preference.OnPreferenceClickListener {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "application/zip"
@ -118,7 +126,7 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
return@OnPreferenceClickListener true
}
findPreference("status").onPreferenceClickListener = Preference.OnPreferenceClickListener {
private fun statusClickListener() = Preference.OnPreferenceClickListener {
val activity = activity as MainActivity
if (activity.hasDetailPane) {
activity.setDetailView(StatusFragment())
@ -127,7 +135,6 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
}
return@OnPreferenceClickListener true
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
val ctx = context ?: throw IllegalStateException("No context available")

View File

@ -35,19 +35,12 @@ import java.util.*
*/
class AndroidAddressRepository(private val sql: SqlHelper) : AddressRepository {
override fun findContact(ripeOrTag: ByteArray): BitmessageAddress? {
for (address in find("public_key is null")) {
if (address.version > 3) {
if (Arrays.equals(ripeOrTag, address.tag)) return address
} else {
if (Arrays.equals(ripeOrTag, address.ripe)) return address
}
}
return null
}
override fun findContact(ripeOrTag: ByteArray): BitmessageAddress? = findByRipeOrTag("public_key is null", ripeOrTag)
override fun findIdentity(ripeOrTag: ByteArray): BitmessageAddress? {
for (address in find("private_key is not null")) {
override fun findIdentity(ripeOrTag: ByteArray): BitmessageAddress? = findByRipeOrTag("private_key is not null", ripeOrTag)
private fun findByRipeOrTag(where: String, ripeOrTag: ByteArray): BitmessageAddress? {
for (address in find(where)) {
if (address.version > 3) {
if (Arrays.equals(ripeOrTag, address.tag)) return address
} else {
@ -126,24 +119,27 @@ class AndroidAddressRepository(private val sql: SqlHelper) : AddressRepository {
}
private fun getAddress(c: Cursor): BitmessageAddress {
val address: BitmessageAddress
val privateKeyBytes = c.getBlob(c.getColumnIndex(COLUMN_PRIVATE_KEY))
if (privateKeyBytes != null) {
address = BitmessageAddress(PrivateKey.read(ByteArrayInputStream(privateKeyBytes)))
} else {
address = BitmessageAddress(c.getString(c.getColumnIndex(COLUMN_ADDRESS)))
val address = privateKeyBytes?.let {
BitmessageAddress(PrivateKey.read(ByteArrayInputStream(privateKeyBytes)))
} ?:
BitmessageAddress(c.getString(c.getColumnIndex(COLUMN_ADDRESS))).also { address ->
c.getBlob(c.getColumnIndex(COLUMN_PUBLIC_KEY))?.let { publicKeyBytes ->
val pubkey = Factory.readPubkey(address.version, address.stream,
ByteArrayInputStream(publicKeyBytes), publicKeyBytes.size,
false)
address.pubkey = if (address.version == 4L && pubkey is V3Pubkey) {
V4Pubkey(pubkey)
Factory.readPubkey(
version = address.version, stream = address.stream,
input = ByteArrayInputStream(publicKeyBytes), length = publicKeyBytes.size,
encrypted = false
).let {
address.pubkey = if (address.version == 4L && it is V3Pubkey) {
V4Pubkey(it)
} else {
pubkey
it
}
}
}
}
address.alias = c.getString(c.getColumnIndex(COLUMN_ALIAS))
address.isChan = c.getInt(c.getColumnIndex(COLUMN_CHAN)) == 1
address.isSubscribed = c.getInt(c.getColumnIndex(COLUMN_SUBSCRIBED)) == 1
@ -171,18 +167,7 @@ class AndroidAddressRepository(private val sql: SqlHelper) : AddressRepository {
private fun update(address: BitmessageAddress) {
val db = sql.writableDatabase
// Create a new map of values, where column names are the keys
val values = ContentValues()
address.alias?.let { values.put(COLUMN_ALIAS, it) }
address.pubkey?.let { pubkey ->
val out = ByteArrayOutputStream()
pubkey.writeUnencrypted(out)
values.put(COLUMN_PUBLIC_KEY, out.toByteArray())
}
address.privateKey?.let { values.put(COLUMN_PRIVATE_KEY, Encode.bytes(it)) }
if (address.isChan) {
values.put(COLUMN_CHAN, true)
}
values.put(COLUMN_SUBSCRIBED, address.isSubscribed)
val values = getContentValues(address)
val update = db.update(TABLE_NAME, values, "address=?", arrayOf(address.address))
if (update < 0) {
@ -193,20 +178,10 @@ class AndroidAddressRepository(private val sql: SqlHelper) : AddressRepository {
private fun insert(address: BitmessageAddress) {
val db = sql.writableDatabase
// Create a new map of values, where column names are the keys
val values = ContentValues()
val values = getContentValues(address)
values.put(COLUMN_ADDRESS, address.address)
values.put(COLUMN_VERSION, address.version)
values.put(COLUMN_ALIAS, address.alias)
address.pubkey?.let { pubkey ->
val out = ByteArrayOutputStream()
pubkey.writeUnencrypted(out)
values.put(COLUMN_PUBLIC_KEY, out.toByteArray())
} ?: {
values.put(COLUMN_PUBLIC_KEY, null as ByteArray?)
}.invoke()
address.privateKey?.let { values.put(COLUMN_PRIVATE_KEY, Encode.bytes(it)) }
values.put(COLUMN_CHAN, address.isChan)
values.put(COLUMN_SUBSCRIBED, address.isSubscribed)
val insert = db.insert(TABLE_NAME, null, values)
if (insert < 0) {
@ -214,6 +189,22 @@ class AndroidAddressRepository(private val sql: SqlHelper) : AddressRepository {
}
}
private fun getContentValues(address: BitmessageAddress): ContentValues {
val values = ContentValues()
address.alias?.let { values.put(COLUMN_ALIAS, it) }
address.pubkey?.let { pubkey ->
val out = ByteArrayOutputStream()
pubkey.writer().writeUnencrypted(out)
values.put(COLUMN_PUBLIC_KEY, out.toByteArray())
}
address.privateKey?.let { values.put(COLUMN_PRIVATE_KEY, Encode.bytes(it)) }
if (address.isChan) {
values.put(COLUMN_CHAN, true)
}
values.put(COLUMN_SUBSCRIBED, address.isSubscribed)
return values
}
override fun remove(address: BitmessageAddress) {
val db = sql.writableDatabase
db.delete(TABLE_NAME, "address = ?", arrayOf(address.address))

View File

@ -73,7 +73,7 @@ object Drawables {
address.pubkey?.apply {
link.append(if (address.alias == null) '?' else '&')
val pubkey = ByteArrayOutputStream()
writeUnencrypted(pubkey)
writer().writeUnencrypted(pubkey)
link.append("pubkey=").append(Base64.encodeToString(pubkey.toByteArray(), URL_SAFE or NO_WRAP))
}