|
27 | 27 | import android.app.NotificationChannel; |
28 | 28 | import android.app.NotificationManager; |
29 | 29 | import android.content.Context; |
| 30 | +import android.content.Intent; |
30 | 31 | import android.content.pm.PackageManager; |
31 | 32 | import android.net.ConnectivityManager; |
32 | 33 | import android.net.Network; |
33 | 34 | import android.net.NetworkCapabilities; |
| 35 | +import android.net.Uri; |
34 | 36 | import android.os.Build; |
35 | 37 | import android.os.PowerManager; |
36 | 38 | import android.provider.Settings; |
@@ -139,21 +141,49 @@ public static boolean checkNotificationEnabled(Context context) { |
139 | 141 | } |
140 | 142 |
|
141 | 143 | public static boolean checkBatteryOptimizations(Context context) { |
142 | | - if ("xiaomi".equalsIgnoreCase(Build.MANUFACTURER)) { |
143 | | - XLog.tag(TAG).i("Battery optimization check skipped [manufacturer=%s, brand=%s, model=%s]", |
144 | | - Build.MANUFACTURER, Build.BRAND, Build.MODEL); |
145 | | - return true; |
146 | | - } |
147 | 144 | boolean isGranted = false; |
148 | 145 | PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE); |
149 | 146 | if (powerManager != null) { |
150 | 147 | isGranted = powerManager.isIgnoringBatteryOptimizations(context.getPackageName()); |
151 | 148 | } |
| 149 | + // MIUI (Xiaomi/Redmi/POCO all report MANUFACTURER=xiaomi) runs its own battery manager; |
| 150 | + // isIgnoringBatteryOptimizations() stays false even after the user grants the exemption. |
| 151 | + // Trusting it would nag on every connect and never let the first-run flow complete, so once |
| 152 | + // we have sent the user to the system dialog we treat the exemption as handled here. |
| 153 | + if (!isGranted && isXiaomi() && SharedPrefUtils.isBatteryOptimizationRequested(context)) { |
| 154 | + isGranted = true; |
| 155 | + } |
152 | 156 | XLog.tag(TAG).i("Battery optimization exemption [granted=%b, manufacturer=%s, brand=%s, model=%s]", |
153 | 157 | isGranted, Build.MANUFACTURER, Build.BRAND, Build.MODEL); |
154 | 158 | return isGranted; |
155 | 159 | } |
156 | 160 |
|
| 161 | + public static boolean isXiaomi() { |
| 162 | + return "xiaomi".equalsIgnoreCase(Build.MANUFACTURER) |
| 163 | + || "xiaomi".equalsIgnoreCase(Build.BRAND) |
| 164 | + || "redmi".equalsIgnoreCase(Build.BRAND) |
| 165 | + || "poco".equalsIgnoreCase(Build.BRAND); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Opens the app-details screen so the user can reach the MIUI background / battery controls |
| 170 | + * (the standard battery-optimization exemption is not enough on MIUI). |
| 171 | + * |
| 172 | + * @return true if some settings screen was launched. |
| 173 | + */ |
| 174 | + public static boolean openMiuiBackgroundSettings(Context context) { |
| 175 | + try { |
| 176 | + Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); |
| 177 | + intent.setData(Uri.parse("package:" + context.getPackageName())); |
| 178 | + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 179 | + context.startActivity(intent); |
| 180 | + return true; |
| 181 | + } catch (Exception e) { |
| 182 | + XLog.tag(TAG).e("Failed to open settings for MIUI guidance: %s", e.getMessage()); |
| 183 | + return false; |
| 184 | + } |
| 185 | + } |
| 186 | + |
157 | 187 | public static boolean checkBackgroundDataTransferRestrictions(Context context) { |
158 | 188 | boolean isGranted = false; |
159 | 189 | ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE); |
|
0 commit comments