| 1 | package com.cakewallet.haven; |
| 2 | |
| 3 | import androidx.annotation.NonNull; |
| 4 | |
| 5 | import io.flutter.embedding.android.FlutterFragmentActivity; |
| 6 | import io.flutter.embedding.engine.FlutterEngine; |
| 7 | import io.flutter.plugins.GeneratedPluginRegistrant; |
| 8 | |
| 9 | import io.flutter.plugin.common.MethodCall; |
| 10 | import io.flutter.plugin.common.MethodChannel; |
| 11 | |
| 12 | import android.os.AsyncTask; |
| 13 | import android.os.Build; |
| 14 | import android.os.Handler; |
| 15 | import android.os.Looper; |
| 16 | import android.view.WindowManager; |
| 17 | import android.content.Intent; |
| 18 | import android.net.Uri; |
| 19 | import android.os.PowerManager; |
| 20 | import android.provider.Settings; |
| 21 | |
| 22 | import java.security.SecureRandom; |
| 23 | |
| 24 | public class MainActivity extends FlutterFragmentActivity { |
| 25 | final String UTILS_CHANNEL = "com.cake_wallet/native_utils"; |
| 26 | |
| 27 | @Override |
| 28 | public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) { |
| 29 | GeneratedPluginRegistrant.registerWith(flutterEngine); |
| 30 | |
| 31 | MethodChannel utilsChannel = |
| 32 | new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), |
| 33 | UTILS_CHANNEL); |
| 34 | |
| 35 | utilsChannel.setMethodCallHandler(this::handle); |
| 36 | } |
| 37 | |
| 38 | private void handle(@NonNull MethodCall call, @NonNull MethodChannel.Result result) { |
| 39 | Handler handler = new Handler(Looper.getMainLooper()); |
| 40 | |
| 41 | try { |
| 42 | switch (call.method) { |
| 43 | case "sec_random": |
| 44 | int count = call.argument("count"); |
| 45 | SecureRandom random = new SecureRandom(); |
| 46 | byte bytes[] = new byte[count]; |
| 47 | random.nextBytes(bytes); |
| 48 | handler.post(() -> result.success(bytes)); |
| 49 | break; |
| 50 | case "disableBatteryOptimization": |
| 51 | disableBatteryOptimization(); |
| 52 | handler.post(() -> result.success(null)); |
| 53 | break; |
| 54 | case "isBatteryOptimizationDisabled": |
| 55 | boolean isDisabled = isBatteryOptimizationDisabled(); |
| 56 | handler.post(() -> result.success(isDisabled)); |
| 57 | break; |
| 58 | default: |
| 59 | handler.post(() -> result.notImplemented()); |
| 60 | } |
| 61 | } catch (Exception e) { |
| 62 | handler.post(() -> result.error("UNCAUGHT_ERROR", e.getMessage(), null)); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | private void disableBatteryOptimization() { |
| 67 | String packageName = getPackageName(); |
| 68 | PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE); |
| 69 | if (!pm.isIgnoringBatteryOptimizations(packageName)) { |
| 70 | Intent intent = new Intent(); |
| 71 | intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); |
| 72 | intent.setData(Uri.parse("package:" + packageName)); |
| 73 | startActivity(intent); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | private boolean isBatteryOptimizationDisabled() { |
| 78 | String packageName = getPackageName(); |
| 79 | PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE); |
| 80 | return pm.isIgnoringBatteryOptimizations(packageName); |
| 81 | } |
| 82 | |
| 83 | } |