Skip to content

Commit b89927a

Browse files
feat(GmsCore support): Open vendor specific DontKillMyApp if available (#4952)
1 parent dda7473 commit b89927a

File tree

1 file changed

+68
-4
lines changed

1 file changed

+68
-4
lines changed

extensions/shared/library/src/main/java/app/revanced/extension/shared/GmsCoreSupport.java

+68-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package app.revanced.extension.shared;
22

33
import static app.revanced.extension.shared.StringRef.str;
4+
import static app.revanced.extension.shared.requests.Route.Method.GET;
45

56
import android.annotation.SuppressLint;
67
import android.app.Activity;
@@ -15,10 +16,16 @@
1516
import android.os.PowerManager;
1617
import android.provider.Settings;
1718

19+
import androidx.annotation.Nullable;
1820
import androidx.annotation.RequiresApi;
1921

22+
import java.net.HttpURLConnection;
2023
import java.net.MalformedURLException;
2124
import java.net.URL;
25+
import java.util.Locale;
26+
27+
import app.revanced.extension.shared.requests.Requester;
28+
import app.revanced.extension.shared.requests.Route;
2229

2330
@SuppressWarnings("unused")
2431
public class GmsCoreSupport {
@@ -29,10 +36,24 @@ public class GmsCoreSupport {
2936
= getGmsCoreVendorGroupId() + ".android.gms";
3037
private static final Uri GMS_CORE_PROVIDER
3138
= Uri.parse("content://" + getGmsCoreVendorGroupId() + ".android.gsf.gservices/prefix");
32-
private static final String DONT_KILL_MY_APP_LINK
33-
= "https://dontkillmyapp.com";
39+
private static final String DONT_KILL_MY_APP_URL
40+
= "https://dontkillmyapp.com/";
41+
private static final Route DONT_KILL_MY_APP_MANUFACTURER_API
42+
= new Route(GET, "/api/v2/{manufacturer}.json");
43+
private static final String DONT_KILL_MY_APP_NAME_PARAMETER
44+
= "?app=MicroG";
45+
private static final String BUILD_MANUFACTURER
46+
= Build.MANUFACTURER.toLowerCase(Locale.ROOT).replace(" ", "-");
47+
48+
/**
49+
* If a manufacturer specific page exists on DontKillMyApp.
50+
*/
51+
@Nullable
52+
private static volatile Boolean DONT_KILL_MY_APP_MANUFACTURER_SUPPORTED;
3453

3554
private static void open(String queryOrLink) {
55+
Logger.printInfo(() -> "Opening link: " + queryOrLink);
56+
3657
Intent intent;
3758
try {
3859
// Check if queryOrLink is a valid URL.
@@ -86,7 +107,7 @@ public static void checkGmsCore(Activity context) {
86107

87108
// Do not exit. If the app exits before launch completes (and without
88109
// opening another activity), then on some devices such as Pixel phone Android 10
89-
// no toast will be shown and the app will continually be relaunched
110+
// no toast will be shown and the app will continually relaunch
90111
// with the appearance of a hung app.
91112
}
92113

@@ -122,11 +143,12 @@ public static void checkGmsCore(Activity context) {
122143
try (var client = context.getContentResolver().acquireContentProviderClient(GMS_CORE_PROVIDER)) {
123144
if (client == null) {
124145
Logger.printInfo(() -> "GmsCore is not running in the background");
146+
checkIfDontKillMyAppSupportsManufacturer();
125147

126148
showBatteryOptimizationDialog(context,
127149
"gms_core_dialog_not_whitelisted_not_allowed_in_background_message",
128150
"gms_core_dialog_open_website_text",
129-
(dialog, id) -> open(DONT_KILL_MY_APP_LINK));
151+
(dialog, id) -> openDontKillMyApp());
130152
}
131153
}
132154
} catch (Exception ex) {
@@ -141,6 +163,48 @@ private static void openGmsCoreDisableBatteryOptimizationsIntent(Activity activi
141163
activity.startActivityForResult(intent, 0);
142164
}
143165

166+
private static void checkIfDontKillMyAppSupportsManufacturer() {
167+
Utils.runOnBackgroundThread(() -> {
168+
try {
169+
final long start = System.currentTimeMillis();
170+
HttpURLConnection connection = Requester.getConnectionFromRoute(
171+
DONT_KILL_MY_APP_URL, DONT_KILL_MY_APP_MANUFACTURER_API, BUILD_MANUFACTURER);
172+
connection.setConnectTimeout(5000);
173+
connection.setReadTimeout(5000);
174+
175+
final boolean supported = connection.getResponseCode() == 200;
176+
Logger.printInfo(() -> "Manufacturer is " + (supported ? "" : "NOT ")
177+
+ "listed on DontKillMyApp: " + BUILD_MANUFACTURER
178+
+ " fetch took: " + (System.currentTimeMillis() - start) + "ms");
179+
DONT_KILL_MY_APP_MANUFACTURER_SUPPORTED = supported;
180+
} catch (Exception ex) {
181+
Logger.printInfo(() -> "Could not check if manufacturer is listed on DontKillMyApp: "
182+
+ BUILD_MANUFACTURER, ex);
183+
DONT_KILL_MY_APP_MANUFACTURER_SUPPORTED = null;
184+
}
185+
});
186+
}
187+
188+
private static void openDontKillMyApp() {
189+
final Boolean manufacturerSupported = DONT_KILL_MY_APP_MANUFACTURER_SUPPORTED;
190+
191+
String manufacturerPageToOpen;
192+
if (manufacturerSupported == null) {
193+
// Fetch has not completed yet. Only happens on extremely slow internet connections
194+
// and the user spends less than 1 second reading what's on screen.
195+
// Instead of waiting for the fetch (which may timeout),
196+
// open the website without a vendor.
197+
manufacturerPageToOpen = "";
198+
} else if (manufacturerSupported) {
199+
manufacturerPageToOpen = BUILD_MANUFACTURER;
200+
} else {
201+
// No manufacturer specific page exists. Open the general page.
202+
manufacturerPageToOpen = "general";
203+
}
204+
205+
open(DONT_KILL_MY_APP_URL + manufacturerPageToOpen + DONT_KILL_MY_APP_NAME_PARAMETER);
206+
}
207+
144208
/**
145209
* @return If GmsCore is not whitelisted from battery optimizations.
146210
*/

0 commit comments

Comments
 (0)