dev
java 93 lines 3.47 KB
Raw
1 package com.cakewallet.cake_wallet;
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.Bundle;
15 import android.os.Handler;
16 import android.os.Looper;
17 import android.view.WindowManager;
18 import android.content.Intent;
19 import android.net.Uri;
20 import android.os.PowerManager;
21 import android.provider.Settings;
22
23 import java.security.SecureRandom;
24
25 public class MainActivity extends FlutterFragmentActivity {
26 final String UTILS_CHANNEL = "com.cake_wallet/native_utils";
27 boolean isAppSecure = false;
28
29 @Override
30 public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
31 GeneratedPluginRegistrant.registerWith(flutterEngine);
32
33 MethodChannel utilsChannel =
34 new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(),
35 UTILS_CHANNEL);
36
37 utilsChannel.setMethodCallHandler(this::handle);
38 }
39
40 private void handle(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
41 Handler handler = new Handler(Looper.getMainLooper());
42
43 try {
44 switch (call.method) {
45 case "sec_random":
46 int count = call.argument("count");
47 SecureRandom random = new SecureRandom();
48 byte bytes[] = new byte[count];
49 random.nextBytes(bytes);
50 handler.post(() -> result.success(bytes));
51 break;
52 case "setIsAppSecure":
53 isAppSecure = call.argument("isAppSecure");
54 if (isAppSecure) {
55 getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
56 } else {
57 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
58 }
59 break;
60 case "disableBatteryOptimization":
61 disableBatteryOptimization();
62 handler.post(() -> result.success(null));
63 break;
64 case "isBatteryOptimizationDisabled":
65 boolean isDisabled = isBatteryOptimizationDisabled();
66 handler.post(() -> result.success(isDisabled));
67 break;
68 default:
69 handler.post(() -> result.notImplemented());
70 }
71 } catch (Exception e) {
72 handler.post(() -> result.error("UNCAUGHT_ERROR", e.getMessage(), null));
73 }
74 }
75
76 private void disableBatteryOptimization() {
77 String packageName = getPackageName();
78 PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
79 if (!pm.isIgnoringBatteryOptimizations(packageName)) {
80 Intent intent = new Intent();
81 intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
82 intent.setData(Uri.parse("package:" + packageName));
83 startActivity(intent);
84 }
85 }
86
87 private boolean isBatteryOptimizationDisabled() {
88 String packageName = getPackageName();
89 PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
90 return pm.isIgnoringBatteryOptimizations(packageName);
91 }
92
93 }