summaryrefslogtreecommitdiff
path: root/main/src/ui/java/de/blinkt/openvpn/fragments/Settings_Connections.kt
blob: 8ed9d9efd73449b5a8e57b7492e0dd0b723ffb6c (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
/*
 * Copyright (c) 2012-2016 Arne Schwabe
 * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt
 */
package de.blinkt.openvpn.fragments

import android.os.Build
import android.os.Bundle
import android.view.*
import android.widget.Checkable
import android.widget.ImageButton
import android.widget.TextView
import de.blinkt.openvpn.fragments.Settings_Fragment
import de.blinkt.openvpn.fragments.ConnectionsAdapter
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.LinearLayoutManager
import de.blinkt.openvpn.R

class Settings_Connections : Settings_Fragment(), View.OnClickListener {
    private lateinit var mConnectionsAdapter: ConnectionsAdapter
    private lateinit var mWarning: TextView
    private lateinit var mUseRandomRemote: Checkable
    private lateinit var mRecyclerView: RecyclerView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setHasOptionsMenu(true)
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
            inflater.inflate(R.menu.connections, menu)
        super.onCreateOptionsMenu(menu, inflater)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    )
            : View? {
        val v = inflater.inflate(R.layout.connections, container, false)
        mWarning = v.findViewById<View>(R.id.noserver_active_warning) as TextView
        mRecyclerView = v.findViewById<View>(R.id.connection_recycler_view) as RecyclerView
        val dpwidth = (container!!.width / resources.displayMetrics.density).toInt()
        var columns = dpwidth / 290
        columns = 1.coerceAtLeast(columns)
        mConnectionsAdapter = ConnectionsAdapter(activity, this, mProfile)

        //mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(columns, StaggeredGridLayoutManager.VERTICAL));
        mRecyclerView.setHasFixedSize(true)
        mRecyclerView.layoutManager =
            LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
        mRecyclerView.adapter = mConnectionsAdapter
        val fab_button = v.findViewById<View>(R.id.add_new_remote) as ImageButton?
        fab_button?.setOnClickListener(this)
        mUseRandomRemote = v.findViewById<View>(R.id.remote_random) as Checkable
        mUseRandomRemote.isChecked = mProfile.mRemoteRandom
        mConnectionsAdapter.displayWarningIfNoneEnabled()
        return v
    }

    override fun onClick(v: View) {
        if (v.id == R.id.add_new_remote) {
            mConnectionsAdapter.addRemote()
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == R.id.add_new_remote) mConnectionsAdapter.addRemote()
        return super.onOptionsItemSelected(item)
    }

    override fun savePreferences() {
        mConnectionsAdapter.saveProfile()
        mProfile.mRemoteRandom = mUseRandomRemote.isChecked
    }

    fun setWarningVisible(showWarning: Int) {
        mWarning.visibility = showWarning
    }
}