From 9225ec35b54c4026d1d3d8265b1db3fed4e07d7a Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 12 Dec 2023 16:07:48 +0100 Subject: [PATCH] Use ContextCompat --- .../android/activities/PeertubeBaseMainActivity.java | 7 ++----- .../java/app/fedilab/android/BaseMainActivity.java | 10 +++------- .../android/mastodon/activities/ComposeActivity.java | 6 +----- .../android/mastodon/activities/MediaActivity.java | 6 +----- .../android/mastodon/activities/ProfileActivity.java | 6 +----- .../mastodon/activities/admin/AdminActionActivity.java | 7 ++----- .../ui/fragment/timeline/FragmentMastodonContext.java | 7 ++----- .../timeline/FragmentMastodonDirectMessage.java | 6 +----- .../timeline/FragmentMastodonNotification.java | 10 +++------- .../ui/fragment/timeline/FragmentMastodonTimeline.java | 7 ++----- .../android/peertube/activities/PeertubeActivity.java | 6 +----- .../peertube/activities/PeertubeMainActivity.java | 7 ++----- .../android/peertube/services/RetrieveInfoService.java | 7 ++----- 13 files changed, 23 insertions(+), 69 deletions(-) diff --git a/app/src/fdroid/java/app/fedilab/android/activities/PeertubeBaseMainActivity.java b/app/src/fdroid/java/app/fedilab/android/activities/PeertubeBaseMainActivity.java index 0d0bddb4..1d25888c 100644 --- a/app/src/fdroid/java/app/fedilab/android/activities/PeertubeBaseMainActivity.java +++ b/app/src/fdroid/java/app/fedilab/android/activities/PeertubeBaseMainActivity.java @@ -25,6 +25,7 @@ import android.os.Handler; import android.os.Looper; import android.view.View; +import androidx.core.content.ContextCompat; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import java.io.IOException; @@ -148,11 +149,7 @@ public abstract class PeertubeBaseMainActivity extends BaseActivity implements C } } }; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - registerReceiver(manage_chromecast, new IntentFilter(Helper.RECEIVE_CAST_SETTINGS), Context.RECEIVER_NOT_EXPORTED); - } else { - registerReceiver(manage_chromecast, new IntentFilter(Helper.RECEIVE_CAST_SETTINGS)); - } + ContextCompat.registerReceiver(PeertubeBaseMainActivity.this, manage_chromecast, new IntentFilter(Helper.RECEIVE_CAST_SETTINGS), ContextCompat.RECEIVER_NOT_EXPORTED); } @Override diff --git a/app/src/main/java/app/fedilab/android/BaseMainActivity.java b/app/src/main/java/app/fedilab/android/BaseMainActivity.java index 991bdabf..75d4f2e5 100644 --- a/app/src/main/java/app/fedilab/android/BaseMainActivity.java +++ b/app/src/main/java/app/fedilab/android/BaseMainActivity.java @@ -73,6 +73,7 @@ import androidx.appcompat.widget.PopupMenu; import androidx.appcompat.widget.SearchView; import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityOptionsCompat; +import androidx.core.content.ContextCompat; import androidx.core.view.GravityCompat; import androidx.cursoradapter.widget.CursorAdapter; import androidx.fragment.app.Fragment; @@ -1529,13 +1530,8 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt binding.toolbarSearch.setOnSearchClickListener(v -> binding.tabLayout.setVisibility(View.VISIBLE)); //For receiving data from other activities - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - registerReceiver(broadcast_data, new IntentFilter(Helper.BROADCAST_DATA), Context.RECEIVER_NOT_EXPORTED); - registerReceiver(broadcast_error_message, new IntentFilter(Helper.INTENT_COMPOSE_ERROR_MESSAGE), Context.RECEIVER_NOT_EXPORTED); - } else { - registerReceiver(broadcast_data, new IntentFilter(Helper.BROADCAST_DATA)); - registerReceiver(broadcast_error_message, new IntentFilter(Helper.INTENT_COMPOSE_ERROR_MESSAGE)); - } + ContextCompat.registerReceiver(BaseMainActivity.this, broadcast_data, new IntentFilter(Helper.BROADCAST_DATA), ContextCompat.RECEIVER_NOT_EXPORTED); + ContextCompat.registerReceiver(BaseMainActivity.this, broadcast_error_message, new IntentFilter(Helper.INTENT_COMPOSE_ERROR_MESSAGE), ContextCompat.RECEIVER_NOT_EXPORTED); if (emojis == null || !emojis.containsKey(BaseMainActivity.currentInstance) || emojis.get(BaseMainActivity.currentInstance) == null) { new Thread(() -> { try { diff --git a/app/src/main/java/app/fedilab/android/mastodon/activities/ComposeActivity.java b/app/src/main/java/app/fedilab/android/mastodon/activities/ComposeActivity.java index 04460aab..57bfb246 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/activities/ComposeActivity.java +++ b/app/src/main/java/app/fedilab/android/mastodon/activities/ComposeActivity.java @@ -734,11 +734,7 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana } } MastodonHelper.loadPPMastodon(binding.profilePicture, account.mastodon_account); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - registerReceiver(imageReceiver, new IntentFilter(Helper.INTENT_SEND_MODIFIED_IMAGE), android.content.Context.RECEIVER_NOT_EXPORTED); - } else { - registerReceiver(imageReceiver, new IntentFilter(Helper.INTENT_SEND_MODIFIED_IMAGE)); - } + ContextCompat.registerReceiver(ComposeActivity.this, imageReceiver, new IntentFilter(Helper.INTENT_SEND_MODIFIED_IMAGE), ContextCompat.RECEIVER_NOT_EXPORTED); if (timer != null) { timer.scheduleAtFixedRate(new TimerTask() { @Override diff --git a/app/src/main/java/app/fedilab/android/mastodon/activities/MediaActivity.java b/app/src/main/java/app/fedilab/android/mastodon/activities/MediaActivity.java index a7bfb6e2..249230f5 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/activities/MediaActivity.java +++ b/app/src/main/java/app/fedilab/android/mastodon/activities/MediaActivity.java @@ -140,11 +140,7 @@ public class MediaActivity extends BaseTransparentActivity implements OnDownload binding.mediaViewpager.setAdapter(mPagerAdapter); binding.mediaViewpager.setSaveEnabled(false); binding.mediaViewpager.setCurrentItem(mediaPosition - 1); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - registerReceiver(onDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE), android.content.Context.RECEIVER_NOT_EXPORTED); - } else { - registerReceiver(onDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); - } + ContextCompat.registerReceiver(MediaActivity.this, onDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE), ContextCompat.RECEIVER_NOT_EXPORTED); String description = attachments.get(mediaPosition - 1).description; handler = new Handler(); if (attachments.get(mediaPosition - 1).status != null) { diff --git a/app/src/main/java/app/fedilab/android/mastodon/activities/ProfileActivity.java b/app/src/main/java/app/fedilab/android/mastodon/activities/ProfileActivity.java index 2f348888..5e293f14 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/activities/ProfileActivity.java +++ b/app/src/main/java/app/fedilab/android/mastodon/activities/ProfileActivity.java @@ -206,11 +206,7 @@ public class ProfileActivity extends BaseActivity { } //Check if account is homeMuted accountsVM.isMuted(currentAccount, account).observe(this, result -> homeMuted = result != null && result); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - registerReceiver(broadcast_data, new IntentFilter(Helper.BROADCAST_DATA), android.content.Context.RECEIVER_NOT_EXPORTED); - } else { - registerReceiver(broadcast_data, new IntentFilter(Helper.BROADCAST_DATA)); - } + ContextCompat.registerReceiver(ProfileActivity.this, broadcast_data, new IntentFilter(Helper.BROADCAST_DATA), ContextCompat.RECEIVER_NOT_EXPORTED); } diff --git a/app/src/main/java/app/fedilab/android/mastodon/activities/admin/AdminActionActivity.java b/app/src/main/java/app/fedilab/android/mastodon/activities/admin/AdminActionActivity.java index b3a313bb..aa82c40a 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/activities/admin/AdminActionActivity.java +++ b/app/src/main/java/app/fedilab/android/mastodon/activities/admin/AdminActionActivity.java @@ -30,6 +30,7 @@ import android.view.MenuItem; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; +import androidx.core.content.ContextCompat; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import androidx.localbroadcastmanager.content.LocalBroadcastManager; @@ -86,11 +87,7 @@ public class AdminActionActivity extends BaseBarActivity { setContentView(binding.getRoot()); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - registerReceiver(mReceiver, new IntentFilter(Helper.BROADCAST_DATA), android.content.Context.RECEIVER_NOT_EXPORTED); - } else { - registerReceiver(mReceiver, new IntentFilter(Helper.BROADCAST_DATA)); - } + ContextCompat.registerReceiver(AdminActionActivity.this, mReceiver, new IntentFilter(Helper.BROADCAST_DATA), ContextCompat.RECEIVER_NOT_EXPORTED); if (getSupportActionBar() != null) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); } diff --git a/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonContext.java b/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonContext.java index 795dafe6..43bbc24a 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonContext.java +++ b/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonContext.java @@ -27,6 +27,7 @@ import android.view.View; import android.view.ViewGroup; import androidx.annotation.NonNull; +import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; import androidx.localbroadcastmanager.content.LocalBroadcastManager; @@ -197,11 +198,7 @@ public class FragmentMastodonContext extends Fragment { .observe(getViewLifecycleOwner(), this::initializeContextView); } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - requireActivity().registerReceiver(receive_action, new IntentFilter(Helper.RECEIVE_STATUS_ACTION), android.content.Context.RECEIVER_NOT_EXPORTED); - } else { - requireActivity().registerReceiver(receive_action, new IntentFilter(Helper.RECEIVE_STATUS_ACTION)); - } + ContextCompat.registerReceiver(requireActivity(), receive_action, new IntentFilter(Helper.RECEIVE_STATUS_ACTION), ContextCompat.RECEIVER_NOT_EXPORTED); return binding.getRoot(); } diff --git a/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonDirectMessage.java b/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonDirectMessage.java index 4d791323..c241d188 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonDirectMessage.java +++ b/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonDirectMessage.java @@ -207,11 +207,7 @@ public class FragmentMastodonDirectMessage extends Fragment { statusCompose.text = binding.text.getText().toString(); onSubmit(prepareDraft(statusCompose, MainActivity.currentInstance, MainActivity.currentUserID)); }); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - requireActivity().registerReceiver(broadcast_data, new IntentFilter(Helper.BROADCAST_DATA), android.content.Context.RECEIVER_NOT_EXPORTED); - } else { - requireActivity().registerReceiver(broadcast_data, new IntentFilter(Helper.BROADCAST_DATA)); - } + ContextCompat.registerReceiver(requireActivity(), broadcast_data, new IntentFilter(Helper.BROADCAST_DATA), ContextCompat.RECEIVER_NOT_EXPORTED); binding.text.setKeyBoardInputCallbackListener((inputContentInfo, flags, opts) -> { if (inputContentInfo != null) { Uri uri = inputContentInfo.getContentUri(); diff --git a/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonNotification.java b/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonNotification.java index 5ecc42fa..92c4afb0 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonNotification.java +++ b/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonNotification.java @@ -28,6 +28,7 @@ import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; import androidx.localbroadcastmanager.content.LocalBroadcastManager; @@ -206,13 +207,8 @@ public class FragmentMastodonNotification extends Fragment implements Notificati } aggregateNotification = false; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - requireActivity().registerReceiver(receive_action, new IntentFilter(Helper.RECEIVE_STATUS_ACTION), android.content.Context.RECEIVER_NOT_EXPORTED); - requireActivity().registerReceiver(receive_refresh, new IntentFilter(Helper.RECEIVE_REFRESH_NOTIFICATIONS_ACTION), android.content.Context.RECEIVER_NOT_EXPORTED); - } else { - requireActivity().registerReceiver(receive_action, new IntentFilter(Helper.RECEIVE_STATUS_ACTION)); - requireActivity().registerReceiver(receive_refresh, new IntentFilter(Helper.RECEIVE_REFRESH_NOTIFICATIONS_ACTION)); - } + ContextCompat.registerReceiver(requireActivity(), receive_action, new IntentFilter(Helper.RECEIVE_STATUS_ACTION), ContextCompat.RECEIVER_NOT_EXPORTED); + ContextCompat.registerReceiver(requireActivity(), receive_refresh, new IntentFilter(Helper.RECEIVE_REFRESH_NOTIFICATIONS_ACTION), ContextCompat.RECEIVER_NOT_EXPORTED); return root; } diff --git a/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonTimeline.java b/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonTimeline.java index 2978762c..593dde70 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonTimeline.java +++ b/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonTimeline.java @@ -33,6 +33,7 @@ import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; import androidx.localbroadcastmanager.content.LocalBroadcastManager; @@ -454,11 +455,7 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - requireActivity().registerReceiver(receive_action, new IntentFilter(Helper.RECEIVE_STATUS_ACTION), android.content.Context.RECEIVER_NOT_EXPORTED); - } else { - requireActivity().registerReceiver(receive_action, new IntentFilter(Helper.RECEIVE_STATUS_ACTION)); - } + ContextCompat.registerReceiver(requireActivity(), receive_action, new IntentFilter(Helper.RECEIVE_STATUS_ACTION), ContextCompat.RECEIVER_NOT_EXPORTED); binding = FragmentPaginationBinding.inflate(inflater, container, false); return binding.getRoot(); } diff --git a/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeActivity.java b/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeActivity.java index 957e04cc..ea6b87fc 100644 --- a/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeActivity.java +++ b/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeActivity.java @@ -1591,11 +1591,7 @@ public class PeertubeActivity extends BasePeertubeActivity implements CommentLis } } }; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - registerReceiver(mPowerKeyReceiver, theFilter, android.content.Context.RECEIVER_NOT_EXPORTED); - } else { - registerReceiver(mPowerKeyReceiver, theFilter); - } + ContextCompat.registerReceiver(PeertubeActivity.this, mPowerKeyReceiver, theFilter, ContextCompat.RECEIVER_NOT_EXPORTED); } private void unregisterReceiver() { diff --git a/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeMainActivity.java b/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeMainActivity.java index 12422cfd..110589d6 100644 --- a/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeMainActivity.java +++ b/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeMainActivity.java @@ -55,6 +55,7 @@ import androidx.appcompat.app.AlertDialog; import androidx.appcompat.widget.SearchView; import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.TooltipCompat; +import androidx.core.content.ContextCompat; import androidx.core.view.GravityCompat; import androidx.drawerlayout.widget.DrawerLayout; import androidx.fragment.app.Fragment; @@ -163,11 +164,7 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity { binding = super.binding; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - registerReceiver(broadcast_data, new IntentFilter(app.fedilab.android.mastodon.helper.Helper.BROADCAST_DATA), android.content.Context.RECEIVER_NOT_EXPORTED); - } else { - registerReceiver(broadcast_data, new IntentFilter(app.fedilab.android.mastodon.helper.Helper.BROADCAST_DATA)); - } + ContextCompat.registerReceiver(PeertubeMainActivity.this, broadcast_data, new IntentFilter(app.fedilab.android.mastodon.helper.Helper.BROADCAST_DATA), ContextCompat.RECEIVER_NOT_EXPORTED); Intent intentActvity = getIntent(); if (intentActvity != null) { diff --git a/app/src/main/java/app/fedilab/android/peertube/services/RetrieveInfoService.java b/app/src/main/java/app/fedilab/android/peertube/services/RetrieveInfoService.java index 2aaf8667..e90bda3d 100644 --- a/app/src/main/java/app/fedilab/android/peertube/services/RetrieveInfoService.java +++ b/app/src/main/java/app/fedilab/android/peertube/services/RetrieveInfoService.java @@ -30,6 +30,7 @@ import android.os.Looper; import androidx.annotation.Nullable; import androidx.core.app.NotificationCompat; +import androidx.core.content.ContextCompat; import java.util.LinkedHashMap; import java.util.Objects; @@ -51,11 +52,7 @@ public class RetrieveInfoService extends Service implements NetworkStateReceiver super.onCreate(); networkStateReceiver = new NetworkStateReceiver(); networkStateReceiver.addListener(this); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION), android.content.Context.RECEIVER_NOT_EXPORTED); - } else { - registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION)); - } + ContextCompat.registerReceiver(RetrieveInfoService.this, networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION), ContextCompat.RECEIVER_NOT_EXPORTED); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, getString(R.string.notification_channel_name),