Skip to content

Commit 58f5526

Browse files
committed
Upload
0 parents  commit 58f5526

File tree

28 files changed

+679
-0
lines changed

28 files changed

+679
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.gradle/
2+
local.properties
3+
.idea/
4+
build

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 24
5+
buildToolsVersion "24.0.1"
6+
7+
defaultConfig {
8+
applicationId "com.rmpi.scriptablekakaobot"
9+
minSdkVersion 21
10+
targetSdkVersion 23
11+
versionCode 1
12+
versionName "1.0"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
}
21+
22+
dependencies {
23+
compile fileTree(dir: 'libs', include: ['*.jar'])
24+
testCompile 'junit:junit:4.12'
25+
compile 'com.android.support:appcompat-v7:24.1.1'
26+
}

app/libs/rhino-1.7.7.1.jar

1.15 MB
Binary file not shown.

app/proguard-rules.pro

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in C:\Program Files (x86)\Android\android-sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.rmpi.scriptablekakaobot;
2+
3+
import android.app.Application;
4+
import android.test.ApplicationTestCase;
5+
6+
/**
7+
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8+
*/
9+
public class ApplicationTest extends ApplicationTestCase<Application> {
10+
public ApplicationTest() {
11+
super(Application.class);
12+
}
13+
}

app/src/main/AndroidManifest.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.rmpi.scriptablekakaobot">
4+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
5+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
6+
<uses-permission android:name="android.permission.VIBRATE" />
7+
<uses-permission android:name="android.permission.RECORD_AUDIO" />
8+
<uses-permission android:name="android.permission.INTERNET" />
9+
10+
<application
11+
android:allowBackup="true"
12+
android:icon="@mipmap/ic_launcher"
13+
android:label="@string/app_name"
14+
android:supportsRtl="true"
15+
android:theme="@style/AppTheme">
16+
<activity android:name=".MainActivity">
17+
<intent-filter>
18+
<action android:name="android.intent.action.MAIN"/>
19+
20+
<category android:name="android.intent.category.LAUNCHER"/>
21+
</intent-filter>
22+
</activity>
23+
<service android:name=".KakaotalkListener"
24+
android:label="@string/app_name"
25+
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
26+
<intent-filter>
27+
<action android:name="android.service.notification.NotificationListenerService" />
28+
</intent-filter>
29+
</service>
30+
</application>
31+
32+
</manifest>
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.rmpi.scriptablekakaobot;
2+
3+
import android.app.Notification;
4+
import android.app.PendingIntent;
5+
import android.app.RemoteInput;
6+
import android.content.Intent;
7+
import android.os.Bundle;
8+
import android.os.Environment;
9+
import android.os.Process;
10+
import android.service.notification.NotificationListenerService;
11+
import android.service.notification.StatusBarNotification;
12+
import android.text.Html;
13+
import android.text.SpannableString;
14+
import android.util.Log;
15+
import org.mozilla.javascript.*;
16+
import org.mozilla.javascript.annotations.JSStaticFunction;
17+
18+
import java.io.File;
19+
import java.io.FileReader;
20+
21+
public class KakaotalkListener extends NotificationListenerService {
22+
private static Function responder;
23+
private static ScriptableObject execScope;
24+
private static Notification.Action lastSession;
25+
private static android.content.Context execContext;
26+
27+
@Override
28+
public void onNotificationPosted(StatusBarNotification sbn) {
29+
super.onNotificationPosted(sbn);
30+
31+
if (!MainActivity.getOn(getApplicationContext())) return;
32+
33+
if (sbn.getPackageName().equals("com.kakao.talk")) {
34+
Notification.WearableExtender wExt = new Notification.WearableExtender(sbn.getNotification());
35+
for (Notification.Action act : wExt.getActions())
36+
if (act.getRemoteInputs() != null && act.getRemoteInputs().length > 0)
37+
if (act.title.toString().toLowerCase().contains("reply") ||
38+
act.title.toString().toLowerCase().contains("Reply") ||
39+
act.title.toString().toLowerCase().contains("답장")) {
40+
execContext = getApplicationContext();
41+
lastSession = act;
42+
callResponder(sbn.getNotification().extras.getString("android.title"), sbn.getNotification().extras.get("android.text"));
43+
}
44+
}
45+
}
46+
47+
static void initializeScript() {
48+
try {
49+
File scriptDir = new File(Environment.getExternalStorageDirectory() + File.separator + "kbot");
50+
if (!scriptDir.exists()) scriptDir.mkdirs();
51+
File script = new File(scriptDir, "response.js");
52+
if (!script.exists()) script.createNewFile();
53+
Context parseContext = org.mozilla.javascript.Context.enter();
54+
parseContext.setOptimizationLevel(-1);
55+
Script script_real = parseContext.compileReader(new FileReader(script), script.getName(), 0, null);
56+
ScriptableObject scope = parseContext.initStandardObjects();
57+
ScriptableObject.defineClass(scope, Kakaotalk.class);
58+
execScope = scope;
59+
script_real.exec(parseContext, scope);
60+
responder = (Function) scope.get("response", scope);
61+
Context.exit();
62+
} catch (Exception e) {
63+
Log.e("parser", "?", e);
64+
Process.killProcess(Process.myPid());
65+
return;
66+
}
67+
}
68+
69+
private void callResponder(String room, Object msg) {
70+
if (responder == null || execScope == null) initializeScript();
71+
Context parseContext = Context.enter();
72+
parseContext.setOptimizationLevel(-1);
73+
String sender;
74+
String _msg;
75+
76+
if (msg instanceof String) {
77+
sender = room;
78+
_msg = (String) msg;
79+
} else {
80+
String html = Html.toHtml((SpannableString) msg);
81+
Log.d("parser", html);
82+
sender = Html.fromHtml(html.split("<b>")[1].split("</b>")[0]).toString();
83+
_msg = Html.fromHtml(html.split("</b>")[1].split("</p>")[0].substring(1)).toString();
84+
}
85+
86+
responder.call(parseContext, execScope, execScope, new Object[] { room, _msg, sender});
87+
}
88+
89+
public static class Kakaotalk extends ScriptableObject {
90+
public Kakaotalk() {
91+
super();
92+
}
93+
94+
@Override
95+
public String getClassName() {
96+
return "Kakaotalk";
97+
}
98+
99+
@JSStaticFunction
100+
public static void replyLast(String value) {
101+
if (lastSession == null) return;
102+
Intent sendIntent = new Intent();
103+
Bundle msg = new Bundle();
104+
for (RemoteInput inputable : lastSession.getRemoteInputs()) msg.putCharSequence(inputable.getResultKey(), value);
105+
RemoteInput.addResultsToIntent(lastSession.getRemoteInputs(), sendIntent, msg);
106+
107+
try {
108+
lastSession.actionIntent.send(execContext, 0, sendIntent);
109+
} catch (PendingIntent.CanceledException e) {
110+
111+
}
112+
}
113+
}
114+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.rmpi.scriptablekakaobot;
2+
3+
import android.Manifest;
4+
import android.annotation.TargetApi;
5+
import android.content.Context;
6+
import android.content.Intent;
7+
import android.content.pm.PackageManager;
8+
import android.os.Build;
9+
import android.support.annotation.NonNull;
10+
import android.support.v7.app.AppCompatActivity;
11+
import android.os.Bundle;
12+
import android.view.View;
13+
import android.widget.CompoundButton;
14+
import android.widget.Switch;
15+
import org.mozilla.javascript.Function;
16+
17+
public class MainActivity extends AppCompatActivity {
18+
private static String PREFS_KEY = "bot";
19+
private static String ON_KEY = "on";
20+
private boolean granted = true;
21+
22+
// UI
23+
24+
@Override
25+
protected void onCreate(Bundle savedInstanceState) {
26+
super.onCreate(savedInstanceState);
27+
grantPermission();
28+
KakaotalkListener.initializeScript();
29+
setContentView(R.layout.activity_main);
30+
Switch onOffSwitch = (Switch) findViewById(R.id.switch1);
31+
onOffSwitch.setChecked(getOn(this));
32+
onOffSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
33+
@Override
34+
public void onCheckedChanged(CompoundButton v, boolean b) {
35+
putOn(getApplicationContext(), b);
36+
}
37+
});
38+
}
39+
40+
public void onSettingsClick(View v) {
41+
startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
42+
}
43+
44+
public void onReloadClick(View v) {
45+
KakaotalkListener.initializeScript();
46+
}
47+
48+
// Util
49+
50+
private void grantPermission() {
51+
if (Build.VERSION.SDK_INT >= 23)
52+
if (!(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)) {
53+
granted = false;
54+
requestPermissions(new String[] { Manifest.permission.READ_EXTERNAL_STORAGE }, 1);
55+
Thread permChecker = new Thread(new Runnable() {
56+
@Override
57+
public void run() {
58+
long currTime = System.currentTimeMillis();
59+
while(!granted) if (System.currentTimeMillis() - currTime > 10000) MainActivity.this.finish();
60+
}
61+
});
62+
permChecker.start();
63+
64+
try {
65+
permChecker.join();
66+
} catch (InterruptedException e) {
67+
finish();
68+
}
69+
}
70+
71+
}
72+
73+
@TargetApi(23)
74+
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
75+
if (requestCode == 1)
76+
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
77+
granted = true;
78+
}
79+
80+
static boolean getOn(Context ctx) {
81+
return ctx.getSharedPreferences(PREFS_KEY, MODE_PRIVATE).getBoolean(ON_KEY, false);
82+
}
83+
84+
private static void putOn(Context ctx, boolean value) {
85+
ctx.getSharedPreferences(PREFS_KEY, MODE_PRIVATE).edit().putBoolean(ON_KEY, value).apply();
86+
}
87+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:paddingLeft="@dimen/activity_horizontal_margin"
8+
android:paddingRight="@dimen/activity_horizontal_margin"
9+
android:paddingTop="@dimen/activity_vertical_margin"
10+
android:paddingBottom="@dimen/activity_vertical_margin"
11+
tools:context="com.rmpi.scriptablekakaobot.MainActivity">
12+
13+
<Switch
14+
android:layout_width="wrap_content"
15+
android:layout_height="wrap_content"
16+
android:text="온 / 오프"
17+
android:id="@+id/switch1" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"
18+
android:switchPadding="20dp" android:checked="false"/>
19+
<TextView
20+
android:layout_width="wrap_content"
21+
android:layout_height="wrap_content"
22+
android:textAppearance="?android:attr/textAppearanceMedium"
23+
android:text="Android Wear 앱 필요\n알림 확인 권한 필요"
24+
android:id="@+id/textView"
25+
android:layout_marginTop="24dp" android:layout_below="@+id/switch1" android:layout_centerHorizontal="true"
26+
android:textColor="#ff0000"/>
27+
<Button
28+
style="?android:attr/buttonStyleSmall"
29+
android:layout_width="wrap_content"
30+
android:layout_height="wrap_content"
31+
android:text="권한 설정하러 가기"
32+
android:id="@+id/button" android:layout_below="@+id/textView" android:layout_centerHorizontal="true"
33+
android:layout_marginTop="22dp" android:nestedScrollingEnabled="false" android:onClick="onSettingsClick"/>
34+
<Button
35+
style="?android:attr/buttonStyleSmall"
36+
android:layout_width="wrap_content"
37+
android:layout_height="wrap_content"
38+
android:text="스크립트 리로드"
39+
android:id="@+id/button2"
40+
android:layout_below="@+id/button" android:layout_centerHorizontal="true" android:onClick="onReloadClick"/>
41+
</RelativeLayout>
3.34 KB
Loading
2.15 KB
Loading
4.73 KB
Loading
7.54 KB
Loading
10.2 KB
Loading
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<resources>
2+
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
3+
(such as screen margins) for screens with more than 820dp of available width. This
4+
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
5+
<dimen name="activity_horizontal_margin">64dp</dimen>
6+
</resources>

