用java写一个完整的Shizuku的内容提供器
时间: 2024-02-22 07:00:23 浏览: 138
一个完整的java web项目 (包含数据库)
5星 · 资源好评率100%
以下是使用Java编写的完整Shizuku内容提供器的示例代码,主要是演示如何使用Shizuku API来访问受保护的系统资源:
```java
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.Binder;
import android.os.Bundle;
import android.os.Process;
import android.os.UserHandle;
import android.util.Log;
import android.widget.Toast;
import java.util.Arrays;
import java.util.List;
import moe.shizuku.api.ShizukuApi;
public class MyContentProvider extends ContentProvider {
private static final String TAG = "MyContentProvider";
private static final String AUTHORITY = "com.example.myapp.provider";
private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
private static final int TYPE_STRING = 1;
private static final int TYPE_INT = 2;
private static final int TYPE_BOOLEAN = 3;
private static final int TYPE_FLOAT = 4;
private static final int TYPE_LONG = 5;
private static final int CODE_STRING = 1;
private static final int CODE_INT = 2;
private static final int CODE_BOOLEAN = 3;
private static final int CODE_FLOAT = 4;
private static final int CODE_LONG = 5;
private static final String KEY_NAME = "name";
private static final String KEY_TYPE = "type";
private static final String KEY_VALUE = "value";
static {
uriMatcher.addURI(AUTHORITY, "string/*", CODE_STRING);
uriMatcher.addURI(AUTHORITY, "int/*", CODE_INT);
uriMatcher.addURI(AUTHORITY, "boolean/*", CODE_BOOLEAN);
uriMatcher.addURI(AUTHORITY, "float/*", CODE_FLOAT);
uriMatcher.addURI(AUTHORITY, "long/*", CODE_LONG);
}
@Override
public boolean onCreate() {
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
String name = uri.getLastPathSegment();
int type = uriMatcher.match(uri);
if (type <= 0) {
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
if (ShizukuApi.checkSelfPermission() != ShizukuApi.PERMISSION_GRANTED) {
throw new SecurityException("Missing Shizuku permission");
}
switch (type) {
case CODE_STRING:
String value = getStringValue(name);
MatrixCursor cursor = new MatrixCursor(new String[]{KEY_NAME, KEY_TYPE, KEY_VALUE});
cursor.addRow(new Object[]{name, TYPE_STRING, value});
return cursor;
case CODE_INT:
int intValue = getIntValue(name);
MatrixCursor cursor1 = new MatrixCursor(new String[]{KEY_NAME, KEY_TYPE, KEY_VALUE});
cursor1.addRow(new Object[]{name, TYPE_INT, intValue});
return cursor1;
case CODE_BOOLEAN:
boolean booleanValue = getBooleanValue(name);
MatrixCursor cursor2 = new MatrixCursor(new String[]{KEY_NAME, KEY_TYPE, KEY_VALUE});
cursor2.addRow(new Object[]{name, TYPE_BOOLEAN, booleanValue});
return cursor2;
case CODE_FLOAT:
float floatValue = getFloatValue(name);
MatrixCursor cursor3 = new MatrixCursor(new String[]{KEY_NAME, KEY_TYPE, KEY_VALUE});
cursor3.addRow(new Object[]{name, TYPE_FLOAT, floatValue});
return cursor3;
case CODE_LONG:
long longValue = getLongValue(name);
MatrixCursor cursor4 = new MatrixCursor(new String[]{KEY_NAME, KEY_TYPE, KEY_VALUE});
cursor4.addRow(new Object[]{name, TYPE_LONG, longValue});
return cursor4;
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)) {
case CODE_STRING:
return "vnd.android.cursor.item/vnd." + AUTHORITY + ".string";
case CODE_INT:
return "vnd.android.cursor.item/vnd." + AUTHORITY + ".int";
case CODE_BOOLEAN:
return "vnd.android.cursor.item/vnd." + AUTHORITY + ".boolean";
case CODE_FLOAT:
return "vnd.android.cursor.item/vnd." + AUTHORITY + ".float";
case CODE_LONG:
return "vnd.android.cursor.item/vnd." + AUTHORITY + ".long";
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
throw new UnsupportedOperationException("Not supported");
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
throw new UnsupportedOperationException("Not supported");
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
throw new UnsupportedOperationException("Not supported");
}
private String getStringValue(String name) {
try {
Bundle result = ShizukuApi.transact("com.android.providers.settings", null, "GET_system", new Class[]{String.class}, new Object[]{name}, Bundle.class);
if (result != null) {
return result.getString("value");
}
} catch (Throwable e) {
Log.e(TAG, "getStringValue: ", e);
}
return null;
}
private int getIntValue(String name) {
try {
Bundle result = ShizukuApi.transact("com.android.providers.settings", null, "GET_system", new Class[]{String.class}, new Object[]{name}, Bundle.class);
if (result != null) {
return result.getInt("value");
}
} catch (Throwable e) {
Log.e(TAG, "getIntValue: ", e);
}
return 0;
}
private boolean getBooleanValue(String name) {
try {
Bundle result = ShizukuApi.transact("com.android.providers.settings", null, "GET_system", new Class[]{String.class}, new Object[]{name}, Bundle.class);
if (result != null) {
return result.getBoolean("value");
}
} catch (Throwable e) {
Log.e(TAG, "getBooleanValue: ", e);
}
return false;
}
private float getFloatValue(String name) {
try {
Bundle result = ShizukuApi.transact("com.android.providers.settings", null, "GET_system", new Class[]{String.class}, new Object[]{name}, Bundle.class);
if (result != null) {
return result.getFloat("value");
}
} catch (Throwable e) {
Log.e(TAG, "getFloatValue: ", e);
}
return 0;
}
private long getLongValue(String name) {
try {
Bundle result = ShizukuApi.transact("com.android.providers.settings", null, "GET_system", new Class[]{String.class}, new Object[]{name}, Bundle.class);
if (result != null) {
return result.getLong("value");
}
} catch (Throwable e) {
Log.e(TAG, "getLongValue: ", e);
}
return 0;
}
}
```
在此示例中,我们使用Shizuku API来访问受保护的系统资源,例如系统设置。我们使用UriMatcher来匹配传入的URI,并根据请求的类型调用相应的方法来获取相应的值。我们还检查了Shizuku权限,如果权限缺失,则会抛出SecurityException。
阅读全文