1
1
package app .revanced .extension .shared ;
2
2
3
3
import static app .revanced .extension .shared .StringRef .str ;
4
+ import static app .revanced .extension .shared .requests .Route .Method .GET ;
4
5
5
6
import android .annotation .SuppressLint ;
6
7
import android .app .Activity ;
15
16
import android .os .PowerManager ;
16
17
import android .provider .Settings ;
17
18
19
+ import androidx .annotation .Nullable ;
18
20
import androidx .annotation .RequiresApi ;
19
21
22
+ import java .net .HttpURLConnection ;
20
23
import java .net .MalformedURLException ;
21
24
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 ;
22
29
23
30
@ SuppressWarnings ("unused" )
24
31
public class GmsCoreSupport {
@@ -29,10 +36,24 @@ public class GmsCoreSupport {
29
36
= getGmsCoreVendorGroupId () + ".android.gms" ;
30
37
private static final Uri GMS_CORE_PROVIDER
31
38
= 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 ;
34
53
35
54
private static void open (String queryOrLink ) {
55
+ Logger .printInfo (() -> "Opening link: " + queryOrLink );
56
+
36
57
Intent intent ;
37
58
try {
38
59
// Check if queryOrLink is a valid URL.
@@ -86,7 +107,7 @@ public static void checkGmsCore(Activity context) {
86
107
87
108
// Do not exit. If the app exits before launch completes (and without
88
109
// 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
90
111
// with the appearance of a hung app.
91
112
}
92
113
@@ -122,11 +143,12 @@ public static void checkGmsCore(Activity context) {
122
143
try (var client = context .getContentResolver ().acquireContentProviderClient (GMS_CORE_PROVIDER )) {
123
144
if (client == null ) {
124
145
Logger .printInfo (() -> "GmsCore is not running in the background" );
146
+ checkIfDontKillMyAppSupportsManufacturer ();
125
147
126
148
showBatteryOptimizationDialog (context ,
127
149
"gms_core_dialog_not_whitelisted_not_allowed_in_background_message" ,
128
150
"gms_core_dialog_open_website_text" ,
129
- (dialog , id ) -> open ( DONT_KILL_MY_APP_LINK ));
151
+ (dialog , id ) -> openDontKillMyApp ( ));
130
152
}
131
153
}
132
154
} catch (Exception ex ) {
@@ -141,6 +163,48 @@ private static void openGmsCoreDisableBatteryOptimizationsIntent(Activity activi
141
163
activity .startActivityForResult (intent , 0 );
142
164
}
143
165
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
+
144
208
/**
145
209
* @return If GmsCore is not whitelisted from battery optimizations.
146
210
*/
0 commit comments