Skip to content

Commit 3f79f14

Browse files
committed
Add space for background worker execution info in ETM Activity
Signed-off-by: Jonas Mayer <jonas.a.mayer@gmx.net>
1 parent 251ef24 commit 3f79f14

7 files changed

Lines changed: 140 additions & 3 deletions

File tree

app/src/main/java/com/nextcloud/client/etm/pages/EtmBackgroundJobsFragment.kt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ class EtmBackgroundJobsFragment : EtmBaseFragment() {
5050
val started = view.findViewById<TextView>(R.id.etm_background_job_started)
5151
val progress = view.findViewById<TextView>(R.id.etm_background_job_progress)
5252
private val progressRow = view.findViewById<View>(R.id.etm_background_job_progress_row)
53+
val executionCount = view.findViewById<TextView>(R.id.etm_background_execution_count)
54+
val executionLog = view.findViewById<TextView>(R.id.etm_background_execution_logs)
55+
private val executionLogRow = view.findViewById<View>(R.id.etm_background_execution_logs_row)
5356

5457
var progressEnabled: Boolean = progressRow.visibility == View.VISIBLE
5558
get() {
@@ -63,6 +66,20 @@ class EtmBackgroundJobsFragment : EtmBaseFragment() {
6366
View.GONE
6467
}
6568
}
69+
70+
var logsEnabled: Boolean = executionLogRow.visibility == View.VISIBLE
71+
get() {
72+
return executionLogRow.visibility == View.VISIBLE
73+
}
74+
set(value) {
75+
field = value
76+
executionLogRow.visibility = if (value) {
77+
View.VISIBLE
78+
} else {
79+
View.GONE
80+
}
81+
}
82+
6683
}
6784

6885
private val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:MM:ssZ", Locale.getDefault())
@@ -74,7 +91,12 @@ class EtmBackgroundJobsFragment : EtmBaseFragment() {
7491

7592
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
7693
val view = inflater.inflate(R.layout.etm_background_job_list_item, parent, false)
77-
return ViewHolder(view)
94+
val viewHolder = ViewHolder(view)
95+
viewHolder.logsEnabled = false
96+
view.setOnClickListener {
97+
viewHolder.logsEnabled = !viewHolder.logsEnabled
98+
}
99+
return viewHolder
78100
}
79101

80102
override fun getItemCount(): Int {
@@ -94,6 +116,8 @@ class EtmBackgroundJobsFragment : EtmBaseFragment() {
94116
} else {
95117
vh.progressEnabled = false
96118
}
119+
vh.executionCount.text = "0"
120+
vh.executionLog.text = "None"
97121
}
98122
}
99123

app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ import androidx.work.workDataOf
3737
import com.nextcloud.client.account.User
3838
import com.nextcloud.client.core.Clock
3939
import com.nextcloud.client.documentscan.GeneratePdfFromImagesWork
40+
import com.nextcloud.client.preferences.AppPreferences
4041
import com.owncloud.android.datamodel.OCFile
42+
import java.time.LocalDate
4143
import java.util.Date
4244
import java.util.UUID
4345
import java.util.concurrent.TimeUnit
46+
import javax.inject.Inject
4447
import kotlin.reflect.KClass
4548

