Commit 02f5bfc2 authored by Administrator's avatar Administrator

init

parents
Pipeline #2433 canceled with stages
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.externalNativeBuild
# kdiff3 ignore
*.orig
# maven ignore
target/
# eclipse ignore
.settings/
.project
.classpath
# idea ignore
.idea/
*.ipr
*.iml
*.iws
# temp ignore
*.log
*.cache
*.diff
*.patch
*.tmp
# system ignore
.DS_Store
Thumbs.db
*.apk
build/
apps/
ratelConfig.properties
script/dist
script/dist.zip
container-builder-repkg/src/main/resources/ratel_engine.properties
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.virjar.majora.adr"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
archivesBaseName = "Majora_${versionName}".replace(' ', '_')
}
signingConfigs {
release {
storeFile rootProject.file('script/hermes_key')
storePassword "hermes"
keyAlias "hermes"
keyPassword "hermes"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
//debug ,也使用release的key,hermes代码无法在线调试,所以没有debug的意义
debug {
signingConfig signingConfigs.release
}
}
lintOptions {
abortOnError false
}
packagingOptions {
exclude 'META-INF/INDEX.LIST'
exclude 'META-INF/io.netty.versions.properties'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'org.apache.commons:commons-lang3:3.6'
implementation 'commons-io:commons-io:2.6'
implementation "com.virjar.majora:majora-sdk:1.0"
}
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.virjar.majora.adr">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- 前台服务,保持安排的优先级 -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application
android:name=".TheApp"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.virjar.majora.adr.KeepAliveService"
android:enabled="true"
android:exported="false" />
</application>
</manifest>
\ No newline at end of file
package com.virjar.majora.adr;
import android.Manifest;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Process;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import com.virjar.majora.client.sdk.log.MajoraLogger;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Random;
import java.util.UUID;
public class ClientIdentifier {
private static final String clientIdFileName = "echo_client_id.txt";
private static final String UN_RESOLVE = "un_resolve_";
public static boolean isAndroid = isAndroidEnv();
private static String clientIdInMemory;
public static String id() {
if (clientIdInMemory != null) {
return clientIdInMemory;
}
// from cache file
File file = resolveIdCacheFile();
if (file.exists()) {
try {
String s = readFile(file);
if (s != null && !s.isEmpty() && !s.startsWith(UN_RESOLVE)) {
clientIdInMemory = s;
return clientIdInMemory;
}
} catch (IOException e) {
MajoraLogger.getLogger().error("can not read id file: " + file.getAbsolutePath(), e);
}
}
clientIdInMemory = generateClientId() + "_" + new Random().nextInt(10000);
try {
writeFile(file, clientIdInMemory);
} catch (IOException e) {
MajoraLogger.getLogger().error("can not write id file: " + file.getAbsolutePath(), e);
}
return clientIdInMemory;
}
private static void writeFile(File file, String data) throws IOException {
try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
fileOutputStream.write(data.getBytes());
}
}
private static String readFile(File file) throws IOException {
FileInputStream stream = new FileInputStream(file);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
copyStream(stream, byteArrayOutputStream);
return byteArrayOutputStream.toString();
}
private static void copyStream(InputStream inputStream, OutputStream outputStream) throws IOException {
byte[] buf = new byte[2048];
int len;
while ((len = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, len);
}
}
private static String generateClientId() {
if (isAndroid) {
String s = generateClientIdForAndroid();
if (s != null && !s.isEmpty()) {
return s;
}
}
String mac = generateClientIdForNormalJVM();
if (!TextUtils.isEmpty(mac)) {
return mac;
}
return UN_RESOLVE + UUID.randomUUID().toString();
}
private static String generateClientIdForNormalJVM() {
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
if (networkInterface.isVirtual()) {
continue;
}
if (networkInterface.isLoopback()) {
continue;
}
byte[] hardwareAddress = networkInterface.getHardwareAddress();
if (hardwareAddress == null) {
continue;
}
return parseByte(hardwareAddress[0]) + ":" +
parseByte(hardwareAddress[1]) + ":" +
parseByte(hardwareAddress[2]) + ":" +
parseByte(hardwareAddress[3]) + ":" +
parseByte(hardwareAddress[4]) + ":" +
parseByte(hardwareAddress[5]);
}
return null;
} catch (SocketException e) {
return null;
}
}
private static String parseByte(byte b) {
int intValue;
if (b >= 0) {
intValue = b;
} else {
intValue = 256 + b;
}
return Integer.toHexString(intValue);
}
private static String generateClientIdForAndroid() {
Application application = TheApp.getApplication();
if (application.checkPermission(Manifest.permission.READ_PHONE_STATE, Process.myPid(), Process.myUid())
== PackageManager.PERMISSION_GRANTED
) {
TelephonyManager telephonyManager = (TelephonyManager) application.getSystemService(Context.TELEPHONY_SERVICE);
String imei;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
imei = telephonyManager.getImei();
} else {
imei = telephonyManager.getDeviceId();
}
if (!TextUtils.isEmpty(imei)) {
return imei;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String serial = Build.getSerial();
if (!TextUtils.isEmpty(serial)) {
return serial;
}
}
} else {
MajoraLogger.getLogger().warn("need permission :" + Manifest.permission.READ_PHONE_STATE);
}
String serial = Build.SERIAL;
if ("unknown".equalsIgnoreCase(serial)) {
return "";
}
return serial;
}
private static File resolveIdCacheFile() {
if (isAndroid) {
return resolveAndroidCacheIdFile();
}
return resolveJvmEnvCacheIdFile();
}
private static File resolveJvmEnvCacheIdFile() {
String userHome = System.getProperty("user.home");
File base;
if (userHome != null && !userHome.trim().isEmpty()) {
base = new File(userHome);
} else {
base = new File(".");
}
return new File(base, clientIdFileName);
}
private static File resolveAndroidCacheIdFile() {
Application application = TheApp.getApplication();
return new File(application.getFilesDir(), clientIdFileName);
}
private static boolean isAndroidEnv() {
try {
Class.forName("android.util.Log");
return true;
} catch (Throwable throwable) {
//ignore
}
return false;
}
}
package com.virjar.majora.adr;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.IBinder;
import android.preference.PreferenceManager;
import com.virjar.majora.client.sdk.client.MajoraClient;
import com.virjar.majora.client.sdk.log.MajoraLogger;
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
public class KeepAliveService extends Service {
private static boolean start = false;
public static void startService(Context context) {
if (start) {
return;
}
setNotifyChannel(context);
Intent intent = new Intent(context, KeepAliveService.class);
context.startService(intent);
}
private static void setNotifyChannel(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return;
}
String packageName = context.getPackageName();
@SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(
packageName + ":majora",
"channel", NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.YELLOW);
notificationChannel.setShowBadge(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
if (manager == null) {
return;
}
manager.createNotificationChannel(notificationChannel);
}
private void onServiceStartupInternal() {
String packageName = getPackageName();
PackageManager pm = getPackageManager();
Intent launchIntent = pm.getLaunchIntentForPackage(packageName);
if (launchIntent == null) {
MajoraLogger.getLogger()
.warn("no launchIntent for package: " + getPackageName());
return;
}
Bitmap icon;
try {
icon = drawableToBitmap(
pm.getPackageInfo(packageName, PackageManager.GET_META_DATA)
.applicationInfo.loadIcon(pm)
);
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(e);
}
Notification.Builder builder = new Notification.Builder(this);
// 设置PendingIntent
builder.setContentIntent(PendingIntent.getActivity(this, 0, launchIntent, FLAG_UPDATE_CURRENT))
.setLargeIcon(icon)
.setContentTitle("majora service")
.setContentText("to make sure majora service alive")
.setWhen(System.currentTimeMillis());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId(packageName + ":majora");
}
Notification notification = builder.build();
notification.defaults = Notification.DEFAULT_SOUND;
startForeground(110, notification);
startProxyService();
start = true;
}
private void startProxyService() {
SharedPreferences spf = PreferenceManager.getDefaultSharedPreferences(this);
String serverHost = spf.getString("server_host", "majora.virjar.com");
int serverPort = spf.getInt("server_port", 5879);
new MajoraClient(serverHost, serverPort, ClientIdentifier.id()).startUp();
}
// 5. Drawable----> Bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {
// 获取 drawable 长宽
int width = drawable.getIntrinsicWidth();
int heigh = drawable.getIntrinsicHeight();
drawable.setBounds(0, 0, width, heigh);
// 获取drawable的颜色格式
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
// 创建bitmap
Bitmap bitmap = Bitmap.createBitmap(width, heigh, config);
// 创建bitmap画布
Canvas canvas = new Canvas(bitmap);
// 将drawable 内容画到画布中
drawable.draw(canvas);
return bitmap;
}
@Override
public IBinder onBind(Intent intent) {
onServiceStartupInternal();
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
onServiceStartupInternal();
return START_STICKY;
}
}
package com.virjar.majora.adr;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.goSetting);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SettingsActivity.go(MainActivity.this);
}
});
}
}
package com.virjar.majora.adr;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.support.v7.app.AppCompatActivity;
public class SettingsActivity extends AppCompatActivity {
public static void go(Context context) {
Intent intent = new Intent(context, SettingsActivity.class);
context.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
if (savedInstanceState == null) {
SettingsFragment mSettingsFragment = new SettingsFragment();
replaceFragment(R.id.settings_container, mSettingsFragment);
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void replaceFragment(int viewId, android.app.Fragment fragment) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(viewId, fragment).commit();
}
/**
* A placeholder fragment containing a settings view.
*/
@SuppressLint("WorldReadableFiles")
public static class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_setting);
}
}
}
package com.virjar.majora.adr;
import android.app.Application;
public class TheApp extends Application {
private static TheApp theApp;
public static Application getApplication() {
return theApp;
}
@Override
public void onCreate() {
super.onCreate();
theApp = this;
KeepAliveService.startService(this);
}
}
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="108dp"
android:height="108dp"
android:viewportHeight="108"
android:viewportWidth="108">
<path
android:fillType="evenOdd"
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
android:strokeColor="#00000000"
android:strokeWidth="1">
<aapt:attr name="android:fillColor">
<gradient
android:endX="78.5885"
android:endY="90.9159"
android:startX="48.7653"
android:startY="61.0927"
android:type="linear">
<item
android:color="#44000000"
android:offset="0.0" />
<item
android:color="#00000000"
android:offset="1.0" />
</gradient>
</aapt:attr>
</path>
<path
android:fillColor="#FFFFFF"
android:fillType="nonZero"
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
android:strokeColor="#00000000"
android:strokeWidth="1" />
</vector>
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportHeight="108"
android:viewportWidth="108">
<path
android:fillColor="#26A69A"
android:pathData="M0,0h108v108h-108z" />
<path
android:fillColor="#00000000"
android:pathData="M9,0L9,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M19,0L19,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M29,0L29,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M39,0L39,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M49,0L49,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M59,0L59,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M69,0L69,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M79,0L79,108"
android:strokeColor="#33FFFFFF"