print( sjw.iq == 200) output : true

[Cordova] Plugin 만들기 본문

HYBRID

[Cordova] Plugin 만들기

sjwiq200 2019. 12. 23. 19:52
728x90
반응형

우선 코르도바 프로젝트를 생성하겠습니다!

cordova create KakaoTalk com.sjwiq200.cordova.kakao KakaoTalk

플러그인 기본 템플릿 생성을 위한 plugman을 설치해줍시다.

 

npm install -g plugman



cordova 프로젝트 루트로 들어가서 플러그인 기본 템플릿을 생성합니다.

 

plugman create --name KakaoTalk --plugin_id com.sjwiq200.plugin.kakao --plugin_version "0.0.1"

 

그 다음 기본 템플릿이 생성된 디렉 토리로 들어가 플랫폼을 추가해줍니다.

 

plugman platform add --platform_name android

 

그러면 다음과 같은 프로젝트 구조를 보실 수 있습니다.

 

KakaoTalk.js 파일을 보면

 

var exec = require('cordova/exec');

var KakaoTalk = {
    share : function(options, successCallback, errorCallback) {
        exec(successCallback, errorCallback, 'KakaoTalk', 'share', [options]);
    }
};

module.exports = KakaoTalk;

 

다음과 같이 되어 있습니다.

 

플러그인을 사용할 때 KakaoTalk.share(options, function (success) {}, function (error) {} ) 이런식으로 사용하면 됩니다.

 

그리고 exec 메소드를 호출함으로 KakaoTalk.java 파일에서 init 후 execute 메소드를 실행하게 됩니다.

 

package com.sjwiq200.plugin.kakao;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.kakao.auth.ApprovalType;
import com.kakao.auth.AuthType;
import com.kakao.auth.IApplicationConfig;
import com.kakao.auth.ISessionCallback;
import com.kakao.auth.ISessionConfig;
import com.kakao.auth.KakaoAdapter;
import com.kakao.auth.KakaoSDK;
import com.kakao.auth.Session;
import com.kakao.kakaolink.v2.KakaoLinkResponse;
import com.kakao.kakaolink.v2.KakaoLinkService;
import com.kakao.message.template.ButtonObject;
import com.kakao.message.template.CommerceTemplate;
import com.kakao.message.template.ContentObject;
import com.kakao.message.template.FeedTemplate;
import com.kakao.message.template.LinkObject;
import com.kakao.message.template.ListTemplate;
import com.kakao.message.template.LocationTemplate;
import com.kakao.message.template.SocialObject;
import com.kakao.message.template.TextTemplate;
import com.kakao.network.ErrorResult;
import com.kakao.network.callback.ResponseCallback;
import com.kakao.usermgmt.UserManagement;
import com.kakao.usermgmt.callback.LogoutResponseCallback;
import com.kakao.usermgmt.callback.MeResponseCallback;
import com.kakao.usermgmt.response.model.UserProfile;
import com.kakao.util.KakaoParameterException;
import com.kakao.util.exception.KakaoException;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

public class KakaoTalk extends CordovaPlugin {

	private static final String LOG_TAG = "KakaoTalk";
	private static volatile Activity currentActivity;
	private KakaoLinkResponseCallback kakaoLinkResponseCallback;

	/**
	 * Initialize cordova plugin kakaotalk
	 * @param cordova
	 * @param webView
	 */
	public void initialize(CordovaInterface cordova, CordovaWebView webView)
	{
		Log.v(LOG_TAG, "kakao : initialize");
		super.initialize(cordova, webView);
		currentActivity = this.cordova.getActivity();
		KakaoSDK.init(new KakaoSDKAdapter());
	}

	/**
	 * Execute plugin
	 * @param action
	 * @param options
	 * @param callbackContext
	 */
	public boolean execute(final String action, JSONArray options, final CallbackContext callbackContext) throws JSONException
	{
		Log.v(LOG_TAG, "kakao : execute " + action);
		cordova.setActivityResultCallback(this);
//		callback = new SessionCallback(callbackContext);
		removeSessionCallback();
//		Session.getCurrentSession().addCallback(callback);

		if (action.equals("share")) {

			kakaoLinkResponseCallback = new KakaoLinkResponseCallback(callbackContext);
			this.share(options, callbackContext);

			return true;
		}
		return false;
	}

