Skip to content

Commit

Permalink
Refactor: Port PermissionInfo related classes (Android) (#1219)
Browse files Browse the repository at this point in the history
* Port `Environment.isExternalStorageManager`

* Port `PermissionInfo` related classes

* Remove unnecessary `BinaryMessenger`s
  • Loading branch information
JeroenWeener authored Nov 15, 2023
1 parent 40e41a2 commit 2672794
Show file tree
Hide file tree
Showing 46 changed files with 3,945 additions and 624 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
* arguments of callbacks methods to a Dart instance.
*/
public class ActivityFlutterApiImpl {
// To ease adding additional methods, this value is added prematurely.
@SuppressWarnings({"unused", "FieldCanBeLocal"})
private final BinaryMessenger binaryMessenger;

private final InstanceManager instanceManager;

private final ActivityFlutterApi api;
Expand All @@ -36,7 +32,6 @@ public ActivityFlutterApiImpl(
@NonNull BinaryMessenger binaryMessenger,
@NonNull InstanceManager instanceManager
) {
this.binaryMessenger = binaryMessenger;
this.instanceManager = instanceManager;
api = new ActivityFlutterApi(binaryMessenger);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.Map;
import java.util.UUID;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.PluginRegistry;

/**
Expand Down Expand Up @@ -47,10 +46,6 @@ public class ActivityHostApiImpl implements
*/
static final int DEFAULT_REQUEST_CODE_ACTIVITY_FOR_RESULT = 834370754;

// To ease adding additional methods, this value is added prematurely.
@SuppressWarnings({"unused", "FieldCanBeLocal"})
private final BinaryMessenger binaryMessenger;

private final InstanceManager instanceManager;

/**
Expand All @@ -72,14 +67,11 @@ public class ActivityHostApiImpl implements
/**
* Constructs an {@link ActivityHostApiImpl}.
*
* @param binaryMessenger used to communicate with Dart over asynchronous messages
* @param instanceManager maintains instances stored to communicate with attached Dart objects
*/
public ActivityHostApiImpl(
@NonNull BinaryMessenger binaryMessenger,
@NonNull InstanceManager instanceManager
) {
this.binaryMessenger = binaryMessenger;
this.instanceManager = instanceManager;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
* arguments of callbacks methods to a Dart instance.
*/
public class AlarmManagerFlutterApiImpl {
// To ease adding additional methods, this value is added prematurely.
@SuppressWarnings({"unused", "FieldCanBeLocal"})
private final BinaryMessenger binaryMessenger;

private final InstanceManager instanceManager;

private final AlarmManagerFlutterApi api;
Expand All @@ -36,7 +32,6 @@ public AlarmManagerFlutterApiImpl(
@NonNull BinaryMessenger binaryMessenger,
@NonNull InstanceManager instanceManager
) {
this.binaryMessenger = binaryMessenger;
this.instanceManager = instanceManager;
api = new AlarmManagerFlutterApi(binaryMessenger);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,23 @@

import java.util.UUID;

import io.flutter.plugin.common.BinaryMessenger;

/**
* Host API implementation for `AlarmManager`.
*
* <p>This class may handle instantiating and adding native object instances that are attached to a
* Dart instance or handle method calls on the associated native class or an instance of the class.
*/
public class AlarmManagerHostApiImpl implements AlarmManagerHostApi {
// To ease adding additional methods, this value is added prematurely.
@SuppressWarnings({"unused", "FieldCanBeLocal"})
private final BinaryMessenger binaryMessenger;

private final InstanceManager instanceManager;

/**
* Constructs an {@link AlarmManagerHostApiImpl}.
*
* @param binaryMessenger used to communicate with Dart over asynchronous messages
* @param instanceManager maintains instances stored to communicate with attached Dart objects
*/
public AlarmManagerHostApiImpl(
@NonNull BinaryMessenger binaryMessenger,
@NonNull InstanceManager instanceManager
) {
this.binaryMessenger = binaryMessenger;
this.instanceManager = instanceManager;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.baseflow.permissionhandler;

import android.content.pm.PackageManager.ApplicationInfoFlags;

import androidx.annotation.NonNull;

import com.baseflow.instancemanager.InstanceManager;
import com.baseflow.permissionhandler.PermissionHandlerPigeon.ApplicationInfoFlagsFlutterApi;

import java.util.UUID;

import io.flutter.plugin.common.BinaryMessenger;

/**
* Flutter API implementation for `ApplicationInfoFlags`.
*
* <p>This class may handle adding native instances that are attached to a Dart instance or passing
* arguments of callbacks methods to a Dart instance.
*/
public class ApplicationInfoFlagsFlutterApiImpl {
private final InstanceManager instanceManager;

private final ApplicationInfoFlagsFlutterApi api;

/**
* Constructs a {@link ApplicationInfoFlagsFlutterApiImpl}.
*
* @param binaryMessenger used to communicate with Dart over asynchronous messages
* @param instanceManager maintains instances stored to communicate with attached Dart objects
*/
public ApplicationInfoFlagsFlutterApiImpl(
@NonNull BinaryMessenger binaryMessenger,
@NonNull InstanceManager instanceManager
) {
this.instanceManager = instanceManager;
api = new ApplicationInfoFlagsFlutterApi(binaryMessenger);
}

/**
* Stores the `ApplicationInfoFlags` instance and notifies Dart to create and store a new
* `ApplicationInfoFlags` instance that is attached to this one. If `instance` has already been
* added, this method does nothing.
*/
public void create(@NonNull ApplicationInfoFlags instance) {
if (!instanceManager.containsInstance(instance)) {
final UUID applicationInfoFlagsInstanceUuid = instanceManager.addHostCreatedInstance(instance);
api.create(applicationInfoFlagsInstanceUuid.toString(), reply -> {});
}
}

/**
* Disposes of the `ApplicationInfoFlags` instance in the instance manager and notifies Dart to
* do the same. If `instance` was already disposed, this method does nothing.
*/
public void dispose(ApplicationInfoFlags instance) {
final UUID applicationInfoFlagsInstanceUuid = instanceManager.getIdentifierForStrongReference(instance);
if (applicationInfoFlagsInstanceUuid != null) {
api.dispose(applicationInfoFlagsInstanceUuid.toString(), reply -> {});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.baseflow.permissionhandler;

import android.content.pm.PackageManager;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.baseflow.instancemanager.InstanceManager;
import com.baseflow.permissionhandler.PermissionHandlerPigeon.ApplicationInfoFlagsHostApi;

/**
* Host API implementation for `ApplicationInfoFlags`.
*
* <p>This class may handle instantiating and adding native object instances that are attached to a
* Dart instance or handle method calls on the associated native class or an instance of the class.
*/
public class ApplicationInfoFlagsHostApiImpl implements ApplicationInfoFlagsHostApi {
private final InstanceManager instanceManager;

private final ApplicationInfoFlagsFlutterApiImpl flutterApi;

/**
* Constructs an {@link ApplicationInfoFlagsHostApiImpl}.
*
* @param instanceManager maintains instances stored to communicate with attached Dart objects
*/
public ApplicationInfoFlagsHostApiImpl(
@NonNull ApplicationInfoFlagsFlutterApiImpl flutterApi,
@NonNull InstanceManager instanceManager
) {
this.flutterApi = flutterApi;
this.instanceManager = instanceManager;
}

@RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
@NonNull
@Override
public String of(@NonNull Long value) {
final PackageManager.ApplicationInfoFlags flags = PackageManager.ApplicationInfoFlags.of(value);
flutterApi.create(flags);
return instanceManager.getIdentifierForStrongReference(flags).toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.baseflow.permissionhandler;

import android.content.pm.PackageManager.ComponentInfoFlags;

import androidx.annotation.NonNull;

import com.baseflow.instancemanager.InstanceManager;
import com.baseflow.permissionhandler.PermissionHandlerPigeon.ComponentInfoFlagsFlutterApi;

import java.util.UUID;

import io.flutter.plugin.common.BinaryMessenger;

/**
* Flutter API implementation for `ComponentInfoFlags`.
*
* <p>This class may handle adding native instances that are attached to a Dart instance or passing
* arguments of callbacks methods to a Dart instance.
*/
public class ComponentInfoFlagsFlutterApiImpl {
private final InstanceManager instanceManager;

private final ComponentInfoFlagsFlutterApi api;

/**
* Constructs a {@link ComponentInfoFlagsFlutterApiImpl}.
*
* @param binaryMessenger used to communicate with Dart over asynchronous messages
* @param instanceManager maintains instances stored to communicate with attached Dart objects
*/
public ComponentInfoFlagsFlutterApiImpl(
@NonNull BinaryMessenger binaryMessenger,
@NonNull InstanceManager instanceManager
) {
this.instanceManager = instanceManager;
api = new ComponentInfoFlagsFlutterApi(binaryMessenger);
}

/**
* Stores the `ComponentInfoFlags` instance and notifies Dart to create and store a new
* `ComponentInfoFlags` instance that is attached to this one. If `instance` has already been
* added, this method does nothing.
*/
public void create(@NonNull ComponentInfoFlags instance) {
if (!instanceManager.containsInstance(instance)) {
final UUID componentInfoFlagsInstanceUuid = instanceManager.addHostCreatedInstance(instance);
api.create(componentInfoFlagsInstanceUuid.toString(), reply -> {});
}
}

/**
* Disposes of the `ComponentInfoFlags` instance in the instance manager and notifies Dart to do
* the same. If `instance` was already disposed, this method does nothing.
*/
public void dispose(ComponentInfoFlags instance) {
final UUID componentInfoFlagsInstanceUuid = instanceManager.getIdentifierForStrongReference(instance);
if (componentInfoFlagsInstanceUuid != null) {
api.dispose(componentInfoFlagsInstanceUuid.toString(), reply -> {});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.baseflow.permissionhandler;

import android.content.pm.PackageManager;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.baseflow.instancemanager.InstanceManager;
import com.baseflow.permissionhandler.PermissionHandlerPigeon.ComponentInfoFlagsHostApi;

/**
* Host API implementation for `ComponentInfoFlags`.
*
* <p>This class may handle instantiating and adding native object instances that are attached to a
* Dart instance or handle method calls on the associated native class or an instance of the class.
*/
public class ComponentInfoFlagsHostApiImpl implements ComponentInfoFlagsHostApi {
private final InstanceManager instanceManager;

private final ComponentInfoFlagsFlutterApiImpl flutterApi;

/**
* Constructs an {@link ComponentInfoFlagsHostApiImpl}.
*
* @param instanceManager maintains instances stored to communicate with attached Dart objects
*/
public ComponentInfoFlagsHostApiImpl(
@NonNull ComponentInfoFlagsFlutterApiImpl flutterApi,
@NonNull InstanceManager instanceManager
) {
this.flutterApi = flutterApi;
this.instanceManager = instanceManager;
}

@RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
@NonNull
@Override
public String of(@NonNull Long value) {
final PackageManager.ComponentInfoFlags flags = PackageManager.ComponentInfoFlags.of(value);
flutterApi.create(flags);
return instanceManager.getIdentifierForStrongReference(flags).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
* arguments of callbacks methods to a Dart instance.
*/
public class ContextFlutterApiImpl {
// To ease adding additional methods, this value is added prematurely.
@SuppressWarnings({"unused", "FieldCanBeLocal"})
private final BinaryMessenger binaryMessenger;

private final InstanceManager instanceManager;

private final ContextFlutterApi api;
Expand All @@ -36,7 +32,6 @@ public ContextFlutterApiImpl(
@NonNull BinaryMessenger binaryMessenger,
@NonNull InstanceManager instanceManager
) {
this.binaryMessenger = binaryMessenger;
this.instanceManager = instanceManager;
api = new ContextFlutterApi(binaryMessenger);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,13 @@

import java.util.UUID;

import io.flutter.plugin.common.BinaryMessenger;

/**
* Host API implementation for `Context`.
*
* <p>This class may handle instantiating and adding native object instances that are attached to a
* Dart instance or handle method calls on the associated native class or an instance of the class.
*/
public class ContextHostApiImpl implements ContextHostApi {
// To ease adding additional methods, this value is added prematurely.
@SuppressWarnings({"unused", "FieldCanBeLocal"})
private final BinaryMessenger binaryMessenger;

private final InstanceManager instanceManager;

private final PowerManagerFlutterApiImpl powerManagerFlutterApi;
Expand All @@ -41,22 +35,19 @@ public class ContextHostApiImpl implements ContextHostApi {
/**
* Constructs an {@link ContextHostApiImpl}.
*
* @param binaryMessenger used to communicate with Dart over asynchronous messages
* @param instanceManager maintains instances stored to communicate with attached Dart objects
*/
public ContextHostApiImpl(
@NonNull PowerManagerFlutterApiImpl powerManagerFlutterApi,
@NonNull AlarmManagerFlutterApiImpl alarmManagerFlutterApi,
@NonNull PackageManagerFlutterApiImpl packageManagerFlutterApi,
@NonNull NotificationManagerFlutterApiImpl notificationManagerFlutterApi,
@NonNull BinaryMessenger binaryMessenger,
@NonNull InstanceManager instanceManager
) {
this.powerManagerFlutterApi = powerManagerFlutterApi;
this.alarmManagerFlutterApi = alarmManagerFlutterApi;
this.packageManagerFlutterApi = packageManagerFlutterApi;
this.notificationManagerFlutterApi = notificationManagerFlutterApi;
this.binaryMessenger = binaryMessenger;
this.instanceManager = instanceManager;
}

Expand Down
Loading

0 comments on commit 2672794

Please sign in to comment.