-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainActivity.java
170 lines (155 loc) · 6.88 KB
/
MainActivity.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
package org.meowcat.mp3desktop.qin;
import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.AudioDeviceInfo;
import android.media.AudioManager;
import android.os.BatteryManager;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Objects;
public class MainActivity extends AppCompatActivity {
BatteryManager mBatteryManager;
AudioManager mAudioManager;
WebView mWebView;
long keyDownTimestamp = 0;
public static void openApp(Context context, String packageName) {
final Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
Objects.requireNonNull(intent).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
context.startActivity(intent);
}
public static void openApp(Context context, String packageName, String activityName) {
Intent intent = new Intent().setComponent(new ComponentName(packageName, activityName));
Objects.requireNonNull(intent).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
context.startActivity(intent);
}
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBatteryManager = (BatteryManager) getSystemService(BATTERY_SERVICE);
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mWebView = findViewById(R.id.webView);
mWebView.setBackgroundColor(0);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new JavaScript(), "java");
mWebView.getSettings().setDefaultTextEncodingName("utf-8");
mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
AlertDialog.Builder b = new AlertDialog.Builder(mWebView.getContext());
String[] content = message.split(":",2);
b.setTitle(content[0]);
b.setMessage(content[1]);
b.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
});
b.setCancelable(false);
b.create().show();
return true;
}
});
mWebView.loadUrl("file:///android_asset/meowcat/meowcat.html");
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (keyDownTimestamp == 0) {
keyDownTimestamp = System.currentTimeMillis();
}
return true;
case KeyEvent.KEYCODE_VOLUME_UP:
return true;
default:
return super.onKeyDown(keyCode, event);
}
}
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_HOME:
case KeyEvent.KEYCODE_APP_SWITCH:
case KeyEvent.KEYCODE_BACK:
return true;
default:
return super.onKeyUp(keyCode, event);
}
}
public void finish() {}
@SuppressWarnings("unused")
public class JavaScript {
@JavascriptInterface
public void startApplication(final String packageName) {
try {
getPackageManager().getPackageInfo(packageName, PackageManager.GET_GIDS);
openApp(getApplicationContext(), packageName);
} catch (PackageManager.NameNotFoundException e) {
Toast.makeText(MainActivity.this, "组件" + packageName + "缺失,无法启动。", Toast.LENGTH_SHORT).show();
}
}
@JavascriptInterface
public void startApplication(final String packageName, final String activityName) {
try {
getPackageManager().getPackageInfo(packageName, PackageManager.GET_GIDS);
openApp(getApplicationContext(), packageName, activityName);
} catch (PackageManager.NameNotFoundException e) {
Toast.makeText(MainActivity.this, "组件" + packageName + "缺失,无法启动。", Toast.LENGTH_SHORT).show();
}
}
@JavascriptInterface
public String getStringInfo(final String info) {
switch (info) {
case "DeviceBrand":
return Build.BRAND;
case "DeviceModel":
return Build.MODEL;
case "DeviceSDK":
return "" + Build.VERSION.SDK_INT;
case "Version":
return BuildConfig.VERSION_NAME;
default:
return null;
}
}
@JavascriptInterface
public int getIntInfo(final String info) {
switch (info) {
case "BatteryNumber":
return mBatteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
case "BatteryStatus":
return mBatteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_STATUS);
case "HeadSet":
if (mAudioManager == null) {
return 0;
}
AudioDeviceInfo[] audioDevices = mAudioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
for (AudioDeviceInfo deviceInfo : audioDevices) {
if (deviceInfo.getType() == AudioDeviceInfo.TYPE_WIRED_HEADPHONES || deviceInfo.getType() == AudioDeviceInfo.TYPE_WIRED_HEADSET) {
return 1;
} else if (deviceInfo.getType() == AudioDeviceInfo.TYPE_USB_HEADSET) {
return 2;
}
}
return 0;
default:
return 0;
}
}
}
}