Skip to content

Commit a264dd3

Browse files
committed
[Sync] Fixed Element ID Export/Import Issue
This fix addresses the problem where the application would erroneously export and import the ID of an element. This issue previously caused conflicts when attempting to import an element with an existing ID in the database, resulting in the overwriting of database entries.
1 parent c862768 commit a264dd3

File tree

7 files changed

+81
-5
lines changed

7 files changed

+81
-5
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package de.davis.passwordmanager.gson.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.FIELD)
10+
public @interface Exclude {
11+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package de.davis.passwordmanager.gson.strategies;
2+
3+
import com.google.gson.ExclusionStrategy;
4+
import com.google.gson.FieldAttributes;
5+
6+
import de.davis.passwordmanager.gson.annotations.Exclude;
7+
8+
public class ExcludeAnnotationStrategy implements ExclusionStrategy {
9+
@Override
10+
public boolean shouldSkipField(FieldAttributes f) {
11+
return f.getAnnotation(Exclude.class) != null;
12+
}
13+
14+
@Override
15+
public boolean shouldSkipClass(Class<?> clazz) {
16+
return false;
17+
}
18+
}

app/src/main/java/de/davis/passwordmanager/security/element/SecureElement.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import de.davis.passwordmanager.R;
2323
import de.davis.passwordmanager.dashboard.Item;
24+
import de.davis.passwordmanager.gson.annotations.Exclude;
2425

2526
@Entity
2627
public class SecureElement implements Serializable, Comparable<SecureElement>, Item {
@@ -45,6 +46,7 @@ public class SecureElement implements Serializable, Comparable<SecureElement>, I
4546
private boolean favorite;
4647

4748
@PrimaryKey(autoGenerate = true)
49+
@Exclude
4850
private long id;
4951

5052
@ColumnInfo(name = "created_at")

app/src/main/java/de/davis/passwordmanager/security/element/creditcard/CreditCardDetails.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.davis.passwordmanager.security.element.creditcard;
22

3+
import java.util.Objects;
4+
35
import de.davis.passwordmanager.security.element.ElementDetail;
46
import de.davis.passwordmanager.security.element.SecureElement;
57
import de.davis.passwordmanager.utils.CreditCardUtil;
@@ -67,4 +69,28 @@ public void setExpirationDate(String expirationDate) {
6769
public int getType() {
6870
return SecureElement.TYPE_CREDIT_CARD;
6971
}
72+
73+
@Override
74+
public boolean equals(Object o) {
75+
if (this == o) return true;
76+
if (o == null || getClass() != o.getClass()) return false;
77+
78+
CreditCardDetails that = (CreditCardDetails) o;
79+
80+
if (!Objects.equals(cardholder, that.cardholder))
81+
return false;
82+
if (!Objects.equals(expirationDate, that.expirationDate))
83+
return false;
84+
if (!cardNumber.equals(that.cardNumber)) return false;
85+
return Objects.equals(cvv, that.cvv);
86+
}
87+
88+
@Override
89+
public int hashCode() {
90+
int result = cardholder != null ? cardholder.hashCode() : 0;
91+
result = 31 * result + (expirationDate != null ? expirationDate.hashCode() : 0);
92+
result = 31 * result + cardNumber.hashCode();
93+
result = 31 * result + (cvv != null ? cvv.hashCode() : 0);
94+
return result;
95+
}
7096
}

app/src/main/java/de/davis/passwordmanager/security/element/password/PasswordDetails.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.davis.passwordmanager.security.element.password;
22

3+
import java.util.Objects;
4+
35
import de.davis.passwordmanager.security.Cryptography;
46
import de.davis.passwordmanager.security.element.ElementDetail;
57
import de.davis.passwordmanager.security.element.SecureElement;
@@ -57,4 +59,17 @@ public String getPassword(){
5759
public int getType() {
5860
return SecureElement.TYPE_PASSWORD;
5961
}
62+
63+
@Override
64+
public boolean equals(Object o) {
65+
if (this == o) return true;
66+
if (o == null || getClass() != o.getClass()) return false;
67+
PasswordDetails that = (PasswordDetails) o;
68+
return getPassword().equals(that.getPassword()) && Objects.equals(origin, that.origin) && Objects.equals(username, that.username);
69+
}
70+
71+
@Override
72+
public int hashCode() {
73+
return Objects.hash(origin, username, getPassword());
74+
}
6075
}

app/src/main/java/de/davis/passwordmanager/sync/DataTransfer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ protected void handleResult(Handler handler, Result result){
6161
new MaterialAlertDialogBuilder(getContext())
6262
.setTitle(R.string.error_title)
6363
.setMessage(error.getMessage())
64-
.setPositiveButton(R.string.ok,
65-
(dialog, which) -> dialog.dismiss())
64+
.setPositiveButton(R.string.ok, (dialog, which) -> {})
6665
.show();
6766

6867
else if (result instanceof Result.Success success)

app/src/main/java/de/davis/passwordmanager/sync/keygo/KeyGoTransfer.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import de.davis.passwordmanager.R;
3232
import de.davis.passwordmanager.database.SecureElementDatabase;
3333
import de.davis.passwordmanager.dialog.EditDialogBuilder;
34+
import de.davis.passwordmanager.gson.strategies.ExcludeAnnotationStrategy;
3435
import de.davis.passwordmanager.security.Cryptography;
3536
import de.davis.passwordmanager.security.element.ElementDetail;
3637
import de.davis.passwordmanager.security.element.SecureElement;
@@ -73,7 +74,10 @@ public JsonElement serialize(ElementDetail src, java.lang.reflect.Type typeOfSrc
7374

7475
public KeyGoTransfer(Context context) {
7576
super(context);
76-
this.gson = new GsonBuilder().registerTypeAdapter(ElementDetail.class, new ElementDetailTypeAdapter()).create();
77+
this.gson = new GsonBuilder()
78+
.registerTypeAdapter(ElementDetail.class, new ElementDetailTypeAdapter())
79+
.setExclusionStrategies(new ExcludeAnnotationStrategy())
80+
.create();
7781
}
7882

7983
@Override
@@ -97,7 +101,8 @@ protected Result importElements(InputStream inputStream, String password) throws
97101

98102
int existed = 0;
99103
for (SecureElement element : list) {
100-
if(elements.stream().anyMatch(e -> e.getTitle().equals(element.getTitle()))) {
104+
if(elements.stream().anyMatch(e -> e.getTitle().equals(element.getTitle())
105+
&& e.getDetail().equals(element.getDetail()))) {
101106
existed++;
102107
continue;
103108
}
@@ -142,6 +147,7 @@ public void start(int type, Uri uri) {
142147
.show();
143148

144149
alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(v -> {
150+
alertDialog.dismiss();
145151
String password = ((EditText)alertDialog.findViewById(R.id.textInputEditText)).getText().toString();
146152

147153
if(password.isEmpty()){
@@ -151,7 +157,6 @@ public void start(int type, Uri uri) {
151157
}
152158

153159
start(type, uri, password);
154-
alertDialog.dismiss();
155160
});
156161
}
157162
}

0 commit comments

Comments
 (0)