Skip to content

Commit

Permalink
Replaced SDK version check with BuildVersionHelper everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
federicoiosue committed Oct 26, 2023
1 parent 147bbfb commit 8cebb0b
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,15 +60,15 @@ 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;
}

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;


Expand All @@ -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)
Expand All @@ -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());
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
Expand Down

0 comments on commit 8cebb0b

Please sign in to comment.