Skip to content
This repository has been archived by the owner on Dec 3, 2024. It is now read-only.

Commit

Permalink
Simplify conditions that check for lollipop, api 21, version (#2023)
Browse files Browse the repository at this point in the history
Lollipop, API 21, has been the min sdk version for over a year in this
project.
There were still some conditions in the code, which checked for api 21,
that can be removed or simplified.
  • Loading branch information
adamszewe authored Dec 10, 2023
1 parent 32ab0ca commit ae31ced
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public class FolderActivity extends SyncthingActivity
private static final String IGNORE_FILE_NAME = ".stignore";

private Folder mFolder;
// Contains SAF readwrite access URI on API level >= Build.VERSION_CODES.LOLLIPOP (21)
private Uri mFolderUri = null;
// Indicates the result of the write test to mFolder.path on dialog init or after a path change.
Boolean mCanWriteToPath = false;
Expand Down Expand Up @@ -216,12 +215,6 @@ public void onCreate(Bundle savedInstanceState) {
*/
@SuppressLint("InlinedAPI")
private void onPathViewClick() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
startActivityForResult(FolderPickerActivity.createIntent(this, mFolder.path, null),
FolderPickerActivity.DIRECTORY_REQUEST_CODE);
return;
}

// This has to be android.net.Uri as it implements a Parcelable.
android.net.Uri externalFilesDirUri = FileUtils.getExternalFilesDirUri(FolderActivity.this);

Expand Down Expand Up @@ -460,8 +453,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
.show();
return true;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP &&
mFolderUri != null) {
if (mFolderUri != null) {
/**
* Normally, syncthing takes care of creating the ".stfolder" marker.
* This fails on newer android versions if the syncthing binary only has
Expand Down Expand Up @@ -516,7 +508,6 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == Activity.RESULT_OK && requestCode == CHOOSE_FOLDER_REQUEST) {
// This result case only occurs on API level >= Build.VERSION_CODES.LOLLIPOP (21)
mFolderUri = data.getData();
if (mFolderUri == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,6 @@ static File getLogFile(Context context) {
* to syncthing core v0.14.53+.
*/
public static Boolean osSupportsTLS12() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
// Pre-Lollipop devices don't support TLS 1.2
return false;
}

if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N) {
/**
* SSLProtocolException: SSL handshake failed on Android N/7.0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,7 @@ public void showStopSyncthingWarningNotification() {
Constants.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT));


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
nb.setCategory(Notification.CATEGORY_ERROR);
}
nb.setCategory(Notification.CATEGORY_ERROR);
mNotificationManager.notify(ID_STOP_BACKGROUND_WARNING, nb.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,9 @@ public RunConditionMonitor(Context context, OnRunConditionChangedListener listen
ReceiverManager.registerReceiver(mContext, new BatteryReceiver(), filter);

// PowerSaveModeChangedReceiver
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ReceiverManager.registerReceiver(mContext,
new PowerSaveModeChangedReceiver(),
new IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED));
}
ReceiverManager.registerReceiver(mContext,
new PowerSaveModeChangedReceiver(),
new IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED));

// SyncStatusObserver to monitor android's "AutoSync" quick toggle.
mSyncStatusObserverHandle = ContentResolver.addStatusChangeListener(
Expand Down Expand Up @@ -207,11 +205,9 @@ private RunConditionCheckResult decideShouldRun() {
}

// Power saving
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (prefRespectPowerSaving && isPowerSaving()) {
Log.v(TAG, "decideShouldRun: prefRespectPowerSaving && isPowerSaving");
blockerReasons.add(POWERSAVING_ENABLED);
}
if (prefRespectPowerSaving && isPowerSaving()) {
Log.v(TAG, "decideShouldRun: prefRespectPowerSaving && isPowerSaving");
blockerReasons.add(POWERSAVING_ENABLED);
}

// Android global AutoSync setting.
Expand Down Expand Up @@ -298,38 +294,14 @@ private boolean wifiWhitelistConditionMet(boolean prefWifiWhitelistEnabled,
* Functions for run condition information retrieval.
*/
private boolean isCharging() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
// API level < 21
return isCharging_API16();
} else {
// API level >= 21
return isCharging_API17();
}
}

@TargetApi(16)
private boolean isCharging_API16() {
Intent batteryIntent = mContext.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
return status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
}

