diff --git a/src/org/kde/kdeconnect/BackgroundService.java b/src/org/kde/kdeconnect/BackgroundService.java index 8fe6b791..1e6e7945 100644 --- a/src/org/kde/kdeconnect/BackgroundService.java +++ b/src/org/kde/kdeconnect/BackgroundService.java @@ -37,6 +37,7 @@ import android.text.TextUtils; import android.util.Log; import androidx.core.app.NotificationCompat; +import androidx.core.content.ContextCompat; import org.kde.kdeconnect.Backends.BaseLink; import org.kde.kdeconnect.Backends.BaseLinkProvider; @@ -141,7 +142,7 @@ public class BackgroundService extends Service { if (NotificationHelper.isPersistentNotificationEnabled(this)) { //Update the foreground notification with the currently connected device list - NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + NotificationManager nm = ContextCompat.getSystemService(this, NotificationManager.class); nm.notify(FOREGROUND_NOTIFICATION_ID, createForegroundNotification()); } } @@ -316,7 +317,7 @@ public class BackgroundService extends Service { } public void changePersistentNotificationVisibility(boolean visible) { - NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + NotificationManager nm = ContextCompat.getSystemService(this, NotificationManager.class); if (visible) { nm.notify(FOREGROUND_NOTIFICATION_ID, createForegroundNotification()); } else { diff --git a/src/org/kde/kdeconnect/Device.java b/src/org/kde/kdeconnect/Device.java index 6d576a59..04a9af1c 100644 --- a/src/org/kde/kdeconnect/Device.java +++ b/src/org/kde/kdeconnect/Device.java @@ -396,7 +396,7 @@ public class Device implements BaseLink.PacketReceiver { Resources res = getContext().getResources(); - final NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE); + final NotificationManager notificationManager = ContextCompat.getSystemService(getContext(), NotificationManager.class); Notification noti = new NotificationCompat.Builder(getContext(), NotificationHelper.Channels.DEFAULT) .setContentTitle(res.getString(R.string.pairing_request_from, getName())) @@ -414,7 +414,8 @@ public class Device implements BaseLink.PacketReceiver { } public void hidePairingNotification() { - final NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE); + final NotificationManager notificationManager = ContextCompat.getSystemService(getContext(), + NotificationManager.class); notificationManager.cancel(notificationId); } diff --git a/src/org/kde/kdeconnect/Helpers/NotificationHelper.java b/src/org/kde/kdeconnect/Helpers/NotificationHelper.java index a2305afe..a3603d1b 100644 --- a/src/org/kde/kdeconnect/Helpers/NotificationHelper.java +++ b/src/org/kde/kdeconnect/Helpers/NotificationHelper.java @@ -8,6 +8,8 @@ import android.content.SharedPreferences; import android.os.Build; import android.preference.PreferenceManager; +import androidx.core.content.ContextCompat; + import org.kde.kdeconnect_tp.R; public class NotificationHelper { @@ -45,7 +47,7 @@ public class NotificationHelper { return; } - NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + NotificationManager manager = ContextCompat.getSystemService(context, NotificationManager.class); NotificationChannel persistentChannel = new NotificationChannel( Channels.PERSISTENT, diff --git a/src/org/kde/kdeconnect/Helpers/TelephonyHelper.java b/src/org/kde/kdeconnect/Helpers/TelephonyHelper.java index a4a01b6c..92fc833d 100644 --- a/src/org/kde/kdeconnect/Helpers/TelephonyHelper.java +++ b/src/org/kde/kdeconnect/Helpers/TelephonyHelper.java @@ -31,6 +31,7 @@ import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; +import androidx.core.content.ContextCompat; import java.util.ArrayList; import java.util.Collections; @@ -48,7 +49,8 @@ public class TelephonyHelper { public static List getActiveSubscriptionIDs( @NonNull Context context) throws SecurityException { - SubscriptionManager subscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE); + SubscriptionManager subscriptionManager = ContextCompat.getSystemService(context, + SubscriptionManager.class); if (subscriptionManager == null) { // I don't know why or when this happens... Log.w(LOGGING_TAG, "Could not get SubscriptionManager"); @@ -79,7 +81,8 @@ public class TelephonyHelper { // See: https://developer.android.com/about/versions/android-5.1.html#multisim // There were vendor-specific implmentations before then, but those are very difficult to support // S/O Reference: https://stackoverflow.com/a/28571835/3723163 - TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); + TelephonyManager telephonyManager = ContextCompat.getSystemService(context, + TelephonyManager.class); if (telephonyManager == null) { // I don't know why or when this happens... Log.w(LOGGING_TAG, "Could not get TelephonyManager"); @@ -89,7 +92,8 @@ public class TelephonyHelper { return Collections.singletonList(phoneNumber); } else { // Potentially multi-sim case - SubscriptionManager subscriptionManager = (SubscriptionManager)context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE); + SubscriptionManager subscriptionManager = ContextCompat.getSystemService(context, + SubscriptionManager.class); if (subscriptionManager == null) { // I don't know why or when this happens... Log.w(LOGGING_TAG, "Could not get SubscriptionManager"); diff --git a/src/org/kde/kdeconnect/Helpers/TrustedNetworkHelper.java b/src/org/kde/kdeconnect/Helpers/TrustedNetworkHelper.java index 4580ba91..a8f5d7de 100644 --- a/src/org/kde/kdeconnect/Helpers/TrustedNetworkHelper.java +++ b/src/org/kde/kdeconnect/Helpers/TrustedNetworkHelper.java @@ -1,9 +1,5 @@ package org.kde.kdeconnect.Helpers; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - import android.Manifest; import android.content.Context; import android.content.pm.PackageManager; @@ -12,12 +8,12 @@ import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.preference.PreferenceManager; import android.text.TextUtils; -import android.util.Log; import androidx.core.content.ContextCompat; -import org.kde.kdeconnect.UserInterface.PermissionsAlertDialogFragment; -import org.kde.kdeconnect_tp.R; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; public class TrustedNetworkHelper { @@ -70,7 +66,8 @@ public class TrustedNetworkHelper { } public String currentSSID() { - WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); + WifiManager wifiManager = ContextCompat.getSystemService(context.getApplicationContext(), + WifiManager.class); if (wifiManager == null) return ""; WifiInfo wifiInfo = wifiManager.getConnectionInfo(); if (wifiInfo.getSupplicantState() != SupplicantState.COMPLETED) { diff --git a/src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardFloatingActivity.java b/src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardFloatingActivity.java index 20789b4f..82d001e8 100644 --- a/src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardFloatingActivity.java +++ b/src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardFloatingActivity.java @@ -20,15 +20,15 @@ package org.kde.kdeconnect.Plugins.ClibpoardPlugin; -import androidx.appcompat.app.AppCompatActivity; - import android.content.ClipData; import android.content.ClipboardManager; -import android.content.Context; import android.os.Bundle; import android.view.WindowManager; import android.widget.Toast; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.ContextCompat; + import org.kde.kdeconnect.BackgroundService; import org.kde.kdeconnect.Device; import org.kde.kdeconnect_tp.R; @@ -56,7 +56,8 @@ public class ClipboardFloatingActivity extends AppCompatActivity { super.onWindowFocusChanged(hasFocus); if (hasFocus) { // We are now sure that clipboard can be accessed from here. - ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); + ClipboardManager clipboardManager = ContextCompat.getSystemService(this, + ClipboardManager.class); ClipData.Item item; if (clipboardManager.hasPrimaryClip()) { item = clipboardManager.getPrimaryClip().getItemAt(0); diff --git a/src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardListener.java b/src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardListener.java index 294c58a2..ddccdb1d 100644 --- a/src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardListener.java +++ b/src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardListener.java @@ -28,6 +28,8 @@ import android.os.Build; import android.os.Handler; import android.os.Looper; +import androidx.core.content.ContextCompat; + import java.util.HashSet; @TargetApi(Build.VERSION_CODES.HONEYCOMB) @@ -68,7 +70,7 @@ public class ClipboardListener { context = ctx; new Handler(Looper.getMainLooper()).post(() -> { - cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); + cm = ContextCompat.getSystemService(context, ClipboardManager.class); listener = () -> { try { diff --git a/src/org/kde/kdeconnect/Plugins/FindMyPhonePlugin/FindMyPhonePlugin.java b/src/org/kde/kdeconnect/Plugins/FindMyPhonePlugin/FindMyPhonePlugin.java index ba3fff95..f2f93b13 100644 --- a/src/org/kde/kdeconnect/Plugins/FindMyPhonePlugin/FindMyPhonePlugin.java +++ b/src/org/kde/kdeconnect/Plugins/FindMyPhonePlugin/FindMyPhonePlugin.java @@ -23,7 +23,6 @@ package org.kde.kdeconnect.Plugins.FindMyPhonePlugin; import android.app.Activity; import android.app.NotificationManager; import android.app.PendingIntent; -import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.media.AudioManager; @@ -37,6 +36,7 @@ import android.util.Log; import androidx.annotation.RequiresApi; import androidx.core.app.NotificationCompat; +import androidx.core.content.ContextCompat; import org.kde.kdeconnect.Helpers.DeviceHelper; import org.kde.kdeconnect.Helpers.NotificationHelper; @@ -81,10 +81,10 @@ public class FindMyPhonePlugin extends Plugin { @Override public boolean onCreate() { - notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + notificationManager = ContextCompat.getSystemService(context, NotificationManager.class); notificationId = (int) System.currentTimeMillis(); - audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); - powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); + audioManager = ContextCompat.getSystemService(context, AudioManager.class); + powerManager = ContextCompat.getSystemService(context, PowerManager.class); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); Uri ringtone; diff --git a/src/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadActivity.java b/src/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadActivity.java index bfd06699..b205b61d 100644 --- a/src/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadActivity.java +++ b/src/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadActivity.java @@ -20,7 +20,6 @@ package org.kde.kdeconnect.Plugins.MousePadPlugin; -import android.content.Context; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; @@ -34,11 +33,11 @@ import android.view.MotionEvent; import android.view.View; import android.view.inputmethod.InputMethodManager; -import org.kde.kdeconnect.BackgroundService; -import org.kde.kdeconnect.UserInterface.ThemeUtil; -import org.kde.kdeconnect_tp.R; - import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.ContextCompat; + +import org.kde.kdeconnect.BackgroundService; +import org.kde.kdeconnect_tp.R; public class MousePadActivity extends AppCompatActivity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener, MousePadGestureDetector.OnGestureListener { private String deviceId; @@ -367,7 +366,7 @@ public class MousePadActivity extends AppCompatActivity implements GestureDetect //TODO: Does not work on KitKat with or without requestFocus() private void showKeyboard() { - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + InputMethodManager imm = ContextCompat.getSystemService(this, InputMethodManager.class); keyListenerView.requestFocus(); imm.toggleSoftInputFromWindow(keyListenerView.getWindowToken(), 0, 0); } diff --git a/src/org/kde/kdeconnect/Plugins/MprisPlugin/AlbumArtCache.java b/src/org/kde/kdeconnect/Plugins/MprisPlugin/AlbumArtCache.java index c90b8b3d..db18dfe4 100644 --- a/src/org/kde/kdeconnect/Plugins/MprisPlugin/AlbumArtCache.java +++ b/src/org/kde/kdeconnect/Plugins/MprisPlugin/AlbumArtCache.java @@ -47,6 +47,7 @@ import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import androidx.collection.LruCache; +import androidx.core.content.ContextCompat; /** * Handles the cache for album art @@ -110,7 +111,8 @@ final class AlbumArtCache { Log.e("KDE/Mpris/AlbumArtCache", "Could not open the album art disk cache!", e); } - connectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); + connectivityManager = ContextCompat.getSystemService(context.getApplicationContext(), + ConnectivityManager.class); } /** diff --git a/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisMediaSession.java b/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisMediaSession.java index 6b1654d1..f33d6cd9 100644 --- a/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisMediaSession.java +++ b/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisMediaSession.java @@ -48,6 +48,7 @@ import java.util.HashSet; import androidx.annotation.RequiresApi; import androidx.core.app.NotificationCompat; import androidx.core.app.TaskStackBuilder; +import androidx.core.content.ContextCompat; import androidx.media.app.NotificationCompat.MediaStyle; /** @@ -461,14 +462,14 @@ public class MprisMediaSession implements SharedPreferences.OnSharedPreferenceCh //Display the notification mediaSession.setActive(true); - final NotificationManager nm = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE); + final NotificationManager nm = ContextCompat.getSystemService(context, NotificationManager.class); nm.notify(MPRIS_MEDIA_NOTIFICATION_ID, notification.build()); }); } public void closeMediaNotification() { //Remove the notification - NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + NotificationManager nm = ContextCompat.getSystemService(context, NotificationManager.class); nm.cancel(MPRIS_MEDIA_NOTIFICATION_ID); //Clear the current player and media session diff --git a/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlugin.java b/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlugin.java index ba77592a..4b63ccf6 100644 --- a/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlugin.java @@ -21,7 +21,6 @@ package org.kde.kdeconnect.Plugins.MprisReceiverPlugin; import android.content.ComponentName; -import android.content.Context; import android.media.session.MediaController; import android.media.session.MediaSessionManager; import android.os.Build; @@ -30,6 +29,11 @@ import android.os.Looper; import android.provider.Settings; import android.util.Log; +import androidx.annotation.Nullable; +import androidx.annotation.RequiresApi; +import androidx.core.content.ContextCompat; +import androidx.fragment.app.DialogFragment; + import org.kde.kdeconnect.Helpers.AppsHelper; import org.kde.kdeconnect.NetworkPacket; import org.kde.kdeconnect.Plugins.NotificationsPlugin.NotificationReceiver; @@ -42,10 +46,6 @@ import org.kde.kdeconnect_tp.R; import java.util.HashMap; import java.util.List; -import androidx.annotation.Nullable; -import androidx.annotation.RequiresApi; -import androidx.fragment.app.DialogFragment; - @PluginFactory.LoadablePlugin @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1) public class MprisReceiverPlugin extends Plugin { @@ -65,7 +65,7 @@ public class MprisReceiverPlugin extends Plugin { players = new HashMap<>(); try { - MediaSessionManager manager = (MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE); + MediaSessionManager manager = ContextCompat.getSystemService(context, MediaSessionManager.class); if (null == manager) return false; @@ -85,7 +85,7 @@ public class MprisReceiverPlugin extends Plugin { @Override public void onDestroy() { super.onDestroy(); - MediaSessionManager manager = (MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE); + MediaSessionManager manager = ContextCompat.getSystemService(context, MediaSessionManager.class); if (manager != null && mediaSessionChangeListener != null) { manager.removeOnActiveSessionsChangedListener(mediaSessionChangeListener); mediaSessionChangeListener = null; diff --git a/src/org/kde/kdeconnect/Plugins/PingPlugin/PingPlugin.java b/src/org/kde/kdeconnect/Plugins/PingPlugin/PingPlugin.java index 61e61936..e1170d90 100644 --- a/src/org/kde/kdeconnect/Plugins/PingPlugin/PingPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/PingPlugin/PingPlugin.java @@ -24,10 +24,12 @@ import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; -import android.content.Context; import android.content.Intent; import android.util.Log; +import androidx.core.app.NotificationCompat; +import androidx.core.content.ContextCompat; + import org.kde.kdeconnect.Helpers.NotificationHelper; import org.kde.kdeconnect.NetworkPacket; import org.kde.kdeconnect.Plugins.Plugin; @@ -35,8 +37,6 @@ import org.kde.kdeconnect.Plugins.PluginFactory; import org.kde.kdeconnect.UserInterface.MainActivity; import org.kde.kdeconnect_tp.R; -import androidx.core.app.NotificationCompat; - @PluginFactory.LoadablePlugin public class PingPlugin extends Plugin { @@ -79,7 +79,7 @@ public class PingPlugin extends Plugin { id = 42; //A unique id to create only one notification } - NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + NotificationManager notificationManager = ContextCompat.getSystemService(context, NotificationManager.class); Notification noti = new NotificationCompat.Builder(context, NotificationHelper.Channels.DEFAULT) .setContentTitle(device.getName()) diff --git a/src/org/kde/kdeconnect/Plugins/PresenterPlugin/PresenterActivity.java b/src/org/kde/kdeconnect/Plugins/PresenterPlugin/PresenterActivity.java index e4f3208f..21fc9641 100644 --- a/src/org/kde/kdeconnect/Plugins/PresenterPlugin/PresenterActivity.java +++ b/src/org/kde/kdeconnect/Plugins/PresenterPlugin/PresenterActivity.java @@ -20,7 +20,6 @@ package org.kde.kdeconnect.Plugins.PresenterPlugin; -import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; @@ -36,12 +35,13 @@ import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.ContextCompat; +import androidx.media.VolumeProviderCompat; + import org.kde.kdeconnect.BackgroundService; import org.kde.kdeconnect_tp.R; -import androidx.appcompat.app.AppCompatActivity; -import androidx.media.VolumeProviderCompat; - public class PresenterActivity extends AppCompatActivity implements SensorEventListener { private MediaSessionCompat mMediaSession; @@ -74,7 +74,7 @@ public class PresenterActivity extends AppCompatActivity implements SensorEventL if (sensorManager != null) { return; //Already enabled } - sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); + sensorManager = ContextCompat.getSystemService(this, SensorManager.class); findViewById(R.id.pointer_button).setVisibility(View.VISIBLE); findViewById(R.id.pointer_button).setOnTouchListener((v, event) -> { if(event.getAction() == MotionEvent.ACTION_DOWN){ @@ -147,7 +147,7 @@ public class PresenterActivity extends AppCompatActivity implements SensorEventL } if (mMediaSession != null) { - PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE); + PowerManager pm = ContextCompat.getSystemService(this, PowerManager.class); boolean screenOn; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) { screenOn = pm.isInteractive(); diff --git a/src/org/kde/kdeconnect/Plugins/ReceiveNotificationsPlugin/ReceiveNotificationsPlugin.java b/src/org/kde/kdeconnect/Plugins/ReceiveNotificationsPlugin/ReceiveNotificationsPlugin.java index 15702e87..06a20f84 100644 --- a/src/org/kde/kdeconnect/Plugins/ReceiveNotificationsPlugin/ReceiveNotificationsPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/ReceiveNotificationsPlugin/ReceiveNotificationsPlugin.java @@ -23,12 +23,14 @@ package org.kde.kdeconnect.Plugins.ReceiveNotificationsPlugin; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; -import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; +import androidx.core.app.NotificationCompat; +import androidx.core.content.ContextCompat; + import org.kde.kdeconnect.Helpers.NotificationHelper; import org.kde.kdeconnect.NetworkPacket; import org.kde.kdeconnect.Plugins.Plugin; @@ -38,8 +40,6 @@ import org.kde.kdeconnect_tp.R; import java.io.InputStream; -import androidx.core.app.NotificationCompat; - @PluginFactory.LoadablePlugin public class ReceiveNotificationsPlugin extends Plugin { @@ -106,7 +106,7 @@ public class ReceiveNotificationsPlugin extends Plugin { } } - NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + NotificationManager notificationManager = ContextCompat.getSystemService(context, NotificationManager.class); Notification noti = new NotificationCompat.Builder(context, NotificationHelper.Channels.RECEIVENOTIFICATION) .setContentTitle(np.getString("appName")) diff --git a/src/org/kde/kdeconnect/Plugins/RemoteKeyboardPlugin/RemoteKeyboardService.java b/src/org/kde/kdeconnect/Plugins/RemoteKeyboardPlugin/RemoteKeyboardService.java index 32f5b74c..b02b1e8f 100644 --- a/src/org/kde/kdeconnect/Plugins/RemoteKeyboardPlugin/RemoteKeyboardService.java +++ b/src/org/kde/kdeconnect/Plugins/RemoteKeyboardPlugin/RemoteKeyboardService.java @@ -20,7 +20,6 @@ package org.kde.kdeconnect.Plugins.RemoteKeyboardPlugin; -import android.content.Context; import android.content.Intent; import android.inputmethodservice.InputMethodService; import android.inputmethodservice.Keyboard; @@ -34,8 +33,10 @@ import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputMethodManager; import android.widget.Toast; -import org.kde.kdeconnect.UserInterface.PluginSettingsActivity; +import androidx.core.content.ContextCompat; + import org.kde.kdeconnect.UserInterface.MainActivity; +import org.kde.kdeconnect.UserInterface.PluginSettingsActivity; import org.kde.kdeconnect_tp.R; import java.util.ArrayList; @@ -189,7 +190,7 @@ public class RemoteKeyboardService break; } case 2: { // "keyboard" - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + InputMethodManager imm = ContextCompat.getSystemService(this, InputMethodManager.class); imm.showInputMethodPicker(); break; } diff --git a/src/org/kde/kdeconnect/Plugins/RunCommandPlugin/RunCommandActivity.java b/src/org/kde/kdeconnect/Plugins/RunCommandPlugin/RunCommandActivity.java index 245a98bb..0c263bde 100644 --- a/src/org/kde/kdeconnect/Plugins/RunCommandPlugin/RunCommandActivity.java +++ b/src/org/kde/kdeconnect/Plugins/RunCommandPlugin/RunCommandActivity.java @@ -21,8 +21,8 @@ package org.kde.kdeconnect.Plugins.RunCommandPlugin; +import android.content.ClipData; import android.content.ClipboardManager; -import android.content.Context; import android.os.Build; import android.os.Bundle; import android.util.Log; @@ -35,6 +35,11 @@ import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; +import androidx.annotation.RequiresApi; +import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.ContextCompat; + import com.google.android.material.floatingactionbutton.FloatingActionButton; import org.json.JSONException; @@ -46,10 +51,6 @@ import org.kde.kdeconnect_tp.R; import java.util.ArrayList; import java.util.Collections; -import androidx.annotation.RequiresApi; -import androidx.appcompat.app.AlertDialog; -import androidx.appcompat.app.AppCompatActivity; - public class RunCommandActivity extends AppCompatActivity { private String deviceId; @@ -143,7 +144,7 @@ public class RunCommandActivity extends AppCompatActivity { if (item.getItemId() == R.id.copy_url_to_clipboard) { CommandEntry entry = (CommandEntry) commandItems.get(info.position); String url = "kdeconnect://runcommand/" + deviceId + "/" + entry.getKey(); - ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); + ClipboardManager cm = ContextCompat.getSystemService(this, ClipboardManager.class); cm.setText(url); Toast toast = Toast.makeText(this, R.string.clipboard_toast, Toast.LENGTH_SHORT); toast.show(); diff --git a/src/org/kde/kdeconnect/Plugins/SharePlugin/CompositeReceiveFileJob.java b/src/org/kde/kdeconnect/Plugins/SharePlugin/CompositeReceiveFileJob.java index b264470e..bd995ff1 100644 --- a/src/org/kde/kdeconnect/Plugins/SharePlugin/CompositeReceiveFileJob.java +++ b/src/org/kde/kdeconnect/Plugins/SharePlugin/CompositeReceiveFileJob.java @@ -22,12 +22,16 @@ package org.kde.kdeconnect.Plugins.SharePlugin; import android.app.DownloadManager; import android.content.ActivityNotFoundException; -import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Build; import android.util.Log; +import androidx.annotation.GuardedBy; +import androidx.core.content.ContextCompat; +import androidx.core.content.FileProvider; +import androidx.documentfile.provider.DocumentFile; + import org.kde.kdeconnect.Device; import org.kde.kdeconnect.Helpers.FilesHelper; import org.kde.kdeconnect.Helpers.MediaStoreHelper; @@ -43,10 +47,6 @@ import java.io.OutputStream; import java.util.ArrayList; import java.util.List; -import androidx.annotation.GuardedBy; -import androidx.core.content.FileProvider; -import androidx.documentfile.provider.DocumentFile; - /** * A type of {@link BackgroundJob} that reads Files from another device. * @@ -330,7 +330,8 @@ public class CompositeReceiveFileJob extends BackgroundJob { private void publishFile(DocumentFile fileDocument, long size) { if (!ShareSettingsFragment.isCustomDestinationEnabled(getDevice().getContext())) { Log.i("SharePlugin", "Adding to downloads"); - DownloadManager manager = (DownloadManager) getDevice().getContext().getSystemService(Context.DOWNLOAD_SERVICE); + DownloadManager manager = ContextCompat.getSystemService(getDevice().getContext(), + DownloadManager.class); manager.addCompletedDownload(fileDocument.getUri().getLastPathSegment(), getDevice().getName(), true, fileDocument.getType(), fileDocument.getUri().getPath(), size, false); } else { //Make sure it is added to the Android Gallery anyway diff --git a/src/org/kde/kdeconnect/Plugins/SharePlugin/ReceiveNotification.java b/src/org/kde/kdeconnect/Plugins/SharePlugin/ReceiveNotification.java index 8d74778f..3358685b 100644 --- a/src/org/kde/kdeconnect/Plugins/SharePlugin/ReceiveNotification.java +++ b/src/org/kde/kdeconnect/Plugins/SharePlugin/ReceiveNotification.java @@ -23,7 +23,6 @@ package org.kde.kdeconnect.Plugins.SharePlugin; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; -import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Bitmap; @@ -32,6 +31,10 @@ import android.net.Uri; import android.os.Build; import android.preference.PreferenceManager; +import androidx.core.app.NotificationCompat; +import androidx.core.content.ContextCompat; +import androidx.core.content.FileProvider; + import org.kde.kdeconnect.Device; import org.kde.kdeconnect.Helpers.NotificationHelper; import org.kde.kdeconnect_tp.R; @@ -40,9 +43,6 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; -import androidx.core.app.NotificationCompat; -import androidx.core.content.FileProvider; - class ReceiveNotification { private final NotificationManager notificationManager; private final int notificationId; @@ -59,7 +59,7 @@ class ReceiveNotification { this.jobId = jobId; notificationId = (int) System.currentTimeMillis(); - notificationManager = (NotificationManager) device.getContext().getSystemService(Context.NOTIFICATION_SERVICE); + notificationManager = ContextCompat.getSystemService(device.getContext(), NotificationManager.class); builder = new NotificationCompat.Builder(device.getContext(), NotificationHelper.Channels.FILETRANSFER) .setSmallIcon(android.R.drawable.stat_sys_download) .setAutoCancel(true) diff --git a/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java b/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java index 316004e8..d59b14db 100644 --- a/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java +++ b/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java @@ -23,7 +23,6 @@ package org.kde.kdeconnect.Plugins.SharePlugin; import android.Manifest; import android.app.Activity; import android.content.ClipboardManager; -import android.content.Context; import android.content.Intent; import android.graphics.drawable.Drawable; import android.net.Uri; @@ -33,6 +32,10 @@ import android.os.Looper; import android.util.Log; import android.widget.Toast; +import androidx.annotation.NonNull; +import androidx.annotation.WorkerThread; +import androidx.core.content.ContextCompat; + import org.kde.kdeconnect.Helpers.FilesHelper; import org.kde.kdeconnect.Helpers.IntentHelper; import org.kde.kdeconnect.NetworkPacket; @@ -46,10 +49,6 @@ import org.kde.kdeconnect_tp.R; import java.net.URL; import java.util.ArrayList; -import androidx.annotation.NonNull; -import androidx.annotation.WorkerThread; -import androidx.core.content.ContextCompat; - /** * A Plugin for sharing and receiving files and uris. *

@@ -176,7 +175,7 @@ public class SharePlugin extends Plugin { private void receiveText(NetworkPacket np) { String text = np.getString("text"); - ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); + ClipboardManager cm = ContextCompat.getSystemService(context, ClipboardManager.class); cm.setText(text); handler.post(() -> Toast.makeText(context, R.string.shareplugin_text_saved, Toast.LENGTH_LONG).show()); } diff --git a/src/org/kde/kdeconnect/Plugins/SharePlugin/UploadNotification.java b/src/org/kde/kdeconnect/Plugins/SharePlugin/UploadNotification.java index a0fc55f4..886c8143 100644 --- a/src/org/kde/kdeconnect/Plugins/SharePlugin/UploadNotification.java +++ b/src/org/kde/kdeconnect/Plugins/SharePlugin/UploadNotification.java @@ -23,17 +23,17 @@ package org.kde.kdeconnect.Plugins.SharePlugin; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; -import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import androidx.core.app.NotificationCompat; +import androidx.core.content.ContextCompat; +import androidx.preference.PreferenceManager; + import org.kde.kdeconnect.Device; import org.kde.kdeconnect.Helpers.NotificationHelper; import org.kde.kdeconnect_tp.R; -import androidx.core.app.NotificationCompat; -import androidx.preference.PreferenceManager; - class UploadNotification { private final NotificationManager notificationManager; private NotificationCompat.Builder builder; @@ -46,7 +46,7 @@ class UploadNotification { this.jobId = jobId; notificationId = (int) System.currentTimeMillis(); - notificationManager = (NotificationManager) device.getContext().getSystemService(Context.NOTIFICATION_SERVICE); + notificationManager = ContextCompat.getSystemService(device.getContext(), NotificationManager.class); builder = new NotificationCompat.Builder(device.getContext(), NotificationHelper.Channels.FILETRANSFER) .setSmallIcon(android.R.drawable.stat_sys_upload) .setAutoCancel(true) diff --git a/src/org/kde/kdeconnect/Plugins/TelephonyPlugin/TelephonyPlugin.java b/src/org/kde/kdeconnect/Plugins/TelephonyPlugin/TelephonyPlugin.java index 22a13f91..b6121b64 100644 --- a/src/org/kde/kdeconnect/Plugins/TelephonyPlugin/TelephonyPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/TelephonyPlugin/TelephonyPlugin.java @@ -227,7 +227,7 @@ public class TelephonyPlugin extends Plugin { private void unmuteRinger() { if (isMuted) { - AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + AudioManager am = ContextCompat.getSystemService(context, AudioManager.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { am.setStreamVolume(AudioManager.STREAM_RING, AudioManager.ADJUST_UNMUTE, 0); } else { @@ -239,7 +239,7 @@ public class TelephonyPlugin extends Plugin { private void muteRinger() { if (!isMuted) { - AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + AudioManager am = ContextCompat.getSystemService(context, AudioManager.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { am.setStreamVolume(AudioManager.STREAM_RING, AudioManager.ADJUST_MUTE, 0); } else { diff --git a/src/org/kde/kdeconnect/UserInterface/List/ListAdapter.java b/src/org/kde/kdeconnect/UserInterface/List/ListAdapter.java index 2200d5a3..d746bcb1 100644 --- a/src/org/kde/kdeconnect/UserInterface/List/ListAdapter.java +++ b/src/org/kde/kdeconnect/UserInterface/List/ListAdapter.java @@ -27,6 +27,7 @@ import android.view.ViewGroup; import android.widget.ArrayAdapter; import androidx.annotation.NonNull; +import androidx.core.content.ContextCompat; import java.util.ArrayList; @@ -43,7 +44,7 @@ public class ListAdapter extends ArrayAdapter { public ListAdapter(Context context, ArrayList items) { super(context, 0, items); this.items = items; - layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + layoutInflater = ContextCompat.getSystemService(context, LayoutInflater.class); } @NonNull diff --git a/src/org/kde/kdeconnect/UserInterface/PairingFragment.java b/src/org/kde/kdeconnect/UserInterface/PairingFragment.java index 2f4d5c23..52019ed2 100644 --- a/src/org/kde/kdeconnect/UserInterface/PairingFragment.java +++ b/src/org/kde/kdeconnect/UserInterface/PairingFragment.java @@ -42,6 +42,7 @@ import android.widget.ListView; import android.widget.TextView; import androidx.annotation.NonNull; +import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; @@ -123,7 +124,8 @@ public class PairingFragment extends Fragment implements PairingDeviceItem.Callb updateDeviceList(); } }; - ConnectivityManager connManager = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE); + ConnectivityManager connManager = ContextCompat.getSystemService(requireContext(), + ConnectivityManager.class); connManager.registerNetworkCallback(new NetworkRequest.Builder().build(), (ConnectivityManager.NetworkCallback) networkChangeListener); } @@ -133,7 +135,8 @@ public class PairingFragment extends Fragment implements PairingDeviceItem.Callb @Override public void onDestroyView() { if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - ConnectivityManager connManager = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE); + ConnectivityManager connManager = ContextCompat.getSystemService(requireContext(), + ConnectivityManager.class); connManager.unregisterNetworkCallback((ConnectivityManager.NetworkCallback) networkChangeListener); } @@ -186,7 +189,8 @@ public class PairingFragment extends Fragment implements PairingDeviceItem.Callb ((ListView) rootView.findViewById(R.id.devices_list)).removeHeaderView(headerText); ((ListView) rootView.findViewById(R.id.devices_list)).removeHeaderView(noWifiHeader); ((ListView) rootView.findViewById(R.id.devices_list)).removeHeaderView(notTrustedText); - ConnectivityManager connManager = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE); + ConnectivityManager connManager = ContextCompat.getSystemService(requireContext(), + ConnectivityManager.class); NetworkInfo wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); //Check if we're on Wi-Fi. If we still see a device, don't do anything special if (someDevicesReachable || wifi.isConnected()) { diff --git a/tests/org/kde/kdeconnect/DeviceTest.java b/tests/org/kde/kdeconnect/DeviceTest.java index 9ace96e0..69bd9c45 100644 --- a/tests/org/kde/kdeconnect/DeviceTest.java +++ b/tests/org/kde/kdeconnect/DeviceTest.java @@ -27,6 +27,8 @@ import android.preference.PreferenceManager; import android.util.Base64; import android.util.Log; +import androidx.core.content.ContextCompat; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -111,7 +113,7 @@ public class DeviceTest { PowerMockito.when(PreferenceManager.getDefaultSharedPreferences(any())).thenReturn(defaultSettings); RsaHelper.initialiseRsaKeys(context); - Mockito.when(context.getSystemService(eq(Context.NOTIFICATION_SERVICE))).thenReturn(Mockito.mock(NotificationManager.class)); + Mockito.when(ContextCompat.getSystemService(context, NotificationManager.class)).thenReturn(Mockito.mock(NotificationManager.class)); } @Test