app/src/main/res/values/colors.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<color name="colorPrimary">#3F51B5</color>
4+
<color name="colorPrimaryDark">#303F9F</color>
5+
<color name="colorAccent">#FF4081</color>
6+
</resources>

app/src/main/res/values/dimens.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<resources>
2+
<!-- Default screen margins, per the Android Design guidelines. -->
3+
<dimen name="activity_horizontal_margin">16dp</dimen>
4+
<dimen name="activity_vertical_margin">16dp</dimen>
5+
</resources>

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="app_name">ScriptableKakaoBot</string>
3+
</resources>

app/src/main/res/values/styles.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<resources>
2+
3+
<!-- Base application theme. -->
4+
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
5+
<!-- Customize your theme here. -->
6+
<item name="colorPrimary">@color/colorPrimary</item>
7+
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
8+
<item name="colorAccent">@color/colorAccent</item>
9+
</style>
10+
11+
</resources>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.rmpi.scriptablekakaobot;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
/**
8+
* To work on unit tests, switch the Test Artifact in the Build Variants view.
9+
*/
10+
public class ExampleUnitTest {
11+
@Test
12+
public void addition_isCorrect() throws Exception {
13+
assertEquals(4, 2 + 2);
14+
}
15+
}

0 commit comments

Comments
 (0)