Skip to content

Commit c19639f

Browse files
committed
[Code] Code cleanup
1 parent 2afe70b commit c19639f

File tree

9 files changed

+49
-88
lines changed

9 files changed

+49
-88
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,11 @@ public int getType() {
121121

122122
@StringRes
123123
public int getTypeName(){
124-
switch (getType()) {
125-
case TYPE_PASSWORD:
126-
return R.string.password;
127-
case TYPE_CREDIT_CARD:
128-
default:
129-
return R.string.credit_card;
130-
}
124+
return switch (getType()) {
125+
case TYPE_PASSWORD -> R.string.password;
126+
case TYPE_CREDIT_CARD -> R.string.credit_card;
127+
default -> throw new IllegalStateException("Unexpected value: " + getType());
128+
};
131129
}
132130

133131
public boolean isFavorite() {

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

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -66,34 +66,24 @@ public List<String> getSuggestions() {
6666

6767
@StringRes
6868
public int getString() {
69-
switch (type) {
70-
case RIDICULOUS:
71-
return R.string.ridiculous;
72-
case WEAK:
73-
return R.string.weak;
74-
case MODERATE:
75-
return R.string.moderate;
76-
case STRONG:
77-
return R.string.strong;
78-
default:
79-
return R.string.very_strong;
80-
}
69+
return switch (type) {
70+
case RIDICULOUS -> R.string.ridiculous;
71+
case WEAK -> R.string.weak;
72+
case MODERATE -> R.string.moderate;
73+
case STRONG -> R.string.strong;
74+
default -> R.string.very_strong;
75+
};
8176
}
8277

8378
@AttrRes
8479
private int getColor() {
85-
switch (type) {
86-
case RIDICULOUS:
87-
return R.attr.colorRidiculous;
88-
case WEAK:
89-
return R.attr.colorWeak;
90-
case MODERATE:
91-
return R.attr.colorModerate;
92-
case STRONG:
93-
return R.attr.colorStrong;
94-
default:
95-
return R.attr.colorVeryStrong;
96-
}
80+
return switch (type) {
81+
case RIDICULOUS -> R.attr.colorRidiculous;
82+
case WEAK -> R.attr.colorWeak;
83+
case MODERATE -> R.attr.colorModerate;
84+
case STRONG -> R.attr.colorStrong;
85+
default -> R.attr.colorVeryStrong;
86+
};
9787
}
9888

9989
@ColorInt

app/src/main/java/de/davis/passwordmanager/service/Response.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
@RequiresApi(api = Build.VERSION_CODES.O)
4141
public class Response {
4242

43-
protected static final Pattern VALIDATION_PATTERN = Pattern.compile("^[\\p{all}&&[^\\u2022]].*");
43+
protected static final Pattern VALIDATION_PATTERN = Pattern.compile("^\\p{all}&&[^\\u2022].*");
4444
public static final String EXTRA_FILL_REQUEST = "fill_request";
4545

4646
private final Context context;

app/src/main/java/de/davis/passwordmanager/ui/elements/creditcard/CreateCreditCardActivity.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,15 @@ protected void cardReceived(EmvCard card, CommunicationException e) {
7777
return;
7878
}
7979

80-
switch (card.getState()){
81-
case UNKNOWN:
82-
setNfcMessageError(R.string.nfc_unknown_card);
83-
break;
84-
case LOCKED:
85-
setNfcMessageError(R.string.nfc_locked);
86-
break;
87-
case ACTIVE:
80+
switch (card.getState()) {
81+
case UNKNOWN -> setNfcMessageError(R.string.nfc_unknown_card);
82+
case LOCKED -> setNfcMessageError(R.string.nfc_locked);
83+
case ACTIVE -> {
8884
insertCard(card);
8985
setNfcMessageSuccess(R.string.nfc_success);
90-
break;
91-
default:
92-
break;
86+
}
87+
default -> {
88+
}
9389
}
9490
}
9591
};

app/src/main/java/de/davis/passwordmanager/ui/highlights/HighlightsFragment.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,15 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
5555

5656
binding.materialButtonToggleGroup.addOnButtonCheckedListener((group, checkedId, isChecked) -> viewModel.setState(group.getCheckedButtonId() == binding.lastAdded.getId()));
5757

58-
viewModel.getFavorites().observe(getViewLifecycleOwner(), secureElements -> {
59-
secureElements.forEach(secureElement -> {
60-
View fav_view = getLayoutInflater().inflate(R.layout.fav_layout, null, false);
61-
((TextView)fav_view.findViewById(R.id.title)).setText(secureElement.getTitle());
62-
((ImageView)fav_view.findViewById(R.id.image)).setImageDrawable(secureElement.getIcon(requireContext()));
58+
viewModel.getFavorites().observe(getViewLifecycleOwner(), secureElements -> secureElements.forEach(secureElement -> {
59+
View fav_view = getLayoutInflater().inflate(R.layout.fav_layout, null, false);
60+
((TextView)fav_view.findViewById(R.id.title)).setText(secureElement.getTitle());
61+
((ImageView)fav_view.findViewById(R.id.image)).setImageDrawable(secureElement.getIcon(requireContext()));
6362

64-
fav_view.setOnClickListener(v -> launchElement(secureElement));
63+
fav_view.setOnClickListener(v -> launchElement(secureElement));
6564

66-
((ViewGroup)view.findViewById(R.id.favorite_container)).addView(fav_view);
67-
});
68-
});
65+
((ViewGroup)view.findViewById(R.id.favorite_container)).addView(fav_view);
66+
}));
6967
}
7068

7169
private void launchElement(SecureElement element){

app/src/main/java/de/davis/passwordmanager/ui/login/EnterPasswordFragment.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,5 @@ public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationRes
135135
public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
136136
super.onAuthenticationError(errorCode, errString);
137137
}
138-
139-
@Override
140-
public void onAuthenticationFailed() {
141-
super.onAuthenticationFailed();
142-
}
143138
}
144139
}

