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.
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()
manager?.isSupportDeviceShare(resId, resType, object : IThingResultCallback<Boolean> {
override fun onSuccess(result: Boolean?) {
}
override fun onError(errorCode: String?, errorMessage: String?) {
}
})
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.
Share a device using either of the following two methods:
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)
}
})
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) }
}
})
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?) {
}
})
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
}
}
})
manager?.acceptShare(code, object : IResultCallback {
override fun onError(code: String?, error: String?) {
showToast(error)
}
override fun onSuccess() {
showToast("accept share success")
}
})
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?) {
}
})
manager?.removeReceiver(memberId, resId, resType, object : IResultCallback {
override fun onError(code: String?, error: String?) {
showToast(error)
}
override fun onSuccess() {
showToast("delete success")
}
})
You can share a device using either of the following two methods:
0
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")
}
})
}
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)
}
})
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")
}
})
Sharers are users who shared a device or group with the current user.
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?) {
}
})
manager?.removeReceivedUserShare(memberId, object : IResultCallback {
override fun onError(code: String?, error: String?) {
showToast(error)
}
override fun onSuccess() {
showToast("remove success")
}
})
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)
}
})
manager?.getSharerName(resId,resType,object :IThingResultCallback<String>{
override fun onSuccess(result: String?) {
}
override fun onError(errorCode: String?, errorMessage: String?) {
}
})
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 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() {
}
})
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback