Fix Codacy complaints

This commit is contained in:
TranceLove
2025-05-17 13:56:35 +08:00
committed by Raymond Lai
parent d125cd9e0f
commit 2079512cdf
12 changed files with 116 additions and 130 deletions
+3 -6
View File
@@ -148,8 +148,7 @@
android:configChanges="orientation|screenSize|uiMode"
android:label="@string/setting"
android:name=".ui.activities.PreferencesActivity"
android:theme="@style/appCompatBlack"
android:exported="false">
android:theme="@style/appCompatBlack">
<intent-filter>
<action android:name="android.intent.action.APPLICATION_PREFERENCES" />
<category android:name="android.intent.category.DEFAULT" />
@@ -161,8 +160,7 @@
android:label="@string/textreader"
android:name=".ui.activities.texteditor.TextEditorActivity"
android:configChanges="uiMode"
android:theme="@style/appCompatBlack"
android:exported="true">
android:theme="@style/appCompatBlack">
<intent-filter
tools:ignore="AppLinkUrlError"
android:label="Amaze Text Editor">
@@ -295,8 +293,7 @@
android:exported="true"
android:icon="@drawable/ic_ftp_dark"
android:label="@string/ftp"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE"
android:exported="false">
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
<intent-filter>
<action
android:name="android.service.quicksettings.action.QS_TILE" />
@@ -884,10 +884,8 @@ public class MainActivity extends PermissionsActivity
File usb = getUsbDrive();
if (usb != null && !rv.contains(usb.getPath())) rv.add(usb.getPath());
if (SDK_INT >= KITKAT) {
if (SingletonUsbOtg.getInstance().isDeviceConnected()) {
rv.add(OTGUtil.PREFIX_OTG + "/");
}
if (SDK_INT >= KITKAT && SingletonUsbOtg.INSTANCE.isDeviceConnected()) {
rv.add(OTGUtil.PREFIX_OTG + "/");
}
// Assign a label and icon to each directory
@@ -1383,28 +1381,27 @@ public class MainActivity extends PermissionsActivity
List<UsbOtgRepresentation> connectedDevices = OTGUtil.getMassStorageDevicesConnected(this);
if (!connectedDevices.isEmpty()) {
if (SingletonUsbOtg.getInstance().getUsbOtgRoot() != null
&& OTGUtil.isUsbUriAccessible(this)) {
if (SingletonUsbOtg.INSTANCE.getUsbOtgRoot() != null && OTGUtil.isUsbUriAccessible(this)) {
for (UsbOtgRepresentation device : connectedDevices) {
if (SingletonUsbOtg.getInstance().checkIfRootIsFromDevice(device)) {
if (SingletonUsbOtg.INSTANCE.checkIfRootIsFromDevice(device)) {
isInformationUpdated = true;
break;
}
}
if (!isInformationUpdated) {
SingletonUsbOtg.getInstance().resetUsbOtgRoot();
SingletonUsbOtg.INSTANCE.resetUsbOtgRoot();
}
}
if (!isInformationUpdated) {
SingletonUsbOtg.getInstance().setConnectedDevice(connectedDevices.get(0));
SingletonUsbOtg.INSTANCE.setConnectedDevice(connectedDevices.get(0));
isInformationUpdated = true;
}
}
if (!isInformationUpdated) {
SingletonUsbOtg.getInstance().resetUsbOtgRoot();
SingletonUsbOtg.INSTANCE.resetUsbOtgRoot();
drawer.refreshDrawer();
}
@@ -1424,12 +1421,12 @@ public class MainActivity extends PermissionsActivity
List<UsbOtgRepresentation> connectedDevices =
OTGUtil.getMassStorageDevicesConnected(MainActivity.this);
if (!connectedDevices.isEmpty()) {
SingletonUsbOtg.getInstance().resetUsbOtgRoot();
SingletonUsbOtg.getInstance().setConnectedDevice(connectedDevices.get(0));
SingletonUsbOtg.INSTANCE.resetUsbOtgRoot();
SingletonUsbOtg.INSTANCE.setConnectedDevice(connectedDevices.get(0));
drawer.refreshDrawer();
}
} else if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_DETACHED)) {
SingletonUsbOtg.getInstance().resetUsbOtgRoot();
SingletonUsbOtg.INSTANCE.resetUsbOtgRoot();
drawer.refreshDrawer();
goToMain(null);
}
@@ -1695,7 +1692,7 @@ public class MainActivity extends PermissionsActivity
if (responseCode == Activity.RESULT_OK && intent.getData() != null) {
// otg access
Uri usbOtgRoot = intent.getData();
SingletonUsbOtg.getInstance().setUsbOtgRoot(usbOtgRoot);
SingletonUsbOtg.INSTANCE.setUsbOtgRoot(usbOtgRoot);
mainFragment.loadlist(OTGUtil.PREFIX_OTG, false, OpenMode.OTG, true);
drawer.closeIfNotLocked();
if (drawer.isLocked()) drawer.onDrawerClosed();
@@ -2048,7 +2045,7 @@ public class MainActivity extends PermissionsActivity
if (SDK_INT >= KITKAT) {
if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_DETACHED)) {
SingletonUsbOtg.getInstance().resetUsbOtgRoot();
SingletonUsbOtg.INSTANCE.resetUsbOtgRoot();
drawer.refreshDrawer();
}
}
@@ -828,7 +828,7 @@ public class Drawer implements NavigationView.OnNavigationItemSelectedListener {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
&& (meta.path.contains(OTGUtil.PREFIX_OTG)
|| meta.path.startsWith(OTGUtil.PREFIX_MEDIA_REMOVABLE))
&& SingletonUsbOtg.getInstance().getUsbOtgRoot() == null) {
&& SingletonUsbOtg.INSTANCE.getUsbOtgRoot() == null) {
MaterialDialog dialog = GeneralDialogCreation.showOtgSafExplanationDialog(mainActivity);
dialog
.getActionButton(DialogAction.POSITIVE)
@@ -93,7 +93,7 @@ object OTGUtil {
fileFound: OnFileFound,
) {
val rootUriString =
SingletonUsbOtg.getInstance().usbOtgRoot
SingletonUsbOtg.getUsbOtgRoot()
?: throw NullPointerException("USB OTG root not set!")
return getDocumentFiles(rootUriString, path, context, OpenMode.OTG, fileFound)
}
@@ -163,7 +163,7 @@ object OTGUtil {
createRecursive: Boolean,
): DocumentFile? {
val rootUriString =
SingletonUsbOtg.getInstance().usbOtgRoot
SingletonUsbOtg.getUsbOtgRoot()
?: throw NullPointerException("USB OTG root not set!")
return getDocumentFile(path, rootUriString, context, OpenMode.OTG, createRecursive)
@@ -208,7 +208,7 @@ object OTGUtil {
@RequiresApi(api = KITKAT)
@JvmStatic
fun isUsbUriAccessible(context: Context?): Boolean {
val rootUriString = SingletonUsbOtg.getInstance().usbOtgRoot
val rootUriString = SingletonUsbOtg.getUsbOtgRoot()
return DocumentsContract.isDocumentUri(context, rootUriString)
}
@@ -58,11 +58,11 @@ public class SingletonUsbOtgTest {
Uri rootBefore = Uri.parse("ssh://testuser:testpassword@127.0.0.1:22222");
SingletonUsbOtg.getInstance().setUsbOtgRoot(rootBefore);
SingletonUsbOtg.INSTANCE.setUsbOtgRoot(rootBefore);
controller.pause().resume().get();
Uri rootAfter = SingletonUsbOtg.getInstance().getUsbOtgRoot();
Uri rootAfter = SingletonUsbOtg.INSTANCE.getUsbOtgRoot();
assertEquals(
"Uris are different: (before:) " + rootBefore + " (after:) " + rootAfter,
@@ -91,6 +91,7 @@ import android.net.Uri;
*
* See the end of the source file for distribution license (Modified BSD licence)
*/
@SuppressWarnings("PMD")
public abstract class CloudStreamServer {
private static final Logger LOG = LoggerFactory.getLogger(CloudStreamServer.class);
@@ -91,6 +91,7 @@ import android.net.Uri;
*
* See the end of the source file for distribution license (Modified BSD licence)
*/
@SuppressWarnings("PMD")
public abstract class StreamServer {
private static final Logger LOG = LoggerFactory.getLogger(StreamServer.class);
@@ -17,50 +17,53 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.amaze.filemanager.fileoperations.filesystem.usb
package com.amaze.filemanager.fileoperations.filesystem.usb;
import android.net.Uri
import android.net.Uri;
/**
* USB OTG device representation.
*/
object SingletonUsbOtg {
var connectedDevice: UsbOtgRepresentation? = null
var _usbOtgRoot: Uri? = null
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
val isDeviceConnected: Boolean
get() = connectedDevice != null
public class SingletonUsbOtg {
private static SingletonUsbOtg instance = null;
/**
* Get the root of the connected USB OTG device.
*
* @return the root URI of the connected USB OTG device, or null if no device is connected.
*/
fun getUsbOtgRoot(): Uri? {
return _usbOtgRoot
}
public static SingletonUsbOtg getInstance() {
if (instance == null) instance = new SingletonUsbOtg();
return instance;
}
/**
* Set the root of the connected USB OTG device. Will throw exception if no device is connected.
*
* @param root the root URI of the connected USB OTG device.
*/
fun setUsbOtgRoot(root: Uri?) {
checkNotNull(connectedDevice) { "No device connected!" }
_usbOtgRoot = root
}
private UsbOtgRepresentation connectedDevice = null;
private @Nullable Uri usbOtgRoot;
/**
* Clear the reference to connected device and root.
*/
fun resetUsbOtgRoot() {
connectedDevice = null
_usbOtgRoot = null
}
private SingletonUsbOtg() {}
public void setConnectedDevice(UsbOtgRepresentation connectedDevice) {
this.connectedDevice = connectedDevice;
}
public boolean isDeviceConnected() {
return connectedDevice != null;
}
public void setUsbOtgRoot(@Nullable Uri root) {
if (connectedDevice == null) throw new IllegalStateException("No device connected!");
usbOtgRoot = root;
}
public void resetUsbOtgRoot() {
connectedDevice = null;
usbOtgRoot = null;
}
public @Nullable Uri getUsbOtgRoot() {
return usbOtgRoot;
}
public boolean checkIfRootIsFromDevice(@NonNull UsbOtgRepresentation device) {
return usbOtgRoot != null && connectedDevice.hashCode() == device.hashCode();
}
/**
* Check if the root is from the given device.
*
* Used by [MainActivity.updateUsbInformation].
*/
fun checkIfRootIsFromDevice(device: UsbOtgRepresentation): Boolean {
return _usbOtgRoot != null && connectedDevice.hashCode() == device.hashCode()
}
}
@@ -17,47 +17,33 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.amaze.filemanager.fileoperations.filesystem.usb;
import androidx.annotation.Nullable;
package com.amaze.filemanager.fileoperations.filesystem.usb
/**
* This class replesents a usb device.
*
* @see UsbOtgRepresentation#equals(Object)
* @see UsbOtgRepresentation.equals
*/
public class UsbOtgRepresentation {
data class UsbOtgRepresentation(val productID: Int, val vendorID: Int, val serialNumber: String?) {
/**
* This does not ensure a device is equal to another! This tests parameters to know to a certain
* degree of certainty that a device is "similar enough" to another one to be the same one.
*/
override fun equals(obj: Any?): Boolean {
if (obj !is UsbOtgRepresentation) return false
public final int productID, vendorID;
public final @Nullable String serialNumber;
return productID == obj.productID &&
vendorID == obj.vendorID &&
(
(serialNumber == null && obj.serialNumber == null) ||
serialNumber == obj.serialNumber
)
}
public UsbOtgRepresentation(int productID, int vendorID, @Nullable String serialNumber) {
this.productID = productID;
this.vendorID = vendorID;
this.serialNumber = serialNumber;
}
/**
* This does not ensure a device is equal to another! This tests parameters to know to a certain
* degree of certanty that a device is "similar enough" to another one to be the same one.
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof UsbOtgRepresentation)) return false;
UsbOtgRepresentation other = (UsbOtgRepresentation) obj;
return productID == other.productID
&& vendorID == other.vendorID
&& ((serialNumber == null && other.serialNumber == null)
|| serialNumber.equals(other.serialNumber));
}
@Override
public int hashCode() {
int result = productID;
result = 37 * result + vendorID;
result = 37 * result + (serialNumber != null ? serialNumber.hashCode() : 0);
return result;
}
override fun hashCode(): Int {
var result = productID
result = 37 * result + vendorID
result = 37 * result + (serialNumber?.hashCode() ?: 0)
return result
}
}
@@ -115,11 +115,11 @@ public class CloudStreamSourceTest {
@Test
public void read() throws IOException {
byte[] buff = new byte[10];
int n = cs.read(buff);
int bytesRead = cs.read(buff);
byte[] temp = Arrays.copyOfRange(text, 0, buff.length);
assertArrayEquals(temp, buff);
assertEquals(buff.length, n);
assertEquals(buff.length, bytesRead);
}
/**
@@ -129,12 +129,12 @@ public class CloudStreamSourceTest {
@Test
public void readExceed() throws IOException {
byte[] buff = new byte[100];
int n = cs.read(buff);
int bytesRead = cs.read(buff);
// erase dummy values in the end of buffer
byte[] buffer = Arrays.copyOfRange(buff, 0, n);
byte[] buffer = Arrays.copyOfRange(buff, 0, bytesRead);
assertArrayEquals(text, buffer);
assertEquals(len, n);
assertEquals(len, bytesRead);
}
/**
@@ -145,7 +145,7 @@ public class CloudStreamSourceTest {
public void readClosedException() throws IOException {
cs.close();
byte[] buff = new byte[(int) len];
int n = cs.read(buff);
cs.read(buff);
}
/**
@@ -158,12 +158,12 @@ public class CloudStreamSourceTest {
int start = 5;
int end = 10;
int n = cs.read(buff, start, end);
int bytesRead = cs.read(buff, start, end);
byte[] file = Arrays.copyOfRange(text, 0, end - start);
byte[] buffer = Arrays.copyOfRange(buff, start, end);
assertArrayEquals(file, buffer);
assertEquals(end, n);
assertEquals(end, bytesRead);
}
/**
@@ -191,7 +191,7 @@ public class CloudStreamSourceTest {
int start = 5;
int end = 10;
int n = cs.read(buff, start, end);
cs.read(buff, start, end);
}
/**
@@ -206,9 +206,9 @@ public class CloudStreamSourceTest {
cs.moveTo(readPosition);
cs.open();
int n = cs.read(buff);
int bytesRead = cs.read(buff);
assertEquals(text[readPosition], buff[0]);
assertEquals(buff.length, n);
assertEquals(buff.length, bytesRead);
}
/**
@@ -229,14 +229,14 @@ public class CloudStreamSourceTest {
public void close() {
cs.close();
int n = -1;
int bytesRead = -1;
try {
byte[] buff = new byte[1];
n = cs.read(buff);
bytesRead = cs.read(buff);
} catch (IOException ignored) {
}
assertEquals(-1, n);
assertEquals(-1, bytesRead);
}
/** Purpose: Get length of the text from a file Input: no Expected: return len */
@@ -110,11 +110,11 @@ public class StreamSourceTest {
public void read() throws IOException {
ss.open();
byte[] buff = new byte[10];
int n = ss.read(buff);
int bytesRead = ss.read(buff);
byte[] temp = Arrays.copyOfRange(text, 0, buff.length);
assertArrayEquals(temp, buff);
assertEquals(buff.length, n);
assertEquals(buff.length, bytesRead);
}
/**
@@ -125,12 +125,12 @@ public class StreamSourceTest {
public void readExceed() throws IOException {
ss.open();
byte[] buff = new byte[100];
int n = ss.read(buff);
int bytesRead = ss.read(buff);
// erase dummy values in the end of buffer
byte[] buffer = Arrays.copyOfRange(buff, 0, n);
byte[] buffer = Arrays.copyOfRange(buff, 0, bytesRead);
assertArrayEquals(text, buffer);
assertEquals(text.length, n);
assertEquals(text.length, bytesRead);
}
/**
@@ -142,7 +142,7 @@ public class StreamSourceTest {
ss.open();
ss.close();
byte[] buff = new byte[text.length];
int n = ss.read(buff);
ss.read(buff);
}
/**
@@ -156,12 +156,12 @@ public class StreamSourceTest {
int start = 5;
int end = 10;
int n = ss.read(buff, start, end);
int bytesRead = ss.read(buff, start, end);
byte[] file = Arrays.copyOfRange(text, 0, end - start);
byte[] buffer = Arrays.copyOfRange(buff, start, end);
assertArrayEquals(file, buffer);
assertEquals(end, n);
assertEquals(end, bytesRead);
}
/**
@@ -192,7 +192,7 @@ public class StreamSourceTest {
int start = 5;
int end = 10;
int n = ss.read(buff, start, end);
ss.read(buff, start, end);
}
/**
@@ -207,9 +207,9 @@ public class StreamSourceTest {
ss.moveTo(readPosition);
ss.open();
int n = ss.read(buff);
int bytesRead = ss.read(buff);
assertEquals(text[readPosition], buff[0]);
assertEquals(buff.length, n);
assertEquals(buff.length, bytesRead);
}
/**
@@ -231,14 +231,14 @@ public class StreamSourceTest {
ss.open();
ss.close();
int n = -1;
int bytesRead = -1;
try {
byte[] buff = new byte[1];
n = ss.read(buff);
bytesRead = ss.read(buff);
} catch (IOException ignored) {
}
assertEquals(-1, n);
assertEquals(-1, bytesRead);
}
/** Purpose: Get MIME type Input: no Expected: return "txt" */
@@ -39,6 +39,7 @@ public class ShadowSmbFile {
private File file = null;
@Implementation
@SuppressWarnings("PMD")
public void __constructor__(URL url, NtlmPasswordAuthentication auth) {
// intentionally empty
}