Activity logger, Input validator
To search and validate activities
This commit is contained in:
@@ -0,0 +1,45 @@
|
|||||||
|
package com.example.petstoremobile.utils;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
public class ActivityLogger {
|
||||||
|
|
||||||
|
private static final String LOG_FILE = "log.txt";
|
||||||
|
|
||||||
|
// Logs a general message with a timestamp
|
||||||
|
public static void log(Context context, String message) {
|
||||||
|
String timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date());
|
||||||
|
String entry = timestamp + " | INFO | " + message + "\n";
|
||||||
|
writeToFile(context, entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Logs a database change (ADD, UPDATE, DELETE) with entity type and ID
|
||||||
|
public static void logChange(Context context, String entity, String action, int id) {
|
||||||
|
String timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date());
|
||||||
|
String entry = timestamp + " | DB CHANGE | " + action + " " + entity + " ID: " + id + "\n";
|
||||||
|
writeToFile(context, entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Logs an exception with location info
|
||||||
|
public static void logException(Context context, String location, Exception e) {
|
||||||
|
String timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date());
|
||||||
|
String entry = timestamp + " | ERROR | " + location + ": " + e.getMessage() + "\n";
|
||||||
|
writeToFile(context, entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Writes the log entry to log.txt in internal storage
|
||||||
|
private static void writeToFile(Context context, String entry) {
|
||||||
|
try {
|
||||||
|
FileWriter fw = new FileWriter(context.getFilesDir() + "/" + LOG_FILE, true);
|
||||||
|
fw.write(entry);
|
||||||
|
fw.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,97 @@
|
|||||||
|
package com.example.petstoremobile.utils;
|
||||||
|
|
||||||
|
import android.widget.EditText;
|
||||||
|
|
||||||
|
public class InputValidator {
|
||||||
|
|
||||||
|
// Checks if an EditText field is not empty
|
||||||
|
public static boolean isNotEmpty(EditText field, String fieldName) {
|
||||||
|
if (field.getText().toString().trim().isEmpty()) {
|
||||||
|
field.setError(fieldName + " is required");
|
||||||
|
field.requestFocus();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checks if the value is a positive integer
|
||||||
|
public static boolean isPositiveInteger(EditText field, String fieldName) {
|
||||||
|
String value = field.getText().toString().trim();
|
||||||
|
try {
|
||||||
|
int num = Integer.parseInt(value);
|
||||||
|
if (num < 0) {
|
||||||
|
field.setError(fieldName + " must be a positive number");
|
||||||
|
field.requestFocus();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
field.setError(fieldName + " must be a whole number");
|
||||||
|
field.requestFocus();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checks if the value is a positive decimal number
|
||||||
|
public static boolean isPositiveDecimal(EditText field, String fieldName) {
|
||||||
|
String value = field.getText().toString().trim();
|
||||||
|
try {
|
||||||
|
double num = Double.parseDouble(value);
|
||||||
|
if (num < 0) {
|
||||||
|
field.setError(fieldName + " must be a positive number");
|
||||||
|
field.requestFocus();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
field.setError(fieldName + " must be a number");
|
||||||
|
field.requestFocus();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checks if the email address is valid
|
||||||
|
public static boolean isValidEmail(EditText field) {
|
||||||
|
String email = field.getText().toString().trim();
|
||||||
|
if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
|
||||||
|
field.setError("Enter a valid email address");
|
||||||
|
field.requestFocus();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checks if the phone number is valid (digits, spaces, dashes, brackets allowed)
|
||||||
|
public static boolean isValidPhone(EditText field) {
|
||||||
|
String phone = field.getText().toString().trim();
|
||||||
|
if (phone.isEmpty() || !phone.matches("[0-9\\-\\s\\(\\)\\+]+")) {
|
||||||
|
field.setError("Enter a valid phone number");
|
||||||
|
field.requestFocus();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checks if the date is in YYYY-MM-DD format
|
||||||
|
public static boolean isValidDate(EditText field) {
|
||||||
|
String date = field.getText().toString().trim();
|
||||||
|
if (date.isEmpty() || !date.matches("\\d{4}-\\d{2}-\\d{2}")) {
|
||||||
|
field.setError("Date must be in YYYY-MM-DD format");
|
||||||
|
field.requestFocus();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checks if the time format is valid (e.g. 10:00 AM)
|
||||||
|
public static boolean isValidTime(EditText field) {
|
||||||
|
String time = field.getText().toString().trim();
|
||||||
|
if (time.isEmpty() || !time.matches("\\d{1,2}:\\d{2}\\s?(AM|PM|am|pm)?")) {
|
||||||
|
field.setError("Enter a valid time (e.g. 10:00 AM)");
|
||||||
|
field.requestFocus();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user