|
| 1 | +package com.google.chip.chiptool.clusterclient |
| 2 | + |
| 3 | +import android.content.Intent |
| 4 | +import android.net.Uri |
| 5 | +import android.os.Bundle |
| 6 | +import android.os.Environment |
| 7 | +import android.util.Log |
| 8 | +import android.view.LayoutInflater |
| 9 | +import android.view.View |
| 10 | +import android.view.ViewGroup |
| 11 | +import android.widget.ArrayAdapter |
| 12 | +import androidx.core.content.FileProvider |
| 13 | +import androidx.fragment.app.Fragment |
| 14 | +import androidx.lifecycle.lifecycleScope |
| 15 | +import chip.devicecontroller.ChipDeviceController |
| 16 | +import chip.devicecontroller.DiagnosticLogType |
| 17 | +import chip.devicecontroller.DownloadLogCallback |
| 18 | +import com.google.chip.chiptool.ChipClient |
| 19 | +import com.google.chip.chiptool.R |
| 20 | +import com.google.chip.chiptool.databinding.DiagnosticLogFragmentBinding |
| 21 | +import java.io.File |
| 22 | +import java.io.FileOutputStream |
| 23 | +import java.io.IOException |
| 24 | +import kotlinx.coroutines.CoroutineScope |
| 25 | +import kotlinx.coroutines.launch |
| 26 | + |
| 27 | +class DiagnosticLogFragment : Fragment() { |
| 28 | + private val deviceController: ChipDeviceController |
| 29 | + get() = ChipClient.getDeviceController(requireContext()) |
| 30 | + |
| 31 | + private lateinit var scope: CoroutineScope |
| 32 | + |
| 33 | + private lateinit var addressUpdateFragment: AddressUpdateFragment |
| 34 | + |
| 35 | + private var _binding: DiagnosticLogFragmentBinding? = null |
| 36 | + private val binding |
| 37 | + get() = _binding!! |
| 38 | + |
| 39 | + private val timeout: Long |
| 40 | + get() = binding.timeoutEd.text.toString().toULongOrNull()?.toLong() ?: 0L |
| 41 | + |
| 42 | + private val diagnosticLogTypeList = DiagnosticLogType.values() |
| 43 | + private val diagnosticLogType: DiagnosticLogType |
| 44 | + get() = diagnosticLogTypeList[binding.diagnosticTypeSp.selectedItemPosition] |
| 45 | + |
| 46 | + private var mDownloadFile: File? = null |
| 47 | + private var mDownloadFileOutputStream: FileOutputStream? = null |
| 48 | + |
| 49 | + private var mReceiveFileLen = 0U |
| 50 | + |
| 51 | + override fun onCreateView( |
| 52 | + inflater: LayoutInflater, |
| 53 | + container: ViewGroup?, |
| 54 | + savedInstanceState: Bundle? |
| 55 | + ): View { |
| 56 | + _binding = DiagnosticLogFragmentBinding.inflate(inflater, container, false) |
| 57 | + scope = viewLifecycleOwner.lifecycleScope |
| 58 | + |
| 59 | + addressUpdateFragment = |
| 60 | + childFragmentManager.findFragmentById(R.id.addressUpdateFragment) as AddressUpdateFragment |
| 61 | + |
| 62 | + binding.getDiagnosticLogBtn.setOnClickListener { scope.launch { getDiagnosticLogClick() } } |
| 63 | + |
| 64 | + binding.diagnosticTypeSp.adapter = |
| 65 | + ArrayAdapter( |
| 66 | + requireContext(), |
| 67 | + android.R.layout.simple_spinner_dropdown_item, |
| 68 | + diagnosticLogTypeList |
| 69 | + ) |
| 70 | + |
| 71 | + return binding.root |
| 72 | + } |
| 73 | + |
| 74 | + override fun onDestroyView() { |
| 75 | + super.onDestroyView() |
| 76 | + _binding = null |
| 77 | + } |
| 78 | + |
| 79 | + inner class ChipDownloadLogCallback : DownloadLogCallback { |
| 80 | + override fun onError(fabricIndex: Int, nodeId: Long, errorCode: Long) { |
| 81 | + Log.d(TAG, "onError: $fabricIndex, ${nodeId.toULong()}, $errorCode") |
| 82 | + showMessage("Downloading Failed") |
| 83 | + mDownloadFileOutputStream?.flush() ?: return |
| 84 | + } |
| 85 | + |
| 86 | + override fun onSuccess(fabricIndex: Int, nodeId: Long) { |
| 87 | + Log.d(TAG, "onSuccess: $fabricIndex, ${nodeId.toULong()}") |
| 88 | + mDownloadFileOutputStream?.flush() ?: return |
| 89 | + showMessage("Downloading Completed") |
| 90 | + mDownloadFile?.let { showNotification(it) } ?: return |
| 91 | + } |
| 92 | + |
| 93 | + override fun onTransferData(fabricIndex: Int, nodeId: Long, data: ByteArray): Boolean { |
| 94 | + Log.d(TAG, "onTransferData : ${data.size}") |
| 95 | + if (mDownloadFileOutputStream == null) { |
| 96 | + Log.d(TAG, "mDownloadFileOutputStream or mDownloadFile is null") |
| 97 | + return false |
| 98 | + } |
| 99 | + return addData(mDownloadFileOutputStream!!, data) |
| 100 | + } |
| 101 | + |
| 102 | + private fun addData(outputStream: FileOutputStream, data: ByteArray): Boolean { |
| 103 | + try { |
| 104 | + outputStream.write(data) |
| 105 | + } catch (e: IOException) { |
| 106 | + Log.d(TAG, "IOException", e) |
| 107 | + return false |
| 108 | + } |
| 109 | + mReceiveFileLen += data.size.toUInt() |
| 110 | + showMessage("Receive Data Size : $mReceiveFileLen") |
| 111 | + return true |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private fun getDiagnosticLogClick() { |
| 116 | + mDownloadFile = |
| 117 | + createLogFile( |
| 118 | + deviceController.fabricIndex.toUInt(), |
| 119 | + addressUpdateFragment.deviceId.toULong(), |
| 120 | + diagnosticLogType |
| 121 | + ) |
| 122 | + mDownloadFileOutputStream = FileOutputStream(mDownloadFile) |
| 123 | + deviceController.downloadLogFromNode( |
| 124 | + addressUpdateFragment.deviceId, |
| 125 | + diagnosticLogType, |
| 126 | + timeout, |
| 127 | + ChipDownloadLogCallback() |
| 128 | + ) |
| 129 | + } |
| 130 | + |
| 131 | + private fun isExternalStorageWritable(): Boolean { |
| 132 | + return Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED |
| 133 | + } |
| 134 | + |
| 135 | + private fun createLogFile(fabricIndex: UInt, nodeId: ULong, type: DiagnosticLogType): File? { |
| 136 | + if (!isExternalStorageWritable()) { |
| 137 | + return null |
| 138 | + } |
| 139 | + val now = System.currentTimeMillis() |
| 140 | + val fileName = "${type}_${fabricIndex}_${nodeId}_$now.txt" |
| 141 | + mReceiveFileLen = 0U |
| 142 | + return File(requireContext().getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), fileName) |
| 143 | + } |
| 144 | + |
| 145 | + private fun showNotification(file: File) { |
| 146 | + val intent = |
| 147 | + Intent(Intent.ACTION_VIEW).apply { |
| 148 | + setDataAndType(getFileUri(file), "text/plain") |
| 149 | + addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) |
| 150 | + } |
| 151 | + |
| 152 | + requireActivity().startActivity(intent) |
| 153 | + } |
| 154 | + |
| 155 | + private fun getFileUri(file: File): Uri { |
| 156 | + return FileProvider.getUriForFile( |
| 157 | + requireContext(), |
| 158 | + "${requireContext().packageName}.provider", |
| 159 | + file |
| 160 | + ) |
| 161 | + } |
| 162 | + |
| 163 | + private fun showMessage(msg: String) { |
| 164 | + requireActivity().runOnUiThread { binding.diagnosticLogTv.text = msg } |
| 165 | + } |
| 166 | + |
| 167 | + companion object { |
| 168 | + private const val TAG = "DiagnosticLogFragment" |
| 169 | + |
| 170 | + fun newInstance(): DiagnosticLogFragment = DiagnosticLogFragment() |
| 171 | + } |
| 172 | +} |
0 commit comments