Skip to content
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

[Android]-Custom CommissioningFlow - Redirect back to ChipTool after commissioning #12712

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
lijujayakumar marked this conversation as resolved.
Show resolved Hide resolved
onReturnIntent(intent)
}
}

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

private fun onReturnIntent(intent: Intent) {
val appLinkData = intent.data
// Require URI schema "mt:"
if (!appLinkData?.scheme.equals("mt")) return
// Require URI host "modelinfo"
if (!appLinkData?.host.equals("modelinfo")) return
lijujayakumar marked this conversation as resolved.
Show resolved Hide resolved

// parse payload
try {
val payloadBase64String = appLinkData?.getQueryParameter("payload")
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,10 +141,11 @@
<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>
</resources>