Browse Source

Add space for background worker execution info in ETM Activity

Signed-off-by: Jonas Mayer <jonas.a.mayer@gmx.net>
Jonas Mayer 1 year ago
parent
commit
3f79f149c8

+ 25 - 1
app/src/main/java/com/nextcloud/client/etm/pages/EtmBackgroundJobsFragment.kt

@@ -50,6 +50,9 @@ class EtmBackgroundJobsFragment : EtmBaseFragment() {
             val started = view.findViewById<TextView>(R.id.etm_background_job_started)
             val progress = view.findViewById<TextView>(R.id.etm_background_job_progress)
             private val progressRow = view.findViewById<View>(R.id.etm_background_job_progress_row)
+            val executionCount = view.findViewById<TextView>(R.id.etm_background_execution_count)
+            val executionLog = view.findViewById<TextView>(R.id.etm_background_execution_logs)
+            private val executionLogRow = view.findViewById<View>(R.id.etm_background_execution_logs_row)
 
             var progressEnabled: Boolean = progressRow.visibility == View.VISIBLE
                 get() {
@@ -63,6 +66,20 @@ class EtmBackgroundJobsFragment : EtmBaseFragment() {
                         View.GONE
                     }
                 }
+
+            var logsEnabled: Boolean = executionLogRow.visibility == View.VISIBLE
+                get() {
+                    return executionLogRow.visibility == View.VISIBLE
+                }
+                set(value) {
+                    field = value
+                    executionLogRow.visibility = if (value) {
+                        View.VISIBLE
+                    } else {
+                        View.GONE
+                    }
+                }
+
         }
 
         private val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:MM:ssZ", Locale.getDefault())
@@ -74,7 +91,12 @@ class EtmBackgroundJobsFragment : EtmBaseFragment() {
 
         override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
             val view = inflater.inflate(R.layout.etm_background_job_list_item, parent, false)
-            return ViewHolder(view)
+            val viewHolder = ViewHolder(view)
+            viewHolder.logsEnabled = false
+            view.setOnClickListener {
+                viewHolder.logsEnabled = !viewHolder.logsEnabled
+            }
+            return viewHolder
         }
 
         override fun getItemCount(): Int {
@@ -94,6 +116,8 @@ class EtmBackgroundJobsFragment : EtmBaseFragment() {
             } else {
                 vh.progressEnabled = false
             }
+            vh.executionCount.text = "0"
+            vh.executionLog.text = "None"
         }
     }
 

+ 30 - 1
app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt

@@ -37,10 +37,13 @@ import androidx.work.workDataOf
 import com.nextcloud.client.account.User
 import com.nextcloud.client.core.Clock
 import com.nextcloud.client.documentscan.GeneratePdfFromImagesWork
+import com.nextcloud.client.preferences.AppPreferences
 import com.owncloud.android.datamodel.OCFile
+import java.time.LocalDate
 import java.util.Date
 import java.util.UUID
 import java.util.concurrent.TimeUnit
+import javax.inject.Inject
 import kotlin.reflect.KClass
 
 /**
@@ -63,7 +66,11 @@ internal class BackgroundJobManagerImpl(
     private val clock: Clock
 ) : BackgroundJobManager {
 
+    @Inject
+    private var preferences: AppPreferences? = null
+
     companion object {
+
         const val TAG_ALL = "*" // This tag allows us to retrieve list of all jobs run by Nextcloud client
         const val JOB_CONTENT_OBSERVER = "content_observer"
         const val JOB_PERIODIC_CONTACTS_BACKUP = "periodic_contacts_backup"
@@ -98,6 +105,8 @@ internal class BackgroundJobManagerImpl(
         const val DEFAULT_PERIODIC_JOB_INTERVAL_MINUTES = 15L
         const val DEFAULT_IMMEDIATE_JOB_DELAY_SEC = 3L
 
+        private const val KEEP_LOG_MILLIS = 1000 * 60 * 60 * 24 *3L
+
         fun formatNameTag(name: String, user: User? = null): String {
             return if (user == null) {
                 "$TAG_PREFIX_NAME:$name"
@@ -143,14 +152,34 @@ internal class BackgroundJobManagerImpl(
                     name = metadata.get(TAG_PREFIX_NAME) ?: NOT_SET_VALUE,
                     user = metadata.get(TAG_PREFIX_USER) ?: NOT_SET_VALUE,
                     started = timestamp,
-                    progress = info.progress.getInt("progress", -1)
+                    progress = info.progress.getInt("progress", -1),
                 )
             } else {
                 null
             }
         }
+
+        fun deleteOldLogs(logEntries: MutableList<LogEntry>) : MutableList<LogEntry>{
+
+            logEntries.removeIf {
+                return@removeIf it.started != null &&
+                    Date(Date().time - KEEP_LOG_MILLIS).before(it.started)
+            }
+            return logEntries
+
+        }
+
+
     }
 
+    fun logStartOfWorker(workerName : String){
+        if (preferences == null) return;
+
+        preferences!!.readLogEntry()
+    }
+
+    fun logEndOfWorker(workerName: String)
+
     /**
      * Create [OneTimeWorkRequest.Builder] pre-set with common attributes
      */

+ 12 - 1
app/src/main/java/com/nextcloud/client/jobs/JobInfo.kt

@@ -19,6 +19,8 @@
  */
 package com.nextcloud.client.jobs
 
