Some changes on exceptions

This commit is contained in:
Nullptr 2023-08-15 17:28:09 +08:00
parent 0d63ce21a8
commit b73eb6f901
No known key found for this signature in database
3 changed files with 56 additions and 26 deletions

View File

@ -20,8 +20,8 @@ android {
}
compileOptions {
targetCompatibility = JavaVersion.VERSION_11
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_17
sourceCompatibility = JavaVersion.VERSION_17
}
publishing {

View File

@ -19,8 +19,8 @@ android {
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
publishing {

View File

@ -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<String> 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);
}
}
}