Skip to content

Commit

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

* Port `PermissionInfo` related classes

* Port `FeatureInfo`

* Port `getSystemFeature` and `hasSystemFeature`

* Port `TelephonyManager`

* Port `LocationManager`

* Port `BluetoothAdapter`

* Port `BluetoothManager`

* Remove unnecessary `BinaryMessenger`s

* Port `ContentResolver`

* Port `Context.getContentResolver`

* Port `Settings.Secure`

* Remove `static` from `SettingsSecure` getters

* Add `Setting` actions

* Remove unnecessary `BinaryMessenger`s
  • Loading branch information
JeroenWeener authored Nov 16, 2023
1 parent 2672794 commit f1c73a9
Show file tree
Hide file tree
Showing 31 changed files with 4,614 additions and 338 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.baseflow.permissionhandler;

import android.bluetooth.BluetoothAdapter;

import androidx.annotation.NonNull;

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

import java.util.UUID;

import io.flutter.plugin.common.BinaryMessenger;

/**
* Flutter API implementation for `BluetoothAdapter`.
*
* <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 BluetoothAdapterFlutterApiImpl {
private final InstanceManager instanceManager;

private final BluetoothAdapterFlutterApi api;

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

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

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


import android.bluetooth.BluetoothAdapter;

import androidx.annotation.NonNull;

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

import java.util.UUID;

/**
* Host API implementation for `BluetoothAdapter`.
*
* <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 BluetoothAdapterHostApiImpl implements BluetoothAdapterHostApi {
private final InstanceManager instanceManager;

private final BluetoothAdapterFlutterApiImpl bluetoothAdapterFlutterApi;

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

@Deprecated
@NonNull
@Override
public String getDefaultAdapter() {
final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapterFlutterApi.create(bluetoothAdapter);
return instanceManager.getIdentifierForStrongReference(bluetoothAdapter).toString();
}

@NonNull
@Override
public Boolean isEnabled(@NonNull String instanceId) {
final UUID instanceUuid = UUID.fromString(instanceId);
final BluetoothAdapter bluetoothAdapter = instanceManager.getInstance(instanceUuid);
return bluetoothAdapter.isEnabled();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.baseflow.permissionhandler;

import android.bluetooth.BluetoothManager;

import androidx.annotation.NonNull;

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

import java.util.UUID;

import io.flutter.plugin.common.BinaryMessenger;

/**
* Flutter API implementation for `BluetoothManager`.
*
* <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 BluetoothManagerFlutterApiImpl {
private final InstanceManager instanceManager;

private final BluetoothManagerFlutterApi api;

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

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

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


import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.os.Build;

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

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

import java.util.UUID;

/**
* Host API implementation for `BluetoothManager`.
*
* <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 BluetoothManagerHostApiImpl implements BluetoothManagerHostApi {
private final InstanceManager instanceManager;

private final BluetoothAdapterFlutterApiImpl bluetoothAdapterFlutterApi;

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

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@NonNull
@Override
public String getAdapter(@NonNull String instanceId) {
final UUID instanceUuid = UUID.fromString(instanceId);
final BluetoothManager bluetoothManager = instanceManager.getInstance(instanceUuid);
final BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();

bluetoothAdapterFlutterApi.create(bluetoothAdapter);

return instanceManager.getIdentifierForStrongReference(bluetoothAdapter).toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.baseflow.permissionhandler;

import android.content.ContentResolver;

import androidx.annotation.NonNull;

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

import java.util.UUID;

import io.flutter.plugin.common.BinaryMessenger;

/**
* Flutter API implementation for `ContentResolver`.
*
* <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 ContentResolverFlutterApiImpl {
private final InstanceManager instanceManager;

private final ContentResolverFlutterApi api;

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

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

/**
* Disposes of the `ContentResolver` instance in the instance manager and notifies Dart to do
* the same. If `instance` was already disposed, this method does nothing.
*/
public void dispose(ContentResolver instance) {
final UUID contentResolverInstanceUuid = instanceManager.getIdentifierForStrongReference(instance);
if (contentResolverInstanceUuid != null) {
api.dispose(contentResolverInstanceUuid.toString(), reply -> {});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import android.app.AlarmManager;
import android.app.NotificationManager;
import android.bluetooth.BluetoothManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.os.Build;
import android.os.PowerManager;
import android.telephony.TelephonyManager;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
Expand All @@ -32,6 +37,14 @@ public class ContextHostApiImpl implements ContextHostApi {

private final NotificationManagerFlutterApiImpl notificationManagerFlutterApi;

private final TelephonyManagerFlutterApiImpl telephonyManagerFlutterApi;

private final LocationManagerFlutterApiImpl locationManagerFlutterApi;

private final BluetoothManagerFlutterApiImpl bluetoothManagerFlutterApi;

private final ContentResolverFlutterApiImpl contentResolverFlutterApi;

/**
* Constructs an {@link ContextHostApiImpl}.
*
Expand All @@ -42,12 +55,20 @@ public ContextHostApiImpl(
@NonNull AlarmManagerFlutterApiImpl alarmManagerFlutterApi,
@NonNull PackageManagerFlutterApiImpl packageManagerFlutterApi,
@NonNull NotificationManagerFlutterApiImpl notificationManagerFlutterApi,
@NonNull TelephonyManagerFlutterApiImpl telephonyManagerFlutterApi,
@NonNull LocationManagerFlutterApiImpl locationManagerFlutterApi,
@NonNull BluetoothManagerFlutterApiImpl bluetoothManagerFlutterApi,
@NonNull ContentResolverFlutterApiImpl contentResolverFlutterApi,
@NonNull InstanceManager instanceManager
) {
this.powerManagerFlutterApi = powerManagerFlutterApi;
this.alarmManagerFlutterApi = alarmManagerFlutterApi;
this.packageManagerFlutterApi = packageManagerFlutterApi;
this.notificationManagerFlutterApi = notificationManagerFlutterApi;
this.telephonyManagerFlutterApi = telephonyManagerFlutterApi;
this.locationManagerFlutterApi = locationManagerFlutterApi;
this.bluetoothManagerFlutterApi = bluetoothManagerFlutterApi;
this.contentResolverFlutterApi = contentResolverFlutterApi;
this.instanceManager = instanceManager;
}

Expand Down Expand Up @@ -87,7 +108,7 @@ public void startActivity(
}

@Override
@NonNull public String getSystemService(
public String getSystemService(
@NonNull String instanceId,
@NonNull String name
) {
Expand All @@ -102,9 +123,21 @@ public void startActivity(
alarmManagerFlutterApi.create((AlarmManager) systemService);
} else if (systemService instanceof NotificationManager) {
notificationManagerFlutterApi.create((NotificationManager) systemService);
} else if (systemService instanceof TelephonyManager) {
telephonyManagerFlutterApi.create((TelephonyManager) systemService);
} else if (systemService instanceof LocationManager) {
locationManagerFlutterApi.create((LocationManager) systemService);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
if (systemService instanceof BluetoothManager) {
bluetoothManagerFlutterApi.create((BluetoothManager) systemService);
}
}

final UUID systemServiceUuid = instanceManager.getIdentifierForStrongReference(systemService);

if (systemServiceUuid == null) {
return null;
}
return systemServiceUuid.toString();
}

Expand All @@ -122,4 +155,19 @@ public void startActivity(
final UUID packageManagerUuid = instanceManager.getIdentifierForStrongReference(packageManager);
return packageManagerUuid.toString();
}

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

final ContentResolver contentResolver = context.getContentResolver();

contentResolverFlutterApi.create(contentResolver);

final UUID contentResolverUuid = instanceManager.getIdentifierForStrongReference(contentResolver);
return contentResolverUuid.toString();
}
}
Loading

0 comments on commit f1c73a9

Please sign in to comment.