-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add nfc functionality; - handle nfc message in code reader fragment; - add base fragment with binding;
- Loading branch information
1 parent
0f8e558
commit 62530cb
Showing
8 changed files
with
297 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
app/src/main/java/dgca/verifier/app/android/base/BindingFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* ---license-start | ||
* eu-digital-green-certificates / dgca-verifier-app-android | ||
* --- | ||
* Copyright (C) 2021 T-Systems International GmbH and all other contributors | ||
* --- | ||
* 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. | ||
* ---license-end | ||
* | ||
* Created by mykhailo.nester on 25/08/2021, 15:19 | ||
*/ | ||
|
||
package dgca.verifier.app.android.base | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.fragment.app.Fragment | ||
import androidx.viewbinding.ViewBinding | ||
|
||
abstract class BindingFragment<T : ViewBinding> : Fragment() { | ||
|
||
private var _binding: T? = null | ||
val binding get() = _binding!! | ||
|
||
abstract fun onCreateBinding(inflater: LayoutInflater, container: ViewGroup?): T | ||
|
||
open fun onDestroyBinding(binding: T) { | ||
} | ||
|
||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { | ||
val innerBinding = onCreateBinding(inflater, container) | ||
_binding = innerBinding | ||
return innerBinding.root | ||
} | ||
|
||
override fun onDestroyView() { | ||
val innerBinding = _binding | ||
if (innerBinding != null) { | ||
onDestroyBinding(innerBinding) | ||
} | ||
|
||
_binding = null | ||
|
||
super.onDestroyView() | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
app/src/main/java/dgca/verifier/app/android/nfc/NdefParser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* ---license-start | ||
* eu-digital-green-certificates / dgca-verifier-app-android | ||
* --- | ||
* Copyright (C) 2021 T-Systems International GmbH and all other contributors | ||
* --- | ||
* 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. | ||
* ---license-end | ||
* | ||
* Created by mykhailo.nester on 17/08/2021, 18:52 | ||
*/ | ||
|
||
package dgca.verifier.app.android.nfc | ||
|
||
import android.nfc.NdefMessage | ||
import android.nfc.NdefRecord | ||
import timber.log.Timber | ||
import java.io.UnsupportedEncodingException | ||
import java.util.* | ||
import kotlin.experimental.and | ||
|
||
object NdefParser { | ||
|
||
fun parse(message: NdefMessage): List<ParsedNdefRecord> = getRecords(message.records) | ||
|
||
private fun getRecords(records: Array<NdefRecord>): List<ParsedNdefRecord> = | ||
records.map { | ||
it.parse() ?: object : ParsedNdefRecord { | ||
override fun str(): String { | ||
return String(it.payload) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun NdefRecord.parse(): ParsedNdefRecord? { | ||
return if (tnf == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(type, NdefRecord.RTD_TEXT)) { | ||
try { | ||
val recordPayload = payload | ||
|
||
/* | ||
* payload[0] contains the "Status Byte Encodings" field, per the | ||
* NFC Forum "Text Record Type Definition" section 3.2.1. | ||
* | ||
* bit7 is the Text Encoding Field. | ||
* | ||
* if (Bit_7 == 0): The text is encoded in UTF-8 if (Bit_7 == 1): | ||
* The text is encoded in UTF16 | ||
* | ||
* Bit_6 is reserved for future use and must be set to zero. | ||
* | ||
* Bits 5 to 0 are the length of the IANA language code. | ||
*/ | ||
val textEncoding = if (recordPayload[0] and 128.toByte() == 0.toByte()) { | ||
Charsets.UTF_8 | ||
} else { | ||
Charsets.UTF_16 | ||
} | ||
|
||
val languageCodeLength: Int = (recordPayload[0] and 63.toByte()).toInt() | ||
val text = String( | ||
recordPayload, languageCodeLength + 1, | ||
recordPayload.size - languageCodeLength - 1, textEncoding | ||
) | ||
return TextRecord(text) | ||
} catch (e: UnsupportedEncodingException) { | ||
Timber.w("We got a malformed tag.") | ||
return null | ||
} | ||
} else { | ||
null | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
app/src/main/java/dgca/verifier/app/android/nfc/ParsedNdefRecord.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* ---license-start | ||
* eu-digital-green-certificates / dgca-verifier-app-android | ||
* --- | ||
* Copyright (C) 2021 T-Systems International GmbH and all other contributors | ||
* --- | ||
* 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. | ||
* ---license-end | ||
* | ||
* Created by mykhailo.nester on 17/08/2021, 18:52 | ||
*/ | ||
|
||
package dgca.verifier.app.android.nfc | ||
|
||
interface ParsedNdefRecord { | ||
fun str(): String | ||
} |
Oops, something went wrong.