NekoX/TMessagesProj/src/main/java/org/telegram/messenger/NativeLoader.java

215 lines
6.8 KiB
Java
Raw Normal View History

/*
* This is the source code of Telegram for Android v. 1.3.x.
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2019-01-23 18:03:33 +01:00
* Copyright Nikolai Kudashov, 2013-2018.
*/
2015-09-24 22:52:02 +02:00
package org.telegram.messenger;
2018-07-30 04:07:02 +02:00
import android.annotation.SuppressLint;
import android.content.Context;
2020-03-30 18:49:17 +02:00
import android.content.pm.ApplicationInfo;
import android.os.Build;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class NativeLoader {
2021-04-14 03:44:46 +02:00
private final static int LIB_VERSION = 38;
2014-10-24 15:43:45 +02:00
private final static String LIB_NAME = "tmessages." + LIB_VERSION;
2020-03-30 18:49:17 +02:00
private final static String LIB_SO_NAME = "lib" + LIB_NAME + ".so";
private final static String LOCALE_LIB_SO_NAME = "lib" + LIB_NAME + "loc.so";
private String crashPath = "";
private static volatile boolean nativeLoaded = false;
2020-03-30 18:49:17 +02:00
private static File getNativeLibraryDir(Context context) {
File f = null;
if (context != null) {
try {
f = new File((String)ApplicationInfo.class.getField("nativeLibraryDir").get(context.getApplicationInfo()));
} catch (Throwable th) {
th.printStackTrace();
}
}
if (f == null) {
f = new File(context.getApplicationInfo().dataDir, "lib");
}
if (f.isDirectory()) {
return f;
}
return null;
}
2020-03-30 18:49:17 +02:00
@SuppressLint({"UnsafeDynamicallyLoadedCode", "SetWorldReadable"})
private static boolean loadFromZip(Context context, File destDir, File destLocalFile, String folder) {
try {
for (File file : destDir.listFiles()) {
file.delete();
}
} catch (Exception e) {
FileLog.e(e);
}
ZipFile zipFile = null;
InputStream stream = null;
try {
zipFile = new ZipFile(context.getApplicationInfo().sourceDir);
ZipEntry entry = zipFile.getEntry("lib/" + folder + "/" + LIB_SO_NAME);
if (entry == null) {
throw new Exception("Unable to find file in apk:" + "lib/" + folder + "/" + LIB_NAME);
}
stream = zipFile.getInputStream(entry);
OutputStream out = new FileOutputStream(destLocalFile);
byte[] buf = new byte[4096];
int len;
while ((len = stream.read(buf)) > 0) {
Thread.yield();
out.write(buf, 0, len);
}
out.close();
destLocalFile.setReadable(true, false);
destLocalFile.setExecutable(true, false);
destLocalFile.setWritable(true);
try {
System.load(destLocalFile.getAbsolutePath());
nativeLoaded = true;
} catch (Error e) {
FileLog.e(e);
}
return true;
} catch (Exception e) {
FileLog.e(e);
} finally {
if (stream != null) {
try {
stream.close();
} catch (Exception e) {
FileLog.e(e);
}
}
2020-03-30 18:49:17 +02:00
if (zipFile != null) {
try {
zipFile.close();
} catch (Exception e) {
FileLog.e(e);
}
}
}
2020-03-30 18:49:17 +02:00
return false;
}
2018-07-30 04:07:02 +02:00
@SuppressLint("UnsafeDynamicallyLoadedCode")
2020-03-30 18:49:17 +02:00
public static synchronized void initNativeLibs(Context context) {
if (nativeLoaded) {
return;
}
2015-09-24 22:52:02 +02:00
try {
2018-07-30 04:07:02 +02:00
try {
2020-03-30 18:49:17 +02:00
System.loadLibrary(LIB_NAME);
nativeLoaded = true;
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
2020-03-30 18:49:17 +02:00
FileLog.d("loaded normal lib");
2018-07-30 04:07:02 +02:00
}
2020-03-30 18:49:17 +02:00
return;
2018-07-30 04:07:02 +02:00
} catch (Error e) {
FileLog.e(e);
}
2015-09-24 22:52:02 +02:00
String folder;
try {
2018-07-30 04:07:02 +02:00
String str = Build.CPU_ABI;
if (Build.CPU_ABI.equalsIgnoreCase("x86_64")) {
folder = "x86_64";
} else if (Build.CPU_ABI.equalsIgnoreCase("arm64-v8a")) {
folder = "arm64-v8a";
} else if (Build.CPU_ABI.equalsIgnoreCase("armeabi-v7a")) {
folder = "armeabi-v7a";
} else if (Build.CPU_ABI.equalsIgnoreCase("armeabi")) {
folder = "armeabi";
} else if (Build.CPU_ABI.equalsIgnoreCase("x86")) {
folder = "x86";
} else if (Build.CPU_ABI.equalsIgnoreCase("mips")) {
folder = "mips";
} else {
folder = "armeabi";
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.e("Unsupported arch: " + Build.CPU_ABI);
}
}
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
folder = "armeabi";
}
String javaArch = System.getProperty("os.arch");
if (javaArch != null && javaArch.contains("686")) {
folder = "x86";
}
2020-03-30 18:49:17 +02:00
/*File destFile = getNativeLibraryDir(context);
if (destFile != null) {
destFile = new File(destFile, LIB_SO_NAME);
if (destFile.exists()) {
try {
System.loadLibrary(LIB_NAME);
nativeLoaded = true;
return;
} catch (Error e) {
FileLog.e(e);
}
}
}*/
2014-10-24 15:43:45 +02:00
File destDir = new File(context.getFilesDir(), "lib");
destDir.mkdirs();
2020-03-30 18:49:17 +02:00
File destLocalFile = new File(destDir, LOCALE_LIB_SO_NAME);
2015-09-24 22:52:02 +02:00
if (destLocalFile.exists()) {
2014-10-24 15:43:45 +02:00
try {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
2020-03-30 18:49:17 +02:00
FileLog.d("Load local lib");
2018-07-30 04:07:02 +02:00
}
2020-03-30 18:49:17 +02:00
System.load(destLocalFile.getAbsolutePath());
nativeLoaded = true;
return;
2014-10-24 15:43:45 +02:00
} catch (Error e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
}
2014-10-24 15:43:45 +02:00
destLocalFile.delete();
}
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
2020-03-30 18:49:17 +02:00
FileLog.e("Library not found, arch = " + folder);
2018-07-30 04:07:02 +02:00
}
2020-03-30 18:49:17 +02:00
if (loadFromZip(context, destDir, destLocalFile, folder)) {
return;
2014-10-24 15:43:45 +02:00
}
} catch (Throwable e) {
e.printStackTrace();
}
2014-06-14 14:18:58 +02:00
try {
2020-03-30 18:49:17 +02:00
System.loadLibrary(LIB_NAME);
nativeLoaded = true;
2014-06-14 14:18:58 +02:00
} catch (Error e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2014-06-14 14:18:58 +02:00
}
}
2015-09-24 22:52:02 +02:00
2020-03-30 18:49:17 +02:00
private static native void init(String path, boolean enable);
//public static native void crash();
}