4649
/**
@@ -63,7 +66,11 @@ internal class BackgroundJobManagerImpl(
6366
private val clock: Clock
6467
) : BackgroundJobManager {
6568

69+
@Inject
70+
private var preferences: AppPreferences? = null
71+
6672
companion object {
73+
6774
const val TAG_ALL = "*" // This tag allows us to retrieve list of all jobs run by Nextcloud client
6875
const val JOB_CONTENT_OBSERVER = "content_observer"
6976
const val JOB_PERIODIC_CONTACTS_BACKUP = "periodic_contacts_backup"
@@ -98,6 +105,8 @@ internal class BackgroundJobManagerImpl(
98105
const val DEFAULT_PERIODIC_JOB_INTERVAL_MINUTES = 15L
99106
const val DEFAULT_IMMEDIATE_JOB_DELAY_SEC = 3L
100107

108+
private const val KEEP_LOG_MILLIS = 1000 * 60 * 60 * 24 *3L
109+
101110
fun formatNameTag(name: String, user: User? = null): String {
102111
return if (user == null) {
103112
"$TAG_PREFIX_NAME:$name"
@@ -143,14 +152,34 @@ internal class BackgroundJobManagerImpl(
143152
name = metadata.get(TAG_PREFIX_NAME) ?: NOT_SET_VALUE,
144153
user = metadata.get(TAG_PREFIX_USER) ?: NOT_SET_VALUE,
145154
started = timestamp,
146-
progress = info.progress.getInt("progress", -1)
155+
progress = info.progress.getInt("progress", -1),
147156
)
148157
} else {
149158
null
150159
}
151160
}
161+
162+
fun deleteOldLogs(logEntries: MutableList<LogEntry>) : MutableList<LogEntry>{
163+
164+
logEntries.removeIf {
165+
return@removeIf it.started != null &&
166+
Date(Date().time - KEEP_LOG_MILLIS).before(it.started)
167+
}
168+
return logEntries
169+
170+
}
171+
172+
152173
}
153174

175+
fun logStartOfWorker(workerName : String){
176+
if (preferences == null) return;
177+
178+
preferences!!.readLogEntry()
179+
}
180+
181+
fun logEndOfWorker(workerName: String)
182+
154183
/**
155184
* Create [OneTimeWorkRequest.Builder] pre-set with common attributes
156185
*/

app/src/main/java/com/nextcloud/client/jobs/JobInfo.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
*/
2020
package com.nextcloud.client.jobs
2121

22+
import androidx.work.ListenableWorker
23+
import com.google.common.util.concurrent.ListenableFuture
2224
import java.util.Date
2325
import java.util.UUID
2426

@@ -28,5 +30,14 @@ data class JobInfo(
2830
val name: String = "",
2931
val user: String = "",
3032
val started: Date = Date(0),
31-
val progress: Int = 0
33+
val progress: Int = 0,
34+
val executionLog: MutableList<LogEntry>? = null
35+
)
36+
37+
38+
data class LogEntry (
39+
val started: Date? = null,
40+
val finished: Date? = null,
41+
val result: String? = null,
42+
var worker: String = "None"
3243
)

app/src/main/java/com/nextcloud/client/preferences/AppPreferences.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
package com.nextcloud.client.preferences;
2424

2525
import com.nextcloud.appReview.AppReviewShownModel;
26+
import com.nextcloud.client.jobs.LogEntry;
2627
import com.owncloud.android.datamodel.OCFile;
2728
import com.owncloud.android.utils.FileSortOrder;
2829

30+
import java.util.List;
31+
2932
import androidx.annotation.NonNull;
3033
import androidx.annotation.Nullable;
3134

