From 07f3e778d3d4cc4001071f47716222bbd003b1f6 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Wed, 15 Jul 2020 20:47:28 +0530 Subject: [PATCH 1/5] Use ContextCompat.startActivity(). --- .../views/apps/AppListItemController.java | 32 +++++++++---------- .../views/categories/AppCardController.java | 26 +++++++-------- 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/org/fdroid/fdroid/views/apps/AppListItemController.java b/app/src/main/java/org/fdroid/fdroid/views/apps/AppListItemController.java index 74057f24d..0d818f793 100644 --- a/app/src/main/java/org/fdroid/fdroid/views/apps/AppListItemController.java +++ b/app/src/main/java/org/fdroid/fdroid/views/apps/AppListItemController.java @@ -11,13 +11,6 @@ import android.graphics.Outline; import android.net.Uri; import android.os.Build; import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.core.app.ActivityOptionsCompat; -import androidx.core.content.ContextCompat; -import androidx.localbroadcastmanager.content.LocalBroadcastManager; -import androidx.core.util.Pair; -import androidx.recyclerview.widget.RecyclerView; import android.text.TextUtils; import android.view.View; import android.view.ViewOutlineProvider; @@ -27,6 +20,15 @@ import android.widget.ImageButton; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.core.app.ActivityOptionsCompat; +import androidx.core.content.ContextCompat; +import androidx.core.util.Pair; +import androidx.localbroadcastmanager.content.LocalBroadcastManager; +import androidx.recyclerview.widget.RecyclerView; + import org.fdroid.fdroid.AppUpdateStatusManager; import org.fdroid.fdroid.AppUpdateStatusManager.AppUpdateStatus; import org.fdroid.fdroid.Preferences; @@ -444,15 +446,13 @@ public abstract class AppListItemController extends RecyclerView.ViewHolder { Intent intent = new Intent(activity, AppDetailsActivity.class); intent.putExtra(AppDetailsActivity.EXTRA_APPID, currentApp.packageName); - if (Build.VERSION.SDK_INT >= 21) { - String transitionAppIcon = activity.getString(R.string.transition_app_item_icon); - Pair iconTransitionPair = Pair.create((View) icon, transitionAppIcon); - Bundle bundle = ActivityOptionsCompat - .makeSceneTransitionAnimation(activity, iconTransitionPair).toBundle(); - activity.startActivity(intent, bundle); - } else { - activity.startActivity(intent); - } + String transitionAppIcon = activity.getString(R.string.transition_app_item_icon); + Pair iconTransitionPair = Pair.create((View) icon, transitionAppIcon); + // unchecked since the right type is passed as 2nd varargs arg: Pair + @SuppressWarnings("unchecked") + Bundle bundle = ActivityOptionsCompat + .makeSceneTransitionAnimation(activity, iconTransitionPair).toBundle(); + ContextCompat.startActivity(activity, intent, bundle); } }; diff --git a/app/src/main/java/org/fdroid/fdroid/views/categories/AppCardController.java b/app/src/main/java/org/fdroid/fdroid/views/categories/AppCardController.java index 7df851d07..15209347a 100644 --- a/app/src/main/java/org/fdroid/fdroid/views/categories/AppCardController.java +++ b/app/src/main/java/org/fdroid/fdroid/views/categories/AppCardController.java @@ -2,17 +2,19 @@ package org.fdroid.fdroid.views.categories; import android.app.Activity; import android.content.Intent; -import android.os.Build; import android.os.Bundle; +import android.view.View; +import android.widget.ImageView; +import android.widget.TextView; + import androidx.annotation.IdRes; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.app.ActivityOptionsCompat; +import androidx.core.content.ContextCompat; import androidx.core.util.Pair; import androidx.recyclerview.widget.RecyclerView; -import android.view.View; -import android.widget.ImageView; -import android.widget.TextView; + import org.fdroid.fdroid.R; import org.fdroid.fdroid.Utils; import org.fdroid.fdroid.data.App; @@ -117,16 +119,12 @@ public class AppCardController extends RecyclerView.ViewHolder Intent intent = new Intent(activity, AppDetailsActivity.class); intent.putExtra(AppDetailsActivity.EXTRA_APPID, currentApp.packageName); - if (Build.VERSION.SDK_INT >= 21) { - Pair iconTransitionPair = Pair.create((View) icon, - activity.getString(R.string.transition_app_item_icon)); + Pair iconTransitionPair = Pair.create((View) icon, + activity.getString(R.string.transition_app_item_icon)); - // unchecked since the right type is passed as 2nd varargs arg: Pair - @SuppressWarnings("unchecked") - Bundle b = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, iconTransitionPair).toBundle(); - activity.startActivity(intent, b); - } else { - activity.startActivity(intent); - } + // unchecked since the right type is passed as 2nd varargs arg: Pair + @SuppressWarnings("unchecked") + Bundle b = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, iconTransitionPair).toBundle(); + ContextCompat.startActivity(activity, intent, b); } } From 6a0cd0a14ae531fed6320044efe199ee08b41307 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Thu, 16 Jul 2020 06:32:19 +0530 Subject: [PATCH 2/5] Use ContextCompat.getDrawable(). --- .../java/org/fdroid/fdroid/nearby/SelectAppsView.java | 5 +++-- .../fdroid/fdroid/views/SeekBarForegroundThumb.java | 11 ++++------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/app/src/full/java/org/fdroid/fdroid/nearby/SelectAppsView.java b/app/src/full/java/org/fdroid/fdroid/nearby/SelectAppsView.java index bdab14bd6..2ba8dcc69 100644 --- a/app/src/full/java/org/fdroid/fdroid/nearby/SelectAppsView.java +++ b/app/src/full/java/org/fdroid/fdroid/nearby/SelectAppsView.java @@ -23,6 +23,7 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; import androidx.cursoradapter.widget.CursorAdapter; import androidx.loader.app.LoaderManager; import androidx.loader.content.CursorLoader; @@ -147,7 +148,7 @@ public class SelectAppsView extends SwapView implements LoaderManager.LoaderCall private Drawable getDefaultAppIcon(Context context) { if (defaultAppIcon == null) { - defaultAppIcon = context.getResources().getDrawable(android.R.drawable.sym_def_app_icon); + defaultAppIcon = ContextCompat.getDrawable(context, android.R.drawable.sym_def_app_icon); } return defaultAppIcon; } @@ -225,7 +226,7 @@ public class SelectAppsView extends SwapView implements LoaderManager.LoaderCall resource = R.drawable.ic_add_circle_outline; colour = 0xFFD0D0D4; } - imageView.setImageDrawable(getResources().getDrawable(resource)); + imageView.setImageDrawable(ContextCompat.getDrawable(getContext(), resource)); imageView.setColorFilter(colour, PorterDuff.Mode.MULTIPLY); } } diff --git a/app/src/main/java/org/fdroid/fdroid/views/SeekBarForegroundThumb.java b/app/src/main/java/org/fdroid/fdroid/views/SeekBarForegroundThumb.java index 8b9ebad69..7326bbaee 100644 --- a/app/src/main/java/org/fdroid/fdroid/views/SeekBarForegroundThumb.java +++ b/app/src/main/java/org/fdroid/fdroid/views/SeekBarForegroundThumb.java @@ -4,9 +4,11 @@ import android.content.Context; import android.graphics.Canvas; import android.graphics.drawable.Drawable; import android.os.Build; -import androidx.appcompat.widget.AppCompatSeekBar; import android.util.AttributeSet; +import androidx.appcompat.widget.AppCompatSeekBar; +import androidx.core.content.ContextCompat; + import org.fdroid.fdroid.R; /** @@ -34,12 +36,7 @@ public class SeekBarForegroundThumb extends AppCompatSeekBar { private void init(Context context) { this.context = context; - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - tickMark = context.getDrawable(R.drawable.seekbar_tickmark); - } else { - tickMark = context.getResources().getDrawable(R.drawable.seekbar_tickmark); - } + tickMark = ContextCompat.getDrawable(context, R.drawable.seekbar_tickmark); } private Drawable getThumbCompat() { From 5254a6f1aab01362e8ff47aec7b6c6a182576662 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Thu, 16 Jul 2020 06:43:45 +0530 Subject: [PATCH 3/5] Use ContextCompat.getColor(). --- .../fdroid/fdroid/nearby/SelectAppsView.java | 2 +- .../org/fdroid/fdroid/nearby/SwapView.java | 11 +++++--- .../fdroid/nearby/SwapWorkflowActivity.java | 3 ++- .../panic/DestructiveCheckBoxPreference.java | 11 ++++---- .../fdroid/panic/DestructivePreference.java | 9 ++++--- .../panic/SelectInstalledAppsActivity.java | 26 ++++++++++--------- .../fdroid/views/ManageReposActivity.java | 24 ++++++++++------- .../fdroid/views/RepoDetailsActivity.java | 21 ++++++++------- 8 files changed, 62 insertions(+), 45 deletions(-) diff --git a/app/src/full/java/org/fdroid/fdroid/nearby/SelectAppsView.java b/app/src/full/java/org/fdroid/fdroid/nearby/SelectAppsView.java index 2ba8dcc69..6ba6cb4c4 100644 --- a/app/src/full/java/org/fdroid/fdroid/nearby/SelectAppsView.java +++ b/app/src/full/java/org/fdroid/fdroid/nearby/SelectAppsView.java @@ -221,7 +221,7 @@ public class SelectAppsView extends SwapView implements LoaderManager.LoaderCall int colour; if (checked) { resource = R.drawable.ic_check_circle; - colour = getResources().getColor(R.color.swap_bright_blue); + colour = ContextCompat.getColor(getContext(), R.color.swap_bright_blue); } else { resource = R.drawable.ic_add_circle_outline; colour = 0xFFD0D0D4; diff --git a/app/src/full/java/org/fdroid/fdroid/nearby/SwapView.java b/app/src/full/java/org/fdroid/fdroid/nearby/SwapView.java index a7901ef07..c2dcaa251 100644 --- a/app/src/full/java/org/fdroid/fdroid/nearby/SwapView.java +++ b/app/src/full/java/org/fdroid/fdroid/nearby/SwapView.java @@ -3,10 +3,13 @@ package org.fdroid.fdroid.nearby; import android.annotation.TargetApi; import android.content.Context; import android.content.res.TypedArray; -import androidx.annotation.ColorInt; -import androidx.annotation.LayoutRes; import android.util.AttributeSet; import android.widget.RelativeLayout; + +import androidx.annotation.ColorInt; +import androidx.annotation.LayoutRes; +import androidx.core.content.ContextCompat; + import org.fdroid.fdroid.R; /** @@ -42,7 +45,7 @@ public class SwapView extends RelativeLayout { final TypedArray a = context.obtainStyledAttributes( attrs, R.styleable.SwapView, 0, 0); toolbarColor = a.getColor(R.styleable.SwapView_toolbarColor, - getResources().getColor(R.color.swap_blue)); + ContextCompat.getColor(context, R.color.swap_blue)); toolbarTitle = a.getString(R.styleable.SwapView_toolbarTitle); a.recycle(); } @@ -53,7 +56,7 @@ public class SwapView extends RelativeLayout { final TypedArray a = context.obtainStyledAttributes( attrs, R.styleable.SwapView, 0, 0); toolbarColor = a.getColor(R.styleable.SwapView_toolbarColor, - getResources().getColor(R.color.swap_blue)); + ContextCompat.getColor(context, R.color.swap_blue)); toolbarTitle = a.getString(R.styleable.SwapView_toolbarTitle); a.recycle(); } diff --git a/app/src/full/java/org/fdroid/fdroid/nearby/SwapWorkflowActivity.java b/app/src/full/java/org/fdroid/fdroid/nearby/SwapWorkflowActivity.java index 0be72390f..56ce77818 100644 --- a/app/src/full/java/org/fdroid/fdroid/nearby/SwapWorkflowActivity.java +++ b/app/src/full/java/org/fdroid/fdroid/nearby/SwapWorkflowActivity.java @@ -934,7 +934,8 @@ public class SwapWorkflowActivity extends AppCompatActivity { new QrGenAsyncTask(SwapWorkflowActivity.this, R.id.wifi_qr_code).execute(qrUriString); // Replace all blacks with the background blue. - qrImage.setColorFilter(new LightingColorFilter(0xffffffff, getResources().getColor(R.color.swap_blue))); + qrImage.setColorFilter(new LightingColorFilter(0xffffffff, ContextCompat.getColor(this, + R.color.swap_blue))); final View qrWarningMessage = container.findViewById(R.id.warning_qr_scanner); if (CameraCharacteristicsChecker.getInstance(this).hasAutofocus()) { diff --git a/app/src/full/java/org/fdroid/fdroid/panic/DestructiveCheckBoxPreference.java b/app/src/full/java/org/fdroid/fdroid/panic/DestructiveCheckBoxPreference.java index e09e0afe3..67fe2fb3e 100644 --- a/app/src/full/java/org/fdroid/fdroid/panic/DestructiveCheckBoxPreference.java +++ b/app/src/full/java/org/fdroid/fdroid/panic/DestructiveCheckBoxPreference.java @@ -1,10 +1,12 @@ package org.fdroid.fdroid.panic; import android.content.Context; -import android.content.res.Resources; +import android.util.AttributeSet; + +import androidx.core.content.ContextCompat; import androidx.preference.CheckBoxPreference; import androidx.preference.PreferenceViewHolder; -import android.util.AttributeSet; + import org.fdroid.fdroid.FDroidApp; import org.fdroid.fdroid.R; @@ -31,11 +33,10 @@ public class DestructiveCheckBoxPreference extends CheckBoxPreference { if (!holder.itemView.isEnabled()) { return; } - Resources resources = getContext().getResources(); if (FDroidApp.isAppThemeLight()) { - holder.itemView.setBackgroundColor(resources.getColor(R.color.panic_destructive_light)); + holder.itemView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.panic_destructive_light)); } else { - holder.itemView.setBackgroundColor(resources.getColor(R.color.panic_destructive_dark)); + holder.itemView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.panic_destructive_dark)); } } } diff --git a/app/src/full/java/org/fdroid/fdroid/panic/DestructivePreference.java b/app/src/full/java/org/fdroid/fdroid/panic/DestructivePreference.java index 53a613e74..5927ec02c 100644 --- a/app/src/full/java/org/fdroid/fdroid/panic/DestructivePreference.java +++ b/app/src/full/java/org/fdroid/fdroid/panic/DestructivePreference.java @@ -1,9 +1,12 @@ package org.fdroid.fdroid.panic; import android.content.Context; +import android.util.AttributeSet; + +import androidx.core.content.ContextCompat; import androidx.preference.Preference; import androidx.preference.PreferenceViewHolder; -import android.util.AttributeSet; + import org.fdroid.fdroid.FDroidApp; import org.fdroid.fdroid.R; @@ -28,9 +31,9 @@ public class DestructivePreference extends Preference { public void onBindViewHolder(PreferenceViewHolder holder) { super.onBindViewHolder(holder); if (FDroidApp.isAppThemeLight()) { - holder.itemView.setBackgroundColor(getContext().getResources().getColor(R.color.panic_destructive_light)); + holder.itemView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.panic_destructive_light)); } else { - holder.itemView.setBackgroundColor(getContext().getResources().getColor(R.color.panic_destructive_dark)); + holder.itemView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.panic_destructive_dark)); } } } diff --git a/app/src/full/java/org/fdroid/fdroid/panic/SelectInstalledAppsActivity.java b/app/src/full/java/org/fdroid/fdroid/panic/SelectInstalledAppsActivity.java index f5d68348b..1264c4aee 100644 --- a/app/src/full/java/org/fdroid/fdroid/panic/SelectInstalledAppsActivity.java +++ b/app/src/full/java/org/fdroid/fdroid/panic/SelectInstalledAppsActivity.java @@ -20,23 +20,25 @@ package org.fdroid.fdroid.panic; import android.annotation.SuppressLint; -import android.content.res.Resources; import android.database.Cursor; import android.graphics.LightingColorFilter; import android.graphics.drawable.Drawable; import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.loader.app.LoaderManager; -import androidx.loader.content.CursorLoader; -import androidx.loader.content.Loader; -import androidx.appcompat.app.AppCompatActivity; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; -import androidx.appcompat.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; +import androidx.core.content.ContextCompat; +import androidx.loader.app.LoaderManager; +import androidx.loader.content.CursorLoader; +import androidx.loader.content.Loader; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + import org.fdroid.fdroid.FDroidApp; import org.fdroid.fdroid.Preferences; import org.fdroid.fdroid.R; @@ -116,9 +118,9 @@ public class SelectInstalledAppsActivity extends AppCompatActivity implements Lo menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); checkId = menuItem.getItemId(); if (FDroidApp.isAppThemeLight()) { - Resources resources = getResources(); - Drawable icon = resources.getDrawable(R.drawable.check); - icon.setColorFilter(new LightingColorFilter(0xffffffff, resources.getColor(android.R.color.white))); + Drawable icon = ContextCompat.getDrawable(this, R.drawable.check); + icon.setColorFilter(new LightingColorFilter(0xffffffff, ContextCompat.getColor(this, + android.R.color.white))); menuItem.setIcon(icon); } else { menuItem.setIcon(R.drawable.check); diff --git a/app/src/main/java/org/fdroid/fdroid/views/ManageReposActivity.java b/app/src/main/java/org/fdroid/fdroid/views/ManageReposActivity.java index 15dc3f34e..bddb0a15a 100644 --- a/app/src/main/java/org/fdroid/fdroid/views/ManageReposActivity.java +++ b/app/src/main/java/org/fdroid/fdroid/views/ManageReposActivity.java @@ -35,15 +35,6 @@ import android.net.wifi.WifiManager; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.loader.app.LoaderManager; -import androidx.core.app.NavUtils; -import androidx.core.app.TaskStackBuilder; -import androidx.loader.content.CursorLoader; -import androidx.loader.content.Loader; -import androidx.appcompat.app.AlertDialog; -import androidx.appcompat.app.AppCompatActivity; -import androidx.appcompat.widget.Toolbar; import android.text.Editable; import android.text.TextUtils; import android.text.TextWatcher; @@ -57,6 +48,18 @@ import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; +import androidx.core.app.NavUtils; +import androidx.core.app.TaskStackBuilder; +import androidx.core.content.ContextCompat; +import androidx.loader.app.LoaderManager; +import androidx.loader.content.CursorLoader; +import androidx.loader.content.Loader; + import org.fdroid.fdroid.AddRepoIntentService; import org.fdroid.fdroid.FDroidApp; import org.fdroid.fdroid.IndexUpdater; @@ -538,7 +541,8 @@ public class ManageReposActivity extends AppCompatActivity overwriteMessage.setText(getString(messageRes, name)); overwriteMessage.setVisibility(View.VISIBLE); if (redMessage) { - overwriteMessage.setTextColor(getResources().getColor(R.color.red)); + overwriteMessage.setTextColor(ContextCompat.getColor(ManageReposActivity.this, + R.color.red)); } else { overwriteMessage.setTextColor(defaultTextColour); } diff --git a/app/src/main/java/org/fdroid/fdroid/views/RepoDetailsActivity.java b/app/src/main/java/org/fdroid/fdroid/views/RepoDetailsActivity.java index 28291685c..cbdbc981e 100644 --- a/app/src/main/java/org/fdroid/fdroid/views/RepoDetailsActivity.java +++ b/app/src/main/java/org/fdroid/fdroid/views/RepoDetailsActivity.java @@ -13,14 +13,6 @@ import android.nfc.NfcAdapter; import android.os.Build; import android.os.Bundle; import android.os.Parcelable; -import androidx.annotation.NonNull; -import androidx.core.app.NavUtils; -import androidx.localbroadcastmanager.content.LocalBroadcastManager; -import androidx.appcompat.app.AlertDialog; -import androidx.appcompat.app.AppCompatActivity; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; -import androidx.appcompat.widget.Toolbar; import android.text.TextUtils; import android.text.format.DateUtils; import android.view.LayoutInflater; @@ -33,6 +25,17 @@ import android.widget.CompoundButton; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; +import androidx.core.app.NavUtils; +import androidx.core.content.ContextCompat; +import androidx.localbroadcastmanager.content.LocalBroadcastManager; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + import org.fdroid.fdroid.FDroidApp; import org.fdroid.fdroid.NfcHelper; import org.fdroid.fdroid.NfcNotEnabledActivity; @@ -305,7 +308,7 @@ public class RepoDetailsActivity extends AppCompatActivity { // TODO show the current state of the signature check, not just whether there is a key or not if (TextUtils.isEmpty(repo.fingerprint) && TextUtils.isEmpty(repo.signingCertificate)) { repoFingerprint = getResources().getString(R.string.unsigned); - repoFingerprintView.setTextColor(getResources().getColor(R.color.unsigned)); + repoFingerprintView.setTextColor(ContextCompat.getColor(this, R.color.unsigned)); repoFingerprintDescView.setVisibility(View.VISIBLE); repoFingerprintDescView.setText(getResources().getString(R.string.unsigned_description)); } else { From 8b9c624d757013f5a4758437cec40bec62353a42 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Wed, 22 Jul 2020 07:58:22 +0530 Subject: [PATCH 4/5] Use ContextCompat.startForegroundService(). --- .../org/fdroid/fdroid/nearby/SwapService.java | 19 ++++++------------- .../fdroid/views/main/NearbyViewBinder.java | 10 ++++++---- .../fdroid/views/main/MainActivity.java | 18 +++++++++++------- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/app/src/full/java/org/fdroid/fdroid/nearby/SwapService.java b/app/src/full/java/org/fdroid/fdroid/nearby/SwapService.java index 896959dfc..5c808c7f5 100644 --- a/app/src/full/java/org/fdroid/fdroid/nearby/SwapService.java +++ b/app/src/full/java/org/fdroid/fdroid/nearby/SwapService.java @@ -14,15 +14,15 @@ import android.content.SharedPreferences; import android.net.Uri; import android.net.wifi.WifiManager; import android.os.AsyncTask; -import android.os.Build; import android.os.IBinder; +import android.text.TextUtils; +import android.util.Log; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.app.NotificationCompat; import androidx.localbroadcastmanager.content.LocalBroadcastManager; -import android.text.TextUtils; -import android.util.Log; -import cc.mvdan.accesspoint.WifiApControl; + import org.fdroid.fdroid.FDroidApp; import org.fdroid.fdroid.NotificationHelper; import org.fdroid.fdroid.Preferences; @@ -46,6 +46,8 @@ import java.util.Set; import java.util.Timer; import java.util.TimerTask; +import cc.mvdan.accesspoint.WifiApControl; + /** * Central service which manages all of the different moving parts of swap which are required * to enable p2p swapping of apps. @@ -73,15 +75,6 @@ public class SwapService extends Service { private static WifiManager wifiManager; private static Timer pollConnectedSwapRepoTimer; - public static void start(Context context) { - Intent intent = new Intent(context, SwapService.class); - if (Build.VERSION.SDK_INT < 26) { - context.startService(intent); - } else { - context.startForegroundService(intent); - } - } - public static void stop(Context context) { Intent intent = new Intent(context, SwapService.class); context.stopService(intent); diff --git a/app/src/full/java/org/fdroid/fdroid/views/main/NearbyViewBinder.java b/app/src/full/java/org/fdroid/fdroid/views/main/NearbyViewBinder.java index 0434bfadd..a2331c1d2 100644 --- a/app/src/full/java/org/fdroid/fdroid/views/main/NearbyViewBinder.java +++ b/app/src/full/java/org/fdroid/fdroid/views/main/NearbyViewBinder.java @@ -13,9 +13,6 @@ import android.os.Build; import android.os.Environment; import android.os.storage.StorageManager; import android.os.storage.StorageVolume; -import androidx.annotation.RequiresApi; -import androidx.core.app.ActivityCompat; -import androidx.core.content.ContextCompat; import android.text.TextUtils; import android.util.Log; import android.view.View; @@ -24,6 +21,11 @@ import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; + +import androidx.annotation.RequiresApi; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; + import org.fdroid.fdroid.R; import org.fdroid.fdroid.Utils; import org.fdroid.fdroid.nearby.SDCardScannerService; @@ -86,7 +88,7 @@ public class NearbyViewBinder { ActivityCompat.requestPermissions(activity, new String[]{coarseLocation}, MainActivity.REQUEST_LOCATION_PERMISSIONS); } else { - SwapService.start(activity); + ContextCompat.startForegroundService(activity, new Intent(activity, SwapService.class)); } } }); diff --git a/app/src/main/java/org/fdroid/fdroid/views/main/MainActivity.java b/app/src/main/java/org/fdroid/fdroid/views/main/MainActivity.java index 82f4f2494..d01af3979 100644 --- a/app/src/main/java/org/fdroid/fdroid/views/main/MainActivity.java +++ b/app/src/main/java/org/fdroid/fdroid/views/main/MainActivity.java @@ -30,12 +30,6 @@ import android.content.IntentFilter; import android.net.Uri; import android.os.Build; import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.localbroadcastmanager.content.LocalBroadcastManager; -import androidx.appcompat.app.AppCompatActivity; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; import android.text.TextUtils; import android.util.TypedValue; import android.view.View; @@ -43,9 +37,19 @@ import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.ContextCompat; +import androidx.localbroadcastmanager.content.LocalBroadcastManager; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + import com.ashokvarma.bottomnavigation.BottomNavigationBar; import com.ashokvarma.bottomnavigation.BottomNavigationItem; import com.ashokvarma.bottomnavigation.TextBadgeItem; + import org.fdroid.fdroid.AppUpdateStatusManager; import org.fdroid.fdroid.AppUpdateStatusManager.AppUpdateStatus; import org.fdroid.fdroid.BuildConfig; @@ -276,7 +280,7 @@ public class MainActivity extends AppCompatActivity implements BottomNavigationB super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == REQUEST_LOCATION_PERMISSIONS) { WifiStateChangeService.start(this, null); - SwapService.start(this); + ContextCompat.startForegroundService(this, new Intent(this, SwapService.class)); } else if (requestCode == REQUEST_STORAGE_PERMISSIONS) { Toast.makeText(this, this.getString(R.string.scan_removable_storage_toast, ""), From fb8922b252b08deca030093dcd3c99f3d9ded91c Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Wed, 22 Jul 2020 08:10:03 +0530 Subject: [PATCH 5/5] Use ContextCompat.getSystemService(). --- .../fdroid/fdroid/MainActivityEspressoTest.java | 3 ++- .../fdroid/fdroid/nearby/BonjourManager.java | 17 ++++++++++------- .../fdroid/fdroid/nearby/SelectAppsView.java | 2 +- .../org/fdroid/fdroid/nearby/SwapService.java | 3 ++- .../fdroid/fdroid/nearby/SwapSuccessView.java | 3 ++- .../fdroid/nearby/SwapWorkflowActivity.java | 5 +++-- .../org/fdroid/fdroid/nearby/TreeUriUtils.java | 5 +++-- .../fdroid/nearby/WifiStateChangeService.java | 3 ++- .../fdroid/views/main/NearbyViewBinder.java | 2 +- .../org/fdroid/fdroid/CleanCacheService.java | 4 +++- .../main/java/org/fdroid/fdroid/FDroidApp.java | 9 ++++++--- .../java/org/fdroid/fdroid/Preferences.java | 4 +++- .../java/org/fdroid/fdroid/UpdateService.java | 5 +++-- .../fdroid/net/ConnectivityMonitorService.java | 3 ++- .../views/AppSecurityPermissions.java | 2 +- .../views/InstallConfirmActivity.java | 6 +++--- .../qr/CameraCharacteristicsMinApiLevel21.java | 3 ++- .../fdroid/fdroid/views/AppDetailsActivity.java | 4 +++- .../fdroid/views/ManageReposActivity.java | 5 +++-- .../fdroid/views/apps/AppListActivity.java | 7 ++++--- 20 files changed, 59 insertions(+), 36 deletions(-) diff --git a/app/src/androidTest/java/org/fdroid/fdroid/MainActivityEspressoTest.java b/app/src/androidTest/java/org/fdroid/fdroid/MainActivityEspressoTest.java index 2f1b5877f..ec0b1f0f0 100644 --- a/app/src/androidTest/java/org/fdroid/fdroid/MainActivityEspressoTest.java +++ b/app/src/androidTest/java/org/fdroid/fdroid/MainActivityEspressoTest.java @@ -6,6 +6,7 @@ import android.app.Instrumentation; import android.content.Context; import android.os.Build; +import androidx.core.content.ContextCompat; import androidx.test.core.app.ApplicationProvider; import androidx.test.filters.LargeTest; import androidx.test.platform.app.InstrumentationRegistry; @@ -103,7 +104,7 @@ public class MainActivityEspressoTest { Context context = instrumentation.getTargetContext(); ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); - ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); + ActivityManager activityManager = ContextCompat.getSystemService(context, ActivityManager.class); activityManager.getMemoryInfo(mi); long percentAvail = mi.availMem / mi.totalMem; Log.i(TAG, "RAM: " + mi.availMem + " / " + mi.totalMem + " = " + percentAvail); diff --git a/app/src/full/java/org/fdroid/fdroid/nearby/BonjourManager.java b/app/src/full/java/org/fdroid/fdroid/nearby/BonjourManager.java index 0e319f96d..bd48e1473 100644 --- a/app/src/full/java/org/fdroid/fdroid/nearby/BonjourManager.java +++ b/app/src/full/java/org/fdroid/fdroid/nearby/BonjourManager.java @@ -7,23 +7,27 @@ import android.os.Handler; import android.os.HandlerThread; import android.os.Message; import android.os.Process; -import androidx.localbroadcastmanager.content.LocalBroadcastManager; import android.text.TextUtils; import android.util.Log; + +import androidx.core.content.ContextCompat; +import androidx.localbroadcastmanager.content.LocalBroadcastManager; + import org.fdroid.fdroid.FDroidApp; import org.fdroid.fdroid.Preferences; import org.fdroid.fdroid.Utils; import org.fdroid.fdroid.nearby.peers.BonjourPeer; -import javax.jmdns.JmDNS; -import javax.jmdns.ServiceEvent; -import javax.jmdns.ServiceInfo; -import javax.jmdns.ServiceListener; import java.io.IOException; import java.lang.ref.WeakReference; import java.net.InetAddress; import java.util.HashMap; +import javax.jmdns.JmDNS; +import javax.jmdns.ServiceEvent; +import javax.jmdns.ServiceInfo; +import javax.jmdns.ServiceListener; + /** * Manage {@link JmDNS} in a {@link HandlerThread}. The start process is in * {@link HandlerThread#onLooperPrepared()} so that it is always started before @@ -119,8 +123,7 @@ public class BonjourManager { } sendBroadcast(STATUS_STARTING, null); - final WifiManager wifiManager = (WifiManager) context.getApplicationContext() - .getSystemService(Context.WIFI_SERVICE); + final WifiManager wifiManager = ContextCompat.getSystemService(context, WifiManager.class); handlerThread = new HandlerThread("BonjourManager", Process.THREAD_PRIORITY_LESS_FAVORABLE) { @Override protected void onLooperPrepared() { diff --git a/app/src/full/java/org/fdroid/fdroid/nearby/SelectAppsView.java b/app/src/full/java/org/fdroid/fdroid/nearby/SelectAppsView.java index 6ba6cb4c4..f0409ec9a 100644 --- a/app/src/full/java/org/fdroid/fdroid/nearby/SelectAppsView.java +++ b/app/src/full/java/org/fdroid/fdroid/nearby/SelectAppsView.java @@ -141,7 +141,7 @@ public class SelectAppsView extends SwapView implements LoaderManager.LoaderCall private LayoutInflater getInflater(Context context) { if (inflater == null) { Context themedContext = new ContextThemeWrapper(context, R.style.SwapTheme_AppList_ListItem); - inflater = (LayoutInflater) themedContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + inflater = ContextCompat.getSystemService(themedContext, LayoutInflater.class); } return inflater; } diff --git a/app/src/full/java/org/fdroid/fdroid/nearby/SwapService.java b/app/src/full/java/org/fdroid/fdroid/nearby/SwapService.java index 5c808c7f5..5f88588af 100644 --- a/app/src/full/java/org/fdroid/fdroid/nearby/SwapService.java +++ b/app/src/full/java/org/fdroid/fdroid/nearby/SwapService.java @@ -21,6 +21,7 @@ import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.app.NotificationCompat; +import androidx.core.content.ContextCompat; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import org.fdroid.fdroid.FDroidApp; @@ -365,7 +366,7 @@ public class SwapService extends Service { new IntentFilter(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED)); } - wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); + wifiManager = ContextCompat.getSystemService(getApplicationContext(), WifiManager.class); if (wifiManager != null) { SwapService.putWifiEnabledBeforeSwap(wifiManager.isWifiEnabled()); } diff --git a/app/src/full/java/org/fdroid/fdroid/nearby/SwapSuccessView.java b/app/src/full/java/org/fdroid/fdroid/nearby/SwapSuccessView.java index acdb8411d..bc19e2e73 100644 --- a/app/src/full/java/org/fdroid/fdroid/nearby/SwapSuccessView.java +++ b/app/src/full/java/org/fdroid/fdroid/nearby/SwapSuccessView.java @@ -14,6 +14,7 @@ import android.os.Bundle; import android.os.Handler; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; import androidx.loader.app.LoaderManager; import androidx.loader.content.CursorLoader; import androidx.loader.content.Loader; @@ -361,7 +362,7 @@ public class SwapSuccessView extends SwapView implements LoaderManager.LoaderCal @NonNull private LayoutInflater getInflater(Context context) { if (inflater == null) { - inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + inflater = ContextCompat.getSystemService(context, LayoutInflater.class); } return inflater; } diff --git a/app/src/full/java/org/fdroid/fdroid/nearby/SwapWorkflowActivity.java b/app/src/full/java/org/fdroid/fdroid/nearby/SwapWorkflowActivity.java index 56ce77818..493a96d25 100644 --- a/app/src/full/java/org/fdroid/fdroid/nearby/SwapWorkflowActivity.java +++ b/app/src/full/java/org/fdroid/fdroid/nearby/SwapWorkflowActivity.java @@ -225,7 +225,7 @@ public class SwapWorkflowActivity extends AppCompatActivity { localBroadcastManager.registerReceiver(downloaderInterruptedReceiver, new IntentFilter(Downloader.ACTION_INTERRUPTED)); - wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); + wifiManager = ContextCompat.getSystemService(getApplicationContext(), WifiManager.class); wifiApControl = WifiApControl.getInstance(this); bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); @@ -490,7 +490,8 @@ public class SwapWorkflowActivity extends AppCompatActivity { getSwapService().initTimer(); container.removeAllViews(); - View view = ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(viewRes, container, false); + View view = ContextCompat.getSystemService(this, LayoutInflater.class) + .inflate(viewRes, container, false); currentView = (SwapView) view; currentView.setLayoutResId(viewRes); currentSwapViewLayoutRes = viewRes; diff --git a/app/src/full/java/org/fdroid/fdroid/nearby/TreeUriUtils.java b/app/src/full/java/org/fdroid/fdroid/nearby/TreeUriUtils.java index 2770b0650..999495784 100644 --- a/app/src/full/java/org/fdroid/fdroid/nearby/TreeUriUtils.java +++ b/app/src/full/java/org/fdroid/fdroid/nearby/TreeUriUtils.java @@ -7,7 +7,9 @@ import android.net.Uri; import android.os.Build; import android.os.storage.StorageManager; import android.provider.DocumentsContract; + import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; import java.io.File; import java.lang.reflect.Array; @@ -47,8 +49,7 @@ public final class TreeUriUtils { private static String getVolumePath(final String volumeId, Context context) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return null; try { - StorageManager mStorageManager = - (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); + StorageManager mStorageManager = ContextCompat.getSystemService(context, StorageManager.class); Class storageVolumeClazz = Class.forName("android.os.storage.StorageVolume"); Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList"); Method getUuid = storageVolumeClazz.getMethod("getUuid"); diff --git a/app/src/full/java/org/fdroid/fdroid/nearby/WifiStateChangeService.java b/app/src/full/java/org/fdroid/fdroid/nearby/WifiStateChangeService.java index 8bf0ba673..70a0549c7 100644 --- a/app/src/full/java/org/fdroid/fdroid/nearby/WifiStateChangeService.java +++ b/app/src/full/java/org/fdroid/fdroid/nearby/WifiStateChangeService.java @@ -11,6 +11,7 @@ import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Build; import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import android.text.TextUtils; import android.util.Log; @@ -88,7 +89,7 @@ public class WifiStateChangeService extends IntentService { } Utils.debugLog(TAG, "WiFi change service started."); NetworkInfo ni = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); - wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE); + wifiManager = ContextCompat.getSystemService(getApplicationContext(), WifiManager.class); wifiState = wifiManager.getWifiState(); Utils.debugLog(TAG, "ni == " + ni + " wifiState == " + printWifiState(wifiState)); if (ni == null diff --git a/app/src/full/java/org/fdroid/fdroid/views/main/NearbyViewBinder.java b/app/src/full/java/org/fdroid/fdroid/views/main/NearbyViewBinder.java index a2331c1d2..21c5b7bb5 100644 --- a/app/src/full/java/org/fdroid/fdroid/views/main/NearbyViewBinder.java +++ b/app/src/full/java/org/fdroid/fdroid/views/main/NearbyViewBinder.java @@ -161,7 +161,7 @@ public class NearbyViewBinder { storageVolumeText.setVisibility(View.GONE); requestStorageVolume.setVisibility(View.GONE); - final StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); + final StorageManager storageManager = ContextCompat.getSystemService(context, StorageManager.class); for (final StorageVolume storageVolume : storageManager.getStorageVolumes()) { if (storageVolume.isRemovable() && !storageVolume.isPrimary()) { Log.i(TAG, "StorageVolume: " + storageVolume); diff --git a/app/src/main/java/org/fdroid/fdroid/CleanCacheService.java b/app/src/main/java/org/fdroid/fdroid/CleanCacheService.java index 7131a5ae5..618df2f10 100644 --- a/app/src/main/java/org/fdroid/fdroid/CleanCacheService.java +++ b/app/src/main/java/org/fdroid/fdroid/CleanCacheService.java @@ -12,6 +12,8 @@ import android.os.Process; import android.os.SystemClock; import androidx.annotation.NonNull; import androidx.core.app.JobIntentService; +import androidx.core.content.ContextCompat; + import org.apache.commons.io.FileUtils; import org.fdroid.fdroid.installer.ApkCache; @@ -58,7 +60,7 @@ public class CleanCacheService extends JobIntentService { SystemClock.elapsedRealtime() + 5000, interval, pending); } else { Utils.debugLog(TAG, "Using android-21 JobScheduler for updates"); - JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); + JobScheduler jobScheduler = ContextCompat.getSystemService(context, JobScheduler.class); ComponentName componentName = new ComponentName(context, CleanCacheJobService.class); JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, componentName) .setRequiresDeviceIdle(true) diff --git a/app/src/main/java/org/fdroid/fdroid/FDroidApp.java b/app/src/main/java/org/fdroid/fdroid/FDroidApp.java index 9d9769380..442ef01fa 100644 --- a/app/src/main/java/org/fdroid/fdroid/FDroidApp.java +++ b/app/src/main/java/org/fdroid/fdroid/FDroidApp.java @@ -42,6 +42,8 @@ import android.os.Environment; import android.os.StrictMode; import androidx.annotation.Nullable; import androidx.collection.LongSparseArray; +import androidx.core.content.ContextCompat; + import android.text.TextUtils; import android.util.Base64; import android.util.Log; @@ -428,7 +430,8 @@ public class FDroidApp extends Application { // submitted via app's git repos, so anyone with commit privs there could submit // exploits hidden in images. Luckily, F-Droid doesn't need EXIF at all, and // that is where the JPEG/PNG vulns have been. So it can be entirely stripped. - Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); + Display display = ContextCompat.getSystemService(this, WindowManager.class) + .getDefaultDisplay(); int maxSize = GL10.GL_MAX_TEXTURE_SIZE; // see ImageScaleType.NONE_SAFE javadoc int width = display.getWidth(); if (width > maxSize) { @@ -554,7 +557,7 @@ public class FDroidApp extends Application { * for end users than experiencing a deterministic crash every time F-Droid is started. */ private boolean isAcraProcess() { - ActivityManager manager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE); + ActivityManager manager = ContextCompat.getSystemService(this, ActivityManager.class); List processes = manager.getRunningAppProcesses(); if (processes == null) { return false; @@ -578,7 +581,7 @@ public class FDroidApp extends Application { @TargetApi(16) private int getThreadPoolSize() { if (Build.VERSION.SDK_INT >= 16) { - ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); + ActivityManager activityManager = ContextCompat.getSystemService(this, ActivityManager.class); ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo(); if (activityManager != null) { activityManager.getMemoryInfo(memInfo); diff --git a/app/src/main/java/org/fdroid/fdroid/Preferences.java b/app/src/main/java/org/fdroid/fdroid/Preferences.java index f614a815c..7a4839605 100644 --- a/app/src/main/java/org/fdroid/fdroid/Preferences.java +++ b/app/src/main/java/org/fdroid/fdroid/Preferences.java @@ -28,6 +28,8 @@ import android.content.SharedPreferences; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Build; + +import androidx.core.content.ContextCompat; import androidx.preference.PreferenceManager; import android.text.format.DateUtils; import android.util.Log; @@ -457,7 +459,7 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh * Some users never use WiFi, this lets us check for that state on first run. */ public void setDefaultForDataOnlyConnection(Context context) { - ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + ConnectivityManager cm = ContextCompat.getSystemService(context, ConnectivityManager.class); if (cm == null) { return; } diff --git a/app/src/main/java/org/fdroid/fdroid/UpdateService.java b/app/src/main/java/org/fdroid/fdroid/UpdateService.java index 72617f2af..fb3c067fd 100644 --- a/app/src/main/java/org/fdroid/fdroid/UpdateService.java +++ b/app/src/main/java/org/fdroid/fdroid/UpdateService.java @@ -41,6 +41,7 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.core.app.JobIntentService; import androidx.core.app.NotificationCompat; +import androidx.core.content.ContextCompat; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import org.fdroid.fdroid.data.Apk; @@ -164,7 +165,7 @@ public class UpdateService extends JobIntentService { } } else { Utils.debugLog(TAG, "Using android-21 JobScheduler for updates"); - JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); + JobScheduler jobScheduler = ContextCompat.getSystemService(context, JobScheduler.class); ComponentName componentName = new ComponentName(context, UpdateJobService.class); JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, componentName) .setRequiresDeviceIdle(true) @@ -257,7 +258,7 @@ public class UpdateService extends JobIntentService { super.onCreate(); updateService = this; - notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + notificationManager = ContextCompat.getSystemService(this, NotificationManager.class); notificationBuilder = new NotificationCompat.Builder(this, NotificationHelper.CHANNEL_UPDATES) .setSmallIcon(R.drawable.ic_refresh) diff --git a/app/src/main/java/org/fdroid/fdroid/net/ConnectivityMonitorService.java b/app/src/main/java/org/fdroid/fdroid/net/ConnectivityMonitorService.java index daac48e09..5df37b184 100644 --- a/app/src/main/java/org/fdroid/fdroid/net/ConnectivityMonitorService.java +++ b/app/src/main/java/org/fdroid/fdroid/net/ConnectivityMonitorService.java @@ -9,6 +9,7 @@ import android.net.NetworkInfo; import android.os.Build; import androidx.annotation.NonNull; import androidx.core.app.JobIntentService; +import androidx.core.content.ContextCompat; import androidx.core.net.ConnectivityManagerCompat; import android.util.Log; @@ -79,7 +80,7 @@ public class ConnectivityMonitorService extends JobIntentService { * cases when looking through the network devices, especially on bad ROMs. */ public static int getNetworkState(Context context) { - ConnectivityManager cm = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE); + ConnectivityManager cm = ContextCompat.getSystemService(context, ConnectivityManager.class);; if (cm == null) { return FLAG_NET_UNAVAILABLE; } diff --git a/app/src/main/java/org/fdroid/fdroid/privileged/views/AppSecurityPermissions.java b/app/src/main/java/org/fdroid/fdroid/privileged/views/AppSecurityPermissions.java index c6f0d3356..b70dfa3da 100644 --- a/app/src/main/java/org/fdroid/fdroid/privileged/views/AppSecurityPermissions.java +++ b/app/src/main/java/org/fdroid/fdroid/privileged/views/AppSecurityPermissions.java @@ -236,7 +236,7 @@ public class AppSecurityPermissions { private AppSecurityPermissions(Context context) { this.context = context; - inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + inflater = ContextCompat.getSystemService(this.context, LayoutInflater.class); pm = this.context.getPackageManager(); // Pick up from framework resources instead. newPermPrefix = this.context.getText(R.string.perms_new_perm_prefix); diff --git a/app/src/main/java/org/fdroid/fdroid/privileged/views/InstallConfirmActivity.java b/app/src/main/java/org/fdroid/fdroid/privileged/views/InstallConfirmActivity.java index cfff26048..3c2f315c9 100644 --- a/app/src/main/java/org/fdroid/fdroid/privileged/views/InstallConfirmActivity.java +++ b/app/src/main/java/org/fdroid/fdroid/privileged/views/InstallConfirmActivity.java @@ -18,13 +18,14 @@ package org.fdroid.fdroid.privileged.views; -import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnCancelListener; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.net.Uri; import android.os.Bundle; + +import androidx.core.content.ContextCompat; import androidx.fragment.app.FragmentActivity; import androidx.viewpager.widget.ViewPager; import android.view.LayoutInflater; @@ -118,8 +119,7 @@ public class InstallConfirmActivity extends FragmentActivity implements OnCancel final int n = perms.getPermissionCount(AppSecurityPermissions.WHICH_ALL); if (n > 0) { permVisible = true; - LayoutInflater inflater = (LayoutInflater) getSystemService( - Context.LAYOUT_INFLATER_SERVICE); + LayoutInflater inflater = ContextCompat.getSystemService(this, LayoutInflater.class); View root = inflater.inflate(R.layout.permissions_list, null); if (scrollView == null) { scrollView = (CaffeinatedScrollView) root.findViewById(R.id.scrollview); diff --git a/app/src/main/java/org/fdroid/fdroid/qr/CameraCharacteristicsMinApiLevel21.java b/app/src/main/java/org/fdroid/fdroid/qr/CameraCharacteristicsMinApiLevel21.java index df39d5b44..052f55817 100644 --- a/app/src/main/java/org/fdroid/fdroid/qr/CameraCharacteristicsMinApiLevel21.java +++ b/app/src/main/java/org/fdroid/fdroid/qr/CameraCharacteristicsMinApiLevel21.java @@ -9,6 +9,7 @@ import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.RequiresApi; +import androidx.core.content.ContextCompat; @RequiresApi(Build.VERSION_CODES.LOLLIPOP) public class CameraCharacteristicsMinApiLevel21 extends CameraCharacteristicsChecker { @@ -17,7 +18,7 @@ public class CameraCharacteristicsMinApiLevel21 extends CameraCharacteristicsChe private final CameraManager cameraManager; protected CameraCharacteristicsMinApiLevel21(final Context context) { - this.cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE); + this.cameraManager = ContextCompat.getSystemService(context, CameraManager.class); } @Override diff --git a/app/src/main/java/org/fdroid/fdroid/views/AppDetailsActivity.java b/app/src/main/java/org/fdroid/fdroid/views/AppDetailsActivity.java index 125cafa86..ef22a9f1f 100644 --- a/app/src/main/java/org/fdroid/fdroid/views/AppDetailsActivity.java +++ b/app/src/main/java/org/fdroid/fdroid/views/AppDetailsActivity.java @@ -25,6 +25,7 @@ import android.annotation.TargetApi; import android.app.Activity; import android.app.PendingIntent; import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; @@ -37,6 +38,7 @@ import android.os.Bundle; import android.os.Handler; import androidx.annotation.Nullable; import androidx.coordinatorlayout.widget.CoordinatorLayout; +import androidx.core.content.ContextCompat; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; @@ -702,7 +704,7 @@ public class AppDetailsActivity extends AppCompatActivity if (Build.VERSION.SDK_INT < 18) { return BluetoothAdapter.getDefaultAdapter(); } - return ((android.bluetooth.BluetoothManager) getSystemService(BLUETOOTH_SERVICE)).getAdapter(); + return ContextCompat.getSystemService(this, BluetoothManager.class).getAdapter(); } @Override diff --git a/app/src/main/java/org/fdroid/fdroid/views/ManageReposActivity.java b/app/src/main/java/org/fdroid/fdroid/views/ManageReposActivity.java index bddb0a15a..e633ae14b 100644 --- a/app/src/main/java/org/fdroid/fdroid/views/ManageReposActivity.java +++ b/app/src/main/java/org/fdroid/fdroid/views/ManageReposActivity.java @@ -182,7 +182,7 @@ public class ManageReposActivity extends AppCompatActivity public String getPrimaryClipAsText() { CharSequence text = null; - ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); + ClipboardManager clipboardManager = ContextCompat.getSystemService(this, ClipboardManager.class); if (clipboardManager.hasPrimaryClip()) { ClipData data = clipboardManager.getPrimaryClip(); if (data.getItemCount() > 0) { @@ -838,7 +838,8 @@ public class ManageReposActivity extends AppCompatActivity private void checkIfNewRepoOnSameWifi(NewRepoConfig newRepo) { // if this is a local repo, check we're on the same wifi if (!TextUtils.isEmpty(newRepo.getBssid())) { - WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); + WifiManager wifiManager = ContextCompat.getSystemService(getApplicationContext(), + WifiManager.class); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); String bssid = wifiInfo.getBSSID(); if (TextUtils.isEmpty(bssid)) { /* not all devices have wifi */ diff --git a/app/src/main/java/org/fdroid/fdroid/views/apps/AppListActivity.java b/app/src/main/java/org/fdroid/fdroid/views/apps/AppListActivity.java index 0ed47ebf7..78664ff87 100644 --- a/app/src/main/java/org/fdroid/fdroid/views/apps/AppListActivity.java +++ b/app/src/main/java/org/fdroid/fdroid/views/apps/AppListActivity.java @@ -20,7 +20,6 @@ package org.fdroid.fdroid.views.apps; -import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.graphics.Color; @@ -96,7 +95,8 @@ public class AppListActivity extends AppCompatActivity implements LoaderManager. public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH) { // Hide the keyboard (http://stackoverflow.com/a/1109108 (when pressing search) - InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); + InputMethodManager inputManager = ContextCompat.getSystemService(AppListActivity.this, + InputMethodManager.class); inputManager.hideSoftInputFromWindow(searchInput.getWindowToken(), 0); // Change focus from the search input to the app list. @@ -148,7 +148,8 @@ public class AppListActivity extends AppCompatActivity implements LoaderManager. searchInput.requestFocus(); if (!keyboardStateMonitor.isKeyboardVisible()) { InputMethodManager inputMethodManager = - (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + ContextCompat.getSystemService(AppListActivity.this, + InputMethodManager.class); inputMethodManager.toggleSoftInputFromWindow(v.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0); }