Skip to content

Commit

Permalink
Adding Support to V2 Embedding Flutter API.
Browse files Browse the repository at this point in the history
  • Loading branch information
sc4v3ng3r committed Feb 22, 2020
1 parent 5ce947c commit 183afb6
Show file tree
Hide file tree
Showing 12 changed files with 320 additions and 45 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.3.4+1
* **QUICK FIX** Using V2 embedding Flutter API.


## 0.3.3
* **Bug fix**: Fixing the wrong 'numberOfSongs' property value when
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ To use this plugin, add `flutter_audio_query` as a [dependency in your pubspec.y

```yaml
dependencies:
flutter_audio_query: ^0.3.2
flutter_audio_query: "^0.3.4+1"
```
## API
Expand Down
29 changes: 29 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,35 @@ android {
}
}

// TODO(amirh): Remove this hack once androidx.lifecycle is included on stable. https://github.com/flutter/flutter/issues/42348
afterEvaluate {
def containsEmbeddingDependencies = false
for (def configuration : configurations.all) {
for (def dependency : configuration.dependencies) {
if (dependency.group == 'io.flutter' &&
dependency.name.startsWith('flutter_embedding') &&
dependency.isTransitive())
{
containsEmbeddingDependencies = true
break
}
}
}
if (!containsEmbeddingDependencies) {
android {
dependencies {
def lifecycle_version = "1.1.1"
compileOnly "android.arch.lifecycle:runtime:$lifecycle_version"
compileOnly "android.arch.lifecycle:common:$lifecycle_version"
compileOnly "android.arch.lifecycle:common-java8:$lifecycle_version"
}
}
}
}

dependencies {
api 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.core:core:1.2.0'
implementation 'androidx.annotation:annotation:1.1.0'

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,47 @@
// THE SOFTWARE.
package boaventura.com.devel.br.flutteraudioquery;

import android.app.Application;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;

import boaventura.com.devel.br.flutteraudioquery.delegate.AudioQueryDelegate;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;


import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
//import io.flutter.embedding.engine.plugins.lifecycle.FlutterLifecycleAdapter;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/** FlutterAudioQueryPlugin */
public class FlutterAudioQueryPlugin implements MethodCallHandler {
public class FlutterAudioQueryPlugin implements MethodCallHandler, FlutterPlugin, ActivityAware {

private static final String CHANNEL_NAME = "boaventura.com.devel.br.flutteraudioquery";
private final AudioQueryDelegate m_delegate;
private AudioQueryDelegate m_delegate;
private FlutterPluginBinding m_pluginBinding;
private ActivityPluginBinding m_activityBinding;
private MethodChannel channel;
private Application application;



// This is null when not using v2 embedding;
//private Lifecycle lifecycle;
private LifeCycleObserver observer;

private FlutterAudioQueryPlugin(AudioQueryDelegate delegate){
m_delegate = delegate;
}
Expand All @@ -44,11 +70,19 @@ public static void registerWith(Registrar registrar) {
if (registrar.activity() == null)
return;

final MethodChannel channel = new MethodChannel(registrar.messenger(), CHANNEL_NAME);
final AudioQueryDelegate delegate = new AudioQueryDelegate( registrar );
channel.setMethodCallHandler( new FlutterAudioQueryPlugin(delegate) );

Application application = null;
if (registrar.context() != null) {
application = (Application) (registrar.context().getApplicationContext());
}

final FlutterAudioQueryPlugin plugin = new FlutterAudioQueryPlugin();
plugin.setup(registrar.messenger(), application, registrar.activity(), registrar, null);
}

public FlutterAudioQueryPlugin(){}


@Override
public void onMethodCall(MethodCall call, Result result) {

Expand Down Expand Up @@ -89,4 +123,148 @@ public void onMethodCall(MethodCall call, Result result) {
}
}

}
// embeding V2 implementation
@Override
public void onAttachedToEngine(FlutterPluginBinding binding) { m_pluginBinding = binding; }

@Override
public void onDetachedFromEngine(FlutterPluginBinding binding) { tearDown();}

@Override
public void onAttachedToActivity(ActivityPluginBinding binding) {
m_activityBinding = binding;
setup(
m_pluginBinding.getBinaryMessenger(),
(Application) m_pluginBinding.getApplicationContext(),
m_activityBinding.getActivity(),
null,
m_activityBinding);
}


@Override
public void onDetachedFromActivityForConfigChanges() {

}

@Override
public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {
onAttachedToActivity(binding);
}

@Override
public void onDetachedFromActivity() {
}

private void setup(
final BinaryMessenger messenger,
final Application application,
final Activity activity, final PluginRegistry.Registrar registrar,
final ActivityPluginBinding activityBinding){

this.application = application;
channel = new MethodChannel(messenger, CHANNEL_NAME);


if (registrar != null) {
Log.i("AUDIO_QUERY", "Using V1 EMBEDDING");
// V1 embedding delegate creation
m_delegate = new AudioQueryDelegate(registrar);
observer = new LifeCycleObserver(activity);
application.registerActivityLifecycleCallbacks(observer);

}

else {
Log.i("AUDIO_QUERY", "Using V2 EMBEDDING");
// V2 embedding setup for activity listeners.
m_delegate = new AudioQueryDelegate(application.getApplicationContext(), activity);
activityBinding.addRequestPermissionsResultListener( m_delegate );

//lifecycle = FlutterLifecycleAdapter.getActivityLifecycle(activityBinding);

//lifecycle = (Lifecycle) activityBinding.getLifecycle();
//activityBinding.
//observer = new LifeCycleObserver(activityBinding.getActivity() );
//lifecycle.addObserver(observer);
}
channel.setMethodCallHandler( new FlutterAudioQueryPlugin( m_delegate) );

}

private void tearDown() {
m_activityBinding.removeRequestPermissionsResultListener(m_delegate);
m_activityBinding = null;
// if (lifecycle != null) {
// lifecycle.removeObserver(observer);
// lifecycle = null;
// }
m_delegate = null;
channel.setMethodCallHandler(null);
channel = null;
application.unregisterActivityLifecycleCallbacks(observer);
application = null;
}

private class LifeCycleObserver
implements Application.ActivityLifecycleCallbacks, DefaultLifecycleObserver {

private final Activity thisActivity;

LifeCycleObserver(Activity activity) {
this.thisActivity = activity;
}

@Override
public void onCreate(@NonNull LifecycleOwner owner) {}

@Override
public void onStart(@NonNull LifecycleOwner owner) {}

@Override
public void onResume(@NonNull LifecycleOwner owner) {}

@Override
public void onPause(@NonNull LifecycleOwner owner) {}

@Override
public void onStop(@NonNull LifecycleOwner owner) {
onActivityStopped(thisActivity);
}

@Override
public void onDestroy(@NonNull LifecycleOwner owner) {
onActivityDestroyed(thisActivity);
}

@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}

@Override
public void onActivityStarted(Activity activity) {}

@Override
public void onActivityResumed(Activity activity) {}

@Override
public void onActivityPaused(Activity activity) {}

@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}

@Override
public void onActivityDestroyed(Activity activity) {
if (thisActivity == activity && activity.getApplicationContext() != null) {
((Application) activity.getApplicationContext())
.unregisterActivityLifecycleCallbacks(
this); // Use getApplicationContext() to avoid casting failures
}
}

@Override
public void onActivityStopped(Activity activity) {

}
} //// LifeCycleObserver end

} // end FlutterAudioQueryPlugin
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package boaventura.com.devel.br.flutteraudioquery.delegate;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.util.Log;

