-
Notifications
You must be signed in to change notification settings - Fork 37
/
KakaoLink.java
210 lines (178 loc) · 5.54 KB
/
KakaoLink.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package com.example.link;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
/**
* Copyright 2011 Kakao Corp. All rights reserved.
*
* @author kakaolink@kakao.com
* @version 1.0
*
*/
public class KakaoLink {
private static KakaoLink kakaoLink = null;
private static String KakaoLinkApiVersion = "2.0";
private static String KakaoLinkURLBaseString = "kakaolink://sendurl";
private static Charset KakaoLinkCharset = Charset.forName("UTF-8");
private static String KakaoLinkEncoding = KakaoLinkCharset.name();
private Context context;
private String params;
private KakaoLink(Context context) {
super();
this.context = context;
this.params = getBaseKakaoLinkUrl();
}
/**
* Return the default singleton instance
*
* @param context
* @return KakaoLink instance.
*/
public static KakaoLink getLink(Context context) {
if (kakaoLink != null)
return kakaoLink;
return new KakaoLink(context);
}
/**
* Opens kakaoLink for parameter.
*
* @param activity
* @param params
*/
private void openKakaoLink(Activity activity, String params) {
Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse(params));
activity.startActivity(intent);
}
/**
* Opens kakaoLink URL for parameters.
*
* @param activity
* @param url
* @param message
* @param appId
* your application ID
* @param appVer
* your application version
* @param appName
* your application name
* @param encoding
* recommend UTF-8
*/
public void openKakaoLink(Activity activity, String url, String message, String appId, String appVer, String appName, String encoding) {
if (isEmptyString(url) || isEmptyString(message) || isEmptyString(appId) || isEmptyString(appVer) || isEmptyString(appName) || isEmptyString(encoding))
throw new IllegalArgumentException();
try {
if (KakaoLinkCharset.equals(Charset.forName(encoding)))
message = new String(message.getBytes(encoding), KakaoLinkEncoding);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
this.params = getBaseKakaoLinkUrl();
appendParam("url", url);
appendParam("msg", message);
appendParam("apiver", KakaoLinkApiVersion);
appendParam("appid", appId);
appendParam("appver", appVer);
appendParam("appname", appName);
appendParam("type", "link");
openKakaoLink(activity, params);
}
/**
* Opens kakaoAppLink with parameters.
*
* @param activity
* @param url
* @param message
* @param appId
* your application ID
* @param appVer
* your application version
* @param appName
* your application name
* @param encoding
* recommend UTF-8
* @param metaInfoArray
*/
public void openKakaoAppLink(Activity activity, String url, String message, String appId, String appVer, String appName, String encoding,
ArrayList<Map<String, String>> metaInfoArray) {
if (isEmptyString(url) || isEmptyString(message) || isEmptyString(appId) || isEmptyString(appVer) || isEmptyString(appName) || isEmptyString(encoding)) {
throw new IllegalArgumentException();
}
try {
if (KakaoLinkCharset.equals(Charset.forName(encoding)))
message = new String(message.getBytes(encoding), KakaoLinkEncoding);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
this.params = getBaseKakaoLinkUrl();
appendParam("url", url);
appendParam("msg", message);
appendParam("apiver", KakaoLinkApiVersion);
appendParam("appid", appId);
appendParam("appver", appVer);
appendParam("appname", appName);
appendParam("type", "app");
appendMetaInfo(metaInfoArray);
openKakaoLink(activity, params);
}
/**
* @return Whether the application can open kakaolink URLs.
*/
public boolean isAvailableIntent() {
Uri kakaoLinkTestUri = Uri.parse(KakaoLinkURLBaseString);
Intent intent = new Intent(Intent.ACTION_SEND, kakaoLinkTestUri);
List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (list == null)
return false;
return list.size() > 0;
}
private boolean isEmptyString(String str) {
return (str == null || str.trim().length() == 0);
}
private void appendParam(final String name, final String value) {
try {
String encodedValue = URLEncoder.encode(value, KakaoLinkEncoding);
params = params + name + "=" + encodedValue + "&";
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
private void appendMetaInfo(ArrayList<Map<String, String>> metaInfoArray) {
params += "metainfo=";
JSONObject obj = new JSONObject();
JSONArray arr = new JSONArray();
try {
for (Map<String, String> metaInfo : metaInfoArray) {
JSONObject metaObj = new JSONObject();
for (String key : metaInfo.keySet()) {
metaObj.put(key, metaInfo.get(key));
}
arr.put(metaObj);
}
obj.put("metainfo", arr);
} catch (JSONException e) {
e.printStackTrace();
}
try {
String encodedValue = URLEncoder.encode(obj.toString(), KakaoLinkEncoding);
params += encodedValue;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
private String getBaseKakaoLinkUrl() {
return KakaoLinkURLBaseString + "?";
}
}