Handle share intents when not logged in

This commit is contained in:
Matéo Duparc 2021-06-04 11:48:43 +02:00
parent ad36b78df7
commit ccb32b7fb6
Signed by: hardcoresushi
GPG Key ID: 007F84120107191E
5 changed files with 64 additions and 51 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

@ -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)
}