1
0
mirror of https://github.com/tijder/SmsMatrix.git synced 2024-10-27 18:24:01 +00:00

Merge pull request #62 from PhieF/master

Start on boot and notification on Android O
This commit is contained in:
tijder 2019-11-26 16:25:33 +01:00 committed by GitHub
commit c85515c917
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 1 deletions

View File

@ -7,6 +7,7 @@
<uses-permission android:name="android.permission.SEND_SMS" /> <uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application <application
tools:replace="allowBackup,label" tools:replace="allowBackup,label"
@ -31,6 +32,9 @@
<intent-filter> <intent-filter>
<action android:name="android.intent.action.PHONE_STATE" /> <action android:name="android.intent.action.PHONE_STATE" />
</intent-filter> </intent-filter>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver> </receiver>
<service android:name="eu.droogers.smsmatrix.MatrixService" /> <service android:name="eu.droogers.smsmatrix.MatrixService" />

View File

@ -1,14 +1,22 @@
package eu.droogers.smsmatrix; package eu.droogers.smsmatrix;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service; import android.app.Service;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
/** /**
* Created by gerben on 7-10-17. * Created by gerben on 7-10-17.
*/ */
@ -24,6 +32,7 @@ public class MatrixService extends Service {
private String syncDelay; private String syncDelay;
private String syncTimeout; private String syncTimeout;
private MMSMonitor mms; private MMSMonitor mms;
private String mChannelId = "";
@Override @Override
public void onCreate() { public void onCreate() {
@ -32,6 +41,21 @@ public class MatrixService extends Service {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
if (mChannelId.isEmpty()) {
mChannelId = createNotificationChannel("sync", "Sync Service");
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, mChannelId);
Notification notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText(getApplicationInfo().loadLabel(getPackageManager()))
.setPriority(NotificationCompat.PRIORITY_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(1, notification);
}
SharedPreferences sp = getSharedPreferences("settings", Context.MODE_PRIVATE); SharedPreferences sp = getSharedPreferences("settings", Context.MODE_PRIVATE);
botUsername = sp.getString("botUsername", ""); botUsername = sp.getString("botUsername", "");
botPassword = sp.getString("botPassword", ""); botPassword = sp.getString("botPassword", "");
@ -76,6 +100,17 @@ public class MatrixService extends Service {
} }
@RequiresApi(api = Build.VERSION_CODES.O)
private String createNotificationChannel(String channelId, String channelName){
NotificationChannel chan = new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
service.createNotificationChannel(chan);
return channelId;
}
@Override @Override
public void onDestroy() { public void onDestroy() {
mx.destroy(); mx.destroy();

View File

@ -6,7 +6,9 @@ import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.telephony.SmsMessage; import android.telephony.SmsMessage;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.util.Log;
import androidx.core.content.ContextCompat;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -24,6 +26,10 @@ public class ReceiverListener extends BroadcastReceiver {
} else if (intent.getAction().equals("android.intent.action.PHONE_STATE")) { } else if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
handleIncomingCall(context, intent); handleIncomingCall(context, intent);
} }
else if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Intent intentServ = new Intent(context, MatrixService.class);
ContextCompat.startForegroundService(context, intentServ);
}
} }
private void handleIncomingSMS(Context context, Intent intent) { private void handleIncomingSMS(Context context, Intent intent) {