aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/traccar/manager/StartFragment.kt
blob: ee379a2f2a5f2fe8de81e5a49e4dd30ceab54b2e (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
/*
 * Copyright 2016 - 2021 Anton Tananaev (anton@traccar.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
@file:Suppress("DEPRECATION")
package org.traccar.manager

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.app.Fragment
import android.net.Uri
import android.os.AsyncTask
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import androidx.preference.PreferenceManager
import org.json.JSONException
import org.json.JSONObject
import org.traccar.manager.StartFragment
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.URL

class StartFragment : Fragment(), View.OnClickListener {

    private lateinit var serverField: EditText
    private lateinit var startButton: Button

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_start, container, false)
        serverField = view.findViewById(R.id.field_server)
        startButton = view.findViewById(R.id.button_start)
        startButton.setOnClickListener(this)
        return view
    }

    @SuppressLint("StaticFieldLeak")
    override fun onClick(view: View) {
        startButton.isEnabled = false
        object : AsyncTask<String, Unit, Boolean>() {
            override fun doInBackground(vararg urls: String): Boolean {
                try {
                    val uri = Uri.parse(urls[0]).buildUpon().appendEncodedPath("api/server").build()
                    var url = uri.toString()
                    var urlConnection: HttpURLConnection? = null
                    for (i in 0 until MAX_REDIRECTS) {
                        val resourceUrl = URL(url)
                        urlConnection = resourceUrl.openConnection() as HttpURLConnection
                        urlConnection.instanceFollowRedirects = false
                        when (urlConnection.responseCode) {
                            HttpURLConnection.HTTP_MOVED_PERM, HttpURLConnection.HTTP_MOVED_TEMP -> {
                                url = urlConnection.getHeaderField("Location")
                                continue
                            }
                        }
                        break
                    }
                    val reader = BufferedReader(InputStreamReader(urlConnection?.inputStream))
                    var line: String?
                    val responseBuilder = StringBuilder()
                    while (reader.readLine().also { line = it } != null) {
                        responseBuilder.append(line)
                    }
                    JSONObject(responseBuilder.toString())
                    return true
                } catch (e: IOException) {
                    Log.w(TAG, e)
                } catch (e: JSONException) {
                    Log.w(TAG, e)
                }
                return false
            }

            override fun onPostExecute(result: Boolean) {
                if (activity != null) {
                    if (result) {
                        onSuccess()
                    } else {
                        onError()
                    }
                }
            }
        }.execute(serverField.text.toString())
    }

    private fun onSuccess() {
        PreferenceManager.getDefaultSharedPreferences(activity)
            .edit().putString(MainActivity.PREFERENCE_URL, serverField.text.toString()).apply()
        activity.fragmentManager
            .beginTransaction().replace(android.R.id.content, MainFragment())
            .commitAllowingStateLoss()
    }

    private fun onError() {
        startButton.isEnabled = true
        val alertDialog = AlertDialog.Builder(activity).create()
        alertDialog.setMessage(getString(R.string.error_connection))
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, getString(android.R.string.ok)) { dialog, _ -> dialog.dismiss() }
        alertDialog.show()
    }

    companion object {
        private val TAG = StartFragment::class.java.simpleName
        private const val MAX_REDIRECTS = 5
    }
}