Skip to content

Commit

Permalink
Toggle ON/OFF
Browse files Browse the repository at this point in the history
  • Loading branch information
ibnux committed Aug 31, 2021
1 parent 85e86b1 commit a742d64
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 15 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "com.ibnux.smsgateway"
minSdkVersion 16
targetSdkVersion 29
versionCode 3
versionName "2.0"
versionCode 4
versionName "2.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
33 changes: 28 additions & 5 deletions app/src/main/java/com/ibnux/smsgateway/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
import android.text.InputType;
import android.view.*;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.*;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.MenuItemCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
Expand Down Expand Up @@ -79,7 +78,9 @@ public void onRefresh() {
@Override public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {/* ... */}
}).check();
updateInfo();
checkServices();

if(getSharedPreferences("pref",0).getBoolean("gateway_on",true))
checkServices();

recyclerview.setHasFixedSize(true);
// use a linear layout manager
Expand Down Expand Up @@ -147,6 +148,25 @@ public void run() {
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
MenuItem menuItem = menu.findItem(R.id.menu_gateway_switch);
View view = MenuItemCompat.getActionView(menuItem);
Switch switcha = view.findViewById(R.id.switchForActionBar);
switcha.setChecked(getSharedPreferences("pref",0).getBoolean("gateway_on",true));
switcha.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
getSharedPreferences("pref",0).edit().putBoolean("gateway_on",isChecked).apply();
if(!isChecked){
Intent intent = new Intent("BackgroundService");
intent.putExtra("kill",true);
LocalBroadcastManager.getInstance(MainActivity.this).sendBroadcast(intent);
Toast.makeText(MainActivity.this,"Gateway OFF",Toast.LENGTH_LONG).show();
}else{
checkServices();
Toast.makeText(MainActivity.this,"Gateway ON",Toast.LENGTH_LONG).show();
}
}
});
return true;
}

Expand Down Expand Up @@ -273,7 +293,10 @@ public void run() {
});
else if(intent.hasExtra("newToken"))
updateInfo();
else
else if(intent.hasExtra("kill") && intent.getBooleanExtra("kill",false)){
Fungsi.log("BackgroundService KILLED");
serviceActive = false;
}else
serviceActive = true;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,15 @@ public IBinder onBind(Intent intent) {
@Override
public void onReceive(Context context, Intent intent) {
Fungsi.log("BackgroundService BroadcastReceiver received");
LocalBroadcastManager.getInstance(BackgroundService.this).sendBroadcast(new Intent("MainActivity"));
if(intent.hasExtra("kill") && intent.getBooleanExtra("kill",false)){
Fungsi.log("BackgroundService KILL");
((NotificationManager) Aplikasi.app.getSystemService(Context.NOTIFICATION_SERVICE)).cancelAll();
intent = new Intent("MainActivity");
intent.putExtra("kill",true);
LocalBroadcastManager.getInstance(BackgroundService.this).sendBroadcast(intent);
}else {
LocalBroadcastManager.getInstance(BackgroundService.this).sendBroadcast(new Intent("MainActivity"));
}
}
};
}
16 changes: 12 additions & 4 deletions app/src/main/java/com/ibnux/smsgateway/layanan/PushService.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,12 @@ public void onMessageReceived(RemoteMessage remoteMessage) {

//cek dulu secret vs secret, jika oke, berarti tidak diHash, no expired
if(scrt.equals(secret)){
Fungsi.sendSMS(to, message, this);
writeLog("SEND SUCCESS: " + to + " " + message,this);
if(sp.getBoolean("gateway_on",true)) {
Fungsi.sendSMS(to, message, this);
writeLog("SEND SUCCESS: " + to + " " + message, this);
}else{
writeLog("GATEWAY OFF: " + to + " " + message, this);
}
}else {
int expired = sp.getInt("expired", 3600);
if(TextUtils.isEmpty(time)) time = "0";
Expand All @@ -134,8 +138,12 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
scrt = Fungsi.md5(scrt.trim() + "" + time.trim());
Fungsi.log("MD5 : " + scrt);
if (scrt.toLowerCase().equals(secret.toLowerCase())) {
Fungsi.sendSMS(to, message, this);
writeLog("SEND SUCCESS: " + to + " " + message,this);
if(sp.getBoolean("gateway_on",true)) {
Fungsi.sendSMS(to, message, this);
writeLog("SEND SUCCESS: " + to + " " + message, this);
}else{
writeLog("GATEWAY OFF: " + to + " " + message, this);
}
} else {
writeLog("ERROR: SECRET INVALID : " + to + " " + message,this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ public void onReceive(Context context, Intent intent) {
Log.i("SMS Body", messageBody);
writeLog("SMS: RECEIVED : " + messageFrom + " " + messageBody,context);
if(url!=null){
sendPOST(url, messageFrom, messageBody,"received",context);
if(sp.getBoolean("gateway_on",true)) {
sendPOST(url, messageFrom, messageBody,"received",context);
}else{
writeLog("GATEWAY OFF: SMS NOT POSTED TO SERVER", context);
}

}else{
Log.i("SMS URL", "URL not SET");
}
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/res/layout/switch_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Switch
android:id="@+id/switchForActionBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:layout_centerInParent="true"
/>
</RelativeLayout>
8 changes: 7 additions & 1 deletion app/src/main/res/menu/main_menu.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/menu_gateway_switch"
android:title="SMS Active"
app:showAsAction="always"
app:actionLayout="@layout/switch_item"/>
<item
android:id="@+id/menu_change_expired"
android:title="Change expired" />
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorPrimary</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

</resources>

0 comments on commit a742d64

Please sign in to comment.