Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move Logs/Log screen into fragments #245

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 6 additions & 74 deletions app/src/main/java/org/openziti/mobile/ZitiMobileEdgeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ import android.net.VpnService
import android.os.Build
import android.os.Bundle
import android.os.IBinder
import android.os.Process
import android.os.VibrationEffect
import android.os.Vibrator
import android.os.VibratorManager
import android.text.method.ScrollingMovementMethod
import android.util.Log
import android.view.View
import android.view.WindowManager
import android.view.animation.DecelerateInterpolator
Expand All @@ -35,18 +32,15 @@ import androidx.activity.OnBackPressedCallback
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.commit
import androidx.fragment.app.add
import androidx.fragment.app.commit
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.openziti.mobile.databinding.DashboardBinding
import org.openziti.mobile.debug.DebugInfo
import org.openziti.mobile.debug.DebugInfoActivity
import org.openziti.mobile.fragments.AboutFragment
import org.openziti.mobile.fragments.LogsFragment
import java.util.Timer
import java.util.TimerTask

Expand All @@ -61,7 +55,6 @@ class ZitiMobileEdgeActivity : AppCompatActivity() {
private val AdvancedPage by lazy { binding.AdvancedPage }
private val ConfigPage by lazy { binding.ConfigPage }
private val LogsPage by lazy { binding.LogsPage }
private val LogPage by lazy { binding.LogPage }
private val IdentityDetailsPage by lazy { binding.IdentityDetailsPage }
private val IdentityPage by lazy { binding.IdentityPage }

Expand Down Expand Up @@ -107,17 +100,6 @@ class ZitiMobileEdgeActivity : AppCompatActivity() {
private val IdDetailServicesList by lazy { IdentityDetailsPage.IdDetailServicesList }
private val IdDetailForgetButton by lazy { IdentityDetailsPage.IdDetailForgetButton }

private val LogsLabel by lazy { LogsPage.LogsLabel }
private val BackLogsButton by lazy { LogsPage.BackLogsButton }
private val PacketLogsButton by lazy { LogsPage.PacketLogsButton }
private val ApplicationLogsButton by lazy { LogsPage.ApplicationLogsButton }

private val BackToLogsButton by lazy { LogPage.BackToLogsButton }
private val BackToLogsButton2 by lazy { LogPage.BackToLogsButton2 }
private val CopyLogButton by lazy { LogPage.CopyLogButton }
private val LogDetails by lazy { LogPage.LogDetails }
private val LogTypeTitle by lazy { LogPage.LogTypeTitle }

lateinit var prefs: SharedPreferences
var isMenuOpen = false

Expand All @@ -126,8 +108,6 @@ class ZitiMobileEdgeActivity : AppCompatActivity() {
var mtu = "4000"
var dns = "169.254.0.2"
var state = "startActivity"
var log_application = ""
var log_tunneler = ""
val version = "${BuildConfig.VERSION_NAME}(${BuildConfig.GIT_COMMIT})"

private lateinit var model: TunnelModel
Expand All @@ -154,7 +134,6 @@ class ZitiMobileEdgeActivity : AppCompatActivity() {
var offScreenX = 0
var offScreenY = 0
var openY = 0
var isOpen = false

fun getScreenWidth(): Int {
return Resources.getSystem().displayMetrics.widthPixels
Expand Down Expand Up @@ -248,19 +227,16 @@ class ZitiMobileEdgeActivity : AppCompatActivity() {
AdvancedPage.root.visibility = View.VISIBLE
ConfigPage.root.visibility = View.VISIBLE
LogsPage.root.visibility = View.VISIBLE
LogPage.root.visibility = View.VISIBLE
IdentityDetailsPage.root.visibility = View.VISIBLE
IdentityPage.root.visibility = View.VISIBLE
AdvancedPage.root.alpha = 0f
ConfigPage.root.alpha = 0f
LogsPage.root.alpha = 0f
LogPage.root.alpha = 0f
IdentityPage.root.alpha = 0f
IdentityDetailsPage.root.alpha = 0f
AdvancedPage.root.x = offScreenX.toFloat()
ConfigPage.root.x = offScreenX.toFloat()
LogsPage.root.x = offScreenX.toFloat()
LogPage.root.x = offScreenX.toFloat()
IdentityPage.root.x = offScreenX.toFloat()
IdentityDetailsPage.root.x = offScreenX.toFloat()
openY = offScreenY
Expand Down Expand Up @@ -341,13 +317,6 @@ class ZitiMobileEdgeActivity : AppCompatActivity() {
toggleSlide(AdvancedPage, "advanced")
}

LogsLabel.isLongClickable = true
LogsLabel.setOnLongClickListener {
val intent = Intent(it.context, DebugInfoActivity::class.java)
startActivity(intent)
true
}

FeedbackButton.setOnClickListener {
startActivity(Intent.createChooser(DebugInfo.feedbackIntent(app = ZitiMobileEdgeApp.app),
"Send Email"))
Expand Down Expand Up @@ -381,39 +350,19 @@ class ZitiMobileEdgeActivity : AppCompatActivity() {
BackConfigButton2.setOnClickListener {
toggleSlide(ConfigPage, "advanced")
}
BackLogsButton.setOnClickListener {
toggleSlide(ConfigPage, "advanced")
}
BackToLogsButton.setOnClickListener {
toggleSlide(LogPage, "logs")
}
BackIdentityDetailsButton.setOnClickListener {
toggleSlide(IdentityDetailsPage, "identities")
}
BackToLogsButton2.setOnClickListener {
toggleSlide(LogPage, "logs")
}
BackLogsButton.setOnClickListener {
toggleSlide(LogsPage, "advanced")
}
CopyLogButton.setOnClickListener {
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText("Logs", LogDetails.text.toString())
clipboard.setPrimaryClip(clip)
Toast.makeText(applicationContext,"Log has been copied to your clipboard",Toast.LENGTH_LONG).show()
}

// Advanced Buttons
TunnelButton.setOnClickListener {
toggleSlide(ConfigPage, "config")
}
LogsButton.setOnClickListener {
toggleSlide(LogsPage, "logs")
}
PacketLogsButton.setOnClickListener {
val intent = Intent(it.context, DebugInfoActivity::class.java)
startActivity(intent)
true
supportFragmentManager.commit {
add<LogsFragment>(R.id.fragment_container_view, "logs")
addToBackStack("logs")
}
}

// Dashboard Buttons
Expand All @@ -424,23 +373,6 @@ class ZitiMobileEdgeActivity : AppCompatActivity() {
// toggleSlide(IdentityPage, "identities")
//}

LogDetails.movementMethod = ScrollingMovementMethod()
ApplicationLogsButton.setOnClickListener {
LogTypeTitle.text = ("Application Logs")
LogDetails.text = log_application
GlobalScope.launch(Dispatchers.IO) {
val p = Runtime.getRuntime().exec("logcat -d -t 200 --pid=${Process.myPid()}")
val lines = p.inputStream.bufferedReader().readText()

Log.d("ziti", "log is ${lines.length} bytes")

LogDetails.post {
LogDetails.text = lines
}
}
toggleSlide(binding.LogPage.root, "logdetails")
}

model = (application as ZitiMobileEdgeApp).model
model.identities().observe(this) { contextList ->
binding.IdentityListing.removeAllViews()
Expand Down
91 changes: 91 additions & 0 deletions app/src/main/java/org/openziti/mobile/fragments/LogFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2024 NetFoundry. All rights reserved.
*/

package org.openziti.mobile.fragments

import android.content.ClipData
import android.content.ClipboardManager
import android.os.Bundle
import android.os.Process
import android.text.method.ScrollingMovementMethod
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.content.ContextCompat.getSystemService
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.transition.TransitionInflater
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.openziti.mobile.R
import org.openziti.mobile.databinding.LogBinding

/**
* A simple [Fragment] subclass.
* Use the [LogFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class LogFragment : Fragment() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val inflater = TransitionInflater.from(requireContext())
enterTransition = inflater.inflateTransition(R.transition.fade)
exitTransition = inflater.inflateTransition(R.transition.slide)
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val b = LogBinding.inflate(inflater, container, false).apply {
arguments?.getString(LOG_TITLE)?.let {
LogTypeTitle.text = it
}

BackToLogsButton.setOnClickListener {
parentFragmentManager.popBackStack()
}
BackToLogsButton2.setOnClickListener {
parentFragmentManager.popBackStack()
}

CopyLogButton.setOnClickListener {
val clipboard = getSystemService(requireContext(), ClipboardManager::class.java)
clipboard?.let {
val clip = ClipData.newPlainText("Logs", LogDetails.text.toString())

it.setPrimaryClip(clip)
Toast.makeText(
requireContext(),
"Log has been copied to your clipboard",
Toast.LENGTH_LONG
).show()
}
}


LogDetails.movementMethod = ScrollingMovementMethod()
viewLifecycleOwner.lifecycleScope.launch(Dispatchers.IO) {
val p = Runtime.getRuntime().exec("logcat -d -t 200 --pid=${Process.myPid()}")
val lines = p.inputStream.bufferedReader().readText()

Log.d("ziti", "log is ${lines.length} bytes")

LogDetails.post {
LogDetails.text = lines
}
}

}

return b.root
}

companion object {
val LOG_TITLE = "log_title"
}
}
54 changes: 54 additions & 0 deletions app/src/main/java/org/openziti/mobile/fragments/LogsFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2024 NetFoundry. All rights reserved.
*/
package org.openziti.mobile.fragments

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.fragment.app.add
import androidx.fragment.app.commit
import androidx.transition.TransitionInflater
import org.openziti.mobile.R
import org.openziti.mobile.databinding.LogsBinding
import org.openziti.mobile.debug.DebugInfoActivity

class LogsFragment : Fragment() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val inflater = TransitionInflater.from(requireContext())
enterTransition = inflater.inflateTransition(R.transition.fade)
exitTransition = inflater.inflateTransition(R.transition.slide)
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val b = LogsBinding.inflate(inflater, container, false)
b.BackLogsButton.setOnClickListener {
parentFragmentManager.popBackStack()
}

b.PacketLogsButton.setOnClickListener {
val intent = Intent(it.context, DebugInfoActivity::class.java)
startActivity(intent)
}

b.ApplicationLogsButton.setOnClickListener {
parentFragmentManager.commit {
add<LogFragment>(R.id.fragment_container_view, "log",
args = bundleOf(LogFragment.LOG_TITLE to "Application Logs"))
addToBackStack("log")
}
}

// Inflate the layout for this fragment
return b.root
}
}
Loading