Implement Device Sharing

Last Updated on : 2024-11-12 08:31:47download

This topic describes how to implement the device sharing service. For more information, see Device Sharing.

Device sharing entry

Add a device sharing entry on the device/group details page.

Instantiate a sharing management class.

val mShareManager : IDeviceShareManager = DeviceBusinessDataManager.getInstance().getDeviceShareManager()
val manager : ThingDeviceShareManager = ThingDeviceShareManager()

Device/group sharing features

Implement Device Sharing Implement Device Sharing

Step 1: Determine whether a device or group can be shared

manager?.isSupportDeviceShare(resId, resType, object : IThingResultCallback<Boolean> {
            override fun onSuccess(result: Boolean?) {

            }

            override fun onError(errorCode: String?, errorMessage: String?) {

            }

        })

Step 2: Query the remaining share times of the device or group

resId?.let {
            mShareManager?.querySharedCount(it, resType, object : Business.ResultListener<Long> {
                override fun onFailure(p0: BusinessResponse?, p1: Long?, p2: String?) {

                }

                override fun onSuccess(p0: BusinessResponse?, p1: Long?, p2: String?) {
                    p1?.let {
                        var maxCount = 9999L
                        if (p1 ! = -1L) {
                            maxCount = p1
                        }
                        if (maxCount >= count) {
                            afterAction.invoke()
                        } else {
                            showToast("No sharing times")
                        }
                    }
                }
            })
        }

If -1 is returned, the remaining share times are considered unlimited.

Step 3: Share the device or group

Share a device using either of the following two methods:

  • Method 1: Share with a specific user. You need to retrieve the username on UI.
  • Method 2: Create a short share URL based on the share times. Other users can click it to jump to the app homepage to accept the share invitation.

Share a device or group with a specified user

manager?.shareToUser(
            resId,
            resType,
            homeId,
            userName,
            object : IThingResultCallback<SharedUserInfoBean> {
                override fun onSuccess(result: SharedUserInfoBean?) {
                    showToast("share success")
                }

                override fun onError(errorCode: String?, errorMessage: String?) {
                    showToast(errorMessage)
                }
            })

Create a short share URL

mShareManager?.createShareLink(
            resId.toString(),
            resType,
            homeId,
            5,
            count,
            object : Business.ResultListener<ShareDeviceLinkResultBean> {
                override fun onFailure(
                    p0: BusinessResponse?,
                    p1: ShareDeviceLinkResultBean?,
                    p2: String?
                ) {
                    showToast(p0?.errorMsg)
                }

                override fun onSuccess(
                    p0: BusinessResponse?,
                    p1: ShareDeviceLinkResultBean?,
                    p2: String?
                ) {
                    p1?.let { copyToClipboard(it.code) }
                }
            })

Implement the feature of accepting device/group sharing

Implement Device Sharing

Step 1: Check short URL validity

First, check whether the short URL is valid.

manager?.parseShortLinkAvailability(link, object : IThingResultCallback<Boolean> {
            override fun onSuccess(result: Boolean?) {
                if (result == true) {
                    getSharerInfo(link)
                } else {
                    showToast("The link has expired")
                }
            }

            override fun onError(errorCode: String?, errorMessage: String?) {

            }
        })

Step 2: Get the result of the short URL code

Next, get the information of the short URL, including the shared device and the sharer. The information can be displayed on a pop-up window that contains a button to accept the share invitation.

mShareManager?.parseSharedDeviceLinkResult(code,
            object : Business.ResultListener<ShareShortLinkResult> {
                override fun onFailure(
                    p0: BusinessResponse?,
                    p1: ShareShortLinkResult?,
                    p2: String?
                ) {

                }

                override fun onSuccess(
                    p0: BusinessResponse?,
                    p1: ShareShortLinkResult?,
                    p2: String?
                ) {
                    p1?.let {
                        binding.tvUsername.text = p1.resName
                        binding.ivUserIcon.setImageURI(Uri.parse(p1.resIcon))
                        binding.llSharerInfo.visibility = View.VISIBLE
                    }

                }
            })

Step 3: Accept the share invitation

manager?.acceptShare(code, object : IResultCallback {
            override fun onError(code: String?, error: String?) {
                showToast(error)
            }

            override fun onSuccess() {
                showToast("accept share success")
            }
        })

Features related to current receivers of the shared device or group

Query users who received the sharing invitation for a device or group

Users who received the sharing invitation for a device or group can be returned in a list. Users can be displayed in pages. However, since the user count is typically small, all users can also be displayed at once.

 manager?.getReceivers(
            resId,
            resType,
            1,
            100,
            object : IThingResultCallback<List<ShareMember>> {
                override fun onSuccess(result: List<ShareMember>?) {

                }

                override fun onError(errorCode: String?, errorMessage: String?) {

                }
            })

Remove current receivers of the shared device or group

