Java tutorial
/** * Copyright 2014 Daum Kakao Corp. * * Redistribution and modification in source or binary forms are not permitted without specific prior written permission. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.kakao.kakaolink; import android.app.AlertDialog; import android.content.ActivityNotFoundException; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.text.TextUtils; import com.kakao.kakaolink.internal.KakaoTalkLinkProtocol; import com.kakao.util.KakaoParameterException; import com.kakao.util.helper.CommonProtocol; import com.kakao.util.helper.SystemInfo; import com.kakao.util.helper.TalkProtocol; import com.kakao.util.helper.Utility; import com.kakao.util.helper.log.Logger; import org.json.JSONException; import org.json.JSONObject; // Do not remove blew import line import com.kakao.android.sdk.R; /** * ?? class . * ? ? KakaoTalkLinkMessageBuilder ?. * API ?. */ public class KakaoLink { static final String APP_KEY_PROPERTY = "com.kakao.sdk.AppKey"; private static KakaoLink singltonKakaoLink; private static String appKey; private static String appVer = ""; private static String appPackageName = ""; private static String appKeyHash = ""; /** * ?? API singleton KakaoLink . * @param context ? context * @return ?? class * @throws KakaoParameterException ? ? ? ? */ public static KakaoLink getKakaoLink(final Context context) throws KakaoParameterException { if (singltonKakaoLink != null) return singltonKakaoLink; SystemInfo.initialize(context); if (appKey == null) appKey = Utility.getMetadata(context, APP_KEY_PROPERTY); if (TextUtils.isEmpty(appKey)) throw new KakaoParameterException(context.getString(R.string.com_kakao_alert_appKey)); else { appVer = String.valueOf(Utility.getAppVersion(context)); appPackageName = Utility.getAppPackageName(context); appKeyHash = Utility.getKeyHash(context); singltonKakaoLink = new KakaoLink(); return singltonKakaoLink; } } /** * Builder ?. * @return ?? KakaoTalkLinkMessageBuilder */ public KakaoTalkLinkMessageBuilder createKakaoTalkLinkMessageBuilder() { return new KakaoTalkLinkMessageBuilder(appKey, appVer, makeExtra()); } /** * ? ?? . * @param builder KakaoTalkLinkMessageBuilder * @param context ? context * @throws KakaoParameterException ? ? ? ? */ public void sendMessage(final KakaoTalkLinkMessageBuilder builder, final Context context) throws KakaoParameterException { final Intent intent = TalkProtocol.createKakakoTalkLinkIntent(context, builder.build()); if (intent == null) { //alert install dialog new AlertDialog.Builder(context).setIcon(android.R.drawable.ic_dialog_alert) .setMessage(context.getString(R.string.com_kakao_alert_install_kakaotalk)) .setPositiveButton(android.R.string.ok, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent marketIntent; try { marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(KakaoTalkLinkProtocol.TALK_MARKET_URL_PREFIX + makeReferrer())); marketIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(marketIntent); } catch (ActivityNotFoundException e) { marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(KakaoTalkLinkProtocol.TALK_MARKET_URL_PREFIX_2 + makeReferrer())); marketIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(marketIntent); } } }).create().show(); } else { context.startActivity(intent); } } private String makeReferrer() { JSONObject json = new JSONObject(); try { json.put(CommonProtocol.KA_HEADER_KEY, SystemInfo.getKAHeader()); json.put(KakaoTalkLinkProtocol.APP_KEY, appKey); json.put(KakaoTalkLinkProtocol.APP_VER, appVer); json.put(KakaoTalkLinkProtocol.APP_PACKAGE, appPackageName); } catch (JSONException e) { Logger.w(e); return ""; } return json.toString(); } private JSONObject makeExtra() { JSONObject json = new JSONObject(); try { json.put(CommonProtocol.KA_HEADER_KEY, SystemInfo.getKAHeader()); json.put(KakaoTalkLinkProtocol.APP_PACKAGE, appPackageName); json.put(KakaoTalkLinkProtocol.APP_KEY_HASH, appKeyHash); } catch (JSONException e) { Logger.w(e); return json; } return json; } }