From 9cbfed84de88979d5203251eb0be61aba63b19f3 Mon Sep 17 00:00:00 2001 From: Albert Vaca Cintora Date: Sun, 5 Mar 2023 16:15:03 +0000 Subject: [PATCH] Remove pre-kitkat code paths in SFTP plugin We no longer support KitKat. This simplifies the SFTP plugin quite a bit since it contained a diferent code path for pre-kitkat devices --- res/values/strings.xml | 11 --- res/xml/sftpplugin_preferences.xml | 10 +-- .../SftpPlugin/AndroidFileSystemFactory.java | 13 +-- .../Plugins/SftpPlugin/SftpPlugin.java | 49 +---------- .../SftpPlugin/SftpSettingsFragment.java | 88 ++----------------- 5 files changed, 14 insertions(+), 157 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 83c4ebcc..c9f66603 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -266,23 +266,13 @@ Notifications will be synchronized for the selected apps. Send notifications only if the screen is off pref_notification_screen_off - SD card %d - SD card - (read only) - Camera pictures Add device Hostname or IP address - Detected SD cards - Edit SD card Configured storage locations Add storage location Edit storage location - Add camera folder shortcut - Add a shortcut to the camera folder - Do not add a shortcut to the camera folder key_sftp_preference_category key_sftp_add_storage - key_sftp_add_camera_shotcut key_sftp_storage_info%d" key_sftp_storage_info_list Storage location @@ -292,7 +282,6 @@ This display name is already used Display name cannot be empty Delete - No SD card detected No storage locations configured To access files remotely you have to configure storage locations No players found diff --git a/res/xml/sftpplugin_preferences.xml b/res/xml/sftpplugin_preferences.xml index 90c18d97..9290ebcf 100644 --- a/res/xml/sftpplugin_preferences.xml +++ b/res/xml/sftpplugin_preferences.xml @@ -4,9 +4,8 @@ tools:keep="@xml/sftpplugin_preferences"> - - - \ No newline at end of file diff --git a/src/org/kde/kdeconnect/Plugins/SftpPlugin/AndroidFileSystemFactory.java b/src/org/kde/kdeconnect/Plugins/SftpPlugin/AndroidFileSystemFactory.java index 28351183..de0f8c5f 100644 --- a/src/org/kde/kdeconnect/Plugins/SftpPlugin/AndroidFileSystemFactory.java +++ b/src/org/kde/kdeconnect/Plugins/SftpPlugin/AndroidFileSystemFactory.java @@ -40,17 +40,6 @@ class AndroidFileSystemFactory implements FileSystemFactory { @Override public FileSystemView createFileSystemView(final Session username) { - if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) { - if (roots.size() == 0) { - throw new RuntimeException("roots cannot be empty"); - } - - String[] rootsAsString = new String[roots.size()]; - roots.keySet().toArray(rootsAsString); - - return new AndroidFileSystemView(roots, rootsAsString[0], username.getUsername(), context); - } else { - return new AndroidSafFileSystemView(roots, username.getUsername(), context); - } + return new AndroidSafFileSystemView(roots, username.getUsername(), context); } } diff --git a/src/org/kde/kdeconnect/Plugins/SftpPlugin/SftpPlugin.java b/src/org/kde/kdeconnect/Plugins/SftpPlugin/SftpPlugin.java index eb9f2506..f436c690 100644 --- a/src/org/kde/kdeconnect/Plugins/SftpPlugin/SftpPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/SftpPlugin/SftpPlugin.java @@ -39,7 +39,6 @@ public class SftpPlugin extends Plugin implements SharedPreferences.OnSharedPref private final static String PACKET_TYPE_SFTP_REQUEST = "kdeconnect.sftp.request"; static int PREFERENCE_KEY_STORAGE_INFO_LIST = R.string.sftp_preference_key_storage_info_list; - private static int PREFERENCE_KEY_ADD_CAMERA_SHORTCUT = R.string.sftp_preference_key_add_camera_shortcut; private static final SimpleSftpServer server = new SimpleSftpServer(); @@ -57,11 +56,6 @@ public class SftpPlugin extends Plugin implements SharedPreferences.OnSharedPref public boolean onCreate() { try { server.init(context, device); - - if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) { - return SftpSettingsFragment.getStorageInfoList(context, this).size() != 0; - } - return true; } catch (Exception e) { Log.e("SFTP", "Exception in server.init()", e); @@ -71,11 +65,7 @@ public class SftpPlugin extends Plugin implements SharedPreferences.OnSharedPref @Override public boolean checkOptionalPermissions() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - return SftpSettingsFragment.getStorageInfoList(context, this).size() != 0; - } - - return true; + return SftpSettingsFragment.getStorageInfoList(context, this).size() != 0; } @Override @@ -111,15 +101,8 @@ public class SftpPlugin extends Plugin implements SharedPreferences.OnSharedPref getPathsAndNamesForStorageInfoList(paths, pathNames, storageInfoList); } else { NetworkPacket np2 = new NetworkPacket(PACKET_TYPE_SFTP); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - np2.set("errorMessage", context.getString(R.string.sftp_no_storage_locations_configured)); - } else { - np2.set("errorMessage", context.getString(R.string.sftp_no_sdcard_detected)); - } - + np2.set("errorMessage", context.getString(R.string.sftp_no_storage_locations_configured)); device.sendPacket(np2); - return true; } @@ -158,14 +141,6 @@ public class SftpPlugin extends Plugin implements SharedPreferences.OnSharedPref StorageInfo prevInfo = null; StringBuilder pathBuilder = new StringBuilder(); - boolean addCameraShortcuts = false; - - if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) { - if (preferences != null) { - addCameraShortcuts = preferences.getBoolean(context.getString(PREFERENCE_KEY_ADD_CAMERA_SHORTCUT), true); - } - } - for (StorageInfo curInfo : storageInfoList) { pathBuilder.setLength(0); pathBuilder.append("/"); @@ -188,17 +163,6 @@ public class SftpPlugin extends Plugin implements SharedPreferences.OnSharedPref paths.add(pathBuilder.toString()); pathNames.add(curInfo.displayName); - - if (addCameraShortcuts) { - if (new File(curInfo.uri.getPath(), "/DCIM/Camera").exists()) { - paths.add(pathBuilder.toString() + "/DCIM/Camera"); - if (storageInfoList.size() > 1) { - pathNames.add(context.getString(R.string.sftp_camera) + "(" + curInfo.displayName + ")"); - } else { - pathNames.add(context.getString(R.string.sftp_camera)); - } - } - } } } @@ -240,14 +204,11 @@ public class SftpPlugin extends Plugin implements SharedPreferences.OnSharedPref @Override public void copyGlobalToDeviceSpecificSettings(SharedPreferences globalSharedPreferences) { String KeyStorageInfoList = context.getString(PREFERENCE_KEY_STORAGE_INFO_LIST); - String KeyAddCameraShortcut = context.getString(PREFERENCE_KEY_ADD_CAMERA_SHORTCUT); - if (this.preferences != null && - (!this.preferences.contains(KeyStorageInfoList) || !this.preferences.contains(KeyAddCameraShortcut))) { + if (this.preferences != null && !this.preferences.contains(KeyStorageInfoList)) { this.preferences .edit() .putString(KeyStorageInfoList, globalSharedPreferences.getString(KeyStorageInfoList, "[]")) - .putBoolean(KeyAddCameraShortcut, globalSharedPreferences.getBoolean(KeyAddCameraShortcut, true)) .apply(); } } @@ -257,7 +218,6 @@ public class SftpPlugin extends Plugin implements SharedPreferences.OnSharedPref sharedPreferences .edit() .remove(context.getString(PREFERENCE_KEY_STORAGE_INFO_LIST)) - .remove(context.getString(PREFERENCE_KEY_ADD_CAMERA_SHORTCUT)) .apply(); } @@ -268,8 +228,7 @@ public class SftpPlugin extends Plugin implements SharedPreferences.OnSharedPref @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - if (key.equals(context.getString(PREFERENCE_KEY_STORAGE_INFO_LIST)) || - key.equals(context.getString(PREFERENCE_KEY_ADD_CAMERA_SHORTCUT))) { + if (key.equals(context.getString(PREFERENCE_KEY_STORAGE_INFO_LIST))) { //TODO: There used to be a way to request an un-mount (see desktop SftpPlugin's Mounter::onPackageReceived) but that is not handled anymore by the SftpPlugin on KDE. if (server.isStarted()) { server.stop(); diff --git a/src/org/kde/kdeconnect/Plugins/SftpPlugin/SftpSettingsFragment.java b/src/org/kde/kdeconnect/Plugins/SftpPlugin/SftpSettingsFragment.java index e2ed1c74..b8d8824b 100644 --- a/src/org/kde/kdeconnect/Plugins/SftpPlugin/SftpSettingsFragment.java +++ b/src/org/kde/kdeconnect/Plugins/SftpPlugin/SftpSettingsFragment.java @@ -47,8 +47,6 @@ import java.util.Collections; import java.util.List; import java.util.ListIterator; -//TODO: Is it possible on API 19 to select a directory and then have write permission for everything beneath it -//TODO: Is it necessary to check if uri permissions are still in place? If it is make the user aware of the fact (red text or something) public class SftpSettingsFragment extends PluginSettingsFragment implements StoragePreferenceDialogFragment.Callback, @@ -100,34 +98,16 @@ public class SftpSettingsFragment int colorAccent = ta.getColor(0, 0); ta.recycle(); - int sdkInt = Build.VERSION.SDK_INT; - storageInfoList = getStorageInfoList(requireContext(), plugin); PreferenceScreen preferenceScreen = getPreferenceScreen(); preferenceCategory = preferenceScreen .findPreference(getString(R.string.sftp_preference_key_preference_category)); - if (sdkInt <= 19) { - preferenceCategory.setTitle(R.string.sftp_preference_detected_sdcards); - } else { - preferenceCategory.setTitle(R.string.sftp_preference_configured_storage_locations); - } - addStoragePreferences(preferenceCategory); Preference addStoragePreference = preferenceScreen.findPreference(getString(R.string.sftp_preference_key_add_storage)); addStoragePreference.getIcon().setColorFilter(colorAccent, PorterDuff.Mode.SRC_IN); - - if (sdkInt <= 19) { - addStoragePreference.setVisible(false); - } - - Preference addCameraShortcutPreference = preferenceScreen.findPreference(getString(R.string.sftp_preference_key_add_camera_shortcut)); - - if (sdkInt > 19) { - addCameraShortcutPreference.setVisible(false); - } } private void addStoragePreferences(PreferenceCategory preferenceCategory) { @@ -143,17 +123,11 @@ public class SftpSettingsFragment SftpPlugin.StorageInfo storageInfo = storageInfoList.get(i); StoragePreference preference = new StoragePreference(context); preference.setOnPreferenceChangeListener(this); - if (Build.VERSION.SDK_INT >= 21) { - preference.setOnLongClickListener(this); - } + preference.setOnLongClickListener(this); preference.setKey(getString(R.string.sftp_preference_key_storage_info, i)); preference.setIcon(android.R.color.transparent); preference.setDefaultValue(storageInfo); - if (storageInfo.isFileUri()) { - preference.setDialogTitle(R.string.sftp_preference_edit_sdcard_title); - } else { - preference.setDialogTitle(R.string.sftp_preference_edit_storage_location); - } + preference.setDialogTitle(R.string.sftp_preference_edit_storage_location); preferenceCategory.addPreference(preference); } @@ -266,53 +240,9 @@ public class SftpSettingsFragment Log.e("SFTPSettings", "Couldn't load storage info", e); } - if (Build.VERSION.SDK_INT <= 19) { - addDetectedSDCardsToStorageInfoList(context, storageInfoList); - } - return storageInfoList; } - private static void addDetectedSDCardsToStorageInfoList(@NonNull Context context, List storageInfoList) { - List storageHelperInfoList = StorageHelper.getStorageList(); - - for (StorageHelper.StorageInfo info : storageHelperInfoList) { - // on at least API 17 emulator Environment.isExternalStorageRemovable returns false - if (info.removable || info.path.startsWith(Environment.getExternalStorageDirectory().getPath())) { - StringBuilder displayNameBuilder = new StringBuilder(); - StringBuilder displayNameReadOnlyBuilder = new StringBuilder(); - - Uri sdCardUri = Uri.fromFile(new File(info.path)); - - if (isAlreadyConfigured(storageInfoList, sdCardUri)) { - continue; - } - - int i = 1; - - do { - if (i == 1) { - displayNameBuilder.append(context.getString(R.string.sftp_sdcard)); - } else { - displayNameBuilder.setLength(0); - displayNameBuilder.append(context.getString(R.string.sftp_sdcard_num, i)); - } - - displayNameReadOnlyBuilder - .append(displayNameBuilder) - .append(" ") - .append(context.getString(R.string.sftp_readonly)); - - i++; - } while (!isDisplayNameUnique(storageInfoList, displayNameBuilder.toString(), displayNameReadOnlyBuilder.toString())); - - String displayName = info.readonly ? - displayNameReadOnlyBuilder.toString() : displayNameBuilder.toString(); - - storageInfoList.add(new SftpPlugin.StorageInfo(displayName, Uri.fromFile(new File(info.path)))); - } - } - } private static boolean isDisplayNameUnique(List storageInfoList, String displayName, String displayNameReadOnly) { for (SftpPlugin.StorageInfo info : storageInfoList) { @@ -470,14 +400,12 @@ public class SftpSettingsFragment if (preference.checkbox.isChecked()) { SftpPlugin.StorageInfo info = storageInfoList.remove(i); - if (Build.VERSION.SDK_INT >= 21) { - try { - // This throws when trying to release a URI we don't have access to - requireContext().getContentResolver().releasePersistableUriPermission(info.uri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); - } catch (SecurityException e) { - // Usually safe to ignore, but who knows? - Log.e("SFTP Settings", "Exception", e); - } + try { + // This throws when trying to release a URI we don't have access to + requireContext().getContentResolver().releasePersistableUriPermission(info.uri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + } catch (SecurityException e) { + // Usually safe to ignore, but who knows? + Log.e("SFTP Settings", "Exception", e); } } }