-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'refs/remotes/origin/issue_226_temp_2' i…
…nto issue_226_temp_2
- Loading branch information
Showing
72 changed files
with
1,953 additions
and
601 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
...HIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/DiagnosticLogFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package com.google.chip.chiptool.clusterclient | ||
|
||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.Bundle | ||
import android.os.Environment | ||
import android.util.Log | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.ArrayAdapter | ||
import androidx.core.content.FileProvider | ||
import androidx.fragment.app.Fragment | ||
import androidx.lifecycle.lifecycleScope | ||
import chip.devicecontroller.ChipDeviceController | ||
import chip.devicecontroller.DiagnosticLogType | ||
import chip.devicecontroller.DownloadLogCallback | ||
import com.google.chip.chiptool.ChipClient | ||
import com.google.chip.chiptool.R | ||
import com.google.chip.chiptool.databinding.DiagnosticLogFragmentBinding | ||
import java.io.File | ||
import java.io.FileOutputStream | ||
import java.io.IOException | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
|
||
class DiagnosticLogFragment : Fragment() { | ||
private val deviceController: ChipDeviceController | ||
get() = ChipClient.getDeviceController(requireContext()) | ||
|
||
private lateinit var scope: CoroutineScope | ||
|
||
private lateinit var addressUpdateFragment: AddressUpdateFragment | ||
|
||
private var _binding: DiagnosticLogFragmentBinding? = null | ||
private val binding | ||
get() = _binding!! | ||
|
||
private val timeout: Long | ||
get() = binding.timeoutEd.text.toString().toULongOrNull()?.toLong() ?: 0L | ||
|
||
private val diagnosticLogTypeList = DiagnosticLogType.values() | ||
private val diagnosticLogType: DiagnosticLogType | ||
get() = diagnosticLogTypeList[binding.diagnosticTypeSp.selectedItemPosition] | ||
|
||
private var mDownloadFile: File? = null | ||
private var mDownloadFileOutputStream: FileOutputStream? = null | ||
|
||
private var mReceiveFileLen = 0U | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View { | ||
_binding = DiagnosticLogFragmentBinding.inflate(inflater, container, false) | ||
scope = viewLifecycleOwner.lifecycleScope | ||
|
||
addressUpdateFragment = | ||
childFragmentManager.findFragmentById(R.id.addressUpdateFragment) as AddressUpdateFragment | ||
|
||
binding.getDiagnosticLogBtn.setOnClickListener { scope.launch { getDiagnosticLogClick() } } | ||
|
||
binding.diagnosticTypeSp.adapter = | ||
ArrayAdapter( | ||
requireContext(), | ||
android.R.layout.simple_spinner_dropdown_item, | ||
diagnosticLogTypeList | ||
) | ||
|
||
return binding.root | ||
} | ||
|
||
override fun onDestroyView() { | ||
super.onDestroyView() | ||
_binding = null | ||
} | ||
|
||
inner class ChipDownloadLogCallback : DownloadLogCallback { | ||
override fun onError(fabricIndex: Int, nodeId: Long, errorCode: Long) { | ||
Log.d(TAG, "onError: $fabricIndex, ${nodeId.toULong()}, $errorCode") | ||
showMessage("Downloading Failed") | ||
mDownloadFileOutputStream?.flush() ?: return | ||
} | ||
|
||
override fun onSuccess(fabricIndex: Int, nodeId: Long) { | ||
Log.d(TAG, "onSuccess: $fabricIndex, ${nodeId.toULong()}") | ||
mDownloadFileOutputStream?.flush() ?: return | ||
showMessage("Downloading Completed") | ||
mDownloadFile?.let { showNotification(it) } ?: return | ||
} | ||
|
||
override fun onTransferData(fabricIndex: Int, nodeId: Long, data: ByteArray): Boolean { | ||
Log.d(TAG, "onTransferData : ${data.size}") | ||
if (mDownloadFileOutputStream == null) { | ||
Log.d(TAG, "mDownloadFileOutputStream or mDownloadFile is null") | ||
return false | ||
} | ||
return addData(mDownloadFileOutputStream!!, data) | ||
} | ||
|
||
private fun addData(outputStream: FileOutputStream, data: ByteArray): Boolean { | ||
try { | ||
outputStream.write(data) | ||
} catch (e: IOException) { | ||
Log.d(TAG, "IOException", e) | ||
return false | ||
} | ||
mReceiveFileLen += data.size.toUInt() | ||
showMessage("Receive Data Size : $mReceiveFileLen") | ||
return true | ||
} | ||
} | ||
|
||
private fun getDiagnosticLogClick() { | ||
mDownloadFile = | ||
createLogFile( | ||
deviceController.fabricIndex.toUInt(), | ||
addressUpdateFragment.deviceId.toULong(), | ||
diagnosticLogType | ||
) | ||
mDownloadFileOutputStream = FileOutputStream(mDownloadFile) | ||
deviceController.downloadLogFromNode( | ||
addressUpdateFragment.deviceId, | ||
diagnosticLogType, | ||
timeout, | ||
ChipDownloadLogCallback() | ||
) | ||
} | ||
|
||
private fun isExternalStorageWritable(): Boolean { | ||
return Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED | ||
} | ||
|
||
private fun createLogFile(fabricIndex: UInt, nodeId: ULong, type: DiagnosticLogType): File? { | ||
if (!isExternalStorageWritable()) { | ||
return null | ||
} | ||
val now = System.currentTimeMillis() | ||
val fileName = "${type}_${fabricIndex}_${nodeId}_$now.txt" | ||
mReceiveFileLen = 0U | ||
return File(requireContext().getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), fileName) | ||
} | ||
|
||
private fun showNotification(file: File) { | ||
val intent = | ||
Intent(Intent.ACTION_VIEW).apply { | ||
setDataAndType(getFileUri(file), "text/plain") | ||
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) | ||
} | ||
|
||
requireActivity().startActivity(intent) | ||
} | ||
|
||
private fun getFileUri(file: File): Uri { | ||
return FileProvider.getUriForFile( | ||
requireContext(), | ||
"${requireContext().packageName}.provider", | ||
file | ||
) | ||
} | ||
|
||
private fun showMessage(msg: String) { | ||
requireActivity().runOnUiThread { binding.diagnosticLogTv.text = msg } | ||
} | ||
|
||
companion object { | ||
private const val TAG = "DiagnosticLogFragment" | ||
|
||
fun newInstance(): DiagnosticLogFragment = DiagnosticLogFragment() | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
examples/android/CHIPTool/app/src/main/res/layout/diagnostic_log_fragment.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
xmlns:app="http://schemas.android.com/apk/res-auto"> | ||
|
||
<androidx.fragment.app.FragmentContainerView | ||
android:id="@+id/addressUpdateFragment" | ||
android:name="com.google.chip.chiptool.clusterclient.AddressUpdateFragment" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
app:layout_constraintTop_toTopOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent"/> | ||
|
||
<TextView | ||
android:id="@+id/titleDiagnosticType" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:text="@string/diagnostic_log_type_title_text" | ||
android:textSize="16sp" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@id/addressUpdateFragment" /> | ||
|
||
<Spinner | ||
android:id="@+id/diagnosticTypeSp" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:inputType="text" | ||
android:spinnerMode="dropdown" | ||
android:textSize="16sp" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@id/titleDiagnosticType" /> | ||
|
||
<EditText | ||
android:id="@+id/timeoutTv" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_alignParentStart="true" | ||
android:layout_marginStart="8dp" | ||
android:layout_marginEnd="8dp" | ||
android:enabled="false" | ||
android:padding="8dp" | ||
android:text="@string/diagnostic_log_timeout_title_text" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintEnd_toStartOf="@id/timeoutEd" | ||
app:layout_constraintTop_toBottomOf="@id/diagnosticTypeSp" | ||
android:textSize="16sp" /> | ||
|
||
<EditText | ||
android:id="@+id/timeoutEd" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_marginStart="8dp" | ||
android:layout_marginEnd="8dp" | ||
android:autofillHints="@string/diagnostic_log_timeout_title_text" | ||
android:inputType="numberDecimal" | ||
android:padding="8dp" | ||
app:layout_constraintStart_toEndOf="@id/timeoutTv" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintTop_toBottomOf="@id/diagnosticTypeSp" | ||
android:textSize="16sp" /> | ||
|
||
<Button | ||
android:id="@+id/getDiagnosticLogBtn" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_margin="10dp" | ||
android:text="@string/diagnostic_log_btn_text" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintTop_toBottomOf="@id/timeoutTv"/> | ||
|
||
<TextView | ||
android:id="@+id/diagnosticLogTv" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:minLines="4" | ||
android:padding="16dp" | ||
android:singleLine="false" | ||
android:textSize="20sp" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintTop_toBottomOf="@id/getDiagnosticLogBtn" /> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
examples/android/CHIPTool/app/src/main/res/xml/file_paths.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<paths xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<external-files-path name="external_files" path="." /> | ||
</paths> |
Oops, something went wrong.