+import androidx.work.ListenableWorker
+import com.google.common.util.concurrent.ListenableFuture
 import java.util.Date
 import java.util.UUID
 
@@ -28,5 +30,14 @@ data class JobInfo(
     val name: String = "",
     val user: String = "",
     val started: Date = Date(0),
-    val progress: Int = 0
+    val progress: Int = 0,
+    val executionLog: MutableList<LogEntry>? = null
+)
+
+
+data class LogEntry (
+    val started: Date? = null,
+    val finished: Date? = null,
+    val result: String? = null,
+    var worker: String = "None"
 )

+ 9 - 0
app/src/main/java/com/nextcloud/client/preferences/AppPreferences.java

@@ -23,9 +23,12 @@
 package com.nextcloud.client.preferences;
 
 import com.nextcloud.appReview.AppReviewShownModel;
+import com.nextcloud.client.jobs.LogEntry;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.utils.FileSortOrder;
 
+import java.util.List;
+
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 
@@ -317,6 +320,12 @@ public interface AppPreferences {
      */
     int getLastSeenVersionCode();
 
+    void saveLogEntry(List<LogEntry> logEntryList);
+
+    List<LogEntry> readLogEntry();
+
+
+
     /**
      * Saves the version code as the last seen version code.
      *

+ 21 - 0
app/src/main/java/com/nextcloud/client/preferences/AppPreferencesImpl.java

@@ -28,11 +28,13 @@ import android.content.Context;
 import android.content.SharedPreferences;
 import android.content.res.Configuration;
 
+import com.google.common.reflect.TypeToken;
 import com.google.gson.Gson;
 import com.nextcloud.appReview.AppReviewShownModel;
 import com.nextcloud.client.account.User;
 import com.nextcloud.client.account.UserAccountManager;
 import com.nextcloud.client.account.UserAccountManagerImpl;
+import com.nextcloud.client.jobs.LogEntry;
 import com.owncloud.android.datamodel.ArbitraryDataProvider;
 import com.owncloud.android.datamodel.ArbitraryDataProviderImpl;
 import com.owncloud.android.datamodel.FileDataStorageManager;
@@ -41,6 +43,8 @@ import com.owncloud.android.ui.activity.PassCodeActivity;
 import com.owncloud.android.ui.activity.SettingsActivity;
 import com.owncloud.android.utils.FileSortOrder;
 
+import java.lang.reflect.Type;
+import java.util.List;
 import java.util.Set;
 import java.util.concurrent.CopyOnWriteArraySet;
 
@@ -108,6 +112,8 @@ public final class AppPreferencesImpl implements AppPreferences {
     private static final String PREF__STORAGE_PERMISSION_REQUESTED = "storage_permission_requested";
     private static final String PREF__IN_APP_REVIEW_DATA = "in_app_review_data";
 
+    private static final String LOG_ENTRY = "log_entry";
+
     private final Context context;
     private final SharedPreferences preferences;
     private final UserAccountManager userAccountManager;
@@ -499,6 +505,21 @@ public final class AppPreferencesImpl implements AppPreferences {
         return preferences.getInt(AUTO_PREF__LAST_SEEN_VERSION_CODE, 0);
     }
 
+    @Override
+    public void saveLogEntry(List<LogEntry> logEntryList) {
+        Gson gson = new Gson();
+        String json = gson.toJson(logEntryList);
+        preferences.edit().putString(LOG_ENTRY, json).apply();
+    }
+
+    @Override
+    public List<LogEntry> readLogEntry() {
+        String json = preferences.getString(LOG_ENTRY, null);
+        Gson gson = new Gson();
+        Type listType = new TypeToken<List<LogEntry>>() {}.getType();
+        return gson.fromJson(json, listType);
+    }
+
     @Override
     public void setLastSeenVersionCode(int versionCode) {
         preferences.edit().putInt(AUTO_PREF__LAST_SEEN_VERSION_CODE, versionCode).apply();

+ 41 - 0
app/src/main/res/layout/etm_background_job_list_item.xml

@@ -136,4 +136,45 @@
 
     </TableRow>
 
+    <TableRow
+        android:layout_width="match_parent"
+        android:layout_height="match_parent">
+
+        <TextView
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_marginEnd="20dp"
+            android:text="@string/etm_background_execution_count" />
+
+        <TextView
+            android:id="@+id/etm_background_execution_count"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            tools:text="0" />
+
+    </TableRow>
+
+    <TableRow
+        android:id="@+id/etm_background_execution_logs_row"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content">
+
+        <TextView
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_marginEnd="20dp"
+            android:text="@string/etm_background_execution_log" />
+
+        <ScrollView
+            android:layout_width="match_parent"
+            android:layout_height="80dp">
+
+            <TextView
+                android:id="@+id/etm_background_execution_logs"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content" />
+        </ScrollView>
+
+    </TableRow>
+
 </TableLayout>

+ 2 - 0
app/src/main/res/values/strings.xml

@@ -880,6 +880,8 @@
     <string name="etm_background_job_state">State</string>
     <string name="etm_background_job_started">Started</string>
     <string name="etm_background_job_progress">Progress</string>
+    <string name="etm_background_execution_count">Times run in 48h</string>
+    <string name="etm_background_execution_log">Execution logs</string>
     <string name="etm_migrations">Migrations (app upgrade)</string>
     <string name="etm_transfer">File transfer</string>
     <string name="etm_transfer_remote_path">Remote path</string>