Skip to content

Commit d4b260a

Browse files
author
sungbin5304
committed
🐰 refactoring 🐰
1 parent 9d9166b commit d4b260a

File tree

11 files changed

+248
-203
lines changed

11 files changed

+248
-203
lines changed

app/build.gradle.kts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ dependencies {
8181

8282
Dependencies.Ktx.Core,
8383
Dependencies.Ktx.Config,
84-
// Dependencies.Ktx.Fragment,
8584
Dependencies.Ktx.NavigationUi,
8685
Dependencies.Ktx.NavigationFragment,
8786
Dependencies.Ktx.LifeCycleLiveData,
@@ -93,21 +92,15 @@ dependencies {
9392
Dependencies.Ui.TransformationLayout,
9493
Dependencies.Ui.Browser,
9594
Dependencies.Ui.ShapeOfYou,
96-
Dependencies.Ui.YoYo,
9795
Dependencies.Ui.Lottie,
98-
Dependencies.Ui.SuperBottomSheet,
9996
Dependencies.Ui.SimpleCodeEditor,
10097
Dependencies.Ui.SmoothBottomBar,
10198
Dependencies.Ui.Flexbox,
102-
Dependencies.Ui.JsonViewer,
103-
Dependencies.Ui.Licenser,
10499
Dependencies.Ui.Material,
105100
Dependencies.Ui.Glide,
106101
Dependencies.Ui.ConstraintLayout,
107102

108103
Dependencies.Util.GsonConverter,
109-
Dependencies.Util.YoyoHelper,
110-
Dependencies.Util.HangulParser,
111104
Dependencies.Util.AndroidUtils,
112105
Dependencies.Util.CrashReporter
113106
)

app/src/main/kotlin/com/sungbin/gitkakaobot/GitKakaoBot.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.sungbin.gitkakaobot
22

33
import android.app.Application
4+
import com.sungbin.gitkakaobot.bot.ApiClass
5+
import com.sungbin.gitkakaobot.bot.Bot
46
import com.sungbin.gitkakaobot.util.BotUtil
57
import dagger.hilt.android.HiltAndroidApp
68

@@ -15,6 +17,8 @@ class GitKakaoBot : Application() {
1517
override fun onCreate() {
1618
super.onCreate()
1719

20+
Bot.init(applicationContext)
21+
ApiClass.init(applicationContext)
1822
BotUtil.init(applicationContext)
1923
}
2024

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.sungbin.gitkakaobot.bot
2+
3+
import android.app.Notification
4+
import android.app.RemoteInput
5+
import android.content.Context
6+
import android.content.Intent
7+
import android.graphics.Bitmap
8+
import android.os.Bundle
9+
import com.faendir.rhino_android.RhinoAndroidHelper
10+
import com.sungbin.gitkakaobot.R
11+
import com.sungbin.gitkakaobot.model.Bot
12+
import com.sungbin.gitkakaobot.model.BotCompile
13+
import com.sungbin.gitkakaobot.util.BotUtil
14+
import com.sungbin.gitkakaobot.util.UiUtil
15+
import com.sungbin.gitkakaobot.util.manager.StackManager
16+
import org.mozilla.javascript.ImporterTopLevel
17+
import org.mozilla.javascript.ScriptableObject
18+
19+
20+
/**
21+
* Created by SungBin on 2020-12-18.
22+
*/
23+
24+
object Bot {
25+
26+
private lateinit var context: Context
27+
fun init(context: Context) {
28+
this.context = context
29+
}
30+
31+
fun replyToSession(session: Notification.Action?, value: String) {
32+
if (session == null) {
33+
UiUtil.toast(context, context.getString(R.string.bot_cant_load_session))
34+
} else {
35+
try {
36+
val sendIntent = Intent()
37+
val msg = Bundle()
38+
for (inputable in session.remoteInputs) msg.putCharSequence(
39+
inputable.resultKey,
40+
value
41+
)
42+
RemoteInput.addResultsToIntent(session.remoteInputs, sendIntent, msg)
43+
session.actionIntent.send(context, 0, sendIntent)
44+
} catch (exception: Exception) {
45+
UiUtil.error(context, exception)
46+
}
47+
}
48+
}
49+
50+
fun compileJavaScript(bot: Bot): BotCompile {
51+
return try {
52+
val rhino = RhinoAndroidHelper().enterContext().apply {
53+
languageVersion = org.mozilla.javascript.Context.VERSION_ES6
54+
optimizationLevel = bot.optimization
55+
}
56+
val scope = rhino.initStandardObjects(ImporterTopLevel(rhino)) as ScriptableObject
57+
ScriptableObject.defineClass(scope, ApiClass.Log::class.java, false, true)
58+
ScriptableObject.defineClass(scope, ApiClass.Api::class.java, false, true)
59+
ScriptableObject.defineClass(scope, ApiClass.Scope::class.java, false, true)
60+
ScriptableObject.defineClass(scope, ApiClass.File::class.java, false, true)
61+
rhino.compileString(BotUtil.getBotCode(bot), bot.name, 1, null).exec(rhino, scope)
62+
StackManager.scopes[bot.uuid] = scope
63+
org.mozilla.javascript.Context.exit()
64+
BotCompile(true, null)
65+
} catch (exception: Exception) {
66+
BotCompile(false, exception)
67+
}
68+
}
69+
70+
fun callJsResponder(
71+
bot: Bot,
72+
room: String,
73+
msg: String,
74+
sender: String,
75+
isGroupChat: Boolean,
76+
session: Notification.Action?,
77+
profileImage: Bitmap?,
78+
packageName: String,
79+
isDebugMode: Boolean
80+
) {
81+
try {
82+
val rhino = RhinoAndroidHelper().enterContext().apply {
83+
languageVersion = org.mozilla.javascript.Context.VERSION_ES6
84+
optimizationLevel = bot.optimization
85+
}
86+
val scope = StackManager.scopes[bot.uuid]
87+
val function = StackManager.functions[bot.uuid]
88+
if (!isDebugMode) {
89+
function?.call(
90+
rhino,
91+
scope,
92+
scope,
93+
arrayOf(
94+
room, msg, sender, isGroupChat,
95+
Replier(session),
96+
ImageDB(profileImage), packageName
97+
)
98+
)
99+
} else {
100+
// todo: 디버그 모드
101+
}
102+
org.mozilla.javascript.Context.exit()
103+
} catch (exception: Exception) {
104+
// todo: 오류처리
105+
}
106+
}
107+
108+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.sungbin.gitkakaobot.bot
2+
3+
import android.graphics.Bitmap
4+
import android.util.Base64.encodeToString
5+
import java.io.ByteArrayOutputStream
6+
7+
class ImageDB(private val bitmap: Bitmap?) {
8+
fun getProfileImage(): String? {
9+
if (bitmap == null) return null
10+
val baos = ByteArrayOutputStream()
11+
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos)
12+
val bImage = baos.toByteArray()
13+
return encodeToString(bImage, 0)
14+
}
15+
16+
fun getLastImage() = PicturePathManager.getLastPicture()
17+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.sungbin.gitkakaobot.bot;
2+
3+
import android.graphics.Bitmap;
4+
import android.graphics.BitmapFactory;
5+
import android.os.Environment;
6+
import android.util.Base64;
7+
8+
import java.io.ByteArrayOutputStream;
9+
import java.io.File;
10+
import java.util.Arrays;
11+
import java.util.Comparator;
12+
import java.util.Objects;
13+
14+
import javax.annotation.Nullable;
15+
16+
public class PicturePathManager {
17+
private static final String sdcard = Environment.getExternalStorageDirectory().getAbsolutePath();
18+
private static final String PICTURE_PATH = sdcard + "/Android/data/com.kakao.talk/contents/Mg==";
19+
20+
public static String getLastPictureFolderPath() {
21+
File file = new File(PICTURE_PATH);
22+
File[] list = file.listFiles();
23+
Arrays.sort(Objects.requireNonNull(list), new ModifiedDate());
24+
return list[0].toString();
25+
}
26+
27+
public static File[] getLastPictureFilePathFromFoldPath(String path) {
28+
File file = new File(path);
29+
File[] list = file.listFiles();
30+
Arrays.sort(Objects.requireNonNull(list), new ModifiedDate());
31+
return list;
32+
}
33+
34+
@Nullable
35+
public static String getLastPicture() {
36+
try {
37+
File[] path = getLastPictureFilePathFromFoldPath(getLastPictureFolderPath());
38+
for (File value : path) {
39+
File file = new File(value.toString());
40+
if (Objects.requireNonNull(file.listFiles()).length > 0) {
41+
String picture = getLastPictureFilePathFromFoldPath(file.getPath())[0].toString();
42+
Bitmap bm = BitmapFactory.decodeFile(picture);
43+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
44+
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
45+
byte[] bImage = baos.toByteArray();
46+
return Base64.encodeToString(bImage, 0);
47+
}
48+
}
49+
return null;
50+
} catch (Exception ignored) {
51+
return null;
52+
}
53+
}
54+
55+
private static class ModifiedDate implements Comparator<File> {
56+
public int compare(File f1, File f2) {
57+
return Long.compare(f2.lastModified(), f1.lastModified());
58+
}
59+
}
60+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.sungbin.gitkakaobot.bot
2+
3+
import android.app.Notification
4+
import com.sungbin.gitkakaobot.listener.MessageListener.Companion.replyToSession
5+
import com.sungbin.gitkakaobot.util.BotUtil.showAll
6+
import com.sungbin.gitkakaobot.util.manager.StackManager.sessions
7+
8+
class Replier(
9+
private val session: Notification.Action?
10+
) {
11+
fun reply(message: String) {
12+
replyToSession(session, message)
13+
}
14+
15+
fun reply(room: String, message: String) {
16+
replyToSession(sessions[room], message)
17+
}
18+
19+
fun replyShowAll(message: String, message2: String) {
20+
replyToSession(session, "$message$showAll$message2")
21+
}
22+
23+
fun replyShowAll(room: String, message: String, message2: String) {
24+
replyToSession(sessions[room], "$message$showAll$message2")
25+
}
26+
}

0 commit comments

Comments
 (0)