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 = '2.0.4'
ext.jabitVersion = 'development-SNAPSHOT' ext.jabitVersion = 'feature-refactoring-SNAPSHOT'
ext.supportVersion = '27.0.1' ext.supportVersion = '27.0.1'
dependencies { dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar']) implementation fileTree(dir: 'libs', include: ['*.jar'])

View File

@ -47,86 +47,93 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.preferences) addPreferencesFromResource(R.xml.preferences)
findPreference("about")?.onPreferenceClickListener = Preference.OnPreferenceClickListener { findPreference("about")?.onPreferenceClickListener = aboutClickListener()
(activity as? MainActivity)?.let { activity ->
val libsBuilder = LibsBuilder()
.withActivityTitle(activity.getString(R.string.about))
.withActivityStyle(Libs.ActivityStyle.LIGHT_DARK_TOOLBAR)
.withAboutIconShown(true)
.withAboutVersionShown(true)
.withAboutDescription(getString(R.string.about_app))
if (activity.hasDetailPane) {
activity.setDetailView(libsBuilder.supportFragment())
} else {
libsBuilder.start(activity)
}
}
return@OnPreferenceClickListener true
}
val cleanup = findPreference("cleanup") val cleanup = findPreference("cleanup")
cleanup?.onPreferenceClickListener = Preference.OnPreferenceClickListener { cleanup?.onPreferenceClickListener = cleanupClickListener(cleanup)
val ctx = activity?.applicationContext ?: throw IllegalStateException("Context not available") findPreference("export")?.onPreferenceClickListener = exportClickListener()
cleanup.isEnabled = false findPreference("import")?.onPreferenceClickListener = importClickListener()
Toast.makeText(ctx, R.string.cleanup_notification_start, Toast.LENGTH_SHORT).show() findPreference("status").onPreferenceClickListener = statusClickListener()
}
doAsync { private fun aboutClickListener() = Preference.OnPreferenceClickListener {
val bmc = Singleton.getBitmessageContext(ctx) (activity as? MainActivity)?.let { activity ->
bmc.internals.nodeRegistry.clear() val libsBuilder = LibsBuilder()
bmc.cleanup() .withActivityTitle(activity.getString(R.string.about))
Preferences.cleanupExportDirectory(ctx) .withActivityStyle(Libs.ActivityStyle.LIGHT_DARK_TOOLBAR)
.withAboutIconShown(true)
uiThread { .withAboutVersionShown(true)
Toast.makeText( .withAboutDescription(getString(R.string.about_app))
ctx,
R.string.cleanup_notification_end,
Toast.LENGTH_LONG
).show()
cleanup.isEnabled = true
}
}
return@OnPreferenceClickListener true
}
findPreference("export")?.onPreferenceClickListener = Preference.OnPreferenceClickListener {
val ctx = context ?: throw IllegalStateException("No context available")
indeterminateProgressDialog(R.string.export_data_summary, R.string.export_data).apply {
doAsync {
val exportDirectory = Preferences.getExportDirectory(ctx)
exportDirectory.mkdirs()
val file = Exports.exportData(exportDirectory, ctx)
val contentUri = getUriForFile(ctx, "ch.dissem.apps.abit.fileprovider", file)
val intent = Intent(android.content.Intent.ACTION_SEND)
intent.type = "application/zip"
intent.putExtra(Intent.EXTRA_SUBJECT, "abit-export.zip")
intent.putExtra(Intent.EXTRA_STREAM, contentUri)
startActivityForResult(Intent.createChooser(intent, ""), WRITE_EXPORT_REQUEST_CODE)
uiThread {
dismiss()
}
}
}
return@OnPreferenceClickListener true
}
findPreference("import")?.onPreferenceClickListener = Preference.OnPreferenceClickListener {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "application/zip"
startActivityForResult(intent, READ_IMPORT_REQUEST_CODE)
return@OnPreferenceClickListener true
}
findPreference("status").onPreferenceClickListener = Preference.OnPreferenceClickListener {
val activity = activity as MainActivity
if (activity.hasDetailPane) { if (activity.hasDetailPane) {
activity.setDetailView(StatusFragment()) activity.setDetailView(libsBuilder.supportFragment())
} else { } else {
startActivity<StatusActivity>() libsBuilder.start(activity)
} }
return@OnPreferenceClickListener true
} }
return@OnPreferenceClickListener true
}
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()
doAsync {
val bmc = Singleton.getBitmessageContext(ctx)
bmc.internals.nodeRegistry.clear()
bmc.cleanup()
Preferences.cleanupExportDirectory(ctx)
uiThread {
Toast.makeText(
ctx,
R.string.cleanup_notification_end,
Toast.LENGTH_LONG
).show()
cleanup.isEnabled = true
}
}
return@OnPreferenceClickListener true
}
private fun exportClickListener() = Preference.OnPreferenceClickListener {
val ctx = context ?: throw IllegalStateException("No context available")
indeterminateProgressDialog(R.string.export_data_summary, R.string.export_data).apply {
doAsync {
val exportDirectory = Preferences.getExportDirectory(ctx)
exportDirectory.mkdirs()
val file = Exports.exportData(exportDirectory, ctx)
val contentUri = getUriForFile(ctx, "ch.dissem.apps.abit.fileprovider", file)
val intent = Intent(android.content.Intent.ACTION_SEND)
intent.type = "application/zip"
intent.putExtra(Intent.EXTRA_SUBJECT, "abit-export.zip")
intent.putExtra(Intent.EXTRA_STREAM, contentUri)
startActivityForResult(Intent.createChooser(intent, ""), WRITE_EXPORT_REQUEST_CODE)
uiThread {
dismiss()
}
}
}
return@OnPreferenceClickListener true
}
private fun importClickListener() = Preference.OnPreferenceClickListener {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "application/zip"
startActivityForResult(intent, READ_IMPORT_REQUEST_CODE)
return@OnPreferenceClickListener true
}
private fun statusClickListener() = Preference.OnPreferenceClickListener {
val activity = activity as MainActivity
if (activity.hasDetailPane) {
activity.setDetailView(StatusFragment())
} else {
startActivity<StatusActivity>()
}
return@OnPreferenceClickListener true
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

View File

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

View File

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