Fix Parcelable

This commit is contained in:
Mygod
2019-02-01 15:18:49 +08:00
parent 4de891b459
commit 13e4819511
7 changed files with 36 additions and 22 deletions

View File

@@ -1,5 +1,6 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
if (getGradle().getStartParameter().getTaskRequests().toString().contains("Base"))
@@ -60,6 +61,10 @@ android {
}
}
androidExtensions {
experimental = true
}
dependencies {
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycleVersion"
kapt "androidx.room:room-compiler:$roomVersion"

View File

@@ -2,6 +2,7 @@ package be.mygod.vpnhotspot
import android.content.DialogInterface
import android.os.Bundle
import android.os.Parcelable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@@ -12,10 +13,10 @@ import android.widget.Spinner
import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.versionedparcelable.VersionedParcelable
import be.mygod.vpnhotspot.util.launchUrl
import be.mygod.vpnhotspot.widget.SmartSnackbar
import com.android.billingclient.api.*
import kotlinx.android.parcel.Parcelize
import timber.log.Timber
/**
@@ -23,7 +24,8 @@ import timber.log.Timber
*/
class EBegFragment : AppCompatDialogFragment(), PurchasesUpdatedListener, BillingClientStateListener,
SkuDetailsResponseListener, ConsumeResponseListener {
data class MessageArg(@StringRes val title: Int, @StringRes val message: Int) : VersionedParcelable
@Parcelize
data class MessageArg(@StringRes val title: Int, @StringRes val message: Int) : Parcelable
class MessageDialogFragment : AlertDialogFragment<MessageArg, Empty>() {
override fun AlertDialog.Builder.prepare(listener: DialogInterface.OnClickListener) {
setTitle(arg.title)

View File

@@ -4,36 +4,33 @@ import android.app.Activity
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.versionedparcelable.ParcelUtils
import androidx.versionedparcelable.VersionedParcelable
import kotlinx.android.parcel.Parcelize
/**
* Based on: https://android.googlesource.com/platform/packages/apps/ExactCalculator/+/8c43f06/src/com/android/calculator2/AlertDialogFragment.java
*/
abstract class AlertDialogFragment<Arg : VersionedParcelable, Ret : VersionedParcelable> :
abstract class AlertDialogFragment<Arg : Parcelable, Ret : Parcelable> :
AppCompatDialogFragment(), DialogInterface.OnClickListener {
companion object {
private const val KEY_ARG = "arg"
private const val KEY_RET = "ret"
fun <T : VersionedParcelable> getRet(data: Intent) =
ParcelUtils.getVersionedParcelable<T>(data.extras, KEY_RET)!!
fun <T : Parcelable> getRet(data: Intent) = data.extras!!.getParcelable<T>(KEY_RET)!!
}
protected abstract fun AlertDialog.Builder.prepare(listener: DialogInterface.OnClickListener)
protected val arg by lazy { ParcelUtils.getVersionedParcelable<Arg>(arguments, KEY_ARG)!! }
protected val arg by lazy { arguments!!.getParcelable<Arg>(KEY_ARG)!! }
protected open val ret: Ret? get() = null
fun withArg(arg: Arg) = apply {
arguments = Bundle().also { ParcelUtils.putVersionedParcelable(it, KEY_ARG, arg) }
}
fun withArg(arg: Arg) = apply { arguments = Bundle().apply { putParcelable(KEY_ARG, arg) } }
override fun onCreateDialog(savedInstanceState: Bundle?): AlertDialog =
AlertDialog.Builder(requireContext()).also { it.prepare(this) }.create()
override fun onClick(dialog: DialogInterface?, which: Int) {
targetFragment?.onActivityResult(targetRequestCode, which, ret?.let {
Intent().replaceExtras(Bundle().apply { ParcelUtils.putVersionedParcelable(this, KEY_RET, it) })
Intent().replaceExtras(Bundle().apply { putParcelable(KEY_RET, it) })
})
}
@@ -43,4 +40,5 @@ abstract class AlertDialogFragment<Arg : VersionedParcelable, Ret : VersionedPar
}
}
class Empty : VersionedParcelable
@Parcelize
class Empty : Parcelable

View File

@@ -2,6 +2,7 @@ package be.mygod.vpnhotspot.client
import android.content.DialogInterface
import android.os.Bundle
import android.os.Parcelable
import android.text.format.DateUtils
import android.text.format.Formatter
import android.text.method.LinkMovementMethod
@@ -23,7 +24,6 @@ import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import androidx.versionedparcelable.VersionedParcelable
import be.mygod.vpnhotspot.AlertDialogFragment
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.Empty
@@ -41,6 +41,7 @@ import be.mygod.vpnhotspot.util.SpanFormatter
import be.mygod.vpnhotspot.util.computeIfAbsentCompat
import be.mygod.vpnhotspot.util.toPluralInt
import be.mygod.vpnhotspot.widget.SmartSnackbar
import kotlinx.android.parcel.Parcelize
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
@@ -48,7 +49,8 @@ import kotlinx.coroutines.launch
import java.text.NumberFormat
class ClientsFragment : Fragment(), MainScope by MainScope.Supervisor() {
data class NicknameArg(val mac: Long, val nickname: CharSequence) : VersionedParcelable
@Parcelize
data class NicknameArg(val mac: Long, val nickname: CharSequence) : Parcelable
class NicknameDialogFragment : AlertDialogFragment<NicknameArg, Empty>() {
override fun AlertDialog.Builder.prepare(listener: DialogInterface.OnClickListener) {
setView(R.layout.dialog_nickname)
@@ -76,7 +78,8 @@ class ClientsFragment : Fragment(), MainScope by MainScope.Supervisor() {
}
}
data class StatsArg(val title: CharSequence, val stats: ClientStats) : VersionedParcelable
@Parcelize
data class StatsArg(val title: CharSequence, val stats: ClientStats) : Parcelable
class StatsDialogFragment : AlertDialogFragment<StatsArg, Empty>() {
override fun AlertDialog.Builder.prepare(listener: DialogInterface.OnClickListener) {
setTitle(SpanFormatter.format(getString(R.string.clients_stats_title), arg.title))

View File

@@ -8,6 +8,7 @@ import android.net.wifi.WifiConfiguration
import android.net.wifi.p2p.WifiP2pGroup
import android.os.Bundle
import android.os.IBinder
import android.os.Parcelable
import android.text.method.LinkMovementMethod
import android.view.WindowManager
import android.widget.EditText
@@ -19,7 +20,6 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.lifecycle.get
import androidx.recyclerview.widget.RecyclerView
import androidx.versionedparcelable.VersionedParcelable
import be.mygod.vpnhotspot.*
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.databinding.ListitemRepeaterBinding
@@ -28,6 +28,7 @@ import be.mygod.vpnhotspot.net.wifi.WifiP2pDialogFragment
import be.mygod.vpnhotspot.util.ServiceForegroundConnector
import be.mygod.vpnhotspot.util.formatAddresses
import be.mygod.vpnhotspot.widget.SmartSnackbar
import kotlinx.android.parcel.Parcelize
import timber.log.Timber
import java.net.NetworkInterface
import java.net.SocketException
@@ -112,7 +113,8 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
}
}
data class WpsRet(val pin: String?) : VersionedParcelable
@Parcelize
data class WpsRet(val pin: String?) : Parcelable
class WpsDialogFragment : AlertDialogFragment<Empty, WpsRet>() {
override fun AlertDialog.Builder.prepare(listener: DialogInterface.OnClickListener) {
setTitle(R.string.repeater_wps_dialog_title)

View File

@@ -3,16 +3,17 @@ package be.mygod.vpnhotspot.net.wifi
import android.content.DialogInterface
import android.net.wifi.WifiConfiguration
import android.net.wifi.WifiConfiguration.AuthAlgorithm
import android.os.Parcelable
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.versionedparcelable.VersionedParcelable
import be.mygod.vpnhotspot.AlertDialogFragment
import be.mygod.vpnhotspot.R
import com.google.android.material.textfield.TextInputLayout
import kotlinx.android.parcel.Parcelize
import java.nio.charset.Charset
/**
@@ -22,7 +23,8 @@ import java.nio.charset.Charset
* Related: https://android.googlesource.com/platform/packages/apps/Settings/+/defb1183ecb00d6231bac7d934d07f58f90261ea
*/
class WifiP2pDialogFragment : AlertDialogFragment<WifiP2pDialogFragment.Arg, WifiP2pDialogFragment.Arg>(), TextWatcher {
data class Arg(val configuration: WifiConfiguration) : VersionedParcelable
@Parcelize
data class Arg(val configuration: WifiConfiguration) : Parcelable
private lateinit var mView: View
private lateinit var mSsid: TextView

View File

@@ -1,7 +1,8 @@
package be.mygod.vpnhotspot.room
import android.os.Parcelable
import androidx.room.*
import androidx.versionedparcelable.VersionedParcelable
import kotlinx.android.parcel.Parcelize
import java.net.InetAddress
@Entity(foreignKeys = [ForeignKey(entity = TrafficRecord::class, parentColumns = ["id"], childColumns = ["previousId"],
@@ -61,6 +62,7 @@ data class TrafficRecord(
}
}
@Parcelize
data class ClientStats(
val timestamp: Long = 0,
val count: Long = 0,
@@ -68,4 +70,4 @@ data class ClientStats(
val sentBytes: Long = 0,
val receivedPackets: Long = 0,
val receivedBytes: Long = 0
) : VersionedParcelable
) : Parcelable