6 changed files with 237 additions and 1 deletions
-
13android/app/build.gradle
-
25android/app/src/main/java/com/vernu/sms/database/local/AppDatabase.java
-
17android/app/src/main/java/com/vernu/sms/database/local/DateConverter.java
-
114android/app/src/main/java/com/vernu/sms/database/local/SMS.java
-
27android/app/src/main/java/com/vernu/sms/database/local/SMSDao.java
-
42android/app/src/main/java/com/vernu/sms/dtos/SMSDTO.java
@ -0,0 +1,25 @@ |
|||
package com.vernu.sms.database.local; |
|||
|
|||
import android.content.Context; |
|||
import androidx.room.Database; |
|||
import androidx.room.Room; |
|||
import androidx.room.RoomDatabase; |
|||
|
|||
@Database(entities = {SMS.class}, version = 2) |
|||
public abstract class AppDatabase extends RoomDatabase { |
|||
private static volatile AppDatabase INSTANCE; |
|||
|
|||
public static AppDatabase getInstance(Context context) { |
|||
if (INSTANCE == null) { |
|||
synchronized (AppDatabase.class) { |
|||
if (INSTANCE == null) { |
|||
INSTANCE = Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "db1") |
|||
.build(); |
|||
} |
|||
} |
|||
} |
|||
return INSTANCE; |
|||
} |
|||
|
|||
public abstract SMSDao localReceivedSMSDao(); |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
package com.vernu.sms.database.local; |
|||
|
|||
import androidx.room.TypeConverter; |
|||
|
|||
import java.util.Date; |
|||
|
|||
public class DateConverter { |
|||
@TypeConverter |
|||
public static Date toDate(Long dateLong) { |
|||
return dateLong == null ? null : new Date(dateLong); |
|||
} |
|||
|
|||
@TypeConverter |
|||
public static Long fromDate(Date date) { |
|||
return date == null ? null : date.getTime(); |
|||
} |
|||
} |
|||
@ -0,0 +1,114 @@ |
|||
package com.vernu.sms.database.local; |
|||
|
|||
import androidx.annotation.NonNull; |
|||
import androidx.room.ColumnInfo; |
|||
import androidx.room.Entity; |
|||
import androidx.room.PrimaryKey; |
|||
import androidx.room.TypeConverters; |
|||
|
|||
import java.util.Date; |
|||
|
|||
@Entity(tableName = "sms") |
|||
@TypeConverters(DateConverter.class) |
|||
public class SMS { |
|||
|
|||
@PrimaryKey(autoGenerate = true) |
|||
private int id; |
|||
|
|||
@ColumnInfo(name = "message") |
|||
private String message = ""; |
|||
|
|||
@ColumnInfo(name = "sender") |
|||
private String sender; |
|||
|
|||
@ColumnInfo(name = "received_at") |
|||
private Date receivedAt; |
|||
|
|||
@NonNull |
|||
@ColumnInfo(name = "type") |
|||
private String type; |
|||
|
|||
|
|||
@ColumnInfo(name = "server_acknowledged_at") |
|||
private Date serverAcknowledgedAt; |
|||
|
|||
public SMS() { |
|||
type = null; |
|||
} |
|||
|
|||
public boolean hasServerAcknowledged() { |
|||
return serverAcknowledgedAt != null; |
|||
} |
|||
|
|||
@ColumnInfo(name = "last_acknowledged_request_at") |
|||
private Date lastAcknowledgedRequestAt; |
|||
|
|||
@ColumnInfo(name = "retry_count", defaultValue = "0") |
|||
private int retryCount = 0; |
|||
|
|||
public int getId() { |
|||
return id; |
|||
} |
|||
|
|||
public void setId(int id) { |
|||
this.id = id; |
|||
} |
|||
|
|||
public String getMessage() { |
|||
return message; |
|||
} |
|||
|
|||
public void setMessage(String message) { |
|||
this.message = message; |
|||
} |
|||
|
|||
public String getSender() { |
|||
return sender; |
|||
} |
|||
|
|||
public void setSender(String sender) { |
|||
this.sender = sender; |
|||
} |
|||
|
|||
public Date getServerAcknowledgedAt() { |
|||
return serverAcknowledgedAt; |
|||
} |
|||
|
|||
public void setServerAcknowledgedAt(Date serverAcknowledgedAt) { |
|||
this.serverAcknowledgedAt = serverAcknowledgedAt; |
|||
} |
|||
|
|||
public Date getReceivedAt() { |
|||
return receivedAt; |
|||
} |
|||
|
|||
public void setReceivedAt(Date receivedAt) { |
|||
this.receivedAt = receivedAt; |
|||
} |
|||
|
|||
@NonNull |
|||
public String getType() { |
|||
return type; |
|||
} |
|||
|
|||
public void setType(@NonNull String type) { |
|||
this.type = type; |
|||
} |
|||
|
|||
|
|||
public Date getLastAcknowledgedRequestAt() { |
|||
return lastAcknowledgedRequestAt; |
|||
} |
|||
|
|||
public void setLastAcknowledgedRequestAt(Date lastAcknowledgedRequestAt) { |
|||
this.lastAcknowledgedRequestAt = lastAcknowledgedRequestAt; |
|||
} |
|||
|
|||
public int getRetryCount() { |
|||
return retryCount; |
|||
} |
|||
|
|||
public void setRetryCount(int retryCount) { |
|||
this.retryCount = retryCount; |
|||
} |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
package com.vernu.sms.database.local; |
|||
|
|||
import androidx.room.Dao; |
|||
import androidx.room.Delete; |
|||
import androidx.room.Insert; |
|||
import androidx.room.OnConflictStrategy; |
|||
import androidx.room.Query; |
|||
|
|||
import java.util.List; |
|||
|
|||
@Dao |
|||
public interface SMSDao { |
|||
|
|||
@Query("SELECT * FROM sms") |
|||
List<SMS> getAll(); |
|||
|
|||
@Query("SELECT * FROM sms WHERE id IN (:smsIds)") |
|||
List<SMS> loadAllByIds(int[] smsIds); |
|||
|
|||
@Insert(onConflict = OnConflictStrategy.REPLACE) |
|||
void insertAll(SMS... sms); |
|||
|
|||
|
|||
@Delete |
|||
void delete(SMS sms); |
|||
|
|||
} |
|||
@ -0,0 +1,42 @@ |
|||
package com.vernu.sms.dtos; |
|||
|
|||
import java.util.Date; |
|||
|
|||
public class SMSDTO { |
|||
private String sender; |
|||
private String message; |
|||
private Date receivedAt; |
|||
|
|||
public SMSDTO() { |
|||
} |
|||
|
|||
public SMSDTO(String sender, String message, Date receivedAt) { |
|||
this.sender = sender; |
|||
this.message = message; |
|||
this.receivedAt = receivedAt; |
|||
} |
|||
|
|||
public String getSender() { |
|||
return sender; |
|||
} |
|||
|
|||
public void setSender(String sender) { |
|||
this.sender = sender; |
|||
} |
|||
|
|||
public String getMessage() { |
|||
return message; |
|||
} |
|||
|
|||
public void setMessage(String message) { |
|||
this.message = message; |
|||
} |
|||
|
|||
public Date getReceivedAt() { |
|||
return receivedAt; |
|||
} |
|||
|
|||
public void setReceivedAt(Date receivedAt) { |
|||
this.receivedAt = receivedAt; |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue