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 Settings.canDrawOverlays (Android) #1216

Merged
merged 1 commit into from
Nov 10, 2023
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
@@ -1,10 +1,7 @@
package com.baseflow.permissionhandler;

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;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -56,12 +53,6 @@ public class ActivityHostApiImpl implements

private final InstanceManager instanceManager;

private final PowerManagerFlutterApiImpl powerManagerFlutterApi;

private final AlarmManagerFlutterApiImpl alarmManagerFlutterApi;

private final PackageManagerFlutterApiImpl packageManagerFlutterApi;

/**
* Callbacks to complete a pending permission request.
* <p>
Expand All @@ -85,15 +76,9 @@ public class ActivityHostApiImpl implements
* @param instanceManager maintains instances stored to communicate with attached Dart objects
*/
public ActivityHostApiImpl(
@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 All @@ -109,17 +94,6 @@ public ActivityHostApiImpl(
return ActivityCompat.shouldShowRequestPermissionRationale(activity, permission);
}

@Override
@NonNull public Long checkSelfPermission(
@NonNull String activityInstanceId,
@NonNull String permission
) {
final UUID activityInstanceUuid = UUID.fromString(activityInstanceId);
final Activity activity = instanceManager.getInstance(activityInstanceUuid);

return (long) ActivityCompat.checkSelfPermission(activity, permission);
}

@Override
public void requestPermissions(
@NonNull String activityInstanceId,
Expand Down Expand Up @@ -166,30 +140,6 @@ public boolean onRequestPermissionsResult(
return true;
}

@Override
public void startActivity(
@NonNull String instanceId,
@NonNull String intentInstanceId
) {
final UUID instanceUuid = UUID.fromString(instanceId);
final UUID intentInstanceUuid = UUID.fromString(intentInstanceId);

final Activity activity = instanceManager.getInstance(instanceUuid);
final Intent intent = instanceManager.getInstance(intentInstanceUuid);

ActivityCompat.startActivity(activity, intent, null);
}

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

return activity.getPackageName();
}

@Override
public void startActivityForResult(
@NonNull String instanceId,
Expand Down Expand Up @@ -234,39 +184,4 @@ public boolean onActivityResult(

return true;
}

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

final Object systemService = activity.getSystemService(name);

if (systemService instanceof PowerManager) {
powerManagerFlutterApi.create((PowerManager) systemService);
} else if (systemService instanceof AlarmManager) {
alarmManagerFlutterApi.create((AlarmManager) systemService);
}

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 @@ -311,13 +311,6 @@ public interface ActivityHostApi {
*/
@NonNull
Boolean shouldShowRequestPermissionRationale(@NonNull String instanceId, @NonNull String permission);
/**
* Determine whether the application has been granted a particular permission.
*
* See https://developer.android.com/reference/android/app/Activity#checkSelfPermission(java.lang.String).
*/
@NonNull
Long checkSelfPermission(@NonNull String instanceId, @NonNull String permission);
/**
* Requests permissions to be granted to this application.
*
Expand All @@ -327,43 +320,12 @@ public interface ActivityHostApi {
* https://developer.android.com/reference/android/app/Activity#onRequestPermissionsResult(int,%20java.lang.String[],%20int[]).
*/
void requestPermissions(@NonNull String instanceId, @NonNull List<String> permissions, @Nullable Long requestCode, @NonNull Result<PermissionRequestResult> result);
/**
* Launch a new activity.
*
* See https://developer.android.com/reference/android/content/Context#startActivity(android.content.Intent).
*/
void startActivity(@NonNull String instanceId, @NonNull String intentInstanceId);
/**
* Returns the name of this application's package.
*
* See https://developer.android.com/reference/android/content/Context#getPackageName().
*/
@NonNull
String getPackageName(@NonNull String instanceId);
/**
* Return the handle to a system-level service by name.
*
* The class of the returned object varies by the requested name.
*
* Returns the instance ID of the service.
*
* See https://developer.android.com/reference/android/content/Context#getSystemService(java.lang.String).
*/
@NonNull
String getSystemService(@NonNull String instanceId, @NonNull String name);
/**
* Start an activity for which the application would like a result when it finished.
*
* See https://developer.android.com/reference/android/app/Activity#startActivityForResult(android.content.Intent,%20int).
*/
void startActivityForResult(@NonNull String instanceId, @NonNull String intentInstanceId, @Nullable Long requestCode, @NonNull Result<ActivityResultPigeon> result);
/**
* Returns the instance ID of a PackageManager instance to find global package information.
*
* See https://developer.android.com/reference/android/content/Context#getPackageManager().
*/
@NonNull
String getPackageManager(@NonNull String instanceId);

/** The codec used by ActivityHostApi. */
static @NonNull MessageCodec<Object> getCodec() {
Expand All @@ -386,31 +348,6 @@ static void setup(@NonNull BinaryMessenger binaryMessenger, @Nullable ActivityHo
Boolean output = api.shouldShowRequestPermissionRationale(instanceIdArg, permissionArg);
wrapped.add(0, output);
}
catch (Throwable exception) {
ArrayList<Object> wrappedError = wrapError(exception);
wrapped = wrappedError;
}
reply.reply(wrapped);
});
} else {
channel.setMessageHandler(null);
}
}
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.permission_handler_android.ActivityHostApi.checkSelfPermission", getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
ArrayList<Object> args = (ArrayList<Object>) message;
String instanceIdArg = (String) args.get(0);
String permissionArg = (String) args.get(1);
try {
Long output = api.checkSelfPermission(instanceIdArg, permissionArg);
wrapped.add(0, output);
}
catch (Throwable exception) {
ArrayList<Object> wrappedError = wrapError(exception);
wrapped = wrappedError;
Expand Down Expand Up @@ -452,80 +389,6 @@ public void error(Throwable error) {
channel.setMessageHandler(null);
}
}
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.permission_handler_android.ActivityHostApi.startActivity", getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
ArrayList<Object> args = (ArrayList<Object>) message;
String instanceIdArg = (String) args.get(0);
String intentInstanceIdArg = (String) args.get(1);
try {
api.startActivity(instanceIdArg, intentInstanceIdArg);
wrapped.add(0, null);
}
catch (Throwable exception) {
ArrayList<Object> wrappedError = wrapError(exception);
wrapped = wrappedError;
}
reply.reply(wrapped);
});
} else {
channel.setMessageHandler(null);
}
}
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.permission_handler_android.ActivityHostApi.getPackageName", getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
ArrayList<Object> args = (ArrayList<Object>) message;
String instanceIdArg = (String) args.get(0);
try {
String output = api.getPackageName(instanceIdArg);
wrapped.add(0, output);
}
catch (Throwable exception) {
ArrayList<Object> wrappedError = wrapError(exception);
wrapped = wrappedError;
}
reply.reply(wrapped);
});
} else {
channel.setMessageHandler(null);
}
}
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.permission_handler_android.ActivityHostApi.getSystemService", getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
ArrayList<Object> args = (ArrayList<Object>) message;
String instanceIdArg = (String) args.get(0);
String nameArg = (String) args.get(1);
try {
String output = api.getSystemService(instanceIdArg, nameArg);
wrapped.add(0, output);
}
catch (Throwable exception) {
ArrayList<Object> wrappedError = wrapError(exception);
wrapped = wrappedError;
}
reply.reply(wrapped);
});
} else {
channel.setMessageHandler(null);
}
}
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
Expand Down Expand Up @@ -557,30 +420,6 @@ public void error(Throwable error) {
channel.setMessageHandler(null);
}
}
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.permission_handler_android.ActivityHostApi.getPackageManager", getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
ArrayList<Object> args = (ArrayList<Object>) message;
String instanceIdArg = (String) args.get(0);
try {
String output = api.getPackageManager(instanceIdArg);
wrapped.add(0, output);
}
catch (Throwable exception) {
ArrayList<Object> wrappedError = wrapError(exception);
wrapped = wrappedError;
}
reply.reply(wrapped);
});
} else {
channel.setMessageHandler(null);
}
}
}
}
/**
Expand Down Expand Up @@ -1483,4 +1322,63 @@ public void dispose(@NonNull String instanceIdArg, @NonNull Reply<Void> callback
channelReply -> callback.reply(null));
}
}
/**
* Host API for `Settings`.
*
* 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.
*
* See https://developer.android.com/reference/kotlin/android/provider/Settings.
*
* Generated interface from Pigeon that represents a handler of messages from Flutter.
*/
public interface SettingsHostApi {
/**
* Checks if the specified context can draw on top of other apps.
*
* As of API level 23, an app cannot draw on top of other apps unless it
* declares the [Manifest.permission.systemAlertWindow] permission in its
* manifest, **and** the user specifically grants the app this capability. To
* prompt the user to grant this approval, the app must send an intent with
* the action [Settings.actionManageOverlayPermission], which causes the
* system to display a permission management screen.
*
* See https://developer.android.com/reference/android/provider/Settings#canDrawOverlays(android.content.Context).
*/
@NonNull
Boolean canDrawOverlays(@NonNull String contextInstanceId);

/** The codec used by SettingsHostApi. */
static @NonNull MessageCodec<Object> getCodec() {
return new StandardMessageCodec();
}
/**Sets up an instance of `SettingsHostApi` to handle messages through the `binaryMessenger`. */
static void setup(@NonNull BinaryMessenger binaryMessenger, @Nullable SettingsHostApi api) {
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.permission_handler_android.SettingsHostApi.canDrawOverlays", getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
ArrayList<Object> args = (ArrayList<Object>) message;
String contextInstanceIdArg = (String) args.get(0);
try {
Boolean output = api.canDrawOverlays(contextInstanceIdArg);
wrapped.add(0, output);
}
catch (Throwable exception) {
ArrayList<Object> wrappedError = wrapError(exception);
wrapped = wrappedError;
}
reply.reply(wrapped);
});
} else {
channel.setMessageHandler(null);
}
}
}
}
}
Loading
Loading