Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
mywalkb committed May 31, 2023
2 parents 7c5687e + d972bf4 commit 07c4ef6
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 55 deletions.
115 changes: 66 additions & 49 deletions daemon/src/main/java/org/lsposed/lspd/service/BridgeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import android.os.Looper;
import android.os.Parcel;
import android.os.ServiceManager;
import android.system.ErrnoException;
import android.system.Os;
import android.util.Log;

import java.lang.reflect.Field;
Expand Down Expand Up @@ -69,71 +71,86 @@ public void binderDied() {
};

// For service
// This MUST run in main thread
private static synchronized void sendToBridge(IBinder binder, boolean isRestart) {
do {
bridgeService = ServiceManager.getService(SERVICE_NAME);
if (bridgeService != null && bridgeService.pingBinder()) {
break;
}
assert Looper.myLooper() == Looper.getMainLooper();
try {
Os.seteuid(0);
} catch (ErrnoException e) {
Log.e(TAG, "seteuid 0", e);
}
try {
do {
bridgeService = ServiceManager.getService(SERVICE_NAME);
if (bridgeService != null && bridgeService.pingBinder()) {
break;
}

Log.i(TAG, "service " + SERVICE_NAME + " is not started, wait 1s.");
Log.i(TAG, "service " + SERVICE_NAME + " is not started, wait 1s.");

try {
//noinspection BusyWait
Thread.sleep(1000);
} catch (Throwable e) {
Log.w(TAG, "sleep" + Log.getStackTraceString(e));
}
} while (true);

if (isRestart && listener != null) {
listener.onSystemServerRestarted();
}
try {
//noinspection BusyWait
Thread.sleep(1000);
} catch (Throwable e) {
Log.w(TAG, "sleep" + Log.getStackTraceString(e));
}
} while (true);

try {
bridgeService.linkToDeath(bridgeRecipient, 0);
} catch (Throwable e) {
Log.w(TAG, "linkToDeath " + Log.getStackTraceString(e));
var snapshot = bridgeService;
sendToBridge(binder, snapshot == null || !snapshot.isBinderAlive());
return;
}
if (isRestart && listener != null) {
listener.onSystemServerRestarted();
}

Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
boolean res = false;
// try at most three times
for (int i = 0; i < 3; i++) {
try {
data.writeInterfaceToken(DESCRIPTOR);
data.writeInt(ACTION.ACTION_SEND_BINDER.ordinal());
Log.v(TAG, "binder " + binder.toString());
data.writeStrongBinder(binder);
if (bridgeService == null) break;
res = bridgeService.transact(TRANSACTION_CODE, data, reply, 0);
reply.readException();
bridgeService.linkToDeath(bridgeRecipient, 0);
} catch (Throwable e) {
Log.e(TAG, "send binder " + Log.getStackTraceString(e));
Log.w(TAG, "linkToDeath " + Log.getStackTraceString(e));
var snapshot = bridgeService;
sendToBridge(binder, snapshot == null || !snapshot.isBinderAlive());
return;
} finally {
data.recycle();
reply.recycle();
}

if (res) break;
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
boolean res = false;
// try at most three times
for (int i = 0; i < 3; i++) {
try {
data.writeInterfaceToken(DESCRIPTOR);
data.writeInt(ACTION.ACTION_SEND_BINDER.ordinal());
Log.v(TAG, "binder " + binder.toString());
data.writeStrongBinder(binder);
if (bridgeService == null) break;
res = bridgeService.transact(TRANSACTION_CODE, data, reply, 0);
reply.readException();
} catch (Throwable e) {
Log.e(TAG, "send binder " + Log.getStackTraceString(e));
var snapshot = bridgeService;
sendToBridge(binder, snapshot == null || !snapshot.isBinderAlive());
return;
} finally {
data.recycle();
reply.recycle();
}

Log.w(TAG, "no response from bridge, retry in 1s");
if (res) break;

try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
Log.w(TAG, "no response from bridge, retry in 1s");

try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
}
}
}

if (listener != null) {
listener.onResponseFromBridgeService(res);
if (listener != null) {
listener.onResponseFromBridgeService(res);
}
} finally {
try {
Os.seteuid(1000);
} catch (ErrnoException e) {
Log.e(TAG, "seteuid 1000", e);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class LSPNotificationManager {
private static final IBinder.DeathRecipient recipient = new IBinder.DeathRecipient() {
@Override
public void binderDied() {
Log.w(TAG, "nm is dead");
Log.w(TAG, "notificationManager is dead");
binder.unlinkToDeath(this, 0);
binder = null;
notificationManager = null;
Expand Down Expand Up @@ -96,11 +96,16 @@ static Icon getNotificationIcon() {

static boolean hasNotificationChannelForSystem(
INotificationManager nm, String channelId) throws RemoteException {
NotificationChannel channel;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
return nm.getNotificationChannelForPackage("android", 1000, channelId, null, false) != null;
channel = nm.getNotificationChannelForPackage("android", 1000, channelId, null, false);
} else {
return nm.getNotificationChannelForPackage("android", 1000, channelId, false) != null;
channel = nm.getNotificationChannelForPackage("android", 1000, channelId, false);
}
if(channel != null) {
Log.d(TAG, "hasNotificationChannelForSystem: " + channel);
}
return channel != null;
}

static void createNotificationChannel(INotificationManager nm) throws RemoteException {
Expand All @@ -112,6 +117,7 @@ static void createNotificationChannel(INotificationManager nm) throws RemoteExce
NotificationManager.IMPORTANCE_HIGH);
updated.setShowBadge(false);
if (hasNotificationChannelForSystem(nm, UPDATED_CHANNEL_ID)) {
Log.d(TAG, "update notification channel: " + UPDATED_CHANNEL_ID);
nm.updateNotificationChannelForPackage("android", 1000, updated);
} else {
list.add(updated);
Expand All @@ -122,6 +128,7 @@ static void createNotificationChannel(INotificationManager nm) throws RemoteExce
NotificationManager.IMPORTANCE_MIN);
status.setShowBadge(false);
if (hasNotificationChannelForSystem(nm, STATUS_CHANNEL_ID)) {
Log.d(TAG, "update notification channel: " + STATUS_CHANNEL_ID);
nm.updateNotificationChannelForPackage("android", 1000, status);
} else {
list.add(status);
Expand All @@ -132,11 +139,13 @@ static void createNotificationChannel(INotificationManager nm) throws RemoteExce
NotificationManager.IMPORTANCE_HIGH);
scope.setShowBadge(false);
if (hasNotificationChannelForSystem(nm, SCOPE_CHANNEL_ID)) {
Log.d(TAG, "update notification channel: " + SCOPE_CHANNEL_ID);
nm.updateNotificationChannelForPackage("android", 1000, scope);
} else {
list.add(scope);
}

Log.d(TAG, "create notification channels for android: " + list);
nm.createNotificationChannelsForPackage("android", 1000, new ParceledListSlice<>(list));
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ rikkax-recyclerview = { module = "dev.rikka.rikkax.recyclerview:recyclerview-ktx
rikkax-widget-borderview = { module = "dev.rikka.rikkax.widget:borderview", version = "1.1.0" }
rikkax-widget-mainswitchbar = { module = "dev.rikka.rikkax.widget:mainswitchbar", version = "1.0.2" }

androidx-activity = { module = "androidx.activity:activity", version = "1.7.1" }
androidx-activity = { module = "androidx.activity:activity", version = "1.7.2" }
androidx-annotation = { module = "androidx.annotation:annotation", version = "1.6.0" }
androidx-browser = { module = "androidx.browser:browser", version = "1.5.0" }
androidx-constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version = "2.1.4" }
Expand Down
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#Sun May 21 15:38:01 CST 2023
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@
@Retention(SOURCE)
@Target({METHOD, PARAMETER, FIELD})
public @interface NonNull {
}
}

0 comments on commit 07c4ef6

Please sign in to comment.