import java.util.List;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import boaventura.com.devel.br.flutteraudioquery.loaders.AlbumLoader;
import boaventura.com.devel.br.flutteraudioquery.loaders.ArtistLoader;
Expand Down Expand Up @@ -41,6 +44,7 @@
// *
// */


public class AudioQueryDelegate implements PluginRegistry.RequestPermissionsResultListener,
AudioQueryDelegateInterface {

Expand All @@ -62,18 +66,42 @@ public class AudioQueryDelegate implements PluginRegistry.RequestPermissionsResu
private final GenreLoader m_genreLoader;
private final PlaylistLoader m_playlistLoader;


public AudioQueryDelegate(final Context context, final Activity activity){
m_artistLoader = new ArtistLoader(context );
m_albumLoader = new AlbumLoader(context );
m_songLoader = new SongLoader( context );
m_genreLoader = new GenreLoader( context );
m_playlistLoader = new PlaylistLoader( context );

m_permissionManager = new PermissionManager() {
@Override
public boolean isPermissionGranted(String permissionName) {

return (ContextCompat.checkSelfPermission( activity, permissionName)
== PackageManager.PERMISSION_GRANTED);
}

@Override
public void askForPermission(String permissionName, int requestCode) {
ActivityCompat.requestPermissions(activity, new String[] {permissionName}, requestCode);
}
};
}

public AudioQueryDelegate(final PluginRegistry.Registrar registrar){

m_artistLoader = new ArtistLoader(registrar.context());
m_albumLoader = new AlbumLoader(registrar.context());
m_artistLoader = new ArtistLoader(registrar.context() );
m_albumLoader = new AlbumLoader(registrar.context() );
m_songLoader = new SongLoader( registrar.context() );
m_genreLoader = new GenreLoader( registrar.context() );
m_playlistLoader = new PlaylistLoader( registrar.context() );

m_permissionManager = new PermissionManager() {
@Override
public boolean isPermissionGranted(String permissionName) {
return (ActivityCompat.checkSelfPermission( registrar.activity(), permissionName)

return (ContextCompat.checkSelfPermission( registrar.activity(), permissionName)
== PackageManager.PERMISSION_GRANTED);
}

Expand Down
11 changes: 8 additions & 3 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,19 @@ android {
signingConfig signingConfigs.debug
}
}
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
buildToolsVersion = '29.0.3'
}

flutter {
source '../..'
}

dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Loading

0 comments on commit 183afb6

Please sign in to comment.