- Create BitsyApplication class to manage the connection to graphenej's NetworkService.

- Create ConnectedActivity which is the base activity that handles the communication to graphenej's NetworkService and will be extended by other apps that require communication to the BitShares nodes.
This commit is contained in:
Severiano Jaramillo 2018-11-22 13:35:32 -06:00
parent dfa5df7e83
commit fcbd82e570
9 changed files with 190 additions and 39 deletions

View file

@ -4,6 +4,7 @@
package="cy.agorise.bitsybitshareswallet">
<application
android:name=".utils.BitsyApplication"
android:allowBackup="true"
android:icon="@drawable/bts_logo"
android:label="@string/app_name"

View file

@ -0,0 +1,84 @@
package cy.agorise.bitsybitshareswallet.activities
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Handler
import android.os.IBinder
import android.preference.PreferenceManager
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import cy.agorise.bitsybitshareswallet.utils.Constants
import cy.agorise.graphenej.api.android.NetworkService
import cy.agorise.graphenej.api.calls.GetFullAccounts
import java.util.ArrayList
/**
* Class in charge of managing the connection to graphenej's NetworkService
*/
abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
private val TAG = "ConnectedActivity"
private val mHandler = Handler()
/* Network service connection */
protected var mNetworkService: NetworkService? = null
/**
* Flag used to keep track of the NetworkService binding state
*/
private var mShouldUnbindNetwork: Boolean = false
override fun onResume() {
super.onResume()
val intent = Intent(this, NetworkService::class.java)
if (bindService(intent, this, Context.BIND_AUTO_CREATE)) {
mShouldUnbindNetwork = true
} else {
Log.e(TAG, "Binding to the network service failed.")
}
// mHandler.postDelayed(mCheckMissingPaymentsTask, Constants.MISSING_PAYMENT_CHECK_PERIOD)
// storedOpCount = PreferenceManager.getDefaultSharedPreferences(this)
// .getLong(Constants.KEY_ACCOUNT_OPERATION_COUNT, -1)
}
/**
* Task used to perform a redundant payment check.
*/
// private val mCheckMissingPaymentsTask = object : Runnable {
// override fun run() {
// if (mNetworkService != null && mNetworkService.isConnected()) {
// val userId = PreferenceManager
// .getDefaultSharedPreferences(this@ConnectedActivity)
// .getString(Constants.KEY_CURRENT_ACCOUNT_ID, "")
// if (userId != "") {
// // Checking that we actually have a user id registered in the shared preferences
// val userAccounts = ArrayList<String>()
// userAccounts.add(userId)
// recurrentAccountUpdateId = mNetworkService.sendMessage(
// GetFullAccounts(userAccounts, false),
// GetFullAccounts.REQUIRED_API
// )
// } else {
// Log.w(TAG, "User id is empty")
// }
// } else {
// Log.w(TAG, "NetworkService is null or is not connected. mNetworkService: $mNetworkService")
// }
// mHandler.postDelayed(this, Constants.MISSING_PAYMENT_CHECK_PERIOD)
// }
// }
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
val binder = service as NetworkService.LocalBinder
mNetworkService = binder.service
}
override fun onServiceDisconnected(name: ComponentName?) {
}
}

View file

@ -1,10 +1,9 @@
package cy.agorise.bitsybitshareswallet.activities
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import cy.agorise.bitsybitshareswallet.R
class ImportBrainkeyActivity : AppCompatActivity() {
class ImportBrainkeyActivity : ConnectedActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_import_brainkey)

View file

