Skip to content

Commit

Permalink
Code cleanups for java 17
Browse files Browse the repository at this point in the history
  • Loading branch information
frimtec committed Oct 8, 2023
1 parent 007e801 commit 7fb852f
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 122 deletions.
3 changes: 3 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,38 +1,8 @@
package com.github.frimtec.android.securesmsproxy.domain;

import androidx.annotation.NonNull;

import java.util.Objects;

public final class Application {

private final long id;
private final String name;
private final String listener;
private final String secret;

public Application(long id, String name, String listener, String secret) {
this.id = id;
this.name = name;
this.listener = listener;
this.secret = secret;
}

public long getId() {
return id;
}

public String getName() {
return name;
}

public String getListener() {
return listener;
}

public String getSecret() {
return secret;
}
public record Application(long id, String name, String listener, String secret) {

@Override
public boolean equals(Object o) {
Expand All @@ -50,15 +20,4 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(id);
}

@Override
@NonNull
public String toString() {
return "Application{" +
"id=" + id +
", name='" + name + '\'' +
", listener='" + listener + '\'' +
", secret='" + secret + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
package com.github.frimtec.android.securesmsproxy.domain;

import androidx.annotation.NonNull;

import java.util.Objects;
import java.util.Set;

public final class ApplicationRule {

private final Application application;
private final Set<String> allowedPhoneNumbers;

public ApplicationRule(Application application, Set<String> allowedPhoneNumbers) {
this.application = application;
this.allowedPhoneNumbers = allowedPhoneNumbers;
}

public Application getApplication() {
return application;
}

public Set<String> getAllowedPhoneNumbers() {
return allowedPhoneNumbers;
}
public record ApplicationRule(Application application, Set<String> allowedPhoneNumbers) {

@Override
public boolean equals(Object o) {
Expand All @@ -40,12 +22,4 @@ public int hashCode() {
return Objects.hash(application);
}

@Override
@NonNull
public String toString() {
return "ApplicationRule{" +
"application=" + application +
", allowedPhoneNumbers=" + allowedPhoneNumbers +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ public String insertOrUpdate(
.map(phoneNumberFormatter::toE164)
.collect(Collectors.toSet());
if (applicationRule != null) {
secret = applicationRule.getApplication().getSecret();
secret = applicationRule.application().secret();
ContentValues applicationValues = new ContentValues();
applicationValues.put(TABLE_APPLICATION_COLUMN_LISTENER, listener);
db.update(TABLE_APPLICATION, applicationValues, TABLE_APPLICATION_COLUMN_ID + "=?",
new String[]{String.valueOf(applicationRule.getApplication().getId())});
new String[]{String.valueOf(applicationRule.application().id())});
ContentValues ruleValues = new ContentValues();
ruleValues.put(TABLE_RULE_COLUMN_APPLICATION_ID, applicationRule.getApplication().getId());
ruleValues.put(TABLE_RULE_COLUMN_APPLICATION_ID, applicationRule.application().id());
allowedPhoneNumbersE164Formatted.stream()
.filter(phoneNumber -> !applicationRule.getAllowedPhoneNumbers().contains(phoneNumber))
.filter(phoneNumber -> !applicationRule.allowedPhoneNumbers().contains(phoneNumber))
.forEach(phoneNumber -> {
ruleValues.put(TABLE_RULE_COLUMN_ALLOWED_PHONE_NUMBER, phoneNumber);
db.insert(TABLE_RULE, null, ruleValues);
Expand Down Expand Up @@ -128,7 +128,7 @@ public Map<String, Set<Application>> byPhoneNumbers(Set<String> phoneNumbers) {
Application application = Objects.requireNonNull(
applications.getOrDefault(id, new Application(id, cursor.getString(1), cursor.getString(2), cursor.getString(3)))
);
applications.put(application.getId(), application);
applications.put(application.id(), application);
String phoneNumber = cursor.getString(4);
Set<Application> set = Objects.requireNonNull(
applicationsByPhoneNumber.getOrDefault(phoneNumber, new HashSet<>())
Expand All @@ -150,7 +150,7 @@ private List<ApplicationRule> toApplicationRules(Cursor cursor) {
Application application = Objects.requireNonNull(
applications.getOrDefault(id, new Application(id, cursor.getString(1), cursor.getString(2), cursor.getString(3)))
);
applications.put(application.getId(), application);
applications.put(application.id(), application);
Set<String> phoneNumbers = Objects.requireNonNull(
applicationPhoneNumbers.getOrDefault(application, new HashSet<>())
);
Expand All @@ -167,6 +167,6 @@ private List<ApplicationRule> toApplicationRules(Cursor cursor) {

public void delete(Application application) {
SQLiteDatabase db = this.dbFactory.getDatabase(WRITABLE);
db.delete(TABLE_APPLICATION, TABLE_APPLICATION_COLUMN_ID + "=?", new String[]{String.valueOf(application.getId())});
db.delete(TABLE_APPLICATION, TABLE_APPLICATION_COLUMN_ID + "=?", new String[]{String.valueOf(application.id())});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class SmsListener extends BroadcastReceiver {
Intent intent = new Intent(SecureSmsProxyFacade.ACTION_BROADCAST_SMS_RECEIVED);
intent.putExtra(Intent.EXTRA_TEXT, serializedSmsList);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setComponent(new ComponentName(application.getName(), application.getListener()));
intent.setComponent(new ComponentName(application.name(), application.listener()));
return intent;
};

Expand Down Expand Up @@ -83,8 +83,8 @@ public static void broadcastReceivedSms(Context context, Application application
}

private static void broadcastReceivedSms(Context context, Application application, List<Sms> smsList, BiFunction<Application, String, Intent> smsBroadcastIntentFactory) {
Log.i(TAG, "broadcastReceivedSms, count: " + smsList.size() + "; to application: " + application.getName());
AesOperations aes = new Aes2(application.getSecret());
Log.i(TAG, "broadcastReceivedSms, count: " + smsList.size() + "; to application: " + application.name());
AesOperations aes = new Aes2(application.secret());
String serializedEncryptedSmsList = aes.encrypt(Sms.toJsonArray(smsList));
context.sendOrderedBroadcast(smsBroadcastIntentFactory.apply(application, serializedEncryptedSmsList), null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,19 @@ public void onReceive(Context context, Intent intent) {
Log.w(TAG, String.format("SMS sending blocked because of unregistered sender application: %s.", applicationName));
return;
}
AesOperations aes = new Aes2(applicationRule.getApplication().getSecret());
AesOperations aes = new Aes2(applicationRule.application().secret());
try {
Sms sms = Sms.fromJson(aes.decrypt(text));
if (PHONE_NUMBER_LOOPBACK.equals(sms.getNumber())) {
SmsListener.broadcastReceivedSms(context, applicationRule.getApplication(), Collections.singletonList(sms));
SmsListener.broadcastReceivedSms(context, applicationRule.application(), Collections.singletonList(sms));
} else {
PhoneNumberFormatter phoneNumberFormatter = this.phoneNumberFormatterProvider.apply(context);
String number = phoneNumberFormatter.toE164(sms.getNumber());
if (applicationRule.getAllowedPhoneNumbers().contains(number)) {
if (applicationRule.allowedPhoneNumbers().contains(number)) {
send(context, sms.getSubscriptionId(), number, sms.getText());
} else {
Log.w(TAG, String.format("SMS sending blocked because of not allowed phone number %s of application %s.",
number, applicationRule.getApplication().getName()));
number, applicationRule.application().name()));
}
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ protected void onCreate(Bundle savedInstanceState) {
setupDisclaimer();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}

private void setupAppInfo() {
TextView textView = findViewById(R.id.app_info);
String version = BuildConfig.VERSION_NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ public View getView(int position, @Nullable View convertView, @NonNull ViewGroup
}
ApplicationRule applicationRule = getItem(position);
if (applicationRule != null) {
Application application = applicationRule.getApplication();
Application application = applicationRule.application();
ImageView logo = convertView.findViewById(R.id.application_logo);
packageInfoAccessor.getIcon(application.getName()).ifPresent(logo::setImageDrawable);
packageInfoAccessor.getIcon(application.name()).ifPresent(logo::setImageDrawable);
TextView label = convertView.findViewById(R.id.application_label);

CharSequence labelText = this.packageInfoAccessor.getLabel(application.getName());
if (!packageInfoAccessor.isInstalled(application.getName())) {
CharSequence labelText = this.packageInfoAccessor.getLabel(application.name());
if (!packageInfoAccessor.isInstalled(application.name())) {
labelText = labelText + "\n(not installed)";
}
label.setText(labelText);
TextView phoneNumbers = convertView.findViewById(R.id.application_allowed_phone_numbers);
phoneNumbers.setText(applicationRule.getAllowedPhoneNumbers()
phoneNumbers.setText(applicationRule.allowedPhoneNumbers()
.stream()
.map(phoneNumber -> PhoneNumberUtils.formatNumber(phoneNumber, Locale.getDefault().getCountry()))
.collect(Collectors.joining("\n"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ public boolean onContextItemSelected(MenuItem item) {
}

private void deleteApplicationRule(ApplicationRule applicationRule) {
dao.delete(applicationRule.getApplication());
dao.delete(applicationRule.application());
}

private void refresh() {
List<ApplicationRule> all = dao.all();
listView.setAdapter(new ApplicationRuleArrayAdapter(this, all,
(adapter, applicationRule) -> view -> AlertDialogHelper.areYouSure(adapter.getContext(), (dialog, which) -> {
dao.delete(applicationRule.getApplication());
dao.delete(applicationRule.application());
adapter.remove(applicationRule);
adapter.notifyDataSetChanged();
Toast.makeText(adapter.getContext(), R.string.general_entry_deleted, Toast.LENGTH_SHORT).show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ void testProperties() {
Application application = new Application(12L, "name", "listener", "secret");
Set<String> allowedPhoneNumbers = Collections.singleton("number");
ApplicationRule applicationRule = new ApplicationRule(application, allowedPhoneNumbers);
assertThat(applicationRule.getApplication()).isEqualTo(application);
assertThat(applicationRule.getAllowedPhoneNumbers()).isEqualTo(allowedPhoneNumbers);
assertThat(applicationRule.application()).isEqualTo(application);
assertThat(applicationRule.allowedPhoneNumbers()).isEqualTo(allowedPhoneNumbers);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class ApplicationTest {
@Test
void testProperties() {
Application application = new Application(12L, "name", "listener", "secret");
assertThat(application.getId()).isEqualTo(12L);
assertThat(application.getName()).isEqualTo("name");
assertThat(application.getListener()).isEqualTo("listener");
assertThat(application.getSecret()).isEqualTo("secret");
assertThat(application.id()).isEqualTo(12L);
assertThat(application.name()).isEqualTo("name");
assertThat(application.listener()).isEqualTo("listener");
assertThat(application.secret()).isEqualTo("secret");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static com.github.frimtec.android.securesmsproxy.service.ApplicationRuleDao.ALL_COLUMNS;
import static com.github.frimtec.android.securesmsproxy.service.ApplicationRuleDao.SECRET_LENGTH;
Expand Down Expand Up @@ -259,19 +258,19 @@ private Cursor createCursor(List<List<Object>> expectedApplicationRuleValues) {
values[values.length - 1] = false;
when(cursor.moveToNext()).thenReturn(expectedApplicationRuleValues.size() > 1, values);

List<Long> ids = expectedApplicationRuleValues.stream().map(row -> (Long) row.get(0)).collect(Collectors.toList());
List<Long> ids = expectedApplicationRuleValues.stream().map(row -> (Long) row.get(0)).toList();
when(cursor.getLong(0)).thenReturn(ids.get(0), ids.subList(1, ids.size()).toArray(new Long[0]));

List<String> names = expectedApplicationRuleValues.stream().map(row -> (String) row.get(1)).collect(Collectors.toList());
List<String> names = expectedApplicationRuleValues.stream().map(row -> (String) row.get(1)).toList();
when(cursor.getString(1)).thenReturn(names.get(0), names.subList(1, names.size()).toArray(new String[0]));

List<String> listeners = expectedApplicationRuleValues.stream().map(row -> (String) row.get(2)).collect(Collectors.toList());
List<String> listeners = expectedApplicationRuleValues.stream().map(row -> (String) row.get(2)).toList();
when(cursor.getString(2)).thenReturn(listeners.get(0), listeners.subList(1, listeners.size()).toArray(new String[0]));

List<String> secrets = expectedApplicationRuleValues.stream().map(row -> (String) row.get(3)).collect(Collectors.toList());
List<String> secrets = expectedApplicationRuleValues.stream().map(row -> (String) row.get(3)).toList();
when(cursor.getString(3)).thenReturn(secrets.get(0), secrets.subList(1, secrets.size()).toArray(new String[0]));

List<String> numbers = expectedApplicationRuleValues.stream().map(row -> (String) row.get(4)).collect(Collectors.toList());
List<String> numbers = expectedApplicationRuleValues.stream().map(row -> (String) row.get(4)).toList();
when(cursor.getString(4)).thenReturn(numbers.get(0), numbers.subList(1, numbers.size()).toArray(new String[0]));
}
return cursor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ void onCreate() {
@Test
void queryNotMatchingUri() {
IsAllowedPhoneNumberContentProvider provider = new IsAllowedPhoneNumberContentProvider(null, (uri) -> false);
//noinspection resource
assertThrows(IllegalArgumentException.class, () -> provider.query(mock(Uri.class), new String[0], null, null, null));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,22 @@ class DbHelperTest {

@Test
void onCreate() {
DbHelper dbHelper = new DbHelper(mock(Context.class));
SQLiteDatabase db = mock(SQLiteDatabase.class);
dbHelper.onCreate(db);
verify(db).execSQL(Mockito.startsWith("CREATE TABLE " + TABLE_APPLICATION));
verify(db).execSQL(Mockito.startsWith("CREATE TABLE " + TABLE_RULE));
verify(db).execSQL(Mockito.startsWith("CREATE VIEW " + VIEW_APPLICATION_RULE));
verifyNoMoreInteractions(db);
try (DbHelper dbHelper = new DbHelper(mock(Context.class))) {
SQLiteDatabase db = mock(SQLiteDatabase.class);
dbHelper.onCreate(db);
verify(db).execSQL(Mockito.startsWith("CREATE TABLE " + TABLE_APPLICATION));
verify(db).execSQL(Mockito.startsWith("CREATE TABLE " + TABLE_RULE));
verify(db).execSQL(Mockito.startsWith("CREATE VIEW " + VIEW_APPLICATION_RULE));
verifyNoMoreInteractions(db);
}
}

@Test
void onUpgradeV1ToV1() {
DbHelper dbHelper = new DbHelper(mock(Context.class));
SQLiteDatabase db = mock(SQLiteDatabase.class);
dbHelper.onUpgrade(db, 1, 1);
verifyNoInteractions(db);
try (DbHelper dbHelper = new DbHelper(mock(Context.class))) {
SQLiteDatabase db = mock(SQLiteDatabase.class);
dbHelper.onUpgrade(db, 1, 1);
verifyNoInteractions(db);
}
}
}

0 comments on commit 7fb852f

Please sign in to comment.