From c3ea67edb7532a452569150ba653d01ff3ff5230 Mon Sep 17 00:00:00 2001 From: biezhihua Date: Sun, 4 Apr 2021 18:23:55 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20fileStatus=E4=B8=AD=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../open/android/lib/dsm/demo/MainActivity.kt | 62 ++++++++++++++----- .../src/main/java/open/android/lib/dsm/Dsm.kt | 9 ++- libdsm_core/Dsm.cpp | 18 +++--- 3 files changed, 65 insertions(+), 24 deletions(-) diff --git a/example/example_android/app/src/main/java/open/android/lib/dsm/demo/MainActivity.kt b/example/example_android/app/src/main/java/open/android/lib/dsm/demo/MainActivity.kt index 7c42437..fc7d321 100644 --- a/example/example_android/app/src/main/java/open/android/lib/dsm/demo/MainActivity.kt +++ b/example/example_android/app/src/main/java/open/android/lib/dsm/demo/MainActivity.kt @@ -255,21 +255,52 @@ class MainActivity : AppCompatActivity() { return ViewHolder(view) } + var path = "" override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) { - val fileName = dataset[position] as? String - fileName?.let { + val itemData = dataset[position] + if (itemData is String) { + processShare(itemData, viewHolder) + } else if (itemData is JSONObject) { + val fileName = itemData.getString("name") viewHolder.file_name.text = fileName viewHolder.itemView.setOnClickListener { - val tid = dsm?.treeConnect(fileName) - if (tid != null && tid > 0) { - val result = dsm?.find(tid, "\\*") - Log.d(TAG, "onBindViewHolder() called : $result") - } else { - mainActivity.tip.duration = Snackbar.LENGTH_LONG - mainActivity.tip.setText("错误码 $tid") - mainActivity.tip.show() + if (tid != null) { + val fileStatus = dsm?.fileStatus(tid!!, path + "\\" + fileName) + val data = fileStatus?.getJSONObject("data") + val isDir = data?.getIntValue("is_dir") + if (isDir == 1) { + val dirName = data.getString("name") + val findNext = "$dirName\\*" + dsm?.find(tid!!, findNext)?.getJSONArray("data")?.let { + path += dirName + Log.d(TAG, "onBindViewHolder() called : $it") + setDatas(it) + } + } + Log.d(TAG, "onBindViewHolder() called: status = ${fileStatus}") + } + } + } + } + private var tid: Int? = null + + private fun processShare( + shareName: String, + viewHolder: ViewHolder + ) { + viewHolder.file_name.text = shareName + viewHolder.itemView.setOnClickListener { + tid = dsm?.treeConnect(shareName) + if (tid != null && tid!! > 0) { + dsm?.find(tid!!, "\\*")?.getJSONArray("data")?.let { + Log.d(TAG, "processShare() called : $it") + setDatas(it) } + } else { + mainActivity.tip.duration = Snackbar.LENGTH_LONG + mainActivity.tip.setText("错误码 $tid") + mainActivity.tip.show() } } } @@ -278,15 +309,18 @@ class MainActivity : AppCompatActivity() { fun addData(data: JSONObject) { dataset.add(data) - notifyItemChanged(0, dataset.size) + notifyDataSetChanged() } fun addDatas(datas: JSONArray) { dataset.addAll(datas) - notifyItemChanged(0, dataset.size) + notifyDataSetChanged() } + fun setDatas(datas: JSONArray) { + dataset.clear() + dataset.addAll(datas) + notifyDataSetChanged() + } } - - } diff --git a/libdsm_android/src/main/java/open/android/lib/dsm/Dsm.kt b/libdsm_android/src/main/java/open/android/lib/dsm/Dsm.kt index 6390dac..c3fe896 100644 --- a/libdsm_android/src/main/java/open/android/lib/dsm/Dsm.kt +++ b/libdsm_android/src/main/java/open/android/lib/dsm/Dsm.kt @@ -141,7 +141,8 @@ class Dsm { * @return An a json list. */ fun getShareList(): JSONObject? { - return JSONObject.parseObject(_shareGetListJson(this)) + val result = _shareGetListJson(this) + return JSONObject.parseObject(result) } /** @@ -180,7 +181,8 @@ class Dsm { * @return An json list of files. */ fun find(tid: Int, pattern: String): JSONObject? { - return JSONObject.parseObject(_find(this, tid, pattern)) + val result = _find(this, tid, pattern) + return JSONObject.parseObject(result) } /** @@ -193,7 +195,8 @@ class Dsm { * destory this object with smb_stat_destroy after usage. */ fun fileStatus(tid: Int, path: String): JSONObject? { - return JSONObject.parseObject(_fileStatus(this, tid, path)) + val result = _fileStatus(this, tid, path) + return JSONObject.parseObject(result) } protected fun finalize() { diff --git a/libdsm_core/Dsm.cpp b/libdsm_core/Dsm.cpp index 5b217f3..a192d58 100644 --- a/libdsm_core/Dsm.cpp +++ b/libdsm_core/Dsm.cpp @@ -290,10 +290,13 @@ string *Dsm::find(int tid, const char *pattern) { json file; // name - file["name"] = smb_stat_name(st); + const char *name = smb_stat_name(st); + file["name"] = name; + + LOGD("[%s] find %s %s", __func__, pattern, name); // 0 -> not a directory, != 0 -> directory - file["is_dir"] = (smb_stat_get(st, SMB_STAT_ISDIR) != 0 ? 1 : 0); + file["is_dir"] = smb_stat_get(st, SMB_STAT_ISDIR) != 0 ? 1 : 0; // Get file size file["size"] = smb_stat_get(st, SMB_STAT_SIZE); @@ -336,12 +339,14 @@ string *Dsm::fileStatus(int tid, const char *path) { smb_stat st = smb_fstat(session, smbTid, path); json result; - json data = json::array(); - json file; // name - file["name"] = smb_stat_name(st); + const char *name = smb_stat_name(st); + // FIXME: 修复中文错误 + file["name"] = path; + + LOGD("[%s] fileStatus %s %s", __func__, path, name); // 0 -> not a directory, != 0 -> directory file["is_dir"] = (smb_stat_get(st, SMB_STAT_ISDIR) != 0 ? 1 : 0); @@ -361,9 +366,8 @@ string *Dsm::fileStatus(int tid, const char *path) { // Get file last moditification time file["last_moditification_time"] = smb_stat_get(st, SMB_STAT_MTIME); - data.push_back(file); smb_stat_destroy(st); - result["data"] = data; + result["data"] = file; return new string(result.dump(-1, ' ', true, nlohmann::json::error_handler_t::replace)); }