summaryrefslogtreecommitdiff
path: root/main/src/main/java/de/blinkt/openvpn/activities/OpenSSLSpeed.java
blob: 4720dd602a9bb99d8f661b21c15862ee99191c15 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * 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.app.Activity;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Pair;
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 java.util.Locale;
import java.util.Vector;

import de.blinkt.openvpn.R;
import de.blinkt.openvpn.core.NativeUtils;
import de.blinkt.openvpn.core.OpenVPNService;

public class OpenSSLSpeed extends Activity {

    private static SpeeedTest runTestAlgorithms;
    private EditText mCipher;
    private SpeedArrayAdapter mAdapter;
    private ListView mListView;


    static class SpeedArrayAdapter extends ArrayAdapter<SpeedResult> {

        private final Context mContext;
        private final LayoutInflater mInflater;

        public SpeedArrayAdapter(@NonNull Context context) {
            super(context, 0);
            mContext = context;
            mInflater = LayoutInflater.from(context);

        }

        class ViewHolder {
            TextView ciphername;
            TextView blocksize;
            TextView blocksInTime;
            TextView speed;
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View view, @NonNull ViewGroup parent) {
            SpeedResult res = getItem(position);
            if (view == null) {
                view = mInflater.inflate(R.layout.speedviewitem, parent, false);
                ViewHolder holder = new ViewHolder();
                holder.ciphername = view.findViewById(R.id.ciphername);
                holder.speed = view.findViewById(R.id.speed);
                holder.blocksize = view.findViewById(R.id.blocksize);
                holder.blocksInTime = view.findViewById(R.id.blocksintime);
                view.setTag(holder);
            }

            ViewHolder holder = (ViewHolder) view.getTag();

            double total = res.count * res.length;
            String size = OpenVPNService.humanReadableByteCount((long) res.length, false, mContext.getResources());

            holder.blocksize.setText(size);
            holder.ciphername.setText(res.algorithm);

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

            return view;

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.openssl_speed);
        getActionBar().setDisplayHomeAsUpEnabled(true);

        findViewById(R.id.testSpecific).setOnClickListener((view) -> {
            runAlgorithms(mCipher.getText().toString());
        });
        mCipher = (EditText) findViewById(R.id.ciphername);

        mListView = findViewById(R.id.results);

        mAdapter = new SpeedArrayAdapter(this);
        mListView.setAdapter(mAdapter);

    }

    private void runAlgorithms(String algorithms) {
        if (runTestAlgorithms != null)
            runTestAlgorithms.cancel(true);
        runTestAlgorithms = new SpeeedTest();
        runTestAlgorithms.execute(algorithms.split(" "));
    }


    static class SpeedResult {
        String algorithm;
        boolean failed = false;

        double count;
        double time;
        int length;
        public boolean running=true;

        SpeedResult(String algorithm) {
            this.algorithm = algorithm;
        }
    }


    private class SpeeedTest extends AsyncTask<String, SpeedResult, SpeedResult[]> {


        private boolean mCancel = false;

        @Override
        protected SpeedResult[] doInBackground(String... strings) {
            Vector<SpeedResult> mResult = new Vector<>();

            for (String algorithm : strings) {

                // Skip 16b and 16k as they are not relevevant for VPN
                for (int i = 1; i < NativeUtils.openSSLlengths.length -1 && !mCancel; i++) {
                    SpeedResult result = new SpeedResult(algorithm);
                    result.length = NativeUtils.openSSLlengths[i];
                    mResult.add(result);
                    publishProgress(result);
                    double[] resi = NativeUtils.getOpenSSLSpeed(algorithm, i);
                    if (resi == null) {
                        result.failed = true;
                    } else {
                        result.count = resi[1];
                        result.time = resi[2];
                    }
                    result.running = false;
                    publishProgress(result);
                }
            }

            return mResult.toArray(new SpeedResult[mResult.size()]);

        }

        @Override
        protected void onProgressUpdate(SpeedResult... values) {
            for (SpeedResult r : values) {
                if (r.running)
                    mAdapter.add(r);
                mAdapter.notifyDataSetChanged();
            }
        }

        @Override
        protected void onPostExecute(SpeedResult[] speedResult) {

        }

        @Override
        protected void onCancelled(SpeedResult[] speedResults) {
            mCancel = true;
        }
    }


}