@ -8,8 +8,6 @@ import cy.agorise.bitsybitshareswallet.R
import cy.agorise.bitsybitshareswallet.utils.Constants
import kotlinx.android.synthetic.main.activity_license.*
const val CURRENT_LICENSE_VERSION = 1
class LicenseActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
@ -21,7 +19,7 @@ class LicenseActivity : AppCompatActivity() {
.getInt(Constants.KEY_LAST_AGREED_LICENSE_VERSION, 0)
// If the last agreed license version is the actual one then proceed to the following Activities
if (agreedLicenseVersion == CURRENT_LICENSE_VERSION) {
if (agreedLicenseVersion == Constants.CURRENT_LICENSE_VERSION) {
agree()
} else {
wbLA.loadData(getString(R.string.licence_html), "text/html", "UTF-8")
@ -38,7 +36,7 @@ class LicenseActivity : AppCompatActivity() {
*/
private fun agree() {
PreferenceManager.getDefaultSharedPreferences(this).edit()
.putInt(Constants.KEY_LAST_AGREED_LICENSE_VERSION, CURRENT_LICENSE_VERSION).apply()
.putInt(Constants.KEY_LAST_AGREED_LICENSE_VERSION, Constants.CURRENT_LICENSE_VERSION).apply()
val intent : Intent?

View file

@ -0,0 +1,95 @@
package cy.agorise.bitsybitshareswallet.utils
import android.app.Activity
import android.app.Application
import android.os.Bundle
import cy.agorise.graphenej.api.ApiAccess
import cy.agorise.graphenej.api.android.NetworkServiceManager
class BitsyApplication : Application(), Application.ActivityLifecycleCallbacks {
val BITSHARES_NODE_URLS = arrayOf(
// PP private nodes
"wss://nl.palmpay.io/ws",
"wss://mx.palmpay.io/ws",
// Other public nodes
"wss://bitshares.nu/ws", // Stockholm, Sweden
"wss://bitshares.openledger.info/ws", // Openledger node
"wss://dallas.bitshares.apasia.tech/ws", // Dallas, USA
"wss://atlanta.bitshares.apasia.tech/ws", // Atlanta, USA
"wss://miami.bitshares.apasia.tech/ws", // Miami, USA
"wss://valley.bitshares.apasia.tech/ws", // Silicon Valley, USA
"wss://england.bitshares.apasia.tech/ws", // London, UK
"wss://netherlands.bitshares.apasia.tech/ws", // Amsterdam, Netherlands
"wss://australia.bitshares.apasia.tech/ws", // Sidney, Australia
"wss://bit.btsabc.org/ws", // Hong Kong, China
"wss://node.btscharts.com/ws", // Beijing, Chinawss://node.btscharts.com/ws
"wss://ws.gdex.top", // Shanghai, China
"wss://dex.rnglab.org", // Amsterdam, Netherlands
"wss://api.bts.blckchnd.com", // Falkenstein, Germany
"wss://api-ru.bts.blckchnd.com", // Moscow, Russia
"wss://crazybit.online", // Shenzhen, China?
"wss://citadel.li/node"
)
override fun onCreate() {
super.onCreate()
// Specifying some important information regarding the connection, such as the
// credentials and the requested API accesses
val requestedApis = ApiAccess.API_DATABASE or ApiAccess.API_HISTORY or ApiAccess.API_NETWORK_BROADCAST
val networkManager = NetworkServiceManager.Builder()
.setUserName("")
.setPassword("")
.setRequestedApis(requestedApis)
.setCustomNodeUrls(setupNodes())
.setAutoConnect(true)
.setNodeLatencyVerification(true)
.build(this)
/*
* Registering this class as a listener to all activity's callback cycle events, in order to
* better estimate when the user has left the app and it is safe to disconnect the websocket connection
*/
registerActivityLifecycleCallbacks(networkManager)
/*
* Registering this class as a listener to all activity's callback cycle events, in order to
* better estimate when the user has left the app and it is safe to disconnect the WebSocket connection
* TODO is it necessary??
*/
registerActivityLifecycleCallbacks(this)
}
private fun setupNodes(): String {
val stringBuilder = StringBuilder()
for (url in BITSHARES_NODE_URLS) {
stringBuilder.append(url).append(",")
}
stringBuilder.replace(stringBuilder.length - 1, stringBuilder.length, "")
return stringBuilder.toString()
}
override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {
//TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onActivityStarted(activity: Activity?) {
//TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onActivityResumed(activity: Activity?) {
//TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onActivityPaused(activity: Activity?) {
//TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onActivityStopped(activity: Activity?) { }
override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) { }
override fun onActivityDestroyed(activity: Activity?) { }
}

View file

@ -6,6 +6,9 @@ object Constants {
/** Key used to store the number of the last agreed License version */
const val KEY_LAST_AGREED_LICENSE_VERSION = "key_last_agreed_license_version"
/** Version of the currently used license */
const val CURRENT_LICENSE_VERSION = 1
/** Key used to store if the initial setup is already done or not */
const val KEY_INITIAL_SETUP_DONE = "key_initial_setup_done"

View file

@ -1,30 +0,0 @@
package cy.agorise.bitsybitshareswallet.utils
import android.content.Context
import android.util.AttributeSet
import androidx.annotation.Nullable
import androidx.appcompat.widget.AppCompatImageView
/**
* Created by xd on 1/24/18.
* ImageView which adjusts its size to always create a square
*/
class SquaredImageView : AppCompatImageView {
constructor(context: Context) : super(context)
constructor(context: Context, @Nullable attrs: AttributeSet) : super(context, attrs)
constructor(context: Context, @Nullable attrs: AttributeSet, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
)
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
val size = Math.min(measuredWidth, measuredHeight)
setMeasuredDimension(size, size)
}
}

View file

@ -93,7 +93,7 @@
android:layout_marginTop="@dimen/spacing_different_topic"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:backgroundTint="@color/colorPrimary"
android:text="@string/button__create"/>
android:text="@string/button__create"
android:enabled="false"/>
</LinearLayout>

View file

@ -2,7 +2,8 @@
<resources>
<color name="colorPrimary">#0099d6</color>
<color name="colorPrimaryDark">#006ba4</color>
<color name="colorAccent">#669900</color>
<color name="colorAccent">#0099d6</color>
<color name="black">#000</color>
<color name="gray">#888</color>
</resources>