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 { compileOptions {
targetCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_17
sourceCompatibility = JavaVersion.VERSION_11 sourceCompatibility = JavaVersion.VERSION_17
} }
publishing { publishing {

View File

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

View File

@ -3,7 +3,6 @@ package io.github.libxposed.service;
import static android.os.ParcelFileDescriptor.MODE_READ_ONLY; import static android.os.ParcelFileDescriptor.MODE_READ_ONLY;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.RemoteException; import android.os.RemoteException;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -22,7 +21,11 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
public final class XposedService { public final class XposedService {
public final static class ServiceException extends RuntimeException { public final static class ServiceException extends RuntimeException {
private ServiceException(RemoteException e) { ServiceException(String message) {
super(message);
}
ServiceException(RemoteException e) {
super("Xposed service error", e); super("Xposed service error", e);
} }
} }
@ -148,7 +151,7 @@ public final class XposedService {
* Get the Xposed API version of current implementation. * Get the Xposed API version of current implementation.
* *
* @return API version * @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() { public int getAPIVersion() {
try { try {
@ -162,7 +165,7 @@ public final class XposedService {
* Get the Xposed framework name of current implementation. * Get the Xposed framework name of current implementation.
* *
* @return Framework name * @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 @NonNull
public String getFrameworkName() { public String getFrameworkName() {
@ -177,7 +180,7 @@ public final class XposedService {
* Get the Xposed framework version of current implementation. * Get the Xposed framework version of current implementation.
* *
* @return Framework version * @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 @NonNull
public String getFrameworkVersion() { public String getFrameworkVersion() {
@ -192,7 +195,7 @@ public final class XposedService {
* Get the Xposed framework version code of current implementation. * Get the Xposed framework version code of current implementation.
* *
* @return Framework version code * @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() { public long getFrameworkVersionCode() {
try { try {
@ -206,7 +209,7 @@ public final class XposedService {
* Get the Xposed framework privilege of current implementation. * Get the Xposed framework privilege of current implementation.
* *
* @return Framework privilege * @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 @NonNull
public Privilege getFrameworkPrivilege() { public Privilege getFrameworkPrivilege() {
@ -222,7 +225,7 @@ public final class XposedService {
* Get the application scope of current module. * Get the application scope of current module.
* *
* @return Module scope * @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 @NonNull
public List<String> getScope() { public List<String> getScope() {
@ -238,7 +241,7 @@ public final class XposedService {
* *
* @param packageName Package name of the app to be added * @param packageName Package name of the app to be added
* @param callback Callback to be invoked when the request is completed or error occurred * @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) { public void requestScope(@NonNull String packageName, @NonNull OnScopeEventListener callback) {
try { try {
@ -253,7 +256,7 @@ public final class XposedService {
* *
* @param packageName Package name of the app to be added * @param packageName Package name of the app to be added
* @return null if successful, or non-null with error message * @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 @Nullable
public String removeScope(@NonNull String packageName) { 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 * @param group Group name
* @return The preferences, null if the framework is embedded * @return The preferences
* @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
*/ */
@Nullable @NonNull
public SharedPreferences getRemotePreferences(@NonNull String group) { public SharedPreferences getRemotePreferences(@NonNull String group) {
return mRemotePrefs.computeIfAbsent(group, k -> { return mRemotePrefs.computeIfAbsent(group, k -> {
try { 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) { } catch (RemoteException e) {
if (e.getCause() instanceof UnsupportedOperationException cause) {
throw cause;
}
throw new ServiceException(e); throw new ServiceException(e);
} }
}); });
@ -286,7 +297,8 @@ public final class XposedService {
* Delete a group of remote preferences. * Delete a group of remote preferences.
* *
* @param group Group name * @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) { public void deleteRemotePreferences(@NonNull String group) {
deletionLock.writeLock().lock(); deletionLock.writeLock().lock();
@ -297,6 +309,9 @@ public final class XposedService {
return null; return null;
}); });
} catch (RemoteException e) { } catch (RemoteException e) {
if (e.getCause() instanceof UnsupportedOperationException cause) {
throw cause;
}
throw new ServiceException(e); throw new ServiceException(e);
} finally { } finally {
deletionLock.writeLock().unlock(); deletionLock.writeLock().unlock();
@ -308,7 +323,8 @@ public final class XposedService {
* *
* @param name File name, must not contain path separators and . or .. * @param name File name, must not contain path separators and . or ..
* @return The InputStream * @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 @NonNull
public FileInputStream openRemoteFileInput(@NonNull String name) throws FileNotFoundException { public FileInputStream openRemoteFileInput(@NonNull String name) throws FileNotFoundException {
@ -317,6 +333,9 @@ public final class XposedService {
if (file == null) throw new FileNotFoundException(); if (file == null) throw new FileNotFoundException();
return new FileInputStream(file.getFileDescriptor()); return new FileInputStream(file.getFileDescriptor());
} catch (RemoteException e) { } catch (RemoteException e) {
if (e.getCause() instanceof UnsupportedOperationException cause) {
throw cause;
}
throw new FileNotFoundException(e.getMessage()); 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 name File name, must not contain path separators and . or ..
* @param mode Operating mode * @param mode Operating mode
* @return The OutputStream * @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 @NonNull
public FileOutputStream openRemoteFileOutput(@NonNull String name, int mode) throws FileNotFoundException { public FileOutputStream openRemoteFileOutput(@NonNull String name, int mode) throws FileNotFoundException {
@ -336,6 +356,9 @@ public final class XposedService {
if (file == null) throw new FileNotFoundException(); if (file == null) throw new FileNotFoundException();
return new FileOutputStream(file.getFileDescriptor()); return new FileOutputStream(file.getFileDescriptor());
} catch (RemoteException e) { } catch (RemoteException e) {
if (e.getCause() instanceof UnsupportedOperationException cause) {
throw cause;
}
throw new FileNotFoundException(e.getMessage()); throw new FileNotFoundException(e.getMessage());
} }
} }
@ -346,11 +369,15 @@ public final class XposedService {
* @param name File name, must not contain path separators and . or .. * @param name File name, must not contain path separators and . or ..
* @return true if successful, false if the file does not exist * @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 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 { public boolean deleteRemoteFile(@NonNull String name) throws FileNotFoundException {
try { try {
return mService.deleteRemoteFile(name); return mService.deleteRemoteFile(name);
} catch (RemoteException e) { } catch (RemoteException e) {
if (e.getCause() instanceof UnsupportedOperationException cause) {
throw cause;
}
throw new FileNotFoundException(e.getMessage()); throw new FileNotFoundException(e.getMessage());
} }
} }
@ -359,16 +386,19 @@ public final class XposedService {
* List all files in the module's shared data directory. * List all files in the module's shared data directory.
* *
* @return The file list * @return The file list
* @throws FileNotFoundException If remote file is not supported by the framework * @throws UnsupportedOperationException If the framework is embedded
*/ */
@NonNull @NonNull
public String[] listRemoteFiles() throws FileNotFoundException { public String[] listRemoteFiles() {
try { try {
var files = mService.listRemoteFiles(); var files = mService.listRemoteFiles();
if (files == null) throw new FileNotFoundException(); if (files == null) throw new ServiceException("Framework returns null");
return files; return files;
} catch (RemoteException e) { } catch (RemoteException e) {
throw new FileNotFoundException(e.getMessage()); if (e.getCause() instanceof UnsupportedOperationException cause) {
throw cause;
}
throw new ServiceException(e);
} }
} }
} }