aboutsummaryrefslogtreecommitdiffstats
path: root/openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/OpenPgpServiceConnection.kt
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2020-11-25 03:27:42 +0530
committerHarsh Shandilya <me@msfjarvis.dev>2020-11-29 17:51:16 +0530
commit7b25bf878315a894122e3de74e19add6fcdf5d4c (patch)
treeccb89e08265d628f8ff9a3de562ddb4252199dee /openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/OpenPgpServiceConnection.kt
parentfa2a93769f5cec8397064337fd53659147ba1877 (diff)
downloadHardenedPass-7b25bf878315a894122e3de74e19add6fcdf5d4c.tar.gz
HardenedPass-7b25bf878315a894122e3de74e19add6fcdf5d4c.zip
openpgp-ktx: import at 95eed95fb5654e9dcfe32c3712ae97385e186396
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/OpenPgpServiceConnection.kt')
-rw-r--r--openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/OpenPgpServiceConnection.kt88
1 files changed, 88 insertions, 0 deletions
diff --git a/openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/OpenPgpServiceConnection.kt b/openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/OpenPgpServiceConnection.kt
new file mode 100644
index 00000000..38465f8c
--- /dev/null
+++ b/openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/OpenPgpServiceConnection.kt
@@ -0,0 +1,88 @@
+/*
+ * Copyright © 2019 The Android Password Authors. All Rights Reserved.
+ * SPDX-License-Identifier: LGPL-3.0-only WITH LGPL-3.0-linking-exception
+ */
+package me.msfjarvis.openpgpktx.util
+
+import android.content.ComponentName
+import android.content.Context
+import android.content.Intent
+import android.content.ServiceConnection
+import android.os.IBinder
+import org.openintents.openpgp.IOpenPgpService2
+
+public class OpenPgpServiceConnection(context: Context, providerPackageName: String?) {
+
+ // callback interface
+ public interface OnBound {
+
+ public fun onBound(service: IOpenPgpService2)
+ public fun onError(e: Exception)
+ }
+
+ private val mApplicationContext: Context = context.applicationContext
+ public var service: IOpenPgpService2? = null
+ private set
+ private val mProviderPackageName: String? = providerPackageName
+ private var mOnBoundListener: OnBound? = null
+
+ /**
+ * Create new connection with callback
+ *
+ * @param context
+ * @param providerPackageName specify package name of OpenPGP provider,
+ * e.g., "org.sufficientlysecure.keychain"
+ * @param onBoundListener callback, executed when connection to service has been established
+ */
+ public constructor(
+ context: Context,
+ providerPackageName: String?,
+ onBoundListener: OnBound?
+ ) : this(context, providerPackageName) {
+ mOnBoundListener = onBoundListener
+ }
+
+ public val isBound: Boolean
+ get() = service != null
+
+ private val mServiceConnection: ServiceConnection = object : ServiceConnection {
+ override fun onServiceConnected(name: ComponentName, service: IBinder) {
+ this@OpenPgpServiceConnection.service = IOpenPgpService2.Stub.asInterface(service)
+ mOnBoundListener?.onBound(this@OpenPgpServiceConnection.service!!)
+ }
+
+ override fun onServiceDisconnected(name: ComponentName) {
+ service = null
+ }
+ }
+
+ /**
+ * If not already bound, bind to service!
+ */
+ public fun bindToService() {
+ if (service == null) {
+ // if not already bound...
+ try {
+ val serviceIntent = Intent(OpenPgpApi.SERVICE_INTENT_2)
+ // NOTE: setPackage is very important to restrict the intent to this provider only!
+ serviceIntent.setPackage(mProviderPackageName)
+ val connect = mApplicationContext.bindService(
+ serviceIntent, mServiceConnection,
+ Context.BIND_AUTO_CREATE
+ )
+ if (!connect) {
+ throw Exception("bindService() returned false!")
+ }
+ } catch (e: Exception) {
+ mOnBoundListener?.onError(e)
+ }
+ } else {
+ // already bound, but also inform client about it with callback
+ mOnBoundListener?.onBound(service!!)
+ }
+ }
+
+ public fun unbindFromService() {
+ mApplicationContext.unbindService(mServiceConnection)
+ }
+}