diff --git a/app/src/main/java/com/mridang/speedo/PowerReceiver.java b/app/src/main/java/com/mridang/speedo/PowerReceiver.java index 80ee8f5..2f144fc 100644 --- a/app/src/main/java/com/mridang/speedo/PowerReceiver.java +++ b/app/src/main/java/com/mridang/speedo/PowerReceiver.java @@ -18,7 +18,6 @@ public class PowerReceiver extends BroadcastReceiver { /** * Receiver method for the phone boot that starts the traffic monitoring service */ - @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public void onReceive(Context appContext, Intent bootIntent) { Log.v("BatteryReceiver", "Received a power intent"); diff --git a/app/src/main/java/com/mridang/speedo/TrafficService.java b/app/src/main/java/com/mridang/speedo/TrafficService.java index 11ae9be..a2c606f 100644 --- a/app/src/main/java/com/mridang/speedo/TrafficService.java +++ b/app/src/main/java/com/mridang/speedo/TrafficService.java @@ -23,6 +23,7 @@ import android.os.Binder; import android.os.Build; import android.os.Handler; import android.os.IBinder; +import android.os.Looper; import android.os.Message; import android.os.PowerManager; import androidx.preference.PreferenceManager; @@ -31,6 +32,10 @@ import android.telephony.TelephonyManager; import android.text.format.Formatter; import android.util.Log; +import org.jetbrains.annotations.NotNull; + +import java.util.Objects; + /** * Main service class that monitors the network speed and updates the notification every second */ @@ -100,8 +105,8 @@ public class TrafficService extends Service { Intent ittSettings = new Intent(); ittSettings.setComponent(new ComponentName("com.android.settings", CMP)); - PendingIntent pitSettings = PendingIntent.getActivity(this, 0, ittSettings, PendingIntent.FLAG_IMMUTABLE); - notBuilder = new NotificationCompat.Builder(this); + PendingIntent pitSettings = PendingIntent.getActivity(getApplicationContext(), 0, ittSettings, PendingIntent.FLAG_IMMUTABLE); + notBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID); notBuilder.setSmallIcon(R.drawable.wkb000); notBuilder.setContentIntent(pitSettings); notBuilder.setOngoing(true); @@ -110,7 +115,6 @@ public class TrafficService extends Service { notBuilder.setPriority(Integer.MAX_VALUE); notBuilder.setCategory(NotificationCompat.CATEGORY_SERVICE); notBuilder.setLocalOnly(true); - notBuilder.setChannelId(CHANNEL_ID); setColor(settings.getInt("color", Color.TRANSPARENT)); visibilityPublic(settings.getBoolean("lockscreen", true)); @@ -134,7 +138,7 @@ public class TrafficService extends Service { @Override public void onReceive(Context ctcContext, Intent ittIntent) { - if (ittIntent.getAction().equalsIgnoreCase(Intent.ACTION_SCREEN_OFF)) { + if (Objects.requireNonNull(ittIntent.getAction()).equalsIgnoreCase(Intent.ACTION_SCREEN_OFF)) { Log.d("TrafficService", "Screen off; hiding the notification"); hndNotifier.removeMessages(1); @@ -170,11 +174,9 @@ public class TrafficService extends Service { ittScreen.addAction(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(recScreen, ittScreen); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - IntentFilter ittSaver = new IntentFilter(); - ittScreen.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED); - registerReceiver(recSaver, ittSaver); - } + IntentFilter ittSaver = new IntentFilter(); + ittScreen.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED); + registerReceiver(recSaver, ittSaver); } /** @@ -318,9 +320,11 @@ public class TrafficService extends Service { */ @SuppressLint("CommitPrefEdits") public NotificationHandler(Context ctxContext) { + super(Looper.getMainLooper()); this.ctxContext = ctxContext; } + /** * Handler method that updates the notification icon with the current speed. It is a very * hackish method. We have icons for 1 KB/s to 999 KB/s and 1.0 MB/s to 99.9 MB/s. Every time @@ -332,7 +336,7 @@ public class TrafficService extends Service { * integer references of the drawables in the sequential order. */ @Override - public void handleMessage(Message msgMessage) { + public void handleMessage(@NotNull Message msgMessage) { TrafficService.hndNotifier.sendEmptyMessageDelayed(1, 1000L); @@ -368,7 +372,7 @@ public class TrafficService extends Service { updateIcon(1180 + R.drawable.wkb000); } - Long lngTotal = TrafficStats.getTotalRxBytes() + TrafficStats.getTotalTxBytes(); + long lngTotal = TrafficStats.getTotalRxBytes() + TrafficStats.getTotalTxBytes(); String strTotal = Formatter.formatFileSize(this.ctxContext, lngTotal); TrafficService.notBuilder.setContentInfo(strTotal); TrafficService.mgrNotifications.notify(ID, TrafficService.notBuilder.build());