app/src/main/java/de/davis/passwordmanager/ui/settings/SettingsFragment.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,27 +93,20 @@ public RecyclerView.LayoutManager onCreateLayoutManager() {
9393
}
9494

9595
private void setSummaryForNewAuthentication(Preference preference, int newValue){
96-
switch (newValue){
97-
case 0:
98-
preference.setSummary(R.string.time_disabled);
99-
break;
100-
case 5:
101-
preference.setSummary(R.string.time_every_time);
102-
break;
103-
default:
104-
preference.setSummary(getResources().getQuantityString(R.plurals.time_x_minute, (int) Math.pow(2, newValue), (int)Math.pow(2, newValue)));
96+
switch (newValue) {
97+
case 0 -> preference.setSummary(R.string.time_disabled);
98+
case 5 -> preference.setSummary(R.string.time_every_time);
99+
default ->
100+
preference.setSummary(getResources().getQuantityString(R.plurals.time_x_minute, (int) Math.pow(2, newValue), (int) Math.pow(2, newValue)));
105101
}
106102
}
107103

108104
public static long getTime(int index){
109-
switch (index){
110-
case 0:
111-
return -1;
112-
case 5:
113-
return Long.MAX_VALUE;
114-
default:
115-
return (long) Math.pow(2, index);
116-
}
105+
return switch (index) {
106+
case 0 -> -1;
107+
case 5 -> Long.MAX_VALUE;
108+
default -> (long) Math.pow(2, index);
109+
};
117110
}
118111

119112
@Override

app/src/main/java/de/davis/passwordmanager/ui/views/PasswordStrengthBar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public void update(String password, boolean force){
132132

133133
strengthWarning.setText(strength.getWarning());
134134
strengthWarning.setTextColor(strength.getColor(getContext()));
135-
strengthWarning.setVisibility(password.length() <= 0 || strength.getWarning() == null ? GONE : VISIBLE);
135+
strengthWarning.setVisibility(password.length() == 0 || strength.getWarning() == null ? GONE : VISIBLE);
136136
});
137137
});
138138
executorService.shutdown();

app/src/main/java/de/davis/passwordmanager/utils/GeneratorUtil.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,10 @@ private static String generateRandomResult(int length, String initialValue, Rand
5959
StringBuilder builder = new StringBuilder(initialValue);
6060
for (int i = 0; i < length; i++) {
6161
switch (typesList.get(random.nextInt(typesList.size()))) {
62-
case USE_DIGITS:
63-
generationPolicy.generateDigits(builder, i, random);
64-
break;
65-
66-
case USE_UPPERCASE:
67-
generationPolicy.generateUppercase(builder, i, random);
68-
break;
69-
case USE_LOWERCASE:
70-
generationPolicy.generateLowercase(builder, i, random);
71-
break;
72-
case USE_PUNCTUATION:
73-
generationPolicy.generatePunctuation(builder, i, random);
74-
break;
62+
case USE_DIGITS -> generationPolicy.generateDigits(builder, i, random);
63+
case USE_UPPERCASE -> generationPolicy.generateUppercase(builder, i, random);
64+
case USE_LOWERCASE -> generationPolicy.generateLowercase(builder, i, random);
65+
case USE_PUNCTUATION -> generationPolicy.generatePunctuation(builder, i, random);
7566
}
7667
}
7768

0 commit comments

Comments
 (0)