	private void removeSessionCallback() {
		Session.getCurrentSession().clearCallbacks();
	}

	private void share(JSONArray options, final CallbackContext callbackContext){
		try {
			final JSONObject object = options.getJSONObject(0);
			if (object == null) {
				callbackContext.error("feed template is null.");
				return;
			}
			if (!object.has("content")) {
				callbackContext.error("content is null.");
				return;
			}

			ContentObject contentObject = getContentObject(object.getJSONObject("content"));
			if (contentObject == null) {
				callbackContext.error("Either Content or Content.title/link/imageURL is null.");
				return;
			}

			FeedTemplate.Builder feedTemplateBuilder = new FeedTemplate.Builder(contentObject);

			if (object.has("social")) {
				SocialObject socialObject = getSocialObject(object.getJSONObject("social"));
				if (socialObject != null) {
					feedTemplateBuilder.setSocial(socialObject);
				}
			}

			addButtonsArray(object, feedTemplateBuilder);

			KakaoLinkService.getInstance().sendDefault(currentActivity, feedTemplateBuilder.build(),
					new KakaoLinkResponseCallback(callbackContext));

		} catch (Exception e) {
			e.printStackTrace();
			callbackContext.error(e.getMessage());
		}
	}

	/**
	 * On activity result
	 * @param requestCode
	 * @param resultCode
	 * @param intent
	 */
	public void onActivityResult(int requestCode, int resultCode, Intent intent)
	{
		Log.v(LOG_TAG, "kakao : onActivityResult : " + requestCode + ", code: " + resultCode);
		if (Session.getCurrentSession().handleActivityResult(requestCode, resultCode, intent)) {
			return;
		}
		super.onActivityResult(requestCode, resultCode, intent);
	}


	private ContentObject getContentObject(JSONObject object) {
		if (object == null) {
			return null;
		}
		ContentObject.Builder contentObjectBuilder;
		try {
			LinkObject linkObject = getLinkObject(object.getJSONObject("link"));
			if (!object.has("title") || linkObject == null || !object.has("imageURL")) {
				return null;
			}
			contentObjectBuilder = new ContentObject.Builder(object.getString("title"), object.getString("imageURL"),
					linkObject);

			if (object.has("desc")) {
				contentObjectBuilder.setDescrption(object.getString("desc"));
			}
			if (object.has("imageWidth")) {
				contentObjectBuilder.setImageWidth(object.getInt("imageWidth"));
			}
			if (object.has("imageHeight")) {
				contentObjectBuilder.setImageHeight(object.getInt("imageHeight"));
			}
		} catch (Exception e) {
			return null;
		}

		return contentObjectBuilder.build();
	}

	private LinkObject getLinkObject(JSONObject object) {
		if (object == null) {
			return null;
		}
		LinkObject.Builder linkObjectBuilder = new LinkObject.Builder();
		try {
			if (object.has("webURL")) {
				linkObjectBuilder.setWebUrl(object.getString("webURL"));
			}
			if (object.has("mobileWebURL")) {
				linkObjectBuilder.setMobileWebUrl(object.getString("mobileWebURL"));
			}
			if (object.has("androidExecutionParams")) {
				linkObjectBuilder.setAndroidExecutionParams(object.getString("androidExecutionParams"));
			}
			if (object.has("iosExecutionParams")) {
				linkObjectBuilder.setIosExecutionParams(object.getString("iosExecutionParams"));
			}
		} catch (Exception e) {
			return null;
		}
		return linkObjectBuilder.build();
	}

	private SocialObject getSocialObject(JSONObject object) {
		if (object == null) {
			return null;
		}
		SocialObject.Builder socialObjectBuilder = new SocialObject.Builder();
		try {
			if (object.has("likeCount")) {
				socialObjectBuilder.setLikeCount(object.getInt("likeCount"));
			}
			if (object.has("commentCount")) {
				socialObjectBuilder.setCommentCount(object.getInt("commentCount"));
			}
			if (object.has("sharedCount")) {
				socialObjectBuilder.setSharedCount(object.getInt("sharedCount"));
			}
			if (object.has("viewCount")) {
				socialObjectBuilder.setViewCount(object.getInt("viewCount"));
			}
			if (object.has("subscriberCount")) {
				socialObjectBuilder.setSubscriberCount(object.getInt("subscriberCount"));
			}
		} catch (Exception e) {
			return null;
		}
		return socialObjectBuilder.build();
	}

