Compare commits

...

2 Commits

7 changed files with 116 additions and 94 deletions

View File

@ -29,14 +29,7 @@
</receiver>
<activity android:name=".ChatActivity" android:theme="@style/Theme.AIRA.NoActionBar"/>
<activity android:name=".MainActivity" android:theme="@style/Theme.AIRA.NoActionBar">
<intent-filter android:label="@string/share_label">
<action android:name="android.intent.action.SEND"/>
<action android:name="android.intent.action.SEND_MULTIPLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="*/*"/>
</intent-filter>
</activity>
<activity android:name=".MainActivity" android:theme="@style/Theme.AIRA.NoActionBar"/>
<activity
android:name=".LoginActivity"
android:windowSoftInputMode="adjustResize">
@ -44,6 +37,12 @@
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter android:label="@string/share_label">
<action android:name="android.intent.action.SEND"/>
<action android:name="android.intent.action.SEND_MULTIPLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="*/*"/>
</intent-filter>
</activity>
<activity android:name=".SettingsActivity"/>
</application>

View File

@ -91,60 +91,67 @@ class ChatActivity : ServiceBoundActivity() {
val binder = service as AIRAService.AIRABinder
airaService = binder.getService()
chatAdapter.clear()
val session = airaService.sessions[sessionId]
val contact = airaService.contacts[sessionId]
val avatar = if (contact == null) {
displayIconTrustLevel(false, false)
sessionName = airaService.savedNames[sessionId]
airaService.savedAvatars[sessionId]
if (session == null && contact == null) { //may happen when resuming activity after session disconnect
onDisconnected()
} else {
displayIconTrustLevel(true, contact.verified)
sessionName = contact.name
contact.avatar
}
val ipName = sessionName ?: airaService.sessions[sessionId]!!.ip
binding.toolbar.title.text = ipName
if (avatar == null) {
binding.toolbar.avatar.setTextAvatar(sessionName)
} else {
AIRADatabase.loadAvatar(avatar)?.let { image ->
this@ChatActivity.avatar = image
binding.toolbar.avatar.setImageAvatar(image)
chatAdapter.clear()
val avatar = if (contact == null) {
displayIconTrustLevel(false, false)
sessionName = airaService.savedNames[sessionId]
airaService.savedAvatars[sessionId]
} else {
displayIconTrustLevel(true, contact.verified)
sessionName = contact.name
contact.avatar
}
}
if (contact != null) {
loadMsgs(contact.uuid)
}
airaService.savedMsgs[sessionId]?.let {
for (chatItem in it.asReversed()) {
chatAdapter.newLoadedMessage(chatItem)
val ipName = sessionName ?: airaService.sessions[sessionId]!!.ip
binding.toolbar.title.text = ipName
if (avatar == null) {
binding.toolbar.avatar.setTextAvatar(sessionName)
} else {
AIRADatabase.loadAvatar(avatar)?.let { image ->
this@ChatActivity.avatar = image
binding.toolbar.avatar.setImageAvatar(image)
}
}
}
airaService.receiveFileTransfers[sessionId]?.let {
if (it.shouldAsk) {
it.ask(this@ChatActivity, ipName)
if (contact != null) {
loadMsgs(contact.uuid)
}
}
binding.recyclerChat.smoothScrollToPosition(chatAdapter.itemCount)
val showBottomPanel = {
binding.bottomPanel.visibility = View.VISIBLE
airaService.savedMsgs[sessionId]?.let {
for (chatItem in it.asReversed()) {
chatAdapter.newLoadedMessage(chatItem)
}
}
airaService.receiveFileTransfers[sessionId]?.let {
if (it.shouldAsk) {
it.ask(this@ChatActivity, ipName)
}
}
binding.recyclerChat.smoothScrollToPosition(chatAdapter.itemCount)
if (airaService.isOnline(sessionId)) {
binding.bottomPanel.visibility = View.VISIBLE
binding.recyclerChat.updatePadding(bottom = 0)
} else {
onDisconnected()
}
airaService.setSeen(sessionId, true)
}
airaService.uiCallbacks = object : AIRAService.UiCallbacks {
override fun onConnectFailed(ip: String, errorMsg: String?) {}
override fun onNewSession(sessionId: Int, ip: String) {
if (this@ChatActivity.sessionId == sessionId) {
runOnUiThread {
showBottomPanel()
binding.bottomPanel.visibility = View.VISIBLE
invalidateOptionsMenu()
}
}
}
override fun onSessionDisconnect(sessionId: Int) {
if (this@ChatActivity.sessionId == sessionId) {
runOnUiThread {
val inputManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.hideSoftInputFromWindow(binding.editMessage.windowToken, 0)
binding.bottomPanel.visibility = View.GONE
invalidateOptionsMenu()
onDisconnected()
}
}
}
@ -197,17 +204,19 @@ class ChatActivity : ServiceBoundActivity() {
}
}
airaService.isAppInBackground = false
if (airaService.isOnline(sessionId)) {
showBottomPanel()
binding.recyclerChat.updatePadding(bottom = 0)
}
airaService.setSeen(sessionId, true)
}
override fun onServiceDisconnected(name: ComponentName?) {}
}
}
}
private fun onDisconnected() {
val inputManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.hideSoftInputFromWindow(binding.editMessage.windowToken, 0)
binding.bottomPanel.visibility = View.GONE
invalidateOptionsMenu()
}
private fun displayIconTrustLevel(isContact: Boolean, isVerified: Boolean) {
val setResource = fun (imageView: ImageView, resource: Int?) {
imageView.apply {

View File

@ -1,7 +1,7 @@
package sushi.hardcore.aira
import android.content.Context
import android.content.Intent
import android.os.Binder
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
@ -16,8 +16,12 @@ class CreateIdentityFragment(private val activity: AppCompatActivity) : Fragment
private external fun createNewIdentity(databaseFolder: String, name: String, password: ByteArray?): Boolean
companion object {
fun newInstance(activity: AppCompatActivity): CreateIdentityFragment {
return CreateIdentityFragment(activity)
fun newInstance(activity: AppCompatActivity, binder: Binder): CreateIdentityFragment {
return CreateIdentityFragment(activity).apply {
arguments = Bundle().apply {
putBinder(LoginActivity.BINDER_ARG, binder)
}
}
}
}
@ -71,17 +75,17 @@ class CreateIdentityFragment(private val activity: AppCompatActivity) : Fragment
}
private fun createIdentity(identityName: String, password: ByteArray?) {
val databaseFolder = Constants.getDatabaseFolder(requireContext())
if (createNewIdentity(
databaseFolder,
identityName,
password
)) {
val intent = Intent(activity, MainActivity::class.java)
intent.putExtra("identityName", identityName)
startActivity(intent)
activity.finish()
} else {
var success = false
arguments?.let { bundle ->
bundle.getBinder(LoginActivity.BINDER_ARG)?.let { binder ->
val databaseFolder = Constants.getDatabaseFolder(requireContext())
if (createNewIdentity(databaseFolder, identityName, password)) {
(binder as LoginActivity.ActivityLauncher).launch(identityName)
success = true
}
}
}
if (!success) {
Toast.makeText(activity, R.string.identity_create_failed, Toast.LENGTH_SHORT).show()
}
}

View File

@ -1,6 +1,7 @@
package sushi.hardcore.aira
import android.content.Intent
import android.os.Binder
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
@ -11,6 +12,8 @@ class LoginActivity : AppCompatActivity() {
private external fun getIdentityName(databaseFolder: String): String?
companion object {
const val NAME_ARG = "identityName"
const val BINDER_ARG = "binder"
private external fun initLogging()
init {
System.loadLibrary("aira")
@ -18,6 +21,12 @@ class LoginActivity : AppCompatActivity() {
}
}
inner class ActivityLauncher: Binder() {
fun launch(identityName: String) {
startMainActivity(identityName)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
@ -31,8 +40,7 @@ class LoginActivity : AppCompatActivity() {
val isProtected = AIRADatabase.isIdentityProtected(databaseFolder)
val name = getIdentityName(databaseFolder)
if (AIRAService.isServiceRunning) {
startActivity(Intent(this, MainActivity::class.java))
finish()
startMainActivity(null)
} else if (name != null && !isProtected) {
if (AIRADatabase.loadIdentity(databaseFolder, null)) {
AIRADatabase.clearCache()
@ -45,9 +53,9 @@ class LoginActivity : AppCompatActivity() {
.add(
R.id.fragment_container, if (name == null) {
AIRADatabase.removeIdentityAvatar(databaseFolder)
CreateIdentityFragment.newInstance(this)
CreateIdentityFragment.newInstance(this, ActivityLauncher())
} else {
LoginFragment.newInstance(name)
LoginFragment.newInstance(name, ActivityLauncher())
}
)
.commit()
@ -55,9 +63,11 @@ class LoginActivity : AppCompatActivity() {
}
private fun startMainActivity(identityName: String?) {
val intent = Intent(this, MainActivity::class.java)
intent.putExtra("identityName", identityName)
startActivity(intent)
val mainActivityIntent = Intent(this, MainActivity::class.java)
mainActivityIntent.action = intent.action
mainActivityIntent.putExtras(intent)
mainActivityIntent.putExtra(NAME_ARG, identityName)
startActivity(mainActivityIntent)
finish()
}
}

View File

@ -1,6 +1,5 @@
package sushi.hardcore.aira
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
@ -11,10 +10,12 @@ import sushi.hardcore.aira.databinding.FragmentLoginBinding
class LoginFragment : Fragment() {
companion object {
private const val NAME_ARG = "identityName"
fun newInstance(name: String): LoginFragment {
fun newInstance(name: String, binder: LoginActivity.ActivityLauncher): LoginFragment {
return LoginFragment().apply {
arguments = Bundle().apply { putString(NAME_ARG, name) }
arguments = Bundle().apply {
putBinder(LoginActivity.BINDER_ARG, binder)
putString(LoginActivity.NAME_ARG, name)
}
}
}
}
@ -28,24 +29,23 @@ class LoginFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
arguments?.let { bundle ->
bundle.getString(NAME_ARG)?.let { name ->
val databaseFolder = Constants.getDatabaseFolder(requireContext())
val avatar = AIRADatabase.getIdentityAvatar(databaseFolder)
if (avatar == null) {
binding.avatar.setTextAvatar(name)
} else {
binding.avatar.setImageAvatar(avatar)
}
binding.textIdentityName.text = name
binding.buttonLogin.setOnClickListener {
if (AIRADatabase.loadIdentity(databaseFolder, binding.editPassword.text.toString().toByteArray())) {
AIRADatabase.clearCache()
val intent = Intent(activity, MainActivity::class.java)
intent.putExtra("identityName", name)
startActivity(intent)
activity?.finish()
bundle.getString(LoginActivity.NAME_ARG)?.let { name ->
bundle.getBinder(LoginActivity.BINDER_ARG)?.let { binder ->
val databaseFolder = Constants.getDatabaseFolder(requireContext())
val avatar = AIRADatabase.getIdentityAvatar(databaseFolder)
if (avatar == null) {
binding.avatar.setTextAvatar(name)
} else {
Toast.makeText(activity, R.string.identity_load_failed, Toast.LENGTH_SHORT).show()
binding.avatar.setImageAvatar(avatar)
}
binding.textIdentityName.text = name
binding.buttonLogin.setOnClickListener {
if (AIRADatabase.loadIdentity(databaseFolder, binding.editPassword.text.toString().toByteArray())) {
AIRADatabase.clearCache()
(binder as LoginActivity.ActivityLauncher).launch(name)
} else {
Toast.makeText(activity, R.string.identity_load_failed, Toast.LENGTH_SHORT).show()
}
}
}
}

View File

@ -94,7 +94,7 @@ class MainActivity : ServiceBoundActivity() {
setContentView(binding.root)
setSupportActionBar(binding.toolbar.toolbar)
val identityName = intent.getStringExtra("identityName")
val identityName = intent.getStringExtra(LoginActivity.NAME_ARG)
identityName?.let {
initToolbar(it)
}

View File

@ -22,7 +22,7 @@ class ChatAdapter(
): RecyclerView.Adapter<RecyclerView.ViewHolder>() {
companion object {
const val CONTAINER_MARGIN = 70
const val CONTAINER_MARGIN = 150
const val BUBBLE_HORIZONTAL_PADDING = 40
}