From 59dea60d68c3572c3e216c23c8b4805f89dbad27 Mon Sep 17 00:00:00 2001 From: chihiro hashimoto Date: Sat, 8 Sep 2018 15:53:59 +0800 Subject: [PATCH 01/15] =?UTF-8?q?Update:=20kotlin=20jdk=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index c15effd..ff742f7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -36,7 +36,7 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" implementation 'com.android.support:appcompat-v7:26.0.1' implementation 'com.android.support.constraint:constraint-layout:1.0.2' From 33b730ee70174fd497be81b10b6b19d9c76d0eeb Mon Sep 17 00:00:00 2001 From: chihiro hashimoto Date: Sat, 8 Sep 2018 15:54:12 +0800 Subject: [PATCH 02/15] Update: kotlin version --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 3d88ce7..253bdb4 100644 --- a/build.gradle +++ b/build.gradle @@ -1,7 +1,7 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.2.51' + ext.kotlin_version = '1.2.61' repositories { google() jcenter() From 002dc8db24fd5418c1b9d43187c6b3ddc8e2465b Mon Sep 17 00:00:00 2001 From: chihiro hashimoto Date: Fri, 21 Sep 2018 23:26:51 +0800 Subject: [PATCH 03/15] Update: for deug --- .../chrhsmt/sisheng/debug/AnalyzeActivity.kt | 159 ++++++++++++++++++ .../sisheng/debug/AnalyzedRecordedData.kt | 21 +++ .../chrhsmt/sisheng/debug/SDCardManager.kt | 89 ++++++++++ app/src/main/res/layout/activity_analyze.xml | 85 ++++++++++ 4 files changed, 354 insertions(+) create mode 100644 app/src/main/java/com/chrhsmt/sisheng/debug/AnalyzeActivity.kt create mode 100644 app/src/main/java/com/chrhsmt/sisheng/debug/AnalyzedRecordedData.kt create mode 100644 app/src/main/java/com/chrhsmt/sisheng/debug/SDCardManager.kt create mode 100644 app/src/main/res/layout/activity_analyze.xml diff --git a/app/src/main/java/com/chrhsmt/sisheng/debug/AnalyzeActivity.kt b/app/src/main/java/com/chrhsmt/sisheng/debug/AnalyzeActivity.kt new file mode 100644 index 0000000..3ff0264 --- /dev/null +++ b/app/src/main/java/com/chrhsmt/sisheng/debug/AnalyzeActivity.kt @@ -0,0 +1,159 @@ +package com.chrhsmt.sisheng.debug + +import android.os.Bundle +import android.app.Activity +import android.content.Intent +import android.util.Log +import android.view.View +import android.widget.ArrayAdapter +import android.widget.ListAdapter +import android.widget.Toast +import com.chrhsmt.sisheng.* +import com.chrhsmt.sisheng.exception.AudioServiceException +import com.chrhsmt.sisheng.font.FontUtils +import com.chrhsmt.sisheng.point.PointCalculator +import com.chrhsmt.sisheng.point.SimplePointCalculator +import com.chrhsmt.sisheng.ui.ScreenUtils + +import kotlinx.android.synthetic.main.activity_analyze.* +import java.io.File + +class AnalyzeActivity : Activity() { + + companion object { + val TAG = "AnalyzeActivity" + } + + private var service: AudioService? = null + private var files: List? = null + private var datas: List? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_analyze) + + // フルスクリーンにする + ScreenUtils.setFullScreen(this.window) + ScreenUtils.setScreenBackground(this) + + Settings.setDefaultValue(this@AnalyzeActivity, true) + + this.service = AudioService(null, this) + + val cardManager: SDCardManager = SDCardManager() + cardManager.setUpReadWriteExternalStorage(this) + this.files = cardManager.getFileList() + val lines = cardManager.readCsv(this@AnalyzeActivity, "result.csv") + + this.datas = this.files?.map { file -> + val line = lines.find { line -> line.startsWith(file.name) } + AnalyzedRecordedData(file, line).init() + } + + // ファイルリスト + val fileNames = this.datas?.map { data -> data.toString() } + val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, fileNames) + listFiles.adapter = adapter as ListAdapter + + listFiles.setOnItemClickListener { parent, view, position, id -> + + val reibunInfo = ReibunInfo.getInstance(this) + val itemPosition = this.datas?.get(position)?.id?.minus(1) ?: run { + Log.e(TAG, "Data not found !!!") + return@setOnItemClickListener + } + reibunInfo.setSelectedItem(itemPosition) + + val intent = Intent(this@AnalyzeActivity, ReibunActivity::class.java) + startActivity(intent) + overridePendingTransition(0, 0); + } + + // タイトル長押下された場合は、デバッグ画面に遷移する。 + if (Settings.DEBUG_MODE) { + txtNiniReibun.setOnLongClickListener(View.OnLongClickListener { + val intent = Intent(this@AnalyzeActivity, MainActivity::class.java) + startActivity(intent) + true + }) + } + + btnAnalyze.setOnClickListener({ v: View? -> + Toast.makeText(this@AnalyzeActivity, "解析開始します", Toast.LENGTH_SHORT).show() + + var count = 0 + var map: MutableMap> = HashMap>() + val reibunInfo = ReibunInfo.getInstance(this@AnalyzeActivity) + reibunInfo.getItemList().forEach { + this@AnalyzeActivity.service?.testPlay(it.getMFSZExampleAudioFileName(), playback = false, async = false) + val sampleData = this@AnalyzeActivity.service?.getTestFreq() + map[it.id.toString()] = sampleData!! + } + + var buffer = StringBuilder() + var nullMaleBuffer = StringBuilder() + var nullFemaleBuffer = StringBuilder() + + this@AnalyzeActivity.files?.forEach { file -> + val fileName = file.name + + // ファイル名から条件取得 + ".*-(.*)-(.*)\\.wav".toRegex().find(fileName)?.groups?.let { it -> + val id = it.get(1)?.value + val sex = it.get(2)?.value + if (id != null && !"null".equals(id)) { + val position = id.toInt().minus(1) + reibunInfo.setSelectedItem(position) + val reibunName = reibunInfo.selectedItem!!.getMFSZExampleAudioFileName() + Settings.sampleAudioFileName = reibunName + + if (sex == "null") { + // male + Settings.sex = "m" + this@AnalyzeActivity.extractLastYearData(file, nullMaleBuffer, map[id]!!, id, "m", fileName) + // female + Settings.sex = "f" + this@AnalyzeActivity.extractLastYearData(file, nullFemaleBuffer, map[id]!!, id, "f", fileName) + } else { + Settings.sex = sex + this@AnalyzeActivity.extractLastYearData(file, buffer, map[id]!!, id, sex!!, fileName) + } + } + } + + } + + // file書き出し? + SDCardManager().write(this@AnalyzeActivity, "result.csv", buffer.toString()) + if (nullMaleBuffer.isNotEmpty()) { + SDCardManager().write(this@AnalyzeActivity, "nullMaleResult.csv", nullMaleBuffer.toString()) + } + if (nullFemaleBuffer.isNotEmpty()) { + SDCardManager().write(this@AnalyzeActivity, "nullFemaleResult.csv", nullFemaleBuffer.toString()) + } + + Toast.makeText(this@AnalyzeActivity, String.format("解析完了: %d件", count), Toast.LENGTH_LONG).show() + }) + } + + private fun extractLastYearData(file: File, buffer: StringBuilder, sampleData: MutableList, id: String, sex: String, fileName: String) { + val cardManager: SDCardManager = SDCardManager() + // 去年のデータ抽出 + val path= cardManager.copyAudioFile(file, this@AnalyzeActivity) + this@AnalyzeActivity.service?.testPlay(file.name, path, playback = false, callback = Runnable { + + val data = this@AnalyzeActivity.service?.getTestFreq() + + val calculator: PointCalculator = SimplePointCalculator() + val point = calculator.calc(data!!, sampleData) + if (point.base <= sampleData!!.size) { + // 録音が失敗している場合 +// throw AudioServiceException("不好意思,我听不懂") + } else { + buffer.append(fileName + ", " + id + ", " + sex + ", " + point.score + ", " + point.success() + "\n") + } + + }, async = false) + } + +} diff --git a/app/src/main/java/com/chrhsmt/sisheng/debug/AnalyzedRecordedData.kt b/app/src/main/java/com/chrhsmt/sisheng/debug/AnalyzedRecordedData.kt new file mode 100644 index 0000000..f55bdfe --- /dev/null +++ b/app/src/main/java/com/chrhsmt/sisheng/debug/AnalyzedRecordedData.kt @@ -0,0 +1,21 @@ +package com.chrhsmt.sisheng.debug + +import com.chrhsmt.sisheng.point.Point +import java.io.File + +data class AnalyzedRecordedData(var file: File, var csvLine: String?) { + + var id: Int? = null + var point: Point? = null + + fun init(): AnalyzedRecordedData { + val line = this.csvLine?.split(",") + this.id = line?.get(1)?.trim()?.toInt() + this.point = Point(line?.get(3)?.trim()?.toInt()?: 0, 0.0, 0.0, 0) + return this + } + + override fun toString(): String { + return String.format("%s - %s - %s", this.file.name, this.point?.score, this.point?.success()) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/chrhsmt/sisheng/debug/SDCardManager.kt b/app/src/main/java/com/chrhsmt/sisheng/debug/SDCardManager.kt new file mode 100644 index 0000000..14f610a --- /dev/null +++ b/app/src/main/java/com/chrhsmt/sisheng/debug/SDCardManager.kt @@ -0,0 +1,89 @@ +package com.chrhsmt.sisheng.debug + +import android.annotation.SuppressLint +import android.app.Activity +import android.content.Context +import android.util.Log +import android.widget.Toast +import com.chrhsmt.sisheng.persistence.ExternalMedia +import java.io.* +import java.nio.charset.Charset +import java.util.* +import kotlin.collections.ArrayList + +class SDCardManager { + + companion object { + val TAG = "SDCardManager" + val REQUEST_PERMISSION = 1000; + } + + fun setUpReadWriteExternalStorage(context: Context) { + val dir = ExternalMedia.getExternalDirectoryPath(context) + if (ExternalMedia.isExternalStorageWritable() && dir != null && dir!!.canWrite()) { +// Toast.makeText(this, "SDカードへの書き込みが可能です", Toast.LENGTH_SHORT).show() + Log.d(SDCardManager.TAG, String.format("External media path: %s", dir.absoluteFile)) + } else { + Toast.makeText(context, "SDカードがマウントされていない、もしくは書き込みが不可能な状態です", Toast.LENGTH_SHORT).show() + } + } + + fun readCsv(context: Context, name: String): List { + var lines: List = ArrayList() + val dir = ExternalMedia.getExternalDirectoryPath(context) + if (ExternalMedia.isExternalStorageReadable() && dir != null && dir!!.canRead()) { + val newFile = File(dir, name) + val fr = FileReader(newFile) + lines = fr.readLines() + fr.close() + } else { + Toast.makeText(context, "SDカードがマウントされていない、もしくは書き込みが不可能な状態です", Toast.LENGTH_SHORT).show() + } + return lines + } + + fun write(context: Context, name: String, data: String) { + val dir = ExternalMedia.getExternalDirectoryPath(context) + if (ExternalMedia.isExternalStorageWritable() && dir != null && dir!!.canWrite()) { + val newFile = File(dir, name) + val fos = FileOutputStream(newFile) + fos.write(data.toByteArray(Charset.defaultCharset())) + fos.close() + } else { + Toast.makeText(context, "SDカードがマウントされていない、もしくは書き込みが不可能な状態です", Toast.LENGTH_SHORT).show() + } + } + + fun getFileList(): List { + var ret = ArrayList() + ExternalMedia.saveDir?.takeIf { it -> it.canRead() }?.let { it -> + it.listFiles().forEach { file -> ret.add(file) } + } + return ret + } + + @SuppressLint("WrongConstant") + fun copyAudioFile(file: File, activity: Activity): String { + // ファイル移動 + var dataName = file.name + if (dataName.contains("/")) { + dataName = dataName.replace("/", "_") + } + val path = String.format("/data/data/%s/files/%s", activity.packageName, dataName) + val input = file.inputStream() + val output = activity.openFileOutput(dataName, Context.MODE_ENABLE_WRITE_AHEAD_LOGGING) + val DEFAULT_BUFFER_SIZE = 1024 * 4 + + val buffer = ByteArray(DEFAULT_BUFFER_SIZE) + var n = 0 + while (true) { + n = input.read(buffer) + if (n == -1) break + output.write(buffer, 0, n) + } + output.close() + input.close() + + return path + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_analyze.xml b/app/src/main/res/layout/activity_analyze.xml new file mode 100644 index 0000000..2fa25b5 --- /dev/null +++ b/app/src/main/res/layout/activity_analyze.xml @@ -0,0 +1,85 @@ + + + + + + + + + +