Skip to content

Commit

Permalink
V2.2.1 sdk 14 (#30)
Browse files Browse the repository at this point in the history
* Min sdk 14
* Skip ssl check if needed
* Fix few bugs (service start when no configs, slot index message)
* Add autostart on phone start
* Make dialog scrollable
  • Loading branch information
bogkonstantin authored Feb 13, 2023
1 parent 6c563da commit 3f7bb16
Show file tree
Hide file tree
Showing 15 changed files with 354 additions and 227 deletions.
10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 31
compileSdkVersion 33
buildToolsVersion "30.0.3"

defaultConfig {
applicationId "tech.bogomolov.incomingsmsgateway"
minSdkVersion 23
targetSdkVersion 31
versionCode 8
versionName "2.1.1"
minSdkVersion 14
targetSdkVersion 33
versionCode 9
versionName "2.2.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import android.content.Context;
import android.content.SharedPreferences;

import androidx.lifecycle.Lifecycle;
import androidx.test.core.app.ActivityScenario;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.espresso.ViewInteraction;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -41,6 +44,13 @@ public void clearSharedPrefs() {
editor.commit();
}

@After
public void recreateActivity() {
ActivityScenario<MainActivity> scenario = activityRule.getScenario();
scenario.moveToState(Lifecycle.State.RESUMED);
scenario.recreate();
}

@Test
public void testAddDialogOpen() {
onView(withId(R.id.btn_add)).perform(click());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void testEmptyConfig() {
receiver.onReceive(appContext, this.getIntent());

Mockito.verify(receiver, Mockito.times(0))
.callWebHook(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
.callWebHook(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean());
}

@Test
Expand All @@ -41,7 +41,7 @@ public void testSmsPassedToWebhookByWildcard() {
receiver.onReceive(appContext, this.getIntent());

Mockito.verify(receiver, Mockito.times(1))
.callWebHook(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
.callWebHook(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean());
}

@Test
Expand All @@ -51,7 +51,7 @@ public void testSmsPassedToWebhookByNumber() {
receiver.onReceive(appContext, this.getIntent());

Mockito.verify(receiver, Mockito.times(1))
.callWebHook(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
.callWebHook(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean());
}

@Test
Expand All @@ -61,7 +61,7 @@ public void testSmsNotPassedToWebhook() {
receiver.onReceive(appContext, this.getIntent());

Mockito.verify(receiver, Mockito.times(0))
.callWebHook(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
.callWebHook(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean());
}

@Test
Expand All @@ -73,7 +73,7 @@ public void testMultiplePdus() {
Mockito.verify(receiver, Mockito.times(1))
.callWebHook(Mockito.anyString(),
Mockito.contains("\"text\":\"TestTest\""),
Mockito.anyString());
Mockito.anyString(), Mockito.anyBoolean());
}

private void setPhoneConfig(Context context, String phone) {
Expand Down Expand Up @@ -107,7 +107,7 @@ private SmsReceiver getSmsReceiver() {
Mockito.doCallRealMethod()
.when(receiver).onReceive(Mockito.any(Context.class), Mockito.any(Intent.class));
Mockito.doNothing().when(receiver)
.callWebHook(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
.callWebHook(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean());

return receiver;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,35 @@ public void setup() {

@Test
public void testHttpsSuccess() throws Exception {
WorkInfo workInfo = this.getWorkInfo("https://example.com", "test", "{}");
WorkInfo workInfo = this.getWorkInfo("https://example.com", "test", "{}", false);
assertThat(workInfo.getState(), is(WorkInfo.State.SUCCEEDED));
}

@Test
public void testHttpSuccess() throws Exception {
WorkInfo workInfo = this.getWorkInfo("http://example.com", "test", "{}");
WorkInfo workInfo = this.getWorkInfo("http://example.com", "test", "{}", false);
assertThat(workInfo.getState(), is(WorkInfo.State.SUCCEEDED));
}

@Test
public void testError() throws Exception {
WorkInfo workInfo = this.getWorkInfo("not a url", "test", "{}");
WorkInfo workInfo = this.getWorkInfo("not a url", "test", "{}", false);
assertThat(workInfo.getState(), is(WorkInfo.State.FAILED));
}

private WorkInfo getWorkInfo(String url, String text, String headers) throws Exception {
@Test
public void testSelfSignedCert() throws Exception {
WorkInfo workInfo = this.getWorkInfo(
"https://self-signed.badssl.com/", "test", "{\"User-Agent\":\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36\"}", true);
assertThat(workInfo.getState(), is(WorkInfo.State.SUCCEEDED));
}

private WorkInfo getWorkInfo(String url, String text, String headers, boolean ignoreSsl) throws Exception {
Data input = new Data.Builder()
.put(WebHookWorkRequest.DATA_URL, url)
.put(WebHookWorkRequest.DATA_TEXT, text)
.put(WebHookWorkRequest.DATA_HEADERS, headers)
.put(WebHookWorkRequest.DATA_IGNORE_SSL, ignoreSsl)
.build();

OneTimeWorkRequest request = new OneTimeWorkRequest.Builder(WebHookWorkRequest.class)
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
android:allowBackup="true"
Expand All @@ -20,6 +21,15 @@
android:enabled="true"
android:exported="true" />

<receiver
android:name=".BootCompletedReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

<activity
android:name=".MainActivity"
android:exported="true"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package tech.bogomolov.incomingsmsgateway;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

public class BootCompletedReceiver extends BroadcastReceiver {
@SuppressLint("UnsafeProtectedBroadcastReceiver")
@Override
public void onReceive(Context context, Intent argIntent) {
Intent intent = new Intent(context, SmsReceiverService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ public class ForwardingConfig {
private static final String KEY_URL = "url";
private static final String KEY_TEMPLATE = "template";
private static final String KEY_HEADERS = "headers";
private static final String KEY_IGNORE_SSL = "ignore_ssl";

private String sender;
private String url;
private String template;
private String headers;
private boolean ignoreSsl = false;

public ForwardingConfig(Context context) {
this.context = context;
Expand Down Expand Up @@ -58,12 +60,21 @@ public void setHeaders(String headers) {
this.headers = headers;
}

public boolean getIgnoreSsl() {
return this.ignoreSsl;
}

public void setIgnoreSsl(boolean ignoreSsl) {
this.ignoreSsl = ignoreSsl;
}

public void save() {
try {
JSONObject json = new JSONObject();
json.put(KEY_URL, this.url);
json.put(KEY_TEMPLATE, this.template);
json.put(KEY_HEADERS, this.headers);
json.put(KEY_IGNORE_SSL, this.ignoreSsl);

SharedPreferences.Editor editor = getEditor(context);
editor.putString(this.sender, json.toString());
Expand Down Expand Up @@ -100,6 +111,11 @@ public static ArrayList<ForwardingConfig> getAll(Context context) {
config.setUrl(json.getString(KEY_URL));
config.setTemplate(json.getString(KEY_TEMPLATE));
config.setHeaders(json.getString(KEY_HEADERS));

try {
config.setIgnoreSsl(json.getBoolean(KEY_IGNORE_SSL));
} catch (JSONException ignored) {
}
} catch (JSONException e) {
Log.e("ForwardingConfig", e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.bogomolov.incomingsmsgateway;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -53,7 +54,29 @@ public View getView(int position, View convertView, ViewGroup parent) {

View deleteButton = row.findViewById(R.id.delete_button);
deleteButton.setTag(R.id.delete_button, position);
deleteButton.setOnClickListener(this::onDeleteClick);

return row;
}

public void onDeleteClick(View view) {
ListAdapter listAdapter = this;
final int position = (int) view.getTag(R.id.delete_button);
final ForwardingConfig config = listAdapter.getItem(position);

AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setTitle(R.string.delete_record);
String asterisk = context.getString(R.string.asterisk);
String any = context.getString(R.string.any);
String message = context.getString(R.string.confirm_delete);
message = String.format(message, (config.getSender().equals(asterisk) ? any : config.getSender()));
builder.setMessage(message);

builder.setPositiveButton(R.string.btn_delete, (dialog, id) -> {
listAdapter.remove(config);
config.remove();
});
builder.setNegativeButton(R.string.btn_cancel, null);
builder.show();
}
}
Loading

0 comments on commit 3f7bb16

Please sign in to comment.