-
-
Notifications
You must be signed in to change notification settings - Fork 189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement proxy preferences #726
base: master
Are you sure you want to change the base?
Changes from all commits
8e65c86
66c83f6
ce83c52
c73843e
a1ea52d
f945e3f
84a3e7a
a93f491
b889dcd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,8 +30,16 @@ import de.grobox.transportr.R | |
import de.schildbach.pte.NetworkId | ||
import de.schildbach.pte.NetworkProvider.Optimize | ||
import de.schildbach.pte.NetworkProvider.WalkSpeed | ||
import okhttp3.OkHttpClient | ||
import okhttp3.Request | ||
import java.lang.Exception | ||
import java.net.InetAddress | ||
import java.net.InetSocketAddress | ||
import java.net.Proxy | ||
import java.net.UnknownHostException | ||
import java.util.* | ||
import javax.inject.Inject | ||
import kotlin.Throws | ||
|
||
|
||
class SettingsManager @Inject constructor(private val context: Context) { | ||
|
@@ -92,6 +100,23 @@ class SettingsManager @Inject constructor(private val context: Context) { | |
} | ||
} | ||
|
||
@Throws(UnknownHostException::class, IllegalStateException::class) | ||
fun getProxy(proxyPrefOverrides: Map<String, Any>): Proxy { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All uses of this function seem to set at most one new setting. Why not simply using a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right but I think the code is actually more readable with the map compared to a single Compare val isEnabled = proxyPrefOverrides[PROXY_ENABLE] as Boolean? ?: settings.getBoolean(PROXY_ENABLE, false) to val isEnabled =
if (proxyPrefOverride?.first == PROXY_ENABLE && proxyPrefOverride.second is Boolean)
proxyPrefOverride.second as Boolean
else
settings.getBoolean(PROXY_ENABLE, false) or (avoiding the explicit cast) val overrideKey = proxyPrefOverride?.first
val overrideVal = proxyPrefOverride?.second
val isEnabled = if (overrideKey == PROXY_ENABLE && overrideVal is Boolean)
overrideVal else settings.getBoolean(PROXY_ENABLE, false) |
||
val isEnabled = proxyPrefOverrides[PROXY_ENABLE] as Boolean? ?: settings.getBoolean(PROXY_ENABLE, false) | ||
if (!isEnabled) | ||
return Proxy.NO_PROXY | ||
val typeStr = proxyPrefOverrides[PROXY_PROTOCOL] as String? ?: settings.getString(PROXY_PROTOCOL, null) | ||
val type = when (typeStr) { | ||
"SOCKS" -> Proxy.Type.SOCKS | ||
"HTTP" -> Proxy.Type.HTTP | ||
else -> throw IllegalStateException("Illegal proxy type: " + typeStr) | ||
} | ||
val host = proxyPrefOverrides[PROXY_HOST] as String? ?: settings.getString(PROXY_HOST, null) | ||
val portStr = proxyPrefOverrides[PROXY_PORT] as String? ?: settings.getString(PROXY_PORT, null) | ||
val port = Integer.parseInt(portStr!!) | ||
return Proxy(type, InetSocketAddress(InetAddress.getByName(host), port)) | ||
} | ||
|
||
fun showLocationFragmentOnboarding(): Boolean = settings.getBoolean(LOCATION_ONBOARDING, true) | ||
fun locationFragmentOnboardingShown() { | ||
settings.edit().putBoolean(LOCATION_ONBOARDING, false).apply() | ||
|
@@ -136,6 +161,8 @@ class SettingsManager @Inject constructor(private val context: Context) { | |
} | ||
|
||
companion object { | ||
private const val TAG = "SettingsManager" | ||
|
||
private const val NETWORK_ID_1 = "NetworkId" | ||
private const val NETWORK_ID_2 = "NetworkId2" | ||
private const val NETWORK_ID_3 = "NetworkId3" | ||
|
@@ -147,6 +174,10 @@ class SettingsManager @Inject constructor(private val context: Context) { | |
private const val OPTIMIZE = "pref_key_optimize" | ||
private const val LOCATION_ONBOARDING = "locationOnboarding" | ||
private const val TRIP_DETAIL_ONBOARDING = "tripDetailOnboarding" | ||
internal const val PROXY_ENABLE = "pref_key_proxy_enable" | ||
internal const val PROXY_PROTOCOL = "pref_key_proxy_protocol" | ||
internal const val PROXY_HOST = "pref_key_proxy_host" | ||
internal const val PROXY_PORT = "pref_key_proxy_port" | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Transportr | ||
* | ||
* Copyright (c) 2013 - 2020 Torsten Grote | ||
* | ||
* This program is Free Software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package de.grobox.transportr.ui | ||
|
||
import android.content.Context | ||
import android.content.res.TypedArray | ||
import android.text.Editable | ||
import android.text.TextWatcher | ||
import android.util.AttributeSet | ||
import android.view.inputmethod.InputMethodManager | ||
import android.widget.Button | ||
import android.widget.EditText | ||
import android.widget.TextView | ||
import androidx.appcompat.app.AlertDialog | ||
import androidx.preference.Preference | ||
import de.grobox.transportr.R | ||
|
||
/** | ||
* An editable text preference that gives immediate user feedback by | ||
* disabling the "OK" button of the edit dialog if the entered text | ||
* does not match the validation criteria. | ||
* | ||
* Validation can be configured by setting the <code>validate</code> | ||
* property. | ||
*/ | ||
class ValidatedEditTextPreference(ctx: Context, attrs: AttributeSet) : | ||
Preference(ctx, attrs, R.attr.preferenceStyle) { | ||
|
||
var validate: ((String) -> Boolean) = { true } | ||
private var persistedValue: String = "" | ||
private var currentValue: String = persistedValue | ||
|
||
init { | ||
setSummaryProvider { persistedValue } | ||
|
||
setOnPreferenceClickListener { | ||
currentValue = persistedValue | ||
|
||
val dialog = AlertDialog.Builder(ctx) | ||
.setTitle(title) | ||
.setView(R.layout.dialog_validated_edit_text_preference) | ||
.setCancelable(true) | ||
.setPositiveButton(R.string.ok) { dialogInterface, _ -> | ||
dialogInterface.dismiss() | ||
if (callChangeListener(currentValue)) { | ||
persistString(currentValue) | ||
persistedValue = currentValue | ||
notifyChanged() | ||
} | ||
} | ||
.setNegativeButton(R.string.cancel) { dialogInterface, _ -> | ||
dialogInterface.cancel() | ||
currentValue = persistedValue | ||
} | ||
.create() | ||
dialog.show() | ||
|
||
val editText = dialog.findViewById<EditText>(R.id.text_input)!! | ||
val okButton = dialog.findViewById<Button>(android.R.id.button1)!! | ||
|
||
fun updateUIForValidity() { | ||
okButton.isEnabled = validate(currentValue) | ||
} | ||
updateUIForValidity() | ||
|
||
editText.setText(currentValue, TextView.BufferType.NORMAL) | ||
editText.addTextChangedListener(object : TextWatcher { | ||
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {} | ||
|
||
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {} | ||
|
||
override fun afterTextChanged(text: Editable?) { | ||
currentValue = text.toString() | ||
updateUIForValidity() | ||
} | ||
}) | ||
editText.postDelayed({ | ||
editText.requestFocus() | ||
editText.setSelection(currentValue.length) | ||
val inputMethodManager = ctx.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager | ||
inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT) | ||
}, 100) | ||
|
||
true | ||
} | ||
} | ||
|
||
override fun onGetDefaultValue(a: TypedArray?, index: Int): String? { | ||
return a!!.getString(index) | ||
} | ||
|
||
override fun onSetInitialValue(defaultValue: Any?) { | ||
super.onSetInitialValue(defaultValue) | ||
persistedValue = getPersistedString(defaultValue?.toString()) ?: persistedValue | ||
currentValue = persistedValue | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:paddingHorizontal="20dp"> | ||
|
||
<EditText android:id="@+id/text_input" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content"/> | ||
|
||
</LinearLayout> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer a less disturbing Toast message at this point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it would be a good idea to handle some common errors using a Toast with a simplified message and use an AlertDialog with the Exception's message for anything unexpected?