	private ButtonObject getButtonObject(JSONObject object) {
		if (object == null) {
			return null;
		}
		ButtonObject buttonObject;
		try {
			LinkObject linkObject = getLinkObject(object.getJSONObject("link"));
			if (!object.has("title") || linkObject == null) {
				return null;
			}
			buttonObject = new ButtonObject(object.getString("title"), linkObject);
		} catch (Exception e) {
			return null;
		}

		return buttonObject;
	}

	private void addButtonsArray(JSONObject object, Object template) {
		if (object == null) {
			return;
		}
		try {
			if (object.has("buttons")) {
				JSONArray buttons = new JSONArray(object.getString("buttons"));
				if (buttons.length() < 1) {
					return;
				}
				for (int i = 0; i < buttons.length(); i++) {
					ButtonObject buttonObject = getButtonObject(buttons.getJSONObject(i));
					if (buttonObject == null) {
						continue;
					}
					if (template instanceof FeedTemplate.Builder) {
						((FeedTemplate.Builder) template).addButton(buttonObject);
					} else if (template instanceof ListTemplate.Builder) {
						((ListTemplate.Builder) template).addButton(buttonObject);
					} else if (template instanceof LocationTemplate.Builder) {
						((LocationTemplate.Builder) template).addButton(buttonObject);
					} else if (template instanceof CommerceTemplate.Builder) {
						((CommerceTemplate.Builder) template).addButton(buttonObject);
					} else if (template instanceof TextTemplate.Builder) {
						((TextTemplate.Builder) template).addButton(buttonObject);
					}
				}
			}
		} catch (Exception e) {
			return;
		}
	}

	private class KakaoLinkResponseCallback extends ResponseCallback<KakaoLinkResponse> {

		private CallbackContext callbackContext;

		public KakaoLinkResponseCallback(final CallbackContext callbackContext) {
			this.callbackContext = callbackContext;
		}

		@Override
		public void onFailure(ErrorResult errorResult) {
			callbackContext.error("kakao : SessionCallback.onSessionOpened.requestMe.onFailure - " + errorResult);
		}

		@Override
		public void onSuccess (KakaoLinkResponse result) {
			callbackContext.success(200);
		}
	}



	/**
	 * Return current activity
	 */
	public static Activity getCurrentActivity()
	{
		return currentActivity;
	}

	/**
	 * Set current activity
	 */
	public static void setCurrentActivity(Activity currentActivity)
	{
		currentActivity = currentActivity;
	}

	/**
	 * Class KakaoSDKAdapter
	 */
	private static class KakaoSDKAdapter extends KakaoAdapter {

		@Override
		public ISessionConfig getSessionConfig() {
			return new ISessionConfig() {
				@Override
				public AuthType[] getAuthTypes() {
					return new AuthType[] {AuthType.KAKAO_TALK};
				}

				@Override
				public boolean isUsingWebviewTimer() {
					return false;
				}

				@Override
				public ApprovalType getApprovalType() {
					return ApprovalType.INDIVIDUAL;
				}

				@Override
				public boolean isSaveFormData() {
					return true;
				}

				@Override
				public boolean isSecureMode() {
					return false;
				}
			};
		}

		@Override
		public IApplicationConfig getApplicationConfig() {
			return new IApplicationConfig() {

				@Override
				public Context getApplicationContext() {
					return KakaoTalk.getCurrentActivity().getApplicationContext();
				}
			};
		}
	}

}

 

 

 

 

배포하기전 package.json 을 생성해줍니다.

 

plugman createpackagejson .

 

그리고 나서 해당 플러그인을 바로 적용해보려면!

 

 cordova plugin add --link KakaoTalk

해당 명령어를 입력해주면

config.xml 파일에서 플러그인이 적용된 것을 확인할 수 있습니다

 

 

 

자세한 설명은 다음에 기회가 되면 다시 하러 돌아오겠습니다

 

카카오 공유 관련 cordova plugin을 찾고 계시다면 

 

https://github.com/sjwiq200/cordova-plugin-kakao 를 참조해주세요!

 

감사합니다.

728x90
반응형
Comments