@@ -317,6 +320,12 @@ default void onDarkThemeModeChanged(DarkMode mode) {
317320
*/
318321
int getLastSeenVersionCode();
319322

323+
void saveLogEntry(List<LogEntry> logEntryList);
324+
325+
List<LogEntry> readLogEntry();
326+
327+
328+
320329
/**
321330
* Saves the version code as the last seen version code.
322331
*

app/src/main/java/com/nextcloud/client/preferences/AppPreferencesImpl.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828
import android.content.SharedPreferences;
2929
import android.content.res.Configuration;
3030

31+
import com.google.common.reflect.TypeToken;
3132
import com.google.gson.Gson;
3233
import com.nextcloud.appReview.AppReviewShownModel;
3334
import com.nextcloud.client.account.User;
3435
import com.nextcloud.client.account.UserAccountManager;
3536
import com.nextcloud.client.account.UserAccountManagerImpl;
37+
import com.nextcloud.client.jobs.LogEntry;
3638
import com.owncloud.android.datamodel.ArbitraryDataProvider;
3739
import com.owncloud.android.datamodel.ArbitraryDataProviderImpl;
3840
import com.owncloud.android.datamodel.FileDataStorageManager;
@@ -41,6 +43,8 @@
4143
import com.owncloud.android.ui.activity.SettingsActivity;
4244
import com.owncloud.android.utils.FileSortOrder;
4345

46+
import java.lang.reflect.Type;
47+
import java.util.List;
4448
import java.util.Set;
4549
import java.util.concurrent.CopyOnWriteArraySet;
4650

@@ -108,6 +112,8 @@ public final class AppPreferencesImpl implements AppPreferences {
108112
private static final String PREF__STORAGE_PERMISSION_REQUESTED = "storage_permission_requested";
109113
private static final String PREF__IN_APP_REVIEW_DATA = "in_app_review_data";
110114

115+
private static final String LOG_ENTRY = "log_entry";
116+
111117
private final Context context;
112118
private final SharedPreferences preferences;
113119
private final UserAccountManager userAccountManager;
@@ -499,6 +505,21 @@ public int getLastSeenVersionCode() {
499505
return preferences.getInt(AUTO_PREF__LAST_SEEN_VERSION_CODE, 0);
500506
}
501507

508+
@Override
509+
public void saveLogEntry(List<LogEntry> logEntryList) {
510+
Gson gson = new Gson();
511+
String json = gson.toJson(logEntryList);
512+
preferences.edit().putString(LOG_ENTRY, json).apply();
513+
}
514+
515+
@Override
516+
public List<LogEntry> readLogEntry() {
517+
String json = preferences.getString(LOG_ENTRY, null);
518+
Gson gson = new Gson();
519+
Type listType = new TypeToken<List<LogEntry>>() {}.getType();
520+
return gson.fromJson(json, listType);
521+
}
522+
502523
@Override
503524
public void setLastSeenVersionCode(int versionCode) {
504525
preferences.edit().putInt(AUTO_PREF__LAST_SEEN_VERSION_CODE, versionCode).apply();

app/src/main/res/layout/etm_background_job_list_item.xml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,45 @@
136136

137137
</TableRow>
138138

139+
<TableRow
140+
android:layout_width="match_parent"
141+
android:layout_height="match_parent">
142+
143+
<TextView
144+
android:layout_width="wrap_content"
145+
android:layout_height="wrap_content"
146+
android:layout_marginEnd="20dp"
147+
android:text="@string/etm_background_execution_count" />
148+
149+
<TextView
150+
android:id="@+id/etm_background_execution_count"
151+
android:layout_width="wrap_content"
152+
android:layout_height="wrap_content"
153+
tools:text="0" />
154+
155+
</TableRow>
156+
157+
<TableRow
158+
android:id="@+id/etm_background_execution_logs_row"
159+
android:layout_width="match_parent"
160+
android:layout_height="wrap_content">
161+
162+
<TextView
163+
android:layout_width="wrap_content"
164+
android:layout_height="wrap_content"
165+
android:layout_marginEnd="20dp"
166+
android:text="@string/etm_background_execution_log" />
167+
168+
<ScrollView
169+
android:layout_width="match_parent"
170+
android:layout_height="80dp">
171+
172+
<TextView
173+
android:id="@+id/etm_background_execution_logs"
174+
android:layout_width="match_parent"
175+
android:layout_height="wrap_content" />
176+
</ScrollView>
177+
178+
</TableRow>
179+
139180
</TableLayout>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,8 @@
880880
<string name="etm_background_job_state">State</string>
881881
<string name="etm_background_job_started">Started</string>
882882
<string name="etm_background_job_progress">Progress</string>
883+
<string name="etm_background_execution_count">Times run in 48h</string>
884+
<string name="etm_background_execution_log">Execution logs</string>
883885
<string name="etm_migrations">Migrations (app upgrade)</string>
884886
<string name="etm_transfer">File transfer</string>
885887
<string name="etm_transfer_remote_path">Remote path</string>

0 commit comments

Comments
 (0)