Commit 7ecdf734 authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Added NonNull/Nullable annotations

parent ca3da938
...@@ -3,6 +3,7 @@ package com.frostnerd.dnschanger.database; ...@@ -3,6 +3,7 @@ package com.frostnerd.dnschanger.database;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import com.frostnerd.dnschanger.database.entities.DNSEntry; import com.frostnerd.dnschanger.database.entities.DNSEntry;
...@@ -30,6 +31,7 @@ import java.util.Set; ...@@ -30,6 +31,7 @@ import java.util.Set;
public class DatabaseHelper extends com.frostnerd.utils.database.DatabaseHelper { public class DatabaseHelper extends com.frostnerd.utils.database.DatabaseHelper {
public static final String DATABASE_NAME = "data"; public static final String DATABASE_NAME = "data";
public static final int DATABASE_VERSION = 3; public static final int DATABASE_VERSION = 3;
@NonNull
public static final Set<Class<? extends Entity>> entities = new HashSet<Class<? extends Entity>>(){{ public static final Set<Class<? extends Entity>> entities = new HashSet<Class<? extends Entity>>(){{
add(DNSEntry.class); add(DNSEntry.class);
add(DNSQuery.class); add(DNSQuery.class);
...@@ -40,9 +42,10 @@ public class DatabaseHelper extends com.frostnerd.utils.database.DatabaseHelper ...@@ -40,9 +42,10 @@ public class DatabaseHelper extends com.frostnerd.utils.database.DatabaseHelper
}}; }};
@Nullable @Nullable
private static DatabaseHelper instance; private static DatabaseHelper instance;
@NonNull
private MockedContext wrappedContext; private MockedContext wrappedContext;
public static DatabaseHelper getInstance(Context context){ public static DatabaseHelper getInstance(@NonNull Context context){
return instance == null ? instance = new DatabaseHelper(mock(context)) : instance; return instance == null ? instance = new DatabaseHelper(mock(context)) : instance;
} }
...@@ -50,7 +53,7 @@ public class DatabaseHelper extends com.frostnerd.utils.database.DatabaseHelper ...@@ -50,7 +53,7 @@ public class DatabaseHelper extends com.frostnerd.utils.database.DatabaseHelper
return instance != null; return instance != null;
} }
private DatabaseHelper(Context context) { private DatabaseHelper(@NonNull Context context) {
super(context, DATABASE_NAME, DATABASE_VERSION, entities); super(context, DATABASE_NAME, DATABASE_VERSION, entities);
wrappedContext = (MockedContext) context; wrappedContext = (MockedContext) context;
} }
...@@ -118,62 +121,63 @@ public class DatabaseHelper extends com.frostnerd.utils.database.DatabaseHelper ...@@ -118,62 +121,63 @@ public class DatabaseHelper extends com.frostnerd.utils.database.DatabaseHelper
@Override @Override
public synchronized void close() { public synchronized void close() {
instance = null; instance = null;
if(wrappedContext != null) wrappedContext.destroy(false); wrappedContext.destroy(false);
wrappedContext = null; wrappedContext = null;
super.close(); super.close();
} }
public boolean dnsRuleExists(String host){ public boolean dnsRuleExists(@NonNull String host){
return this.rowExists(DNSRule.class, WhereCondition.equal(findColumn(DNSRule.class, "host"), host)); return this.rowExists(DNSRule.class, WhereCondition.equal(findColumn(DNSRule.class, "host"), host));
} }
public boolean dnsRuleExists(String host, boolean ipv6){ public boolean dnsRuleExists(@NonNull String host, boolean ipv6){
return this.rowExists(DNSRule.class, return this.rowExists(DNSRule.class,
WhereCondition.equal(findColumn(DNSRule.class, "host"), host), WhereCondition.equal(findColumn(DNSRule.class, "host"), host),
WhereCondition.equal(findColumn(DNSRule.class, "ipv6"), ipv6 ? "1" : "0")); WhereCondition.equal(findColumn(DNSRule.class, "ipv6"), ipv6 ? "1" : "0"));
} }
public DNSRule getDNSRule(String host, boolean ipv6){ public DNSRule getDNSRule(@NonNull String host, boolean ipv6){
return getSQLHandler(DNSRule.class).selectFirstRow(this, false, return getSQLHandler(DNSRule.class).selectFirstRow(this, false,
WhereCondition.equal(findColumn(DNSRule.class, "host"), host), WhereCondition.equal(findColumn(DNSRule.class, "host"), host),
WhereCondition.equal(findColumn(DNSRule.class, "ipv6"), ipv6 ? "1" : "0")); WhereCondition.equal(findColumn(DNSRule.class, "ipv6"), ipv6 ? "1" : "0"));
} }
public boolean deleteDNSRule(String host, boolean ipv6){ public boolean deleteDNSRule(@NonNull String host, boolean ipv6){
return delete(DNSRule.class, return delete(DNSRule.class,
WhereCondition.equal(findColumn(DNSRule.class, "host"), host), WhereCondition.equal(findColumn(DNSRule.class, "host"), host),
WhereCondition.equal(findColumn(DNSRule.class, "ipv6"), ipv6 ? "1" : "0")) != 0; WhereCondition.equal(findColumn(DNSRule.class, "ipv6"), ipv6 ? "1" : "0")) != 0;
} }
public int editDNSRule(String host, boolean ipv6, String newTarget){ public int editDNSRule(@NonNull String host, boolean ipv6, @NonNull String newTarget){
DNSRule rule = getDNSRule(host, ipv6); DNSRule rule = getDNSRule(host, ipv6);
rule.setTarget(newTarget); rule.setTarget(newTarget);
return update(rule); return update(rule);
} }
public void createDNSRule(String host, String target, boolean ipv6, boolean wildcard){ public void createDNSRule(@NonNull String host, @NonNull String target, boolean ipv6, boolean wildcard){
insert(new DNSRule(host, target, ipv6, wildcard)); insert(new DNSRule(host, target, ipv6, wildcard));
} }
public void createShortcut(String name, IPPortPair dns1, IPPortPair dns2, IPPortPair dns1v6, IPPortPair dns2v6){ public void createShortcut(@NonNull String name, @NonNull IPPortPair dns1, @Nullable IPPortPair dns2,
if(dns1 != null)insert(dns1); @NonNull IPPortPair dns1v6, @Nullable IPPortPair dns2v6) {
if(dns2 != null)insert(dns2); insert(dns1);
if(dns1v6 != null)insert(dns1v6); if (dns2 != null) insert(dns2);
if(dns2v6 != null)insert(dns2v6); insert(dns1v6);
if (dns2v6 != null) insert(dns2v6);
insert(new Shortcut(name, dns1, dns2, dns1v6, dns2v6)); insert(new Shortcut(name, dns1, dns2, dns1v6, dns2v6));
} }
public void createShortcut(Shortcut shortcut){ private void createShortcut(@NonNull Shortcut shortcut) {
if(shortcut.getDns1() != null)insert(shortcut.getDns1()); insert(shortcut.getDns1());
if(shortcut.getDns2() != null)insert(shortcut.getDns2()); if (shortcut.getDns2() != null) insert(shortcut.getDns2());
if(shortcut.getDns1v6() != null)insert(shortcut.getDns1v6()); insert(shortcut.getDns1v6());
if(shortcut.getDns2v6() != null)insert(shortcut.getDns2v6()); if (shortcut.getDns2v6() != null) insert(shortcut.getDns2v6());
insert(shortcut); insert(shortcut);
} }
@Nullable @Nullable
public DNSEntry findMatchingDNSEntry(String dnsServer){ public DNSEntry findMatchingDNSEntry(@NonNull String dnsServer){
String address = "%" + dnsServer + "%"; String address = "%" + dnsServer + "%";
if(address.equals("%%"))return null; if(address.equals("%%"))return null;
ParsedEntity<DNSEntry> parsedEntity = getSQLHandler(DNSEntry.class); ParsedEntity<DNSEntry> parsedEntity = getSQLHandler(DNSEntry.class);
......
package com.frostnerd.dnschanger.database.entities; package com.frostnerd.dnschanger.database.entities;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.frostnerd.dnschanger.database.serializers.IPPortSerializer; import com.frostnerd.dnschanger.database.serializers.IPPortSerializer;
import com.frostnerd.utils.database.orm.MultitonEntity; import com.frostnerd.utils.database.orm.MultitonEntity;
...@@ -18,30 +19,37 @@ public class DNSEntry extends MultitonEntity implements Comparable<DNSEntry>{ ...@@ -18,30 +19,37 @@ public class DNSEntry extends MultitonEntity implements Comparable<DNSEntry>{
@Serialized(using = IPPortSerializer.class) @Serialized(using = IPPortSerializer.class)
@Named(name = "dns1") @Named(name = "dns1")
@NotNull @NotNull
@NonNull
private IPPortPair dns1; private IPPortPair dns1;
@Named(name = "dns2") @Named(name = "dns2")
@Serialized(using = IPPortSerializer.class) @Serialized(using = IPPortSerializer.class)
@Nullable
private IPPortPair dns2; private IPPortPair dns2;
@Named(name = "dns1v6") @Named(name = "dns1v6")
@Serialized(using = IPPortSerializer.class) @Serialized(using = IPPortSerializer.class)
@NotNull @NotNull
@NonNull
private IPPortPair dns1V6; private IPPortPair dns1V6;
@Named(name = "dns2v6") @Named(name = "dns2v6")
@Serialized(using = IPPortSerializer.class) @Serialized(using = IPPortSerializer.class)
@Nullable
private IPPortPair dns2V6; private IPPortPair dns2V6;
@Named(name = "name") @Named(name = "name")
@NotNull @NotNull
@Unique @Unique
@NonNull
private String name; private String name;
@Named(name = "description") @Named(name = "description")
@NonNull
private String description; private String description;
@Named(name = "shortname") @Named(name = "shortname")
@NonNull
private String shortName; private String shortName;
@Named(name = "customentry") @Named(name = "customentry")
...@@ -104,7 +112,8 @@ public class DNSEntry extends MultitonEntity implements Comparable<DNSEntry>{ ...@@ -104,7 +112,8 @@ public class DNSEntry extends MultitonEntity implements Comparable<DNSEntry>{
"Blocks access to all adult sites. Sites like Reddit are allowed. Google and Bing are set to the Safe Mode.", false), 3); "Blocks access to all adult sites. Sites like Reddit are allowed. Google and Bing are set to the Safe Mode.", false), 3);
} }
public DNSEntry(String name, String shortName, IPPortPair dns1, IPPortPair dns2, IPPortPair dns1V6, IPPortPair dns2V6, String description, boolean customEntry) { public DNSEntry(@NonNull String name, @NonNull String shortName, @NonNull IPPortPair dns1, @Nullable IPPortPair dns2,
@NonNull IPPortPair dns1V6, @Nullable IPPortPair dns2V6, @NonNull String description, boolean customEntry) {
this.name = name; this.name = name;
this.dns1 = dns1; this.dns1 = dns1;
this.dns2 = dns2; this.dns2 = dns2;
...@@ -129,22 +138,27 @@ public class DNSEntry extends MultitonEntity implements Comparable<DNSEntry>{ ...@@ -129,22 +138,27 @@ public class DNSEntry extends MultitonEntity implements Comparable<DNSEntry>{
IPPortPair.wrap(dns1V6, 53),IPPortPair.wrap(dns2V6, 53), description, customEntry); IPPortPair.wrap(dns1V6, 53),IPPortPair.wrap(dns2V6, 53), description, customEntry);
} }
@NonNull
public String getName() { public String getName() {
return name; return name;
} }
@NonNull
public IPPortPair getDns1() { public IPPortPair getDns1() {
return dns1; return dns1;
} }
@Nullable
public IPPortPair getDns2() { public IPPortPair getDns2() {
return dns2; return dns2;
} }
@NonNull
public IPPortPair getDns1V6() { public IPPortPair getDns1V6() {
return dns1V6; return dns1V6;
} }
@Nullable
public IPPortPair getDns2V6() { public IPPortPair getDns2V6() {
return dns2V6; return dns2V6;
} }
...@@ -153,10 +167,12 @@ public class DNSEntry extends MultitonEntity implements Comparable<DNSEntry>{ ...@@ -153,10 +167,12 @@ public class DNSEntry extends MultitonEntity implements Comparable<DNSEntry>{
return customEntry; return customEntry;
} }
@NonNull
public String getDescription() { public String getDescription() {
return description; return description;
} }
@NonNull
public String getShortName() { public String getShortName() {
return shortName; return shortName;
} }
...@@ -165,31 +181,31 @@ public class DNSEntry extends MultitonEntity implements Comparable<DNSEntry>{ ...@@ -165,31 +181,31 @@ public class DNSEntry extends MultitonEntity implements Comparable<DNSEntry>{
return ID; return ID;
} }
public void setName(String name) { public void setName(@NonNull String name) {
this.name = name; this.name = name;
} }
public void setDns1(IPPortPair dns1) { public void setDns1(@NonNull IPPortPair dns1) {
this.dns1 = dns1; this.dns1 = dns1;
} }
public void setDns2(IPPortPair dns2) { public void setDns2(@Nullable IPPortPair dns2) {
this.dns2 = dns2; this.dns2 = dns2;
} }
public void setDns1V6(IPPortPair dns1V6) { public void setDns1V6(@NonNull IPPortPair dns1V6) {
this.dns1V6 = dns1V6; this.dns1V6 = dns1V6;
} }
public void setDns2V6(IPPortPair dns2V6) { public void setDns2V6(@Nullable IPPortPair dns2V6) {
this.dns2V6 = dns2V6; this.dns2V6 = dns2V6;
} }
public void setDescription(String description) { public void setDescription(@NonNull String description) {
this.description = description; this.description = description;
} }
public void setShortName(String shortName) { public void setShortName(@NonNull String shortName) {
this.shortName = shortName; this.shortName = shortName;
} }
...@@ -217,7 +233,7 @@ public class DNSEntry extends MultitonEntity implements Comparable<DNSEntry>{ ...@@ -217,7 +233,7 @@ public class DNSEntry extends MultitonEntity implements Comparable<DNSEntry>{
'}'; '}';
} }
private boolean entryAddressMatches(String ip, IPPortPair pair){ private boolean entryAddressMatches(@Nullable String ip, @Nullable IPPortPair pair){
return ip != null && pair != null && ip.equals(pair.getAddress()); return ip != null && pair != null && ip.equals(pair.getAddress());
} }
} }
\ No newline at end of file
package com.frostnerd.dnschanger.database.entities; package com.frostnerd.dnschanger.database.entities;
import android.support.annotation.NonNull;
import com.frostnerd.utils.database.orm.MultitonEntity; import com.frostnerd.utils.database.orm.MultitonEntity;
import com.frostnerd.utils.database.orm.annotations.Named; import com.frostnerd.utils.database.orm.annotations.Named;
import com.frostnerd.utils.database.orm.annotations.NotNull;
import com.frostnerd.utils.database.orm.annotations.PrimaryKey; import com.frostnerd.utils.database.orm.annotations.PrimaryKey;
import com.frostnerd.utils.database.orm.annotations.Table; import com.frostnerd.utils.database.orm.annotations.Table;
...@@ -18,6 +21,8 @@ import com.frostnerd.utils.database.orm.annotations.Table; ...@@ -18,6 +21,8 @@ import com.frostnerd.utils.database.orm.annotations.Table;
public class DNSQuery extends MultitonEntity { public class DNSQuery extends MultitonEntity {
@PrimaryKey @PrimaryKey
@Named(name = "Host") @Named(name = "Host")
@NonNull
@NotNull
private String host; private String host;
@Named(name = "Ipv6") @Named(name = "Ipv6")
private boolean ipv6; private boolean ipv6;
...@@ -25,7 +30,7 @@ public class DNSQuery extends MultitonEntity { ...@@ -25,7 +30,7 @@ public class DNSQuery extends MultitonEntity {
@Named(name = "Time") @Named(name = "Time")
private long time; private long time;
public DNSQuery(String host, boolean ipv6, long time) { public DNSQuery(@NonNull String host, boolean ipv6, long time) {
this.host = host; this.host = host;
this.ipv6 = ipv6; this.ipv6 = ipv6;
this.time = time; this.time = time;
...@@ -35,6 +40,7 @@ public class DNSQuery extends MultitonEntity { ...@@ -35,6 +40,7 @@ public class DNSQuery extends MultitonEntity {
} }
@NonNull
public String getHost() { public String getHost() {
return host; return host;
} }
......
package com.frostnerd.dnschanger.database.entities; package com.frostnerd.dnschanger.database.entities;
import android.support.annotation.NonNull;
import com.frostnerd.utils.database.orm.MultitonEntity; import com.frostnerd.utils.database.orm.MultitonEntity;
import com.frostnerd.utils.database.orm.annotations.Default; import com.frostnerd.utils.database.orm.annotations.Default;
import com.frostnerd.utils.database.orm.annotations.Named; import com.frostnerd.utils.database.orm.annotations.Named;
...@@ -21,9 +23,12 @@ import com.frostnerd.utils.database.orm.annotations.Table; ...@@ -21,9 +23,12 @@ import com.frostnerd.utils.database.orm.annotations.Table;
public class DNSRule extends MultitonEntity{ public class DNSRule extends MultitonEntity{
@PrimaryKey @PrimaryKey
@Named(name = "Host") @Named(name = "Host")
@NonNull
@NotNull
private String host; private String host;
@NotNull @NotNull
@Named(name = "Target") @Named(name = "Target")
@NonNull
private String target; private String target;
@PrimaryKey @PrimaryKey
@Named(name = "Ipv6") @Named(name = "Ipv6")
...@@ -39,17 +44,19 @@ public class DNSRule extends MultitonEntity{ ...@@ -39,17 +44,19 @@ public class DNSRule extends MultitonEntity{
} }
public DNSRule(String host, String target, boolean ipv6, boolean wildcard) { public DNSRule(@NonNull String host, @NonNull String target, boolean ipv6, boolean wildcard) {
this.host = host; this.host = host;
this.target = target; this.target = target;
this.ipv6 = ipv6; this.ipv6 = ipv6;
this.wildcard = wildcard; this.wildcard = wildcard;
} }
@NonNull
public String getHost() { public String getHost() {
return host; return host;
} }
@NonNull
public String getTarget() { public String getTarget() {
return target; return target;
} }
...@@ -62,7 +69,7 @@ public class DNSRule extends MultitonEntity{ ...@@ -62,7 +69,7 @@ public class DNSRule extends MultitonEntity{
return wildcard; return wildcard;
} }
public void setTarget(String target) { public void setTarget(@NonNull String target) {
this.target = target; this.target = target;
} }
......
package com.frostnerd.dnschanger.database.entities; package com.frostnerd.dnschanger.database.entities;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.frostnerd.utils.database.DatabaseHelper; import com.frostnerd.utils.database.DatabaseHelper;
import com.frostnerd.utils.database.orm.MultitonEntity; import com.frostnerd.utils.database.orm.MultitonEntity;
import com.frostnerd.utils.database.orm.annotations.ForeignKey; import com.frostnerd.utils.database.orm.annotations.ForeignKey;
import com.frostnerd.utils.database.orm.annotations.Named; import com.frostnerd.utils.database.orm.annotations.Named;
import com.frostnerd.utils.database.orm.annotations.NotNull;
import com.frostnerd.utils.database.orm.annotations.Table; import com.frostnerd.utils.database.orm.annotations.Table;
...@@ -19,6 +23,8 @@ import com.frostnerd.utils.database.orm.annotations.Table; ...@@ -19,6 +23,8 @@ import com.frostnerd.utils.database.orm.annotations.Table;
@Table(name = "DNSRuleImport") @Table(name = "DNSRuleImport")
public class DNSRuleImport extends MultitonEntity { public class DNSRuleImport extends MultitonEntity {
@Named(name = "Filename") @Named(name = "Filename")
@NonNull
@NotNull
private String filename; private String filename;
@Named(name = "Time") @Named(name = "Time")
private long time; private long time;
...@@ -29,7 +35,7 @@ public class DNSRuleImport extends MultitonEntity { ...@@ -29,7 +35,7 @@ public class DNSRuleImport extends MultitonEntity {
@Named(name = "LastInsert") @Named(name = "LastInsert")
private long lastInsert; private long lastInsert;
public DNSRuleImport(String filename, long time, long firstInsertRowID, long lastInsertRowID) { public DNSRuleImport(@NonNull String filename, long time, long firstInsertRowID, long lastInsertRowID) {
this.filename = filename; this.filename = filename;
this.time = time; this.time = time;
this.firstInsert = firstInsertRowID;