summaryrefslogtreecommitdiff
path: root/main/src/ui/java/de/blinkt/openvpn/activities/OpenSSLSpeed.kt
blob: 324cd881b0220245fed634337660e351151fd5c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright (c) 2012-2017 Arne Schwabe
 * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt
 */

package de.blinkt.openvpn.activities

import android.content.Context
import android.os.AsyncTask
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.EditText
import android.widget.ListView
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
import de.blinkt.openvpn.R
import de.blinkt.openvpn.core.NativeUtils
import de.blinkt.openvpn.core.OpenVPNService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.*

class OpenSSLSpeed : BaseActivity() {
    private lateinit var mCipher: EditText
    private lateinit var mAdapter: SpeedArrayAdapter
    private lateinit var mListView: ListView


    internal class SpeedArrayAdapter(private val mContext: Context) :
        ArrayAdapter<SpeedResult>(mContext, 0) {
        private val mInflater: LayoutInflater

        init {
            mInflater = LayoutInflater.from(mContext)

        }

        internal data class ViewHolder(
            var ciphername: TextView,
            var speed: TextView,
            var blocksize: TextView,
            var blocksInTime: TextView
        )

        override fun getView(position: Int, v: View?, parent: ViewGroup): View {
            var view = v
            val res = getItem(position)
            if (view == null) {
                view = mInflater.inflate(R.layout.speedviewitem, parent, false)!!
                val holder = ViewHolder(
                    view.findViewById(R.id.ciphername),
                    view.findViewById(R.id.speed),
                    view.findViewById(R.id.blocksize),
                    view.findViewById(R.id.blocksintime)
                )
                view.tag = holder
            }

            val holder = view.tag as ViewHolder

            val total = res!!.count * res.length
            val size = OpenVPNService.humanReadableByteCount(
                res.length.toLong(),
                false,
                mContext.resources
            )

            holder.blocksize.text = size
            holder.ciphername.text = res.algorithm

            if (res.failed) {
                holder.blocksInTime.setText(R.string.openssl_error)
                holder.speed.text = "-"
            } else if (res.running) {
                holder.blocksInTime.setText(R.string.running_test)
                holder.speed.text = "-"
            } else {
                val totalBytes =
                    OpenVPNService.humanReadableByteCount(total.toLong(), false, mContext.resources)
                // TODO: Fix localisation here
                val blockPerSec = OpenVPNService.humanReadableByteCount(
                    (total / res.time).toLong(),
                    false,
                    mContext.resources
                ) + "/s"
                holder.speed.text = blockPerSec
                holder.blocksInTime.text = String.format(
                    Locale.ENGLISH,
                    "%d blocks (%s) in %2.1fs",
                    res.count.toLong(),
                    totalBytes,
                    res.time
                )
            }

            return view

        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.openssl_speed)
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)

        findViewById<View>(R.id.testSpecific).setOnClickListener { _ -> runAlgorithms(mCipher.text.toString()) }
        mCipher = findViewById<View>(R.id.ciphername) as EditText

        mListView = findViewById(R.id.results)

        mAdapter = SpeedArrayAdapter(this)
        mListView.adapter = mAdapter

    }

    private fun runAlgorithms(algorithms: String) {
        lifecycleScope.launch {
            runSpeedTest(algorithms)
        }
    }


    internal class SpeedResult(var algorithm: String) {
        var failed = false

        var count: Double = 0.toDouble()
        var time: Double = 0.toDouble()
        var length: Int = 0
        var running = true
    }

    internal suspend fun showResults(vararg values: SpeedResult) {
        withContext(Dispatchers.Main) {
            for (r in values) {
                if (r.running)
                    mAdapter.add(r)
                mAdapter.notifyDataSetChanged()
            }
        }
    }

    suspend fun runSpeedTest(algorithms: String) {
        withContext(Dispatchers.IO)
        {
            val mResult = Vector<SpeedResult>()

            for (algorithm in algorithms.split(" ")) {
                // Skip 16b and 16k as they are not relevevant for VPN
                var i = 1
                while (i < NativeUtils.openSSLlengths.size - 1) {
                    val result = SpeedResult(algorithm)
                    result.length = NativeUtils.openSSLlengths[i]
                    mResult.add(result)
                    showResults(result)
                    val resi = NativeUtils.getOpenSSLSpeed(algorithm, i)
                    if (resi == null) {
                        result.failed = true
                    } else {
                        result.count = resi[1]
                        result.time = resi[2]
                    }
                    result.running = false
                    showResults(result)
                    i++
                }
            }
        }
    }

}