package com.lishash.lishash.plugins;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.view.FlutterView;
import io.flutter.app.FlutterActivity;
import com.apple.android.music.playback.model.MediaContainerType;
import com.apple.android.music.playback.model.MediaItemType;
import com.apple.android.music.playback.model.PlaybackState;
import com.apple.android.music.playback.model.PlayerMediaItem;
import com.apple.android.music.playback.queue.CatalogPlaybackQueueItemProvider;
import com.apple.android.sdk.authentication.AuthenticationFactory;
import com.apple.android.sdk.authentication.AuthenticationManager;
import com.apple.android.sdk.authentication.TokenError;
import com.apple.android.sdk.authentication.TokenProvider;
import com.apple.android.sdk.authentication.TokenResult;
import com.apple.android.music.playback.controller.MediaPlayerController;
import com.apple.android.music.playback.controller.MediaPlayerControllerFactory;
import com.lishash.lishash.MainActivity;
public class AppleMusicPlugin extends MainActivity {
private static final String CHANNEL_NAME = "lishash.plugins.apple_music";
//MediaPlayerController playerController;
private static final String dev_token = " "; //developer token given by apple musicKit
private String musicUserToken = "";
private MethodChannel methodChannel;
private AuthenticationManager authenticationManager;
private static final int REQUESTCODE_APPLEMUSIC_AUTH = 3456;
private static final String LOG_TAG = "Apple_Music_Plugin";
private MethodChannel.Result authTokenResponse;
static {
try {
System.loadLibrary("c++_shared");
System.loadLibrary("appleMusicSDK");
Log.d(LOG_TAG, "loaded libraries");
} catch (final Exception e) {
Log.e(LOG_TAG, "Could not load library due to: " + Log.getStackTraceString(e));
throw e;
}
}
public AppleMusicPlugin(FlutterView flutterView, Context context, MainActivity activity) {
final MethodChannel channel = new MethodChannel(flutterView, CHANNEL_NAME);
channel.setMethodCallHandler((call, response) -> {
Log.d(LOG_TAG, "Welcome to Apple Music Plugin Handler");
switch (call.method) {
case "authenticate":
Log.d(LOG_TAG, "Apple Music Plugin Auth Handler");
authenticate(response);
break;
case "playSong":
playSong(response);
break;
default:
response.notImplemented();
}
});
//this.mChannel = channel;
this.mContext = context;
this.mActivity = activity;
}
private final Context mContext;
private final Activity mActivity;
private void authenticate(MethodChannel.Result response) {
authTokenResponse = null;
if (authenticationManager == null) {
authenticationManager = AuthenticationFactory.createAuthenticationManager(this.mContext);
Log.d(LOG_TAG, "AuthenticationManager created");
}
Intent intent = authenticationManager.createIntentBuilder(dev_token)
.setHideStartScreen(true)
.setStartScreenMessage("To play the full song, connect Apple Music")
.build();
Log.d(LOG_TAG, "AuthenticationManager intent built");
Log.d(LOG_TAG, "data " + intent);
mActivity.startActivityForResult(intent, REQUESTCODE_APPLEMUSIC_AUTH);
Log.d(LOG_TAG, "Activity started");
authTokenResponse = response;
}
public void getMusicUserToken(Intent data) {
Log.d(LOG_TAG, "Getting music user token");
TokenResult tokenResult = authenticationManager.handleTokenResult(data);
Log.d(LOG_TAG, "tokenResult:" + tokenResult);
if (!tokenResult.isError()) {
musicUserToken = tokenResult.getMusicUserToken();
Log.d(LOG_TAG, "Music user token is: " + musicUserToken);
authTokenResponse.success(musicUserToken);
//creating playback controller
} else {
TokenError error = tokenResult.getError();
Log.d(LOG_TAG, "Error getting token: " + error);
}
}
TokenProvider tokenProvider = new TokenProvider() {
@Override
public String getDeveloperToken() {
Log.d(LOG_TAG, dev_token);
return dev_token;
}
@Override
public String getUserToken() {
Log.d(LOG_TAG, musicUserToken);
return musicUserToken;
}
};
private void playSong(MethodChannel.Result response) {
Log.d(LOG_TAG, "Play Song Button pressed");
MediaPlayerController playerController = MediaPlayerControllerFactory.createLocalController(this.mContext.getApplicationContext(), tokenProvider);
Log.d(LOG_TAG, "Player controller defined");
// MediaPlayerController playerController;
CatalogPlaybackQueueItemProvider.Builder queueProviderBuilder = new CatalogPlaybackQueueItemProvider.Builder();
Log.d(LOG_TAG, "queueProvider built");
queueProviderBuilder.items(MediaItemType.SONG,"673575193");
Log.d(LOG_TAG, "Song added");
//queueProviderBuilder.startItemIndex(queueIndex);
//Log.d(LOG_TAG, "Song added");
playerController.prepare(queueProviderBuilder.build(), true);
Log.d(LOG_TAG, "queue prepared");
// PlayerMediaItem.getArtistName();
}