manager?.removeReceiver(memberId, resId, resType, object : IResultCallback {
            override fun onError(code: String?, error: String?) {
                showToast(error)
            }

            override fun onSuccess() {
                showToast("delete success")

            }
        })

Update the expiration time of sharing

You can share a device using either of the following two methods:

  • Valid permanently. Enter 0 as the end time.
  • Valid for a period of time. Select and enter an expiration date as the end time.
private fun showChangeTimeDialog(memberId: Long) {
        ChangeTimeDialog.Builder(this)
            .setButtonListener(object : ChangeTimeDialog.OnClickListener {
                override fun onForeverButtonClick() {
                    changeTime(memberId,0, 0)
                }

                override fun onSometimeButtonClick(endTime: Long) {
                    changeTime(memberId,1, endTime)
                }
            })
            .build()
            .show()
    }

    private fun changeTime(memberId: Long,mode: Int, endTime: Long) {
        mShareManager!!.updateSharedDeadline(memberId,
            resId!!, resType, mode, endTime, object : Business.ResultListener<Boolean> {
                override fun onFailure(p0: BusinessResponse?, p1: Boolean?, p2: String?) {
                    showToast(p0?.errorMsg)
                }

                override fun onSuccess(p0: BusinessResponse?, p1: Boolean?, p2: String?) {
                    showToast("change time success")
                }

            })
    }

Features related to users who were recently shared with

Query recently shared users

Users who received the sharing invitation for a device or group can be returned in a list.

manager?.getRelationMembers(object : IThingResultCallback<List<ShareMember>> {
            override fun onSuccess(result: List<ShareMember>?) {
                var receivedUserList2: MutableList<ReceiverItemBean> = ArrayList()
                result?.indices?.forEach { i ->
                    val bean = ReceiverItemBean(
                        result[i].iconUrl,
                        result[i].userName,
                        result[i].memberId,
                        result[i].uid
                    )
                    receivedUserList2.add(i, bean)
                }
                allUserAdapter.setData(receivedUserList2, null)
            }

            override fun onError(errorCode: String?, errorMessage: String?) {
                L.i(TAG, errorMessage)
            }
        })

Remove a recently shared user

mShareManager?.deleteSharedContact(userId, object : Business.ResultListener<Boolean> {
            override fun onFailure(p0: BusinessResponse?, p1: Boolean?, p2: String?) {
                showToast(p0?.errorMsg)
            }

            override fun onSuccess(p0: BusinessResponse?, p1: Boolean?, p2: String?) {
                showToast("delete success")
            }
        })

Features related to sharers

Sharers are users who shared a device or group with the current user.

View the sharer list

manager?.getShareReceivedUserList(object : IThingResultCallback<List<SharedUserInfoBean>> {
            override fun onSuccess(result: List<SharedUserInfoBean>?) {
                var receivedUserList3: MutableList<ReceiverItemBean> = ArrayList()
                result?.indices?.forEach { i ->
                    val bean = ReceiverItemBean(
                        result[i].iconUrl,
                        result[i].userName,
                        result[i].memeberId,
                        null
                    )
                    receivedUserList3.add(i, bean)
                }
                sharerListAdapter.setData(receivedUserList3, null)
            }

            override fun onError(errorCode: String?, errorMessage: String?) {

            }
        })

Remove a sharer and shared devices

manager?.removeReceivedUserShare(memberId, object : IResultCallback {
            override fun onError(code: String?, error: String?) {
                showToast(error)
            }

            override fun onSuccess() {
                showToast("remove success")
            }
        })

Query sharer information and shared devices

You can create a sharer details page and get sharer information when you enter it.

manager?.getSharerInfoDetail(memberId, object : IThingResultCallback<ShareReceivedUserDetailBean> {
            override fun onSuccess(result: ShareReceivedUserDetailBean?) {
                result?.let {
                    binding.etNickname.setText(result.remarkName)
                    val beans: MutableList<ItemBean> = ArrayList(8)
                    for (deviceBean in result.devices) {
                        beans.add(getItemBeanFromDevs(deviceBean))
                    }
                    adapter.setData(beans)
                }
            }

            override fun onError(errorCode: String?, errorMessage: String?) {
                showToast(errorMessage)
            }

        })

View the name of a sharer

manager?.getSharerName(resId,resType,object :IThingResultCallback<String>{
            override fun onSuccess(result: String?) {

            }

            override fun onError(errorCode: String?, errorMessage: String?) {

            }
        })

Update the nickname of the sharer

private fun changeNickname(){
        val newName = binding.etNickname.text.toString()
        manager?.renameReceivedShareNickname(memberId,newName,object :IResultCallback{
            override fun onError(code: String?, error: String?) {
                showToast(error)
            }

            override fun onSuccess() {
                showToast("change success")
            }
        })
    }

Remove a shared device or group

Remove the shared device or group that has been received.

manager?.removeReceivedShare(resId,resType,object :IResultCallback{
            override fun onError(code: String?, error: String?) {

            }

            override fun onSuccess() {

            }
        })