@TargetApi(17)
private boolean isCharging_API17() {
Intent intent = mContext.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
return plugged == BatteryManager.BATTERY_PLUGGED_AC ||
plugged == BatteryManager.BATTERY_PLUGGED_USB ||
plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
plugged == BatteryManager.BATTERY_PLUGGED_USB ||
plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
}

@TargetApi(21)
private boolean isPowerSaving() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Log.e(TAG, "isPowerSaving may not be called on pre-lollipop android versions.");
return false;
}
PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
if (powerManager == null) {
Log.e(TAG, "getSystemService(POWER_SERVICE) unexpectedly returned NULL.");
Expand Down
19 changes: 0 additions & 19 deletions app/src/main/java/com/nutomic/syncthingandroid/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ public class FileUtils {

private static final String TAG = "FileUtils";

// TargetApi(21)
private static final Boolean isCompatible = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);

private FileUtils() {
// Private constructor to enforce Singleton pattern.
}
Expand All @@ -36,7 +33,6 @@ private FileUtils() {
private static final String HOME_VOLUME_NAME = "home";

@Nullable
@TargetApi(21)
public static String getAbsolutePathFromSAFUri(Context context, @Nullable final Uri safResultUri) {
Uri treeUri = DocumentsContract.buildDocumentUriUsingTree(safResultUri,
DocumentsContract.getTreeDocumentId(safResultUri));
Expand All @@ -45,10 +41,6 @@ public static String getAbsolutePathFromSAFUri(Context context, @Nullable final

@Nullable
public static String getAbsolutePathFromTreeUri(Context context, @Nullable final Uri treeUri) {
if (!isCompatible) {
Log.e(TAG, "getAbsolutePathFromTreeUri: called on unsupported API level");
return null;
}
if (treeUri == null) {
Log.w(TAG, "getAbsolutePathFromTreeUri: called with treeUri == null");
return null;
Expand Down Expand Up @@ -83,13 +75,7 @@ public static String getAbsolutePathFromTreeUri(Context context, @Nullable final
}
}

@SuppressLint("ObsoleteSdkInt")
@TargetApi(21)
private static String getVolumePath(final String volumeId, Context context) {
if (!isCompatible) {
Log.e(TAG, "getVolumePath called on unsupported API level");
return null;
}
try {
if (HOME_VOLUME_NAME.equals(volumeId)) {
Log.v(TAG, "getVolumePath: isHomeVolume");
Expand Down Expand Up @@ -136,8 +122,6 @@ private static String getVolumePath(final String volumeId, Context context) {
return null;
}

@SuppressLint("ObsoleteSdkInt")
@TargetApi(21)
private static String volumeToPath(Object storageVolumeElement, Class<?> storageVolumeClazz) throws Exception {
try {
// >= API level 30
Expand All @@ -159,7 +143,6 @@ private static String volumeToPath(Object storageVolumeElement, Class<?> storage
* This is crucial to assist the user finding a writeable folder
* to use syncthing's two way sync feature.
*/
@TargetApi(19)
public static android.net.Uri getExternalFilesDirUri(Context context) {
try {
/**
Expand Down Expand Up @@ -192,7 +175,6 @@ public static android.net.Uri getExternalFilesDirUri(Context context) {
return null;
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static String getVolumeIdFromTreeUri(final Uri treeUri) {
final String docId = DocumentsContract.getTreeDocumentId(treeUri);
final String[] split = docId.split(":");
Expand All @@ -203,7 +185,6 @@ private static String getVolumeIdFromTreeUri(final Uri treeUri) {
}
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static String getDocumentPathFromTreeUri(final Uri treeUri) {
final String docId = DocumentsContract.getTreeDocumentId(treeUri);
final String[] split = docId.split(":");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public Languages(Context context) {
* or different than the current system-wide locale. The preference is cleared
* if the language matches the system-wide locale or "System Default" is chosen.
*/
@TargetApi(17)
public void setLanguage(Context context) {
String language = mPreferences.getString(PREFERENCE_LANGUAGE, null);
Locale locale;
Expand All @@ -89,11 +88,7 @@ public void setLanguage(Context context) {

final Resources resources = context.getResources();
Configuration config = resources.getConfiguration();
if (Build.VERSION.SDK_INT >= 17) {
config.setLocale(locale);
} else {
config.locale = locale;
}
config.setLocale(locale);
resources.updateConfiguration(config, resources.getDisplayMetrics());
}

Expand Down

0 comments on commit ae31ced

Please sign in to comment.