2020 */
2121package com.nextcloud.client.etm.pages
2222
23+ import android.annotation.SuppressLint
2324import android.os.Bundle
2425import android.view.LayoutInflater
2526import android.view.Menu
@@ -32,15 +33,23 @@ import androidx.lifecycle.Observer
3233import androidx.recyclerview.widget.DividerItemDecoration
3334import androidx.recyclerview.widget.LinearLayoutManager
3435import androidx.recyclerview.widget.RecyclerView
36+ import com.nextcloud.client.di.Injectable
3537import com.nextcloud.client.etm.EtmBaseFragment
38+ import com.nextcloud.client.jobs.BackgroundJobManagerImpl
3639import com.nextcloud.client.jobs.JobInfo
40+ import com.nextcloud.client.preferences.AppPreferences
3741import com.owncloud.android.R
3842import java.text.SimpleDateFormat
3943import java.util.Locale
44+ import javax.inject.Inject
4045
41- class EtmBackgroundJobsFragment : EtmBaseFragment () {
46+ class EtmBackgroundJobsFragment : EtmBaseFragment (), Injectable {
4247
43- class Adapter (private val inflater : LayoutInflater ) : RecyclerView.Adapter<Adapter.ViewHolder>() {
48+ @Inject
49+ lateinit var preferences: AppPreferences
50+
51+ class Adapter (private val inflater : LayoutInflater , private val preferences : AppPreferences ) :
52+ RecyclerView .Adapter <Adapter .ViewHolder >() {
4453
4554 class ViewHolder (view : View ) : RecyclerView.ViewHolder(view) {
4655 val uuid = view.findViewById<TextView >(R .id.etm_background_job_uuid)
@@ -50,6 +59,10 @@ class EtmBackgroundJobsFragment : EtmBaseFragment() {
5059 val started = view.findViewById<TextView >(R .id.etm_background_job_started)
5160 val progress = view.findViewById<TextView >(R .id.etm_background_job_progress)
5261 private val progressRow = view.findViewById<View >(R .id.etm_background_job_progress_row)
62+ val executionCount = view.findViewById<TextView >(R .id.etm_background_execution_count)
63+ val executionLog = view.findViewById<TextView >(R .id.etm_background_execution_logs)
64+ private val executionLogRow = view.findViewById<View >(R .id.etm_background_execution_logs_row)
65+ val executionTimesRow = view.findViewById<View >(R .id.etm_background_execution_times_row)
5366
5467 var progressEnabled: Boolean = progressRow.visibility == View .VISIBLE
5568 get() {
@@ -63,6 +76,19 @@ class EtmBackgroundJobsFragment : EtmBaseFragment() {
6376 View .GONE
6477 }
6578 }
79+
80+ var logsEnabled: Boolean = executionLogRow.visibility == View .VISIBLE
81+ get() {
82+ return executionLogRow.visibility == View .VISIBLE
83+ }
84+ set(value) {
85+ field = value
86+ executionLogRow.visibility = if (value) {
87+ View .VISIBLE
88+ } else {
89+ View .GONE
90+ }
91+ }
6692 }
6793
6894 private val dateFormat = SimpleDateFormat (" yyyy-MM-dd HH:MM:ssZ" , Locale .getDefault())
@@ -74,13 +100,20 @@ class EtmBackgroundJobsFragment : EtmBaseFragment() {
74100
75101 override fun onCreateViewHolder (parent : ViewGroup , viewType : Int ): ViewHolder {
76102 val view = inflater.inflate(R .layout.etm_background_job_list_item, parent, false )
77- return ViewHolder (view)
103+ val viewHolder = ViewHolder (view)
104+ viewHolder.logsEnabled = false
105+ viewHolder.executionTimesRow.visibility = View .GONE
106+ view.setOnClickListener {
107+ viewHolder.logsEnabled = ! viewHolder.logsEnabled
108+ }
109+ return viewHolder
78110 }
79111
80112 override fun getItemCount (): Int {
81113 return backgroundJobs.size
82114 }
83115
116+ @SuppressLint(" SetTextI18n" )
84117 override fun onBindViewHolder (vh : ViewHolder , position : Int ) {
85118 val info = backgroundJobs[position]
86119 vh.uuid.text = info.id.toString()
@@ -94,6 +127,34 @@ class EtmBackgroundJobsFragment : EtmBaseFragment() {
94127 } else {
95128 vh.progressEnabled = false
96129 }
130+
131+ val logs = preferences.readLogEntry()
132+ val logsForThisWorker =
133+ logs.filter { BackgroundJobManagerImpl .parseTag(it.workerClass)?.second == info.workerClass }
134+ if (logsForThisWorker.isNotEmpty()) {
135+ vh.executionTimesRow.visibility = View .VISIBLE
136+ vh.executionCount.text =
137+ " ${logsForThisWorker.filter { it.started != null }.size} " +
138+ " (${logsForThisWorker.filter { it.finished != null }.size} )"
139+ var logText = " Worker Logs\n\n " +
140+ " *** Does NOT differentiate between immediate or periodic kinds of Work! ***\n " +
141+ " *** Times run in 48h: Times started (Times finished) ***\n "
142+ logsForThisWorker.forEach {
143+ logText + = " ----------------------\n "
144+ logText + = " Worker ${BackgroundJobManagerImpl .parseTag(it.workerClass)?.second} \n "
145+ logText + = if (it.started == null ) {
146+ " ENDED at\n ${it.finished} \n With result: ${it.result} \n "
147+ } else {
148+ " STARTED at\n ${it.started} \n "
149+ }
150+ }
151+ vh.executionLog.text = logText
152+ } else {
153+ vh.executionLog.text = " Worker Logs\n\n " +
154+ " No Entries -> Maybe logging is not implemented for Worker or it has not run yet."
155+ vh.executionCount.text = " 0"
156+ vh.executionTimesRow.visibility = View .GONE
157+ }
97158 }
98159 }
99160
@@ -107,7 +168,7 @@ class EtmBackgroundJobsFragment : EtmBaseFragment() {
107168
108169 override fun onCreateView (inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ): View ? {
109170 val view = inflater.inflate(R .layout.fragment_etm_background_jobs, container, false )
110- adapter = Adapter (inflater)
171+ adapter = Adapter (inflater, preferences )
111172 list = view.findViewById(R .id.etm_background_jobs_list)
112173 list.layoutManager = LinearLayoutManager (context)
113174 list.addItemDecoration(DividerItemDecoration (context, DividerItemDecoration .VERTICAL ))
@@ -127,22 +188,27 @@ class EtmBackgroundJobsFragment : EtmBaseFragment() {
127188 vm.cancelAllJobs()
128189 true
129190 }
191+
130192 R .id.etm_background_jobs_prune -> {
131193 vm.pruneJobs()
132194 true
133195 }
196+
134197 R .id.etm_background_jobs_start_test -> {
135198 vm.startTestJob(periodic = false )
136199 true
137200 }
201+
138202 R .id.etm_background_jobs_schedule_test -> {
139203 vm.startTestJob(periodic = true )
140204 true
141205 }
206+
142207 R .id.etm_background_jobs_cancel_test -> {
143208 vm.cancelTestJob()
144209 true
145210 }
211+
146212 else -> super .onOptionsItemSelected(item)
147213 }
148214 }
0 commit comments