@@ -26,6 +26,7 @@ import android.view.MenuItem
26
26
import android.view.ViewGroup
27
27
import android.widget.*
28
28
import kotlinx.android.synthetic.main.activity_main.*
29
+ import kotlinx.android.synthetic.main.wifi_list_item.view.*
29
30
import net.glxn.qrgen.android.QRCode
30
31
import net.glxn.qrgen.core.scheme.Wifi
31
32
@@ -64,12 +65,35 @@ class MainActivity: AppCompatActivity() {
64
65
// The adapter class for the RecyclerView
65
66
inner class WifiListAdapter : RecyclerView .Adapter <WifiListAdapter .ViewHolder >(), Filterable {
66
67
// The ViewHolder class for the adapter
67
- inner class ViewHolder (val item : TextView ) : RecyclerView.ViewHolder(item) {
68
+ inner class ViewHolder (val item : LinearLayout ) : RecyclerView.ViewHolder(item) {
68
69
// The WifiEntry object associated with the list item
69
70
lateinit var wifiEntry: WifiEntry
70
71
init {
71
- // Set onClick action
72
- item.setOnClickListener {
72
+ fun createAndShowInfoDialog (): Boolean {
73
+ qrDialog = AlertDialog .Builder (this @MainActivity).apply {
74
+ setMessage(if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
75
+ Html .fromHtml(
76
+ " <b>SSID:</b> ${wifiEntry.title} <br>" +
77
+ if (wifiEntry.password != " " ) " <b>Password:</b> ${if (wifiEntry.type != WifiEntry .Type .WEP ) wifiEntry.password else wifiEntry.password.removePrefix(" \" " ).removeSuffix(" \" " )} <br>" else { " " } +
78
+ " <b>Type:</b> ${wifiEntry.type} " ,
79
+ Html .FROM_HTML_MODE_LEGACY )
80
+ } else {
81
+ @Suppress(" DEPRECATION" )
82
+ Html .fromHtml(
83
+ " <b>SSID:</b> ${wifiEntry.title} <br>" +
84
+ if (wifiEntry.password != " " ) " <b>Password:</b> ${if (wifiEntry.type != WifiEntry .Type .WEP ) wifiEntry.password else wifiEntry.password.removePrefix(" \" " ).removeSuffix(" \" " )} <br>" else { " " } +
85
+ " <b>Type:</b> ${wifiEntry.type} "
86
+ )
87
+ }
88
+ )
89
+ setPositiveButton(" Done" ) { dialog, _ -> dialog.dismiss() }
90
+ }.create()
91
+ qrDialog.show()
92
+ return true
93
+ }
94
+
95
+ // Set label onClick action
96
+ item.label.setOnClickListener {
73
97
qrDialog = AlertDialog .Builder (this @MainActivity).apply {
74
98
setTitle(wifiEntry.title)
75
99
setView(ImageView (this @MainActivity).apply {
@@ -88,40 +112,29 @@ class MainActivity: AppCompatActivity() {
88
112
}.create()
89
113
qrDialog.show()
90
114
}
91
- // Set onLongClick action
92
- item.setOnLongClickListener {
93
- qrDialog = AlertDialog .Builder (this @MainActivity).apply {
94
- setMessage(if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
95
- Html .fromHtml(
96
- " <b>SSID:</b> ${wifiEntry.title ? : " <i>ERROR</i>" } <br>" +
97
- if (wifiEntry.password != " " ) " <b>Password:</b> ${if (wifiEntry.type != WifiEntry .Type .WEP ) wifiEntry.password else wifiEntry.password.removePrefix(" \" " ).removeSuffix(" \" " )} <br>" else { " " } +
98
- " <b>Type:</b> ${wifiEntry.type ? : " </i>ERROR</i>" } " ,
99
- Html .FROM_HTML_MODE_LEGACY )
100
- } else {
101
- @Suppress(" DEPRECATION" )
102
- Html .fromHtml(
103
- " <b>SSID:</b> ${wifiEntry.title ? : " <i>ERROR</i>" } <br>" +
104
- if (wifiEntry.password != " " ) " <b>Password:</b> ${if (wifiEntry.type != WifiEntry .Type .WEP ) wifiEntry.password else wifiEntry.password.removePrefix(" \" " ).removeSuffix(" \" " )} <br>" else { " " } +
105
- " <b>Type:</b> ${wifiEntry.type ? : " </i>ERROR</i>" } "
106
- )
107
- }
108
- )
109
- setPositiveButton(" Done" ) { dialog, _ -> dialog.dismiss() }
110
- }.create()
111
- qrDialog.show()
112
- true
113
- }
115
+ // Set label onLongClick action
116
+ item.label.setOnLongClickListener { createAndShowInfoDialog() }
117
+
118
+ // Set security icon onClick action
119
+ item.security.setOnClickListener { createAndShowInfoDialog() }
114
120
}
115
121
}
116
122
117
123
// Creates and returns ViewHolders
118
124
override fun onCreateViewHolder (parent : ViewGroup , viewType : Int ) =
119
- ViewHolder (LayoutInflater .from(parent.context).inflate(R .layout.wifi_list_item, parent, false ) as TextView )
125
+ ViewHolder (LayoutInflater .from(parent.context).inflate(R .layout.wifi_list_item, parent, false ) as LinearLayout )
120
126
121
127
// Binds new data to recycled ViewHolders
122
128
override fun onBindViewHolder (holder : ViewHolder , position : Int ) {
123
129
holder.wifiEntry = wifiList[position]
124
- holder.item.text = holder.wifiEntry.title
130
+ holder.item.label.text = holder.wifiEntry.title
131
+ holder.item.security.setImageDrawable(
132
+ if (holder.wifiEntry.type == WifiEntry .Type .NONE ) {
133
+ ContextCompat .getDrawable(this @MainActivity, R .drawable.ic_signal_wifi_4_bar_24dp)
134
+ } else {
135
+ ContextCompat .getDrawable(this @MainActivity, R .drawable.ic_signal_wifi_4_bar_lock_24dp)
136
+ }
137
+ )
125
138
}
126
139
127
140
// Returns how many list items there are
0 commit comments