refactoring content

This commit is contained in:
yb 2022-07-01 03:47:28 +02:00
parent 50ad2e1ef4
commit 1f9c481244
3 changed files with 163 additions and 1 deletions

View File

@ -122,7 +122,7 @@ mViewModel.getMSerialnumber().observe(getViewLifecycleOwner(), new Observer<Stri
});
checkUserAuth();
//checkUserAuth();
return root;
}

View File

@ -3,6 +3,7 @@ package eu.csc.vehown.persist.localstorage;
import androidx.room.Database;
import androidx.room.RoomDatabase;
import eu.csc.vehown.persist.localstorage.dao.*;
import eu.csc.vehown.persist.localstorage.entity.EntityLog;
import eu.csc.vehown.persist.localstorage.entity.events.EventEntity;
import eu.csc.vehown.persist.localstorage.entity.lease.LeaseEntity;
import eu.csc.vehown.persist.localstorage.entity.lease.LeaseInfoEntity;
@ -23,6 +24,8 @@ import eu.csc.vehown.persist.localstorage.entity.vehicle.VehicleSystemEntity;
LeaseInfoEntity.class,
MobilePhoneEntity.class,
EntityLog.class
},
exportSchema = true,
version = 1)

View File

@ -1,4 +1,163 @@
package eu.csc.vehown.persist.localstorage.entity;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.room.Entity;
import androidx.room.Index;
import androidx.room.PrimaryKey;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import eu.csc.vehown.persist.localstorage.PersistenceFactory;
import eu.csc.vehown.persist.localstorage.VehicleOwnerDatabase;
@Entity(
tableName = EntityLog.TABLE_NAME,
foreignKeys = {
},
indices = {
@Index(value = {"time"})
}
)
public class EntityLog {
static final String TABLE_NAME = "log";
private static boolean ok = true;
private static long count = 0;
private static Long last_cleanup = null;
private static final long LOG_CLEANUP_INTERVAL = 3600 * 1000L; // milliseconds
private static final long LOG_KEEP_DURATION = 12 * 3600 * 1000L; // milliseconds
private static final int LOG_DELETE_BATCH_SIZE = 50;
@PrimaryKey(autoGenerate = true)
public Long id;
@NonNull
public Long time;
@NonNull
public Type type = Type.General;
public Long account;
public Long folder;
public Long message;
@NonNull
public String data;
enum Type {General, Statistics, Scheduling, Network, Account, Protocol, Classification, Notification, Rules, Debug}
static void log(final Context context, @NonNull Type type, String data) {
log(context, type, null, null, null, data);
}
static void log(final Context context, @NonNull Type type, Long account, Long folder, Long message, String data) {
Log.i(EntityLog.class.getSimpleName(), data);
if (context == null)
return;
//if (type == Type.Debug && !(BuildConfig.DEBUG || BuildConfig.TEST_RELEASE))
// return;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean main_log = prefs.getBoolean("main_log", true);
if (!main_log)
return;
final EntityLog entry = new EntityLog();
entry.time = new Date().getTime();
entry.type = type;
entry.account = account;
entry.folder = folder;
entry.message = message;
entry.data = data;
final VehicleOwnerDatabase db = PersistenceFactory.generateDatabase(context);
final Context acontext = context.getApplicationContext();
/*
executor.submit(new Runnable() {
@Override
public void run() {
// Check available storage space
if (!ok || (++count % LOG_DELETE_BATCH_SIZE) == 0) {
long cake = Helper.getAvailableStorageSpace();
boolean wasOk = ok;
ok = (cake > Helper.MIN_REQUIRED_SPACE);
if (!ok)
if (wasOk) {
entry.type = Type.General;
entry.account = null;
entry.folder = null;
entry.message = null;
entry.data = "Insufficient storage space=" +
Helper.humanReadableByteCount(cake) + "/" +
Helper.humanReadableByteCount(Helper.MIN_REQUIRED_SPACE);
} else
return;
}
try {
db.beginTransaction();
db.log().insertLog(entry);
db.setTransactionSuccessful();
} catch (Throwable ex) {
Log.e(ex);
} finally {
db.endTransaction();
}
long now = new Date().getTime();
if (last_cleanup == null || last_cleanup + LOG_CLEANUP_INTERVAL < now) {
last_cleanup = now;
cleanup(acontext, now - LOG_KEEP_DURATION);
}
}
});
*/
}
static void clear(final Context context) {
final Context acontext = context.getApplicationContext();
/*
executor.submit(new Runnable() {
@Override
public void run() {
cleanup(acontext, new Date().getTime());
}
});
*/
}
private static void cleanup(final Context context, final long before) {
/*
Log.i("Log cleanup interval=" + LOG_CLEANUP_INTERVAL);
DB db = DB.getInstance(context);
while (true)
try {
db.beginTransaction();
int logs = db.log().deleteLogs(before, LOG_DELETE_BATCH_SIZE);
db.setTransactionSuccessful();
Log.i("Cleanup logs=" + logs + " before=" + new Date(before));
if (logs < LOG_DELETE_BATCH_SIZE)
break;
} catch (Throwable ex) {
Log.e(ex);
break;
} finally {
db.endTransaction();
}
*/
}
}
/*
import android.content.Context;
import android.content.SharedPreferences;