Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Port PackageManager.canRequestPackageInstalls (Android) #1215

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Activity;
import android.app.AlarmManager;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.PowerManager;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -59,6 +60,8 @@ public class ActivityHostApiImpl implements

private final AlarmManagerFlutterApiImpl alarmManagerFlutterApi;

private final PackageManagerFlutterApiImpl packageManagerFlutterApi;

/**
* Callbacks to complete a pending permission request.
* <p>
Expand All @@ -84,11 +87,13 @@ public class ActivityHostApiImpl implements
public ActivityHostApiImpl(
@NonNull PowerManagerFlutterApiImpl powerManagerFlutterApi,
@NonNull AlarmManagerFlutterApiImpl alarmManagerFlutterApi,
@NonNull PackageManagerFlutterApiImpl packageManagerFlutterApi,
@NonNull BinaryMessenger binaryMessenger,
@NonNull InstanceManager instanceManager
) {
this.alarmManagerFlutterApi = alarmManagerFlutterApi;
this.powerManagerFlutterApi = powerManagerFlutterApi;
this.alarmManagerFlutterApi = alarmManagerFlutterApi;
this.packageManagerFlutterApi = packageManagerFlutterApi;
this.binaryMessenger = binaryMessenger;
this.instanceManager = instanceManager;
}
Expand Down Expand Up @@ -249,4 +254,19 @@ public boolean onActivityResult(
final UUID systemServiceUuid = instanceManager.getIdentifierForStrongReference(systemService);
return systemServiceUuid.toString();
}

@Override
@NonNull public String getPackageManager(
@NonNull String instanceId
) {
final UUID instanceUuid = UUID.fromString(instanceId);
final Activity activity = instanceManager.getInstance(instanceUuid);

final PackageManager packageManager = activity.getPackageManager();

packageManagerFlutterApi.create(packageManager);

final UUID packageManagerUuid = instanceManager.getIdentifierForStrongReference(packageManager);
return packageManagerUuid.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.AlarmManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.PowerManager;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -32,6 +33,8 @@ public class ContextHostApiImpl implements ContextHostApi {

private final AlarmManagerFlutterApiImpl alarmManagerFlutterApi;

private final PackageManagerFlutterApiImpl packageManagerFlutterApi;

/**
* Constructs an {@link ContextHostApiImpl}.
*
Expand All @@ -41,11 +44,13 @@ public class ContextHostApiImpl implements ContextHostApi {
public ContextHostApiImpl(
@NonNull PowerManagerFlutterApiImpl powerManagerFlutterApi,
@NonNull AlarmManagerFlutterApiImpl alarmManagerFlutterApi,
@NonNull PackageManagerFlutterApiImpl packageManagerFlutterApi,
@NonNull BinaryMessenger binaryMessenger,
@NonNull InstanceManager instanceManager
) {
this.powerManagerFlutterApi = powerManagerFlutterApi;
this.alarmManagerFlutterApi = alarmManagerFlutterApi;
this.packageManagerFlutterApi = packageManagerFlutterApi;
this.binaryMessenger = binaryMessenger;
this.instanceManager = instanceManager;
}
Expand Down Expand Up @@ -104,4 +109,19 @@ public void startActivity(
final UUID systemServiceUuid = instanceManager.getIdentifierForStrongReference(systemService);
return systemServiceUuid.toString();
}

@Override
@NonNull public String getPackageManager(
@NonNull String instanceId
) {
final UUID instanceUuid = UUID.fromString(instanceId);
final Context context = instanceManager.getInstance(instanceUuid);

final PackageManager packageManager = context.getPackageManager();

packageManagerFlutterApi.create(packageManager);

final UUID packageManagerUuid = instanceManager.getIdentifierForStrongReference(packageManager);
return packageManagerUuid.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.baseflow.permissionhandler;

import android.content.pm.PackageManager;

import androidx.annotation.NonNull;

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

import java.util.UUID;

import io.flutter.plugin.common.BinaryMessenger;

/**
* Flutter API implementation for `PackageManager`.
*
* <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 PackageManagerFlutterApiImpl {
// To ease adding additional methods, this value is added prematurely.
@SuppressWarnings({"unused", "FieldCanBeLocal"})
private final BinaryMessenger binaryMessenger;

private final InstanceManager instanceManager;

private final PackageManagerFlutterApi api;

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

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

/**
* Disposes of the `PackageManager` instance in the instance manager and notifies Dart to do the
* same. If `instance` was already disposed, this method does nothing.
*/
public void dispose(PackageManager instance) {
final UUID packageManagerInstanceUuid = instanceManager.getIdentifierForStrongReference(instance);
if (packageManagerInstanceUuid != null) {
api.dispose(packageManagerInstanceUuid.toString(), reply -> {});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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.PackageManagerHostApi;

import java.util.UUID;

import io.flutter.plugin.common.BinaryMessenger;

/**
* Host API implementation for `PackageManager`.
*
* <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 PackageManagerHostApiImpl implements PackageManagerHostApi {
// To ease adding additional methods, this value is added prematurely.
@SuppressWarnings({"unused", "FieldCanBeLocal"})
private final BinaryMessenger binaryMessenger;

private final InstanceManager instanceManager;

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

@RequiresApi(api = Build.VERSION_CODES.O)
@NonNull
@Override
public Boolean canRequestPackageInstalls(@NonNull String instanceId) {
final UUID instanceUuid = UUID.fromString(instanceId);
final PackageManager packageManager = instanceManager.getInstance(instanceUuid);

return packageManager.canRequestPackageInstalls();
}
}
Loading
Loading