From b73eb6f9010d44c0c167dc6bdb72091321cc9b63 Mon Sep 17 00:00:00 2001 From: Nullptr Date: Tue, 15 Aug 2023 17:28:09 +0800 Subject: [PATCH] Some changes on exceptions --- interface/build.gradle.kts | 4 +- service/build.gradle.kts | 4 +- .../libxposed/service/XposedService.java | 74 +++++++++++++------ 3 files changed, 56 insertions(+), 26 deletions(-) diff --git a/interface/build.gradle.kts b/interface/build.gradle.kts index 3cb21ac..67d6000 100644 --- a/interface/build.gradle.kts +++ b/interface/build.gradle.kts @@ -20,8 +20,8 @@ android { } compileOptions { - targetCompatibility = JavaVersion.VERSION_11 - sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_17 + sourceCompatibility = JavaVersion.VERSION_17 } publishing { diff --git a/service/build.gradle.kts b/service/build.gradle.kts index 3536471..20251e1 100644 --- a/service/build.gradle.kts +++ b/service/build.gradle.kts @@ -19,8 +19,8 @@ android { } compileOptions { - sourceCompatibility = JavaVersion.VERSION_11 - targetCompatibility = JavaVersion.VERSION_11 + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 } publishing { diff --git a/service/src/main/java/io/github/libxposed/service/XposedService.java b/service/src/main/java/io/github/libxposed/service/XposedService.java index 9453f10..f3b60e3 100644 --- a/service/src/main/java/io/github/libxposed/service/XposedService.java +++ b/service/src/main/java/io/github/libxposed/service/XposedService.java @@ -3,7 +3,6 @@ package io.github.libxposed.service; import static android.os.ParcelFileDescriptor.MODE_READ_ONLY; import android.content.SharedPreferences; -import android.os.Bundle; import android.os.RemoteException; import androidx.annotation.NonNull; @@ -22,7 +21,11 @@ import java.util.concurrent.locks.ReentrantReadWriteLock; public final class XposedService { public final static class ServiceException extends RuntimeException { - private ServiceException(RemoteException e) { + ServiceException(String message) { + super(message); + } + + ServiceException(RemoteException e) { super("Xposed service error", e); } } @@ -148,7 +151,7 @@ public final class XposedService { * Get the Xposed API version of current implementation. * * @return API version - * @throws ServiceException If the service is dead or error occurred + * @throws ServiceException If the service is dead or an error occurred */ public int getAPIVersion() { try { @@ -162,7 +165,7 @@ public final class XposedService { * Get the Xposed framework name of current implementation. * * @return Framework name - * @throws ServiceException If the service is dead or error occurred + * @throws ServiceException If the service is dead or an error occurred */ @NonNull public String getFrameworkName() { @@ -177,7 +180,7 @@ public final class XposedService { * Get the Xposed framework version of current implementation. * * @return Framework version - * @throws ServiceException If the service is dead or error occurred + * @throws ServiceException If the service is dead or an error occurred */ @NonNull public String getFrameworkVersion() { @@ -192,7 +195,7 @@ public final class XposedService { * Get the Xposed framework version code of current implementation. * * @return Framework version code - * @throws ServiceException If the service is dead or error occurred + * @throws ServiceException If the service is dead or an error occurred */ public long getFrameworkVersionCode() { try { @@ -206,7 +209,7 @@ public final class XposedService { * Get the Xposed framework privilege of current implementation. * * @return Framework privilege - * @throws ServiceException If the service is dead or error occurred + * @throws ServiceException If the service is dead or an error occurred */ @NonNull public Privilege getFrameworkPrivilege() { @@ -222,7 +225,7 @@ public final class XposedService { * Get the application scope of current module. * * @return Module scope - * @throws ServiceException If the service is dead or error occurred + * @throws ServiceException If the service is dead or an error occurred */ @NonNull public List getScope() { @@ -238,7 +241,7 @@ public final class XposedService { * * @param packageName Package name of the app to be added * @param callback Callback to be invoked when the request is completed or error occurred - * @throws ServiceException If the service is dead or error occurred + * @throws ServiceException If the service is dead or an error occurred */ public void requestScope(@NonNull String packageName, @NonNull OnScopeEventListener callback) { try { @@ -253,7 +256,7 @@ public final class XposedService { * * @param packageName Package name of the app to be added * @return null if successful, or non-null with error message - * @throws ServiceException If the service is dead or error occurred + * @throws ServiceException If the service is dead or an error occurred */ @Nullable public String removeScope(@NonNull String packageName) { @@ -265,18 +268,26 @@ public final class XposedService { } /** - * Get remote preferences from Xposed framework. + * Get remote preferences from Xposed framework. If the group does not exist, it will be created. * * @param group Group name - * @return The preferences, null if the framework is embedded - * @throws ServiceException If the service is dead or error occurred + * @return The preferences + * @throws ServiceException If the service is dead or an error occurred + * @throws UnsupportedOperationException If the framework is embedded */ - @Nullable + @NonNull public SharedPreferences getRemotePreferences(@NonNull String group) { return mRemotePrefs.computeIfAbsent(group, k -> { try { - return RemotePreferences.newInstance(this, k); + var instance = RemotePreferences.newInstance(this, k); + if (instance == null) { + throw new ServiceException("Framework returns null"); + } + return instance; } catch (RemoteException e) { + if (e.getCause() instanceof UnsupportedOperationException cause) { + throw cause; + } throw new ServiceException(e); } }); @@ -286,7 +297,8 @@ public final class XposedService { * Delete a group of remote preferences. * * @param group Group name - * @throws ServiceException If the service is dead or error occurred + * @throws ServiceException If the service is dead or an error occurred + * @throws UnsupportedOperationException If the framework is embedded */ public void deleteRemotePreferences(@NonNull String group) { deletionLock.writeLock().lock(); @@ -297,6 +309,9 @@ public final class XposedService { return null; }); } catch (RemoteException e) { + if (e.getCause() instanceof UnsupportedOperationException cause) { + throw cause; + } throw new ServiceException(e); } finally { deletionLock.writeLock().unlock(); @@ -308,7 +323,8 @@ public final class XposedService { * * @param name File name, must not contain path separators and . or .. * @return The InputStream - * @throws FileNotFoundException If the file does not exist, the path is forbidden or remote file is not supported by the framework + * @throws FileNotFoundException If the file does not exist or the path is forbidden + * @throws UnsupportedOperationException If the framework is embedded */ @NonNull public FileInputStream openRemoteFileInput(@NonNull String name) throws FileNotFoundException { @@ -317,6 +333,9 @@ public final class XposedService { if (file == null) throw new FileNotFoundException(); return new FileInputStream(file.getFileDescriptor()); } catch (RemoteException e) { + if (e.getCause() instanceof UnsupportedOperationException cause) { + throw cause; + } throw new FileNotFoundException(e.getMessage()); } } @@ -327,7 +346,8 @@ public final class XposedService { * @param name File name, must not contain path separators and . or .. * @param mode Operating mode * @return The OutputStream - * @throws FileNotFoundException If the path is forbidden or remote file is not supported by the framework + * @throws FileNotFoundException If the path is forbidden + * @throws UnsupportedOperationException If the framework is embedded */ @NonNull public FileOutputStream openRemoteFileOutput(@NonNull String name, int mode) throws FileNotFoundException { @@ -336,6 +356,9 @@ public final class XposedService { if (file == null) throw new FileNotFoundException(); return new FileOutputStream(file.getFileDescriptor()); } catch (RemoteException e) { + if (e.getCause() instanceof UnsupportedOperationException cause) { + throw cause; + } throw new FileNotFoundException(e.getMessage()); } } @@ -346,11 +369,15 @@ public final class XposedService { * @param name File name, must not contain path separators and . or .. * @return true if successful, false if the file does not exist * @throws FileNotFoundException If the path is forbidden or remote file is not supported by the framework + * @throws UnsupportedOperationException If the framework is embedded */ public boolean deleteRemoteFile(@NonNull String name) throws FileNotFoundException { try { return mService.deleteRemoteFile(name); } catch (RemoteException e) { + if (e.getCause() instanceof UnsupportedOperationException cause) { + throw cause; + } throw new FileNotFoundException(e.getMessage()); } } @@ -359,16 +386,19 @@ public final class XposedService { * List all files in the module's shared data directory. * * @return The file list - * @throws FileNotFoundException If remote file is not supported by the framework + * @throws UnsupportedOperationException If the framework is embedded */ @NonNull - public String[] listRemoteFiles() throws FileNotFoundException { + public String[] listRemoteFiles() { try { var files = mService.listRemoteFiles(); - if (files == null) throw new FileNotFoundException(); + if (files == null) throw new ServiceException("Framework returns null"); return files; } catch (RemoteException e) { - throw new FileNotFoundException(e.getMessage()); + if (e.getCause() instanceof UnsupportedOperationException cause) { + throw cause; + } + throw new ServiceException(e); } } }