|
| 1 | +package com.example.android_local_network; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.content.pm.PackageManager; |
| 5 | +import androidx.annotation.NonNull; |
| 6 | +import androidx.core.app.ActivityCompat; |
| 7 | +import androidx.core.content.ContextCompat; |
| 8 | +import io.flutter.embedding.engine.plugins.FlutterPlugin; |
| 9 | +import io.flutter.embedding.engine.plugins.activity.ActivityAware; |
| 10 | +import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding; |
| 11 | +import io.flutter.plugin.common.PluginRegistry; |
| 12 | + |
| 13 | +import android.os.Build; |
| 14 | +import android.util.Log; |
| 15 | + |
| 16 | +/** |
| 17 | + * AndroidLocalNetwork handles the ACCESS_LOCAL_NETWORK permission. |
| 18 | + * It can be used via jnigen to check and request permissions from Dart. |
| 19 | + */ |
| 20 | +public class AndroidLocalNetworkPlugin implements FlutterPlugin, ActivityAware, PluginRegistry.RequestPermissionsResultListener { |
| 21 | + private static final String TAG = "AndroidLocalNetwork"; |
| 22 | + private static final String PERMISSION = "android.permission.ACCESS_LOCAL_NETWORK"; |
| 23 | + private static final int REQUEST_CODE = 4853; // Arbitrary request code |
| 24 | + |
| 25 | + private Activity activity; |
| 26 | + private PermissionCallback pendingCallback; |
| 27 | + |
| 28 | + /** |
| 29 | + * Interface for permission result callbacks. |
| 30 | + */ |
| 31 | + public interface PermissionCallback { |
| 32 | + void onResult(boolean granted); |
| 33 | + } |
| 34 | + |
| 35 | + private static AndroidLocalNetworkPlugin instance; |
| 36 | + |
| 37 | + public static AndroidLocalNetworkPlugin getInstance() { |
| 38 | + Log.d(TAG, "getInstance called, instance is " + (instance == null ? "null" : "not null")); |
| 39 | + return instance; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) { |
| 44 | + Log.d(TAG, "onAttachedToEngine"); |
| 45 | + instance = this; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { |
| 50 | + Log.d(TAG, "onDetachedFromEngine"); |
| 51 | + if (instance == this) { |
| 52 | + instance = null; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) { |
| 58 | + Log.d(TAG, "onAttachedToActivity"); |
| 59 | + this.activity = binding.getActivity(); |
| 60 | + binding.addRequestPermissionsResultListener(this); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void onDetachedFromActivityForConfigChanges() { |
| 65 | + Log.d(TAG, "onDetachedFromActivityForConfigChanges"); |
| 66 | + this.activity = null; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) { |
| 71 | + Log.d(TAG, "onReattachedToActivityForConfigChanges"); |
| 72 | + this.activity = binding.getActivity(); |
| 73 | + binding.addRequestPermissionsResultListener(this); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void onDetachedFromActivity() { |
| 78 | + Log.d(TAG, "onDetachedFromActivity"); |
| 79 | + this.activity = null; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Checks if the local area permission is granted. |
| 84 | + */ |
| 85 | + public boolean checkPermission() { |
| 86 | + if (activity == null) { |
| 87 | + Log.w(TAG, "checkPermission: activity is null"); |
| 88 | + return false; |
| 89 | + } |
| 90 | + try { |
| 91 | + boolean granted = ContextCompat.checkSelfPermission(activity, PERMISSION) == PackageManager.PERMISSION_GRANTED; |
| 92 | + Log.d(TAG, "checkPermission for " + PERMISSION + ": " + granted); |
| 93 | + return granted; |
| 94 | + } catch (Exception e) { |
| 95 | + Log.e(TAG, "checkPermission error", e); |
| 96 | + return false; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Requests the local area permission. |
| 102 | + */ |
| 103 | + public void requestPermission(PermissionCallback callback) { |
| 104 | + Log.d(TAG, "requestPermission called. Current API: " + Build.VERSION.SDK_INT); |
| 105 | + |
| 106 | + if (activity == null) { |
| 107 | + Log.w(TAG, "requestPermission: activity is null"); |
| 108 | + callback.onResult(false); |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + if (checkPermission()) { |
| 113 | + callback.onResult(true); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + pendingCallback = callback; |
| 118 | + Log.d(TAG, "requestPermission: requesting from OS: " + PERMISSION); |
| 119 | + ActivityCompat.requestPermissions(activity, new String[]{PERMISSION}, REQUEST_CODE); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public boolean onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { |
| 124 | + Log.d(TAG, "onRequestPermissionsResult: requestCode=" + requestCode); |
| 125 | + if (requestCode == REQUEST_CODE) { |
| 126 | + boolean granted = grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED; |
| 127 | + Log.d(TAG, "onRequestPermissionsResult: granted=" + granted); |
| 128 | + if (pendingCallback != null) { |
| 129 | + pendingCallback.onResult(granted); |
| 130 | + pendingCallback = null; |
| 131 | + } |
| 132 | + return true; |
| 133 | + } |
| 134 | + return false; |
| 135 | + } |
| 136 | +} |
0 commit comments