com.kakao.KakaoLink.java Source code

Java tutorial

Introduction

Here is the source code for com.kakao.KakaoLink.java

Source

/**
 * Copyright 2014 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;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import com.kakao.helper.CommonProtocol;
import com.kakao.helper.Logger;
import com.kakao.helper.SystemInfo;
import com.kakao.helper.TalkProtocol;
import com.kakao.helper.Utility;
import com.kakao.internal.KakaoTalkLinkProtocol;
import com.kakao.sdk.R.string;

/**
 * ??    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 = "";

    /**
     * ?? API  singleton KakaoLink .
     */
    public static KakaoLink getKakaoLink(final Context context) throws KakaoParameterException {
        if (singltonKakaoLink != null)
            return singltonKakaoLink;

        if (appKey == null)
            appKey = Utility.getMetadata(context, APP_KEY_PROPERTY);
        if (TextUtils.isEmpty(appKey))
            throw new KakaoParameterException(context.getString(string.com_kakao_alert_appKey));
        else {
            appVer = String.valueOf(Utility.getAppVersion(context));
            appPackageName = Utility.getAppPackageName(context);
            singltonKakaoLink = new KakaoLink();
            return singltonKakaoLink;
        }
    }

    /**
     *     Builder ?.
     * @return ?? KakaoTalkLinkMessageBuilder
     */
    public KakaoTalkLinkMessageBuilder createKakaoTalkLinkMessageBuilder() {
        return new KakaoTalkLinkMessageBuilder(appKey, appVer);
    }

    /**
     *  ? ??    .
     * @param linkMessage     contents
     * @param context ?  context
     */
    public void sendMessage(final String linkMessage, final Context context) throws KakaoParameterException {
        final Intent intent = TalkProtocol.createKakakoTalkLinkIntent(context, linkMessage);
        if (intent == null) {
            //alert install dialog
            new AlertDialog.Builder(context).setIcon(android.R.drawable.ic_dialog_alert)
                    .setMessage(context.getString(string.com_kakao_alert_install_kakaotalk))
                    .setPositiveButton(android.R.string.ok, new OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            context.startActivity(new Intent(Intent.ACTION_VIEW,
                                    Uri.parse(KakaoTalkLinkProtocol.TALK_MARKET_URL_PREFIX + makeReferrer())));
                        }
                    }).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.getInstance().w(e.getMessage());
            return "";
        }
        return json.toString();
    }
}