From 072f73292425351fe220fdc138c638b16b450ff5 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Tue, 31 Oct 2017 07:50:57 +0100 Subject: [PATCH] Switch to API level 27 and updated libraries --- app/build.gradle | 100 ++++++++++-------- .../apps/abit/AbstractItemListFragment.kt | 10 +- .../dissem/apps/abit/AddressDetailFragment.kt | 83 +++++++-------- .../dissem/apps/abit/AddressListFragment.kt | 64 +++++------ .../apps/abit/ComposeMessageActivity.kt | 21 ++-- .../apps/abit/ComposeMessageFragment.kt | 31 +++--- .../dissem/apps/abit/CreateAddressActivity.kt | 10 +- .../apps/abit/ImportIdentitiesFragment.kt | 7 +- .../ch/dissem/apps/abit/InputWifFragment.kt | 5 +- .../java/ch/dissem/apps/abit/MainActivity.kt | 8 +- .../dissem/apps/abit/MessageDetailFragment.kt | 100 +++++++++--------- .../dissem/apps/abit/MessageListFragment.kt | 61 +++++------ .../ch/dissem/apps/abit/SettingsFragment.kt | 76 ++++++------- .../ch/dissem/apps/abit/StatusActivity.kt | 1 - .../ch/dissem/apps/abit/StatusFragment.kt | 8 +- .../abit/adapter/AddressSelectorAdapter.kt | 14 +-- .../apps/abit/adapter/ContactAdapter.kt | 25 +++-- .../ch/dissem/apps/abit/adapter/Selectable.kt | 2 +- .../abit/adapter/SwipeableMessageAdapter.kt | 70 ++++++------ .../adapter/SwitchingProofOfWorkEngine.kt | 12 +-- .../abit/dialog/AddIdentityDialogFragment.kt | 15 +-- .../DeterministicIdentityDialogFragment.kt | 3 +- .../dialog/SelectEncodingDialogFragment.kt | 6 +- .../apps/abit/drawer/ProfileImageListener.kt | 7 +- .../abit/drawer/ProfileSelectionListener.kt | 4 +- .../abit/listener/ListSelectionListener.kt | 2 +- .../abit/notification/AbstractNotification.kt | 5 +- .../abit/notification/ErrorNotification.kt | 4 +- .../abit/notification/NetworkNotification.kt | 4 +- .../notification/NewMessageNotification.kt | 7 +- .../notification/ProofOfWorkNotification.kt | 4 +- .../dissem/apps/abit/pow/ServerPowEngine.kt | 19 +--- .../repository/AndroidAddressRepository.kt | 20 +--- .../apps/abit/repository/AndroidInventory.kt | 1 - .../repository/AndroidMessageRepository.kt | 27 ----- .../abit/repository/AndroidNodeRegistry.kt | 6 +- .../AndroidProofOfWorkRepository.kt | 3 +- .../dissem/apps/abit/repository/SqlHelper.kt | 85 +++++++-------- .../apps/abit/service/BitmessageService.kt | 2 +- .../apps/abit/service/ProofOfWorkService.kt | 52 +++++---- .../apps/abit/service/ServicePowEngine.kt | 10 +- .../ch/dissem/apps/abit/service/Singleton.kt | 20 ++-- .../apps/abit/service/StartServiceReceiver.kt | 8 +- .../abit/service/StartupNodeOnWifiService.kt | 14 ++- .../synchronization/AuthenticatorService.kt | 1 - .../apps/abit/synchronization/SyncService.kt | 19 +--- .../java/ch/dissem/apps/abit/util/Assets.kt | 36 +++---- .../ch/dissem/apps/abit/util/Constants.kt | 2 +- .../java/ch/dissem/apps/abit/util/Labels.kt | 8 +- .../java/ch/dissem/apps/abit/util/PowStats.kt | 4 +- .../ch/dissem/apps/abit/util/Preferences.kt | 2 - .../java/ch/dissem/apps/abit/util/Strings.kt | 12 +-- app/src/main/res/drawable/header.png | Bin 7087 -> 0 bytes app/src/main/res/drawable/header.xml | 9 ++ app/src/main/res/layout/item_label.xml | 4 +- app/src/main/res/layout/subscription_row.xml | 4 +- app/src/main/res/menu/compose.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- .../AndroidAddressRepositoryTest.kt | 48 +++++---- .../repository/AndroidInventoryTest.kt | 16 ++- .../AndroidMessageRepositoryTest.kt | 34 +++--- .../repository/AndroidNodeRegistryTest.kt | 36 +++---- .../AndroidProofOfWorkRepositoryTest.kt | 94 +++++++++------- .../dissem/bitmessage/repository/TestBase.kt | 42 ++++---- build.gradle | 16 ++- gradle/wrapper/gradle-wrapper.jar | Bin 54212 -> 54708 bytes gradle/wrapper/gradle-wrapper.properties | 4 +- gradlew | 6 +- 68 files changed, 678 insertions(+), 759 deletions(-) delete mode 100644 app/src/main/res/drawable/header.png create mode 100644 app/src/main/res/drawable/header.xml diff --git a/app/build.gradle b/app/build.gradle index 2816855..17960ec 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,18 +8,18 @@ ext { } if (project.hasProperty("project.configs") && new File(project.property("project.configs") + appName + ".gradle").exists()) { - apply from: project.property("project.configs") + appName + ".gradle"; + apply from: project.property("project.configs") + appName + ".gradle" } //noinspection GroovyMissingReturnStatement android { - compileSdkVersion 25 - buildToolsVersion "25.0.2" + compileSdkVersion 27 + buildToolsVersion "26.0.2" defaultConfig { applicationId "ch.dissem.apps." + appName.toLowerCase() minSdkVersion 19 - targetSdkVersion 25 + targetSdkVersion 27 versionCode 17 versionName "1.0-beta17" multiDexEnabled true @@ -28,6 +28,9 @@ android { sourceCompatibility JavaVersion.VERSION_1_7 targetCompatibility JavaVersion.VERSION_1_7 } + lintOptions { + abortOnError false + } buildTypes { release { minifyEnabled false @@ -36,69 +39,74 @@ android { signingConfig signingConfigs.release } } + testOptions { + unitTests { + includeAndroidResources = true + } + } } //ext.jabitVersion = '2.0.4' ext.jabitVersion = 'development-SNAPSHOT' -ext.supportVersion = '25.3.1' +ext.supportVersion = '27.0.0' dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" - compile "org.jetbrains.anko:anko:$anko_version" + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" + implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" + implementation "org.jetbrains.anko:anko:$anko_version" - compile "com.android.support:appcompat-v7:$supportVersion" - compile "com.android.support:preference-v7:$supportVersion" - compile "com.android.support:support-v4:$supportVersion" - compile "com.android.support:design:$supportVersion" - compile "com.android.support:multidex:1.0.1" + implementation "com.android.support:appcompat-v7:$supportVersion" + implementation "com.android.support:preference-v7:$supportVersion" + implementation "com.android.support:cardview-v7:$supportVersion" + implementation "com.android.support:support-v4:$supportVersion" + implementation "com.android.support:design:$supportVersion" + implementation "com.android.support:multidex:1.0.2" - compile "ch.dissem.jabit:jabit-core:$jabitVersion" - compile "ch.dissem.jabit:jabit-networking:$jabitVersion" - compile "ch.dissem.jabit:jabit-cryptography-spongy:$jabitVersion" - compile "ch.dissem.jabit:jabit-extensions:$jabitVersion" - compile "ch.dissem.jabit:jabit-wif:$jabitVersion" - compile "ch.dissem.jabit:jabit-exports:$jabitVersion" + implementation "ch.dissem.jabit:jabit-core:$jabitVersion" + implementation "ch.dissem.jabit:jabit-networking:$jabitVersion" + implementation "ch.dissem.jabit:jabit-cryptography-spongy:$jabitVersion" + implementation "ch.dissem.jabit:jabit-extensions:$jabitVersion" + implementation "ch.dissem.jabit:jabit-wif:$jabitVersion" + implementation "ch.dissem.jabit:jabit-exports:$jabitVersion" - compile 'org.slf4j:slf4j-android:1.7.25' + implementation 'org.slf4j:slf4j-android:1.7.25' - compile 'com.mikepenz:materialize:1.0.1@aar' - compile('com.mikepenz:materialdrawer:5.9.0@aar') { + implementation 'com.mikepenz:materialize:1.1.0@aar' + implementation('com.mikepenz:materialdrawer:5.9.5@aar') { transitive = true } - compile('com.mikepenz:aboutlibraries:5.9.5@aar') { + implementation('com.mikepenz:aboutlibraries:5.9.7@aar') { transitive = true } - compile "com.mikepenz:iconics-core:2.8.3@aar" - compile 'com.mikepenz:google-material-typeface:3.0.1.0.original@aar' - compile 'com.mikepenz:community-material-typeface:1.9.32.1@aar' + implementation "com.mikepenz:iconics-core:2.9.3@aar" + implementation "com.mikepenz:iconics-views:2.9.3@aar" + implementation 'com.mikepenz:google-material-typeface:3.0.1.1.original@aar' + implementation 'com.mikepenz:community-material-typeface:1.9.32.2@aar' - compile 'com.journeyapps:zxing-android-embedded:3.5.0@aar' - compile 'com.google.zxing:core:3.3.0' + implementation 'com.journeyapps:zxing-android-embedded:3.5.0@aar' + implementation 'com.google.zxing:core:3.3.1' - compile 'com.github.kobakei:MaterialFabSpeedDial:1.1.5' - compile 'com.github.amlcurran.showcaseview:library:5.4.3' - compile('com.h6ah4i.android.widget.advrecyclerview:advrecyclerview:0.10.4@aar') { + implementation 'com.github.kobakei:MaterialFabSpeedDial:1.1.7' + implementation 'com.github.amlcurran.showcaseview:library:5.4.3' + // FIXME: switch back to com.h6ah4i.android.widget.advrecyclerview:advrecyclerview:0.10.+ a.s.a.p. + implementation('com.github.h6ah4i:android-advancedrecyclerview:develop-SNAPSHOT@aar') { transitive = true } - compile 'com.github.angads25:filepicker:1.1.0' - compile 'com.android.support.constraint:constraint-layout:1.0.2' + implementation 'com.github.angads25:filepicker:1.1.1' + implementation 'com.android.support.constraint:constraint-layout:1.0.2' - testCompile 'junit:junit:4.12' - testCompile 'org.mockito:mockito-core:2.8.9' - testCompile 'org.hamcrest:hamcrest-library:1.3' - testCompile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" - testCompile 'com.nhaarman:mockito-kotlin-kt1.1:1.5.0' - testCompile 'org.robolectric:robolectric:3.4.2' - testCompile "org.robolectric:shadows-multidex:3.4-rc2" + testImplementation 'junit:junit:4.12' + testImplementation 'org.mockito:mockito-core:2.11.0' + testImplementation 'org.hamcrest:hamcrest-library:1.3' + testImplementation 'com.nhaarman:mockito-kotlin-kt1.1:1.5.0' + testImplementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" + testImplementation 'org.robolectric:robolectric:3.5' + testImplementation "org.robolectric:shadows-multidex:3.5" + + androidTestImplementation "com.android.support:multidex:1.0.2" } idea.module { downloadJavadoc = true downloadSources = true } - -android { - lintOptions { - abortOnError false - } -} diff --git a/app/src/main/java/ch/dissem/apps/abit/AbstractItemListFragment.kt b/app/src/main/java/ch/dissem/apps/abit/AbstractItemListFragment.kt index 3907e7d..a9cd265 100644 --- a/app/src/main/java/ch/dissem/apps/abit/AbstractItemListFragment.kt +++ b/app/src/main/java/ch/dissem/apps/abit/AbstractItemListFragment.kt @@ -40,7 +40,7 @@ abstract class AbstractItemListFragment : ListFragment(), ListHolder { private var activatedPosition = ListView.INVALID_POSITION private var activateOnItemClick: Boolean = false - override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) // Restore the previously serialized activated item position. @@ -92,9 +92,9 @@ abstract class AbstractItemListFragment : ListFragment(), ListHolder { } } - override fun onSaveInstanceState(outState: Bundle?) { + override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) - if (outState != null && activatedPosition != ListView.INVALID_POSITION) { + if (activatedPosition != ListView.INVALID_POSITION) { // Serialize and persist the activated item position. outState.putInt(STATE_ACTIVATED_POSITION, activatedPosition) } @@ -136,9 +136,7 @@ abstract class AbstractItemListFragment : ListFragment(), ListHolder { * nothing. Used only when this fragment is not attached to an activity. */ internal object DummyCallback : ListSelectionListener { - override fun onItemSelected(item: Any) { - // NO OP - } + override fun onItemSelected(item: Any) = Unit // NO OP } companion object { diff --git a/app/src/main/java/ch/dissem/apps/abit/AddressDetailFragment.kt b/app/src/main/java/ch/dissem/apps/abit/AddressDetailFragment.kt index cb36e01..ab7fea0 100644 --- a/app/src/main/java/ch/dissem/apps/abit/AddressDetailFragment.kt +++ b/app/src/main/java/ch/dissem/apps/abit/AddressDetailFragment.kt @@ -49,8 +49,10 @@ class AddressDetailFragment : Fragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - if (arguments.containsKey(ARG_ITEM)) { - item = arguments.getSerializable(ARG_ITEM) as BitmessageAddress + arguments?.let { arguments -> + if (arguments.containsKey(ARG_ITEM)) { + item = arguments.getSerializable(ARG_ITEM) as BitmessageAddress + } } setHasOptionsMenu(true) } @@ -58,7 +60,7 @@ class AddressDetailFragment : Fragment() { override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { inflater.inflate(R.menu.address, menu) - val ctx = activity + val ctx = activity!! Drawables.addIcon(ctx, menu, R.id.write_message, GoogleMaterial.Icon.gmd_mail) Drawables.addIcon(ctx, menu, R.id.share, GoogleMaterial.Icon.gmd_share) Drawables.addIcon(ctx, menu, R.id.delete, GoogleMaterial.Icon.gmd_delete) @@ -69,7 +71,7 @@ class AddressDetailFragment : Fragment() { override fun onOptionsItemSelected(menuItem: MenuItem): Boolean { val item = item ?: return false - val ctx = activity + val ctx = activity ?: return false when (menuItem.itemId) { R.id.write_message -> { val identity = Singleton.getIdentity(ctx) @@ -89,35 +91,35 @@ class AddressDetailFragment : Fragment() { else R.string.delete_contact_warning AlertDialog.Builder(ctx) - .setMessage(warning) - .setPositiveButton(android.R.string.yes) { _, _ -> - Singleton.getAddressRepository(ctx).remove(item) - val mainActivity = MainActivity.getInstance() - if (item.privateKey != null && mainActivity != null) { - mainActivity.removeIdentityEntry(item) - } - this.item = null - ctx.onBackPressed() + .setMessage(warning) + .setPositiveButton(android.R.string.yes) { _, _ -> + Singleton.getAddressRepository(ctx).remove(item) + val mainActivity = MainActivity.getInstance() + if (item.privateKey != null && mainActivity != null) { + mainActivity.removeIdentityEntry(item) } - .setNegativeButton(android.R.string.no, null) - .show() + this.item = null + ctx.onBackPressed() + } + .setNegativeButton(android.R.string.no, null) + .show() return true } R.id.export -> { AlertDialog.Builder(ctx) - .setMessage(R.string.confirm_export) - .setPositiveButton(android.R.string.yes) { _, _ -> - val shareIntent = Intent(Intent.ACTION_SEND) - shareIntent.type = "text/plain" - shareIntent.putExtra(Intent.EXTRA_TITLE, item.toString() + EXPORT_POSTFIX) - val exporter = WifExporter(Singleton - .getBitmessageContext(ctx)) - exporter.addIdentity(item) - shareIntent.putExtra(Intent.EXTRA_TEXT, exporter.toString()) - startActivity(Intent.createChooser(shareIntent, null)) - } - .setNegativeButton(android.R.string.no, null) - .show() + .setMessage(R.string.confirm_export) + .setPositiveButton(android.R.string.yes) { _, _ -> + val shareIntent = Intent(Intent.ACTION_SEND) + shareIntent.type = "text/plain" + shareIntent.putExtra(Intent.EXTRA_TITLE, item.toString() + EXPORT_POSTFIX) + val exporter = WifExporter(Singleton + .getBitmessageContext(ctx)) + exporter.addIdentity(item) + shareIntent.putExtra(Intent.EXTRA_TEXT, exporter.toString()) + startActivity(Intent.createChooser(shareIntent, null)) + } + .setNegativeButton(android.R.string.no, null) + .show() return true } R.id.share -> { @@ -132,30 +134,27 @@ class AddressDetailFragment : Fragment() { } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View - = inflater.inflate(R.layout.fragment_address_detail, container, false) + = inflater.inflate(R.layout.fragment_address_detail, container, false) override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) // Show the dummy content as text in a TextView. item?.let { item -> - val activity = activity - when { - item.isChan -> activity.setTitle(R.string.title_chan_detail) - item.privateKey != null -> activity.setTitle(R.string.title_identity_detail) - item.isSubscribed -> activity.setTitle(R.string.title_subscription_detail) - else -> activity.setTitle(R.string.title_contact_detail) + activity?.let { activity -> + when { + item.isChan -> activity.setTitle(R.string.title_chan_detail) + item.privateKey != null -> activity.setTitle(R.string.title_identity_detail) + item.isSubscribed -> activity.setTitle(R.string.title_subscription_detail) + else -> activity.setTitle(R.string.title_contact_detail) + } } avatar.setImageDrawable(Identicon(item)) name.setText(item.toString()) name.addTextChangedListener(object : TextWatcher { - override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) { - // Nothing to do - } + override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) = Unit // Nothing to do - override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { - // Nothing to do - } + override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) = Unit // Nothing to do override fun afterTextChanged(s: Editable) { item.alias = s.toString() @@ -185,7 +184,7 @@ class AddressDetailFragment : Fragment() { override fun onPause() { item?.let { item -> - Singleton.getAddressRepository(context).save(item) + Singleton.getAddressRepository(context!!).save(item) if (item.privateKey != null) { MainActivity.getInstance()?.updateIdentityEntry(item) } diff --git a/app/src/main/java/ch/dissem/apps/abit/AddressListFragment.kt b/app/src/main/java/ch/dissem/apps/abit/AddressListFragment.kt index abd52fc..80db0e4 100644 --- a/app/src/main/java/ch/dissem/apps/abit/AddressListFragment.kt +++ b/app/src/main/java/ch/dissem/apps/abit/AddressListFragment.kt @@ -45,10 +45,10 @@ class AddressListFragment : AbstractItemListFragment() super.onCreate(savedInstanceState) adapter = object : ArrayAdapter( - activity, - R.layout.subscription_row, - R.id.name, - LinkedList()) { + activity, + R.layout.subscription_row, + R.id.name, + LinkedList()) { override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { val result: View val v: ViewHolder @@ -56,11 +56,11 @@ class AddressListFragment : AbstractItemListFragment() val inflater = LayoutInflater.from(context) val view = inflater.inflate(R.layout.subscription_row, parent, false) v = ViewHolder( - ctx = context, - avatar = view.findViewById(R.id.avatar) as ImageView, - name = view.findViewById(R.id.name) as TextView, - streamNumber = view.findViewById(R.id.stream_number) as TextView, - subscribed = view.findViewById(R.id.subscribed) + ctx = context, + avatar = view.findViewById(R.id.avatar), + name = view.findViewById(R.id.name), + streamNumber = view.findViewById(R.id.stream_number), + subscribed = view.findViewById(R.id.subscribed) ) view.tag = v result = view @@ -89,12 +89,14 @@ class AddressListFragment : AbstractItemListFragment() fun updateList() { adapter.clear() - val addressRepo = Singleton.getAddressRepository(context) - doAsync { - addressRepo.getContactIds() + context?.let { context -> + val addressRepo = Singleton.getAddressRepository(context) + doAsync { + addressRepo.getContactIds() .map { addressRepo.getAddress(it) } .forEach { address -> uiThread { adapter.add(address) } } + } } } @@ -104,23 +106,23 @@ class AddressListFragment : AbstractItemListFragment() menu.add(R.string.scan_qr_code).setIcon(R.drawable.ic_action_qr_code) menu.add(R.string.create_contact).setIcon(R.drawable.ic_action_create_contact) FabUtils.initFab(activity, R.drawable.ic_action_add_contact, menu) - .addOnMenuItemClickListener { _, _, itemId -> - when (itemId) { - 1 -> IntentIntegrator.forSupportFragment(this@AddressListFragment) - .setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES) - .initiateScan() - 2 -> { - val intent = Intent(getActivity(), CreateAddressActivity::class.java) - startActivity(intent) - } - else -> { - } + .addOnMenuItemClickListener { _, _, itemId -> + when (itemId) { + 1 -> IntentIntegrator.forSupportFragment(this@AddressListFragment) + .setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES) + .initiateScan() + 2 -> { + val intent = Intent(getActivity(), CreateAddressActivity::class.java) + startActivity(intent) + } + else -> { } } + } } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View = - inflater.inflate(R.layout.fragment_address_list, container, false) + inflater.inflate(R.layout.fragment_address_list, container, false) override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { if (data != null && data.hasExtra("SCAN_RESULT")) { @@ -131,15 +133,13 @@ class AddressListFragment : AbstractItemListFragment() } } - override fun updateList(label: Void) { - updateList() - } + override fun updateList(label: Void) = updateList() private data class ViewHolder( - val ctx: Context, - val avatar: ImageView, - val name: TextView, - val streamNumber: TextView, - val subscribed: View + val ctx: Context, + val avatar: ImageView, + val name: TextView, + val streamNumber: TextView, + val subscribed: View ) } diff --git a/app/src/main/java/ch/dissem/apps/abit/ComposeMessageActivity.kt b/app/src/main/java/ch/dissem/apps/abit/ComposeMessageActivity.kt index 39310d2..dcea268 100644 --- a/app/src/main/java/ch/dissem/apps/abit/ComposeMessageActivity.kt +++ b/app/src/main/java/ch/dissem/apps/abit/ComposeMessageActivity.kt @@ -47,9 +47,9 @@ class ComposeMessageActivity : AppCompatActivity() { val fragment = ComposeMessageFragment() fragment.arguments = intent.extras supportFragmentManager - .beginTransaction() - .replace(R.id.content, fragment) - .commit() + .beginTransaction() + .replace(R.id.content, fragment) + .commit() } companion object { @@ -61,18 +61,19 @@ class ComposeMessageActivity : AppCompatActivity() { const val EXTRA_ENCODING = "ch.dissem.abit.Message.ENCODING" const val EXTRA_PARENT = "ch.dissem.abit.Message.PARENT" - fun launchReplyTo(fragment: Fragment, item: Plaintext) { - fragment.startActivity(getReplyIntent(fragment.activity, item)) - } + fun launchReplyTo(fragment: Fragment, item: Plaintext) = + fragment.startActivity(getReplyIntent( + ctx = fragment.activity ?: throw IllegalStateException("Fragment not attached to an activity"), + item = item + )) - fun launchReplyTo(activity: Activity, item: Plaintext) { + fun launchReplyTo(activity: Activity, item: Plaintext) = activity.startActivity(getReplyIntent(activity, item)) - } private fun getReplyIntent(ctx: Context, item: Plaintext): Intent { val replyIntent = Intent(ctx, ComposeMessageActivity::class.java) val receivingIdentity = item.to - if (receivingIdentity?.isChan ?: false) { + if (receivingIdentity?.isChan == true) { // reply to chan, not to the sender of the message replyIntent.putExtra(EXTRA_RECIPIENT, receivingIdentity) // I hate when people send as chan, so it won't be the default behaviour. @@ -96,7 +97,7 @@ class ComposeMessageActivity : AppCompatActivity() { replyIntent.putExtra(EXTRA_SUBJECT, prefix + subject) } replyIntent.putExtra(EXTRA_CONTENT, - "\n\n------------------------------------------------------\n" + item.text!!) + "\n\n------------------------------------------------------\n" + item.text!!) return replyIntent } } diff --git a/app/src/main/java/ch/dissem/apps/abit/ComposeMessageFragment.kt b/app/src/main/java/ch/dissem/apps/abit/ComposeMessageFragment.kt index 4174ace..75755ba 100644 --- a/app/src/main/java/ch/dissem/apps/abit/ComposeMessageFragment.kt +++ b/app/src/main/java/ch/dissem/apps/abit/ComposeMessageFragment.kt @@ -59,7 +59,7 @@ class ComposeMessageFragment : Fragment() { arguments?.let { arguments -> var id = arguments.getSerializable(EXTRA_IDENTITY) as? BitmessageAddress if (context != null && (id == null || id.privateKey == null)) { - id = Singleton.getIdentity(context) + id = Singleton.getIdentity(context!!) } if (id?.privateKey != null) { identity = id @@ -97,7 +97,7 @@ class ComposeMessageFragment : Fragment() { if (broadcast) { recipient_input.visibility = View.GONE } else { - val adapter = ContactAdapter(context) + val adapter = ContactAdapter(context!!) recipient_input.setAdapter(adapter) recipient_input.onItemClickListener = AdapterView.OnItemClickListener { _, _, pos, _ -> adapter.getItem(pos) } recipient_input.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { @@ -105,9 +105,7 @@ class ComposeMessageFragment : Fragment() { recipient = adapter.getItem(position) } - override fun onNothingSelected(parent: AdapterView<*>) { - // leave current selection - } + override fun onNothingSelected(parent: AdapterView<*>) = Unit // leave current selection } recipient?.let { recipient_input.setText(it.toString()) } } @@ -148,17 +146,16 @@ class ComposeMessageFragment : Fragment() { } } - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - if (requestCode == 0 && data != null && resultCode == RESULT_OK) { - encoding = data.getSerializableExtra(EXTRA_ENCODING) as Plaintext.Encoding - } else { - super.onActivityResult(requestCode, resultCode, data) - } + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) = if (requestCode == 0 && data != null && resultCode == RESULT_OK) { + encoding = data.getSerializableExtra(EXTRA_ENCODING) as Plaintext.Encoding + } else { + super.onActivityResult(requestCode, resultCode, data) } private fun send() { val builder: Plaintext.Builder - val bmc = Singleton.getBitmessageContext(context) + val ctx = activity ?: throw IllegalStateException("Fragment is not attached to an activity") + val bmc = Singleton.getBitmessageContext(ctx) if (broadcast) { builder = Plaintext.Builder(BROADCAST).from(identity) } else { @@ -167,7 +164,7 @@ class ComposeMessageFragment : Fragment() { try { recipient = BitmessageAddress(inputString) } catch (e: Exception) { - val contacts = Singleton.getAddressRepository(context).getContacts() + val contacts = Singleton.getAddressRepository(ctx).getContacts() for (contact in contacts) { if (inputString.equals(contact.alias, ignoreCase = true)) { recipient = contact @@ -186,7 +183,7 @@ class ComposeMessageFragment : Fragment() { .from(identity) .to(recipient) } - if (!Preferences.requestAcknowledgements(context)) { + if (!Preferences.requestAcknowledgements(ctx)) { builder.preventAck() } when (encoding) { @@ -203,8 +200,8 @@ class ComposeMessageFragment : Fragment() { ) else -> { Toast.makeText( - context, - context.getString(R.string.error_unsupported_encoding, encoding), + ctx, + ctx.getString(R.string.error_unsupported_encoding, encoding), Toast.LENGTH_LONG ).show() builder.message( @@ -214,7 +211,7 @@ class ComposeMessageFragment : Fragment() { } } bmc.send(builder.build()) - activity.finish() + ctx.finish() } } diff --git a/app/src/main/java/ch/dissem/apps/abit/CreateAddressActivity.kt b/app/src/main/java/ch/dissem/apps/abit/CreateAddressActivity.kt index 34f500a..e569d5a 100644 --- a/app/src/main/java/ch/dissem/apps/abit/CreateAddressActivity.kt +++ b/app/src/main/java/ch/dissem/apps/abit/CreateAddressActivity.kt @@ -46,9 +46,9 @@ class CreateAddressActivity : AppCompatActivity() { else setContentView(R.layout.activity_create_bitmessage_address) - val address = findViewById(R.id.address) as TextView - val label = findViewById(R.id.label) as EditText - val subscribe = findViewById(R.id.subscribe) as Switch + val address = findViewById(R.id.address) + val label = findViewById(R.id.label) + val subscribe = findViewById(R.id.subscribe) if (uri != null) { val addressText = getAddress(uri) @@ -70,12 +70,12 @@ class CreateAddressActivity : AppCompatActivity() { address.text = addressText } - val cancel = findViewById(R.id.cancel) as Button + val cancel = findViewById