diff --git a/omniNotes/src/androidTest/java/it/feio/android/omninotes/testutils/BaseAndroidTestCase.java b/omniNotes/src/androidTest/java/it/feio/android/omninotes/testutils/BaseAndroidTestCase.java index b13f174ed..b4a261171 100644 --- a/omniNotes/src/androidTest/java/it/feio/android/omninotes/testutils/BaseAndroidTestCase.java +++ b/omniNotes/src/androidTest/java/it/feio/android/omninotes/testutils/BaseAndroidTestCase.java @@ -19,6 +19,7 @@ import static android.Manifest.permission.ACCESS_COARSE_LOCATION; import static android.Manifest.permission.ACCESS_FINE_LOCATION; +import static android.Manifest.permission.POST_NOTIFICATIONS; import static android.Manifest.permission.READ_EXTERNAL_STORAGE; import static android.Manifest.permission.RECORD_AUDIO; import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE; @@ -33,7 +34,7 @@ import android.content.SharedPreferences; import android.content.res.Configuration; import android.net.Uri; -import android.os.Build; +import android.os.Build.VERSION_CODES; import androidx.test.core.app.ApplicationProvider; import androidx.test.rule.GrantPermissionRule; import com.pixplicity.easyprefs.library.Prefs; @@ -43,6 +44,7 @@ import it.feio.android.omninotes.async.bus.NotesUpdatedEvent; import it.feio.android.omninotes.db.DbHelper; import it.feio.android.omninotes.exceptions.TestException; +import it.feio.android.omninotes.helpers.BuildVersionHelper; import it.feio.android.omninotes.models.Attachment; import it.feio.android.omninotes.models.Category; import it.feio.android.omninotes.models.Note; @@ -96,7 +98,7 @@ public void tearDown() { private static void grantPermissions() { GrantPermissionRule.grant(ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION, READ_EXTERNAL_STORAGE, RECORD_AUDIO); - if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) { + if (BuildVersionHelper.isBelowOrEqual(VERSION_CODES.Q)) { GrantPermissionRule.grant(WRITE_EXTERNAL_STORAGE); } } diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/async/AlarmRestoreOnRebootService.java b/omniNotes/src/main/java/it/feio/android/omninotes/async/AlarmRestoreOnRebootService.java index 8b328d2ca..d65dc7fa3 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/async/AlarmRestoreOnRebootService.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/async/AlarmRestoreOnRebootService.java @@ -19,12 +19,13 @@ import android.content.Context; import android.content.Intent; -import android.os.Build; +import android.os.Build.VERSION_CODES; import androidx.annotation.NonNull; import androidx.core.app.JobIntentService; import it.feio.android.omninotes.BaseActivity; import it.feio.android.omninotes.OmniNotes; import it.feio.android.omninotes.db.DbHelper; +import it.feio.android.omninotes.helpers.BuildVersionHelper; import it.feio.android.omninotes.helpers.LogDelegate; import it.feio.android.omninotes.models.Note; import it.feio.android.omninotes.utils.ReminderHelper; @@ -38,7 +39,7 @@ public class AlarmRestoreOnRebootService extends JobIntentService { public static final int JOB_ID = 0x01; public static void enqueueWork(Context context, Intent work) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + if (BuildVersionHelper.isAboveOrEqual(VERSION_CODES.O)) { enqueueWork(context, AlarmRestoreOnRebootService.class, JOB_ID, work); } else { Intent jobIntent = new Intent(context, AlarmRestoreOnRebootService.class); diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/helpers/GeocodeProviderBaseFactory.java b/omniNotes/src/main/java/it/feio/android/omninotes/helpers/GeocodeProviderBaseFactory.java index c3c5756dc..a62312e98 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/helpers/GeocodeProviderBaseFactory.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/helpers/GeocodeProviderBaseFactory.java @@ -20,7 +20,7 @@ import android.content.Context; import android.content.Intent; import android.location.LocationManager; -import android.os.Build; +import android.os.Build.VERSION_CODES; import android.provider.Settings; import android.widget.Toast; import io.nlopez.smartlocation.location.LocationProvider; @@ -35,7 +35,7 @@ protected GeocodeProviderBaseFactory() { } public static LocationProvider getProvider(Context context) { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P && checkHighAccuracyLocationProvider(context)) { + if (BuildVersionHelper.isBelow(VERSION_CODES.P) && checkHighAccuracyLocationProvider(context)) { Toast.makeText(context, R.string.location_set_high_accuracy, Toast.LENGTH_SHORT).show(); context.startActivity((new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))); } diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/helpers/IntentHelper.java b/omniNotes/src/main/java/it/feio/android/omninotes/helpers/IntentHelper.java index 9b19fb1eb..6af547307 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/helpers/IntentHelper.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/helpers/IntentHelper.java @@ -25,6 +25,7 @@ import android.app.PendingIntent; import android.content.Context; import android.content.Intent; +import android.os.Build.VERSION_CODES; import android.os.Bundle; import androidx.annotation.NonNull; import it.feio.android.omninotes.models.Note; @@ -59,7 +60,7 @@ public static PendingIntent getNotePendingIntent(@NonNull Context context, @NonN public static int immutablePendingIntentFlag(final int flag) { int pIntentFlags = flag; - if (android.os.Build.VERSION.SDK_INT >= 23) { + if (BuildVersionHelper.isAboveOrEqual(VERSION_CODES.M)) { pIntentFlags = pIntentFlags | FLAG_IMMUTABLE; } return pIntentFlags; @@ -67,7 +68,7 @@ public static int immutablePendingIntentFlag(final int flag) { public static int mutablePendingIntentFlag(final int flag) { int pIntentFlags = flag; - if (android.os.Build.VERSION.SDK_INT >= 31) { + if (BuildVersionHelper.isAboveOrEqual(VERSION_CODES.S)) { pIntentFlags = pIntentFlags | FLAG_MUTABLE; } return pIntentFlags; diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/helpers/PermissionsHelper.java b/omniNotes/src/main/java/it/feio/android/omninotes/helpers/PermissionsHelper.java index 9b594647e..15b36fa3f 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/helpers/PermissionsHelper.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/helpers/PermissionsHelper.java @@ -24,6 +24,7 @@ import android.app.Activity; import android.content.pm.PackageManager; import android.os.Build; +import android.os.Build.VERSION_CODES; import android.view.View; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; @@ -64,7 +65,7 @@ public static void requestPermission(Activity activity, String permission, } private static boolean skipPermissionRequest(String permission) { - return Build.VERSION.SDK_INT > Build.VERSION_CODES.Q && permission.equals(WRITE_EXTERNAL_STORAGE); + return BuildVersionHelper.isAbove(VERSION_CODES.Q) && permission.equals(WRITE_EXTERNAL_STORAGE); } private static void requestPermissionExecute(Activity activity, String permission, diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/helpers/notifications/NotificationsHelper.java b/omniNotes/src/main/java/it/feio/android/omninotes/helpers/notifications/NotificationsHelper.java index 0b36af6f3..fc7fa88e2 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/helpers/notifications/NotificationsHelper.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/helpers/notifications/NotificationsHelper.java @@ -39,7 +39,6 @@ import android.provider.Settings; import androidx.activity.ComponentActivity; import androidx.activity.result.contract.ActivityResultContracts.RequestPermission; -import androidx.annotation.RequiresApi; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat.Builder; import androidx.core.content.ContextCompat; @@ -48,6 +47,7 @@ import it.feio.android.omninotes.MainActivity; import it.feio.android.omninotes.R; import it.feio.android.omninotes.async.bus.NotificationsGrantedEvent; +import it.feio.android.omninotes.helpers.BuildVersionHelper; import lombok.NonNull; @@ -69,9 +69,9 @@ public NotificationsHelper(Context mContext) { * Creates the NotificationChannel, but only on API 26+ because the NotificationChannel class is * new and not in the support library */ - @TargetApi(Build.VERSION_CODES.O) + @TargetApi(VERSION_CODES.O) public void initNotificationChannels() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + if (BuildVersionHelper.isAboveOrEqual(VERSION_CODES.O)) { String soundFromPrefs = Prefs.getString("settings_notification_ringtone", null); Uri sound = soundFromPrefs != null ? Uri.parse(soundFromPrefs) @@ -97,7 +97,7 @@ public void initNotificationChannels() { @TargetApi(Build.VERSION_CODES.O) public void updateNotificationChannelsSound() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + if (BuildVersionHelper.isAboveOrEqual(VERSION_CODES.O)) { Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra(Settings.EXTRA_APP_PACKAGE, mContext.getPackageName()); @@ -148,7 +148,7 @@ public NotificationsHelper setLargeIcon(int largeIconResource) { } public NotificationsHelper setRingtone(String ringtone) { - if (ringtone != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { + if (ringtone != null && BuildVersionHelper.isBelow(VERSION_CODES.O)) { mBuilder.setSound(Uri.parse(ringtone)); } return this; @@ -242,13 +242,13 @@ public void cancel(int id) { } public boolean checkNotificationsEnabled(Context context) { - return Build.VERSION.SDK_INT < VERSION_CODES.TIRAMISU + return BuildVersionHelper.isBelow(VERSION_CODES.TIRAMISU) || ContextCompat.checkSelfPermission(context, permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED; } public void askToEnableNotifications(ComponentActivity activity) { - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) { + if (BuildVersionHelper.isAboveOrEqual(VERSION_CODES.TIRAMISU)) { if (!checkNotificationsEnabled(activity)) { activity.registerForActivityResult(new RequestPermission(), isGranted -> EventBus.getDefault().post(new NotificationsGrantedEvent(isGranted))) diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/utils/ShortcutHelper.java b/omniNotes/src/main/java/it/feio/android/omninotes/utils/ShortcutHelper.java index a48798c62..26145732f 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/utils/ShortcutHelper.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/utils/ShortcutHelper.java @@ -31,11 +31,11 @@ import android.content.pm.ShortcutManager; import android.graphics.drawable.Icon; import android.net.Uri; -import android.os.Build; import android.os.Build.VERSION_CODES; import com.pixplicity.easyprefs.library.Prefs; import it.feio.android.omninotes.MainActivity; import it.feio.android.omninotes.R; +import it.feio.android.omninotes.helpers.BuildVersionHelper; import it.feio.android.omninotes.helpers.date.DateHelper; import it.feio.android.omninotes.models.Note; import lombok.SneakyThrows; @@ -54,7 +54,7 @@ public static void addShortcut(Context context, Note note) { .getCreation(), Prefs.getBoolean(PREF_PRETTIFIED_DATES, true)); - if (Build.VERSION.SDK_INT < 26) { + if (BuildVersionHelper.isBelow(VERSION_CODES.O)) { createShortcutPreOreo(context, note, shortcutTitle); } else { createShortcutPostOreo(context, note, shortcutTitle); @@ -118,7 +118,7 @@ private static void createShortcutPreOreo(Context context, Note note, String sho * Removes note shortcut from home launcher */ public static void removeShortcut(Context context, Note note) { - if (Build.VERSION.SDK_INT < 26) { + if (BuildVersionHelper.isBelow(VERSION_CODES.O)) { removeShortcutPreOreo(context, note); } else { removeShortcutPostOreo(context, note); diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/utils/StorageHelper.java b/omniNotes/src/main/java/it/feio/android/omninotes/utils/StorageHelper.java index aeb1a272e..2a000338d 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/utils/StorageHelper.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/utils/StorageHelper.java @@ -17,6 +17,7 @@ package it.feio.android.omninotes.utils; import static android.os.Environment.DIRECTORY_DOWNLOADS; +import static android.widget.Toast.LENGTH_SHORT; import static it.feio.android.omninotes.utils.ConstantsBase.MIME_TYPE_AUDIO; import static it.feio.android.omninotes.utils.ConstantsBase.MIME_TYPE_FILES; import static it.feio.android.omninotes.utils.ConstantsBase.MIME_TYPE_IMAGE; @@ -115,7 +116,7 @@ public static File createExternalStoragePrivateFile(Context mContext, Uri uri, S if (!checkStorage()) { Toast.makeText(mContext, mContext.getString(R.string.storage_not_available), - Toast.LENGTH_SHORT).show(); + LENGTH_SHORT).show(); return null; } File file = createNewAttachmentFile(mContext, extension); @@ -200,18 +201,16 @@ public static boolean deleteExternalStoragePrivateFile(Context mContext, String // Checks for external storage availability if (!checkStorage()) { Toast.makeText(mContext, mContext.getString(R.string.storage_not_available), - Toast.LENGTH_SHORT).show(); + LENGTH_SHORT).show(); return false; } File file = new File(mContext.getExternalFilesDir(null), name); return file.delete(); } - public static boolean delete(Context mContext, String path) { if (!checkStorage()) { - Toast.makeText(mContext, mContext.getString(R.string.storage_not_available), - Toast.LENGTH_SHORT).show(); + Toast.makeText(mContext, mContext.getString(R.string.storage_not_available), LENGTH_SHORT).show(); return false; } try { @@ -223,7 +222,6 @@ public static boolean delete(Context mContext, String path) { return true; } - public static String getRealPathFromURI(Context mContext, Uri contentUri) { String[] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = mContext.getContentResolver().query(contentUri, proj, null, null, null);