Skip to content

Commit

Permalink
[Android]-Custom CommissioningFlow - Redirect back to ChipTool after …
Browse files Browse the repository at this point in the history
…commissioning (#12712)

* [Android] - Custom CommissioningFlow - Define URL for redirection back to CHIPTools

* Update src/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/CHIPToolActivity.kt

Co-authored-by: Austin Hsieh <[email protected]>

* [Android]-Custom CommissioningFlow - detailed logging added

Co-authored-by: Austin Hsieh <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Feb 17, 2022
1 parent 806530f commit 22b39fb
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/android/CHIPTool/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.google.chip.chiptool">

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
Expand Down Expand Up @@ -30,12 +31,18 @@
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="mt" /><!-- Process Matter URIs -->
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="mt" android:host="modelinfo" /> <!-- Process Redirect URIs -->
</intent-filter>
</activity>
</application>

<queries>
<intent>
<action android:name="chip.intent.action.ATTESTATION"/>
<action android:name="chip.intent.action.ATTESTATION" />
</intent>
</queries>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import android.net.Uri
import android.nfc.NdefMessage
import android.nfc.NfcAdapter
import android.os.Bundle
import android.util.Base64
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
Expand All @@ -44,6 +45,7 @@ import com.google.chip.chiptool.setuppayloadscanner.BarcodeFragment
import com.google.chip.chiptool.setuppayloadscanner.CHIPDeviceDetailsFragment
import com.google.chip.chiptool.setuppayloadscanner.CHIPDeviceInfo
import com.google.chip.chiptool.setuppayloadscanner.CHIPLedgerDetailsFragment
import org.json.JSONObject

class CHIPToolActivity :
AppCompatActivity(),
Expand Down Expand Up @@ -72,6 +74,10 @@ class CHIPToolActivity :

if (intent?.action == NfcAdapter.ACTION_NDEF_DISCOVERED)
onNfcIntent(intent)

if (Intent.ACTION_VIEW == intent?.action) {
onReturnIntent(intent)
}
}

override fun onSaveInstanceState(outState: Bundle) {
Expand Down Expand Up @@ -222,6 +228,65 @@ class CHIPToolActivity :
.show()
}

private fun onReturnIntent(intent: Intent) {
val appLinkData = intent.data
// Require URI schema "mt:"
if (!appLinkData?.scheme.equals("mt", true)) {
Log.d(TAG, "Unrecognized URI schema : ${appLinkData?.scheme}")
return
}
// Require URI host "modelinfo"
if (!appLinkData?.host.equals("modelinfo", true)) {
Log.d(TAG, "Unrecognized URI host : ${appLinkData?.host}")
return
}

// parse payload
try {
val payloadBase64String = appLinkData?.getQueryParameter("payload")
if (payloadBase64String.isNullOrEmpty()) {
Log.d(TAG, "Unrecognized payload")
return
}

val decodeBytes = Base64.decode(payloadBase64String, Base64.DEFAULT)
val payloadString = String(decodeBytes)
val payload = JSONObject(payloadString)

// parse payload from JSON
val setupPayload = SetupPayload()
// set defaults
setupPayload.discoveryCapabilities = setOf()
setupPayload.optionalQRCodeInfo = mapOf()

// read from payload
setupPayload.version = payload.getInt("version")
setupPayload.vendorId = payload.getInt("vendorId")
setupPayload.productId = payload.getInt("productId")
setupPayload.commissioningFlow = payload.getInt("commissioningFlow")
setupPayload.discriminator = payload.getInt("discriminator")
setupPayload.setupPinCode = payload.getLong("setupPinCode")

val deviceInfo = CHIPDeviceInfo.fromSetupPayload(setupPayload)
val buttons = arrayOf(
getString(R.string.nfc_tag_action_show)
)

AlertDialog.Builder(this)
.setTitle(R.string.provision_custom_flow_alert_title)
.setItems(buttons) { _, _ ->
onCHIPDeviceInfoReceived(deviceInfo)
}
.create()
.show()

} catch (ex: UnrecognizedQrCodeException) {
Log.e(TAG, "Unrecognized Payload", ex)
Toast.makeText(this, "Unrecognized Setup Payload", Toast.LENGTH_SHORT).show()
return
}
}

companion object {
private const val TAG = "CHIPToolActivity"
private const val ADDRESS_COMMISSIONING_FRAGMENT_TAG = "address_commissioning_fragment"
Expand Down
3 changes: 2 additions & 1 deletion src/android/CHIPTool/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,12 @@
<string name="chip_ledger_info_commissioning_flow_url_text">Loading...</string>
<string name="chip_ledge_info_redirect_btn_text">Redirect</string>
<string name="chip_ledger_info_commissioning_flow_url_not_available">Not available</string>
<string name="provision_custom_flow_alert_title">Commissioning flow Completed.</string>

<string name="dcl_api_root_url">https://dcl.dev.dsr-corporation.com/api/modelinfo/models</string>
<string name="dcl_response_key">result</string>
<string name="dcl_custom_flow_url_key">custom</string>
<string name="custom_flow_return_url"></string>
<string name="custom_flow_return_url">mt://modelinfo</string>
<string name="retrieve_endpoint_list">Retrieve Endpoint List</string>
<string name="invoke">Invoke</string>
<string name="select_a_command">Select a command</string>
Expand Down

0 comments on commit 22b39fb

Please sign in to comment.