From f7f34c6bdd14f2d9a230484af04d3335ee6c4bae Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 7 May 2023 10:13:14 +0200 Subject: [PATCH] fix regressions after target sdk 33 update --- build.gradle | 10 +++---- .../ui/ConversationFragment.java | 3 +-- .../ui/ConversationsActivity.java | 26 +++++++++++++++---- .../siacs/conversations/ui/XmppActivity.java | 3 ++- .../ui/adapter/MessageAdapter.java | 3 ++- .../conversations/ui/service/AudioPlayer.java | 2 +- .../conversations/utils/Compatibility.java | 4 +-- .../conversations/utils/PermissionUtils.java | 2 +- 8 files changed, 35 insertions(+), 18 deletions(-) diff --git a/build.gradle b/build.gradle index 4b12615e7..d1b21e3f5 100644 --- a/build.gradle +++ b/build.gradle @@ -48,8 +48,8 @@ dependencies { implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0' implementation 'com.google.android.material:material:1.8.0' - implementation "androidx.emoji2:emoji2:1.3.0" - freeImplementation "androidx.emoji2:emoji2-bundled:1.3.0" + implementation "androidx.emoji2:emoji2:1.2.0" + freeImplementation "androidx.emoji2:emoji2-bundled:1.2.0" implementation 'org.bouncycastle:bcmail-jdk15on:1.64' //zxing stopped supporting Java 7 so we have to stick with 3.3.3 @@ -90,9 +90,9 @@ android { defaultConfig { minSdkVersion 21 - targetSdkVersion 32 - versionCode 42056 - versionName "2.12.4" + targetSdkVersion 33 + versionCode 42058 + versionName "2.12.5" archivesBaseName += "-$versionName" applicationId "eu.siacs.conversations" resValue "string", "applicationId", applicationId diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index 0f7e9073c..11348b6b7 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -1965,8 +1965,7 @@ public class ConversationFragment extends XmppFragment if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { final List missingPermissions = new ArrayList<>(); for (String permission : permissions) { - if (Config.ONLY_INTERNAL_STORAGE - && permission.equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { + if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU || Config.ONLY_INTERNAL_STORAGE) && permission.equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { continue; } if (activity.checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) { diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java b/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java index c31c3464b..ba231c584 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java @@ -32,6 +32,7 @@ package eu.siacs.conversations.ui; import static eu.siacs.conversations.ui.ConversationFragment.REQUEST_DECRYPT_PGP; +import android.Manifest; import android.annotation.SuppressLint; import android.app.Activity; import android.app.Fragment; @@ -42,6 +43,7 @@ import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.provider.Settings; import android.util.Log; @@ -54,6 +56,7 @@ import androidx.annotation.IdRes; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AlertDialog; +import androidx.core.app.ActivityCompat; import androidx.databinding.DataBindingUtil; import org.openintents.openpgp.util.OpenPgpApi; @@ -207,7 +210,9 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio if (ExceptionHelper.checkForCrash(this)) { return; } - openBatteryOptimizationDialogIfNeeded(); + if (openBatteryOptimizationDialogIfNeeded()) { + return; + } } } @@ -220,16 +225,16 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio getPreferences().edit().putBoolean(getBatteryOptimizationPreferenceKey(), false).apply(); } - private void openBatteryOptimizationDialogIfNeeded() { + private boolean openBatteryOptimizationDialogIfNeeded() { if (isOptimizingBattery() && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M && getPreferences().getBoolean(getBatteryOptimizationPreferenceKey(), true)) { - AlertDialog.Builder builder = new AlertDialog.Builder(this); + final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.battery_optimizations_enabled); builder.setMessage(getString(R.string.battery_optimizations_enabled_dialog, getString(R.string.app_name))); builder.setPositiveButton(R.string.next, (dialog, which) -> { - Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); - Uri uri = Uri.parse("package:" + getPackageName()); + final Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); + final Uri uri = Uri.parse("package:" + getPackageName()); intent.setData(uri); try { startActivityForResult(intent, REQUEST_BATTERY_OP); @@ -241,6 +246,14 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio final AlertDialog dialog = builder.create(); dialog.setCanceledOnTouchOutside(false); dialog.show(); + return true; + } + return false; + } + + private void requestNotificationPermissionIfNeeded() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { + requestPermissions(new String[]{Manifest.permission.POST_NOTIFICATIONS}, REQUEST_POST_NOTIFICATION); } } @@ -305,6 +318,9 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio } else { handleNegativeActivityResult(activityResult.requestCode); } + if (activityResult.requestCode == REQUEST_BATTERY_OP) { + requestNotificationPermissionIfNeeded(); + } } private void handleNegativeActivityResult(int requestCode) { diff --git a/src/main/java/eu/siacs/conversations/ui/XmppActivity.java b/src/main/java/eu/siacs/conversations/ui/XmppActivity.java index b22bdf52a..c6a6bc630 100644 --- a/src/main/java/eu/siacs/conversations/ui/XmppActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/XmppActivity.java @@ -98,6 +98,7 @@ public abstract class XmppActivity extends ActionBarActivity { protected static final int REQUEST_INVITE_TO_CONVERSATION = 0x0102; protected static final int REQUEST_CHOOSE_PGP_ID = 0x0103; protected static final int REQUEST_BATTERY_OP = 0x49ff; + protected static final int REQUEST_POST_NOTIFICATION = 0x50ff; public XmppConnectionService xmppConnectionService; public boolean xmppConnectionServiceBound = false; @@ -809,7 +810,7 @@ public abstract class XmppActivity extends ActionBarActivity { } protected boolean hasStoragePermission(int requestCode) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, requestCode); return false; diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java index 93b3dd303..c33f3fa07 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -6,6 +6,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.graphics.Typeface; +import android.os.Build; import android.preference.PreferenceManager; import android.text.Spannable; import android.text.SpannableString; @@ -873,7 +874,7 @@ public class MessageAdapter extends ArrayAdapter { } public void openDownloadable(Message message) { - if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU && ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ConversationFragment.registerPendingMessage(activity, message); ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, ConversationsActivity.REQUEST_OPEN_MESSAGE); return; diff --git a/src/main/java/eu/siacs/conversations/ui/service/AudioPlayer.java b/src/main/java/eu/siacs/conversations/ui/service/AudioPlayer.java index 08edfadbe..8475688a6 100644 --- a/src/main/java/eu/siacs/conversations/ui/service/AudioPlayer.java +++ b/src/main/java/eu/siacs/conversations/ui/service/AudioPlayer.java @@ -145,7 +145,7 @@ public class AudioPlayer implements View.OnClickListener, MediaPlayer.OnCompleti } private void startStop(ImageButton playPause) { - if (ContextCompat.checkSelfPermission(messageAdapter.getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU && ContextCompat.checkSelfPermission(messageAdapter.getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { pendingOnClickView.push(new WeakReference<>(playPause)); ActivityCompat.requestPermissions(messageAdapter.getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, ConversationsActivity.REQUEST_PLAY_PAUSE); return; diff --git a/src/main/java/eu/siacs/conversations/utils/Compatibility.java b/src/main/java/eu/siacs/conversations/utils/Compatibility.java index b1145794d..04ffc7763 100644 --- a/src/main/java/eu/siacs/conversations/utils/Compatibility.java +++ b/src/main/java/eu/siacs/conversations/utils/Compatibility.java @@ -40,8 +40,8 @@ public class Compatibility { private static final List UNUSED_SETTINGS_PRE_TWENTYSIX = Collections.singletonList("message_notification_settings"); - public static boolean hasStoragePermission(Context context) { - return Build.VERSION.SDK_INT < Build.VERSION_CODES.M + public static boolean hasStoragePermission(final Context context) { + return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU || ContextCompat.checkSelfPermission( context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED; diff --git a/src/main/java/eu/siacs/conversations/utils/PermissionUtils.java b/src/main/java/eu/siacs/conversations/utils/PermissionUtils.java index 004676156..dd74dc57c 100644 --- a/src/main/java/eu/siacs/conversations/utils/PermissionUtils.java +++ b/src/main/java/eu/siacs/conversations/utils/PermissionUtils.java @@ -72,7 +72,7 @@ public class PermissionUtils { public static boolean hasPermission( final Activity activity, final List permissions, final int requestCode) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { final ImmutableList.Builder missingPermissions = new ImmutableList.Builder<>(); for (final String permission : permissions) { if (ActivityCompat.checkSelfPermission(activity, permission)