public class VpService extends Service implements OnDocumentReceived {
final static public String EVENT_START = VpService.class.getSimpleName() + ".START";
final static public String EVENT_STOP = VpService.class.getSimpleName() + ".STOP";
private Server mServer = null;
public VpService() {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
info(getListenedInfo(), R.drawable.ic_bluetooth_black_24dp);
try {
if (!(intent == null)) {
Bundle extra = intent.getExtras();
if (!(extra == null)) {
if (extra.containsKey(VpService.EVENT_START)) {
if (mServer == null) {
AppSettings settings=new AppSettings(RawPrinterApp.getAppContext());
mServer = new Server(this,settings.get_mac());
mServer.start();
}
}
if (extra.containsKey(VpService.EVENT_STOP)) {
if (mServer != null) {
mServer.close();
}
mServer = null;
stopSelf();
return super.onStartCommand(intent, flags, startId);
}
} else {
return super.onStartCommand(intent, flags, startId);
}
}
} catch (Exception e) {
e.printStackTrace();
RawPrinterApp.showError(e.getLocalizedMessage());
}
return super.onStartCommand(intent, flags, startId);
}
private String getListenedInfo() {
return "Bluetooth virtual printer";
}
@RequiresApi(Build.VERSION_CODES.O)
private String createNotificationChannel(NotificationManager notificationManager) {
String channelId = VpService.class.toString();
String channelName = "VirtualBTPrinter_402d";
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_LOW);
// omitted the LED color
channel.setImportance(NotificationManager.IMPORTANCE_NONE);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
return channelId;
}
@Override
public void onDestroy() {
stopForeground(true);
if (mServer != null) {
mServer.close();
}
mServer = null;
super.onDestroy();
}
private void info(String mes, int icon) {
Intent mIntent = new Intent(this, ConfigActivity.class);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = "";
channelId = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? createNotificationChannel(notificationManager) : channelId;
Notification notification = null;
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final Notification.Builder builder = new Notification.Builder(this, channelId);
builder.setSmallIcon(icon);
builder.setCategory(Notification.CATEGORY_SERVICE).setColor(ContextCompat.getColor(Objects.requireNonNull(RawPrinterApp.getAppContext()), R.color.colorPrimary));
builder.setContentIntent(pendingIntent).setAutoCancel(false);
builder.setOngoing(true);
builder.setContentTitle(mes);
notification = builder.build();
} else {
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);
builder.setSmallIcon(icon);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setCategory(Notification.CATEGORY_SERVICE).setColor(getResources().getColor(R.color.colorPrimary));
}
builder.setContentIntent(pendingIntent).setAutoCancel(false);
builder.setOngoing(true);
builder.setContentTitle(mes);
notification = builder.build();
}
}catch(Exception e){
e.printStackTrace();
}
if(notification != null) {
startForeground(999, notification);
}
}
арифметические выражения где аргумент может быть нулл вернут тоже нулл.
Это если данные могут быть плохими. Мало приятного словить вместо суммы такую бяку.