package com.taobao.accs.b; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.res.AssetManager; import android.os.Build; import android.os.Handler; import android.os.HandlerThread; import android.os.Message; import android.os.Process; import android.os.StatFs; import android.os.SystemClock; import android.text.TextUtils; import android.util.Base64; import android.util.Log; import com.taobao.accs.common.Constants; import com.taobao.accs.utl.ALog; import com.taobao.accs.utl.UTMini; import com.taobao.accs.utl.UtilityImpl; import com.taobao.accs.utl.k; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URLEncoder; import java.util.Calendar; import java.util.concurrent.locks.ReentrantLock; import org.eclipse.paho.client.mqttv3.MqttTopic; /* compiled from: Taobao */ public class a implements Handler.Callback { public static final int ACT_START = 0; public static final int ACT_STOP = -1; public static final String AGOO_PID = "agoo.pid"; public static final String EX_FILE_NAME = "DaemonServer"; public static final String PROCESS_NAME = "runServer"; /* renamed from: a reason: collision with root package name */ public static String f11377a = "startservice -n {packname}/com.taobao.accs.ChannelService"; private static final String c = "com.taobao.accs.b.a"; private static int g = 7200; private static int h = 2500; private static final ReentrantLock i = new ReentrantLock(); private static a j = null; /* renamed from: b reason: collision with root package name */ public boolean f11378b = false; private Context d = null; private String e; private int f = 1800; private String k = "100001"; private String l = "tb_accs_eudemon_1.1.3"; private String m; private String n; private int o; private String p; private String q; private int r; private boolean s; private Handler t; private HandlerThread u; public a(Context context, int i2, boolean z) { String str = ""; this.m = str; this.n = "21646297"; this.o = 0; this.p = "100.69.165.28"; this.q = "http://100.69.165.28/agoo/report"; this.r = 80; this.s = true; this.t = null; this.u = null; b(); f11377a = "startservice -n {packname}/com.taobao.accs.ChannelService"; this.d = context; this.f = i2; this.f11378b = z; this.e = a(new Build(), "CPU_ABI"); this.m = "/data/data/" + context.getPackageName(); this.o = Constants.SDK_VERSION_CODE; String[] s2 = UtilityImpl.s(this.d); if (!(s2 == null || s2.length == 0)) { str = s2[0]; } this.n = str; if (k.b(context) == 0) { this.p = "agoodm.m.taobao.com"; this.r = 80; this.q = "http://agoodm.m.taobao.com/agoo/report"; this.k = "1009527"; } else if (k.b(context) == 1) { this.p = "110.75.98.154"; this.r = 80; this.q = "http://agoodm.wapa.taobao.com/agoo/report"; this.k = "1009527"; } else { this.p = "100.69.168.33"; this.r = 80; this.q = "http://100.69.168.33/agoo/report"; this.f = 60; this.k = "9527"; } } private void b() { Log.d(c, "start handler init"); this.u = new HandlerThread("soManager-threads"); this.u.setPriority(5); this.u.start(); this.t = new Handler(this.u.getLooper(), this); } private String c() { if (this.e.startsWith("arm")) { return "armeabi/"; } return this.e + MqttTopic.TOPIC_LEVEL_SEPARATOR; } private static String a(Build build, String str) { try { return Build.class.getField(str).get(build).toString(); } catch (Throwable unused) { return "Unknown"; } } public static a a(Context context, int i2, boolean z) { try { i.lock(); if (j == null) { j = new a(context, i2, z); } } catch (Exception e2) { ALog.e(c, "getInstance", e2, new Object[0]); } catch (Throwable th) { i.unlock(); throw th; } i.unlock(); return j; } private String d() throws IOException { File file = new File(this.d.getFilesDir(), EX_FILE_NAME); if (file.exists()) { file.delete(); } String str = c; ALog.w(str, "open assets from = " + c() + EX_FILE_NAME, new Object[0]); FileOutputStream fileOutputStream = new FileOutputStream(file); InputStream inputStream = null; try { if (this.f11378b) { AssetManager assets = this.d.getAssets(); inputStream = assets.open(c() + EX_FILE_NAME); byte[] bArr = new byte[100]; while (true) { int read = inputStream.read(bArr); if (read <= 0) { break; } fileOutputStream.write(bArr, 0, read); } } else { a(fileOutputStream, file); } if (inputStream != null) { try { inputStream.close(); } catch (IOException e2) { ALog.e(c, "error in close input file", e2, new Object[0]); } } try { fileOutputStream.close(); } catch (IOException e3) { ALog.e(c, "error in close io", e3, new Object[0]); } } catch (Exception e4) { ALog.e(c, "error in copy daemon files", e4, new Object[0]); if (0 != 0) { try { inputStream.close(); } catch (IOException e5) { ALog.e(c, "error in close input file", e5, new Object[0]); } } fileOutputStream.close(); } catch (Throwable th) { if (0 != 0) { try { inputStream.close(); } catch (IOException e6) { ALog.e(c, "error in close input file", e6, new Object[0]); } } try { fileOutputStream.close(); } catch (IOException e7) { ALog.e(c, "error in close io", e7, new Object[0]); } throw th; } return file.getCanonicalPath(); } /* JADX WARNING: Removed duplicated region for block: B:30:0x00a8 */ /* JADX WARNING: Removed duplicated region for block: B:35:0x00ba A[SYNTHETIC, Splitter:B:35:0x00ba] */ /* JADX WARNING: Removed duplicated region for block: B:42:? A[RETURN, SYNTHETIC] */ private void a(FileOutputStream fileOutputStream, File file) throws IOException { Throwable th; IOException e2; String a2 = b.a(this.e); String str = c; ALog.d(str, ">>>soDataSize:datasize:" + a2.length(), new Object[0]); byte[] decode = Base64.decode(a2.getBytes(), 0); String str2 = c; ALog.d(str2, ">>>soDataSize:" + decode.length, new Object[0]); if (decode.length > 0 && fileOutputStream != null) { ByteArrayInputStream byteArrayInputStream = null; StatFs statFs = new StatFs(file.getCanonicalPath()); if (((long) statFs.getBlockSize()) * ((long) statFs.getAvailableBlocks()) < ((long) decode.length)) { Log.e(c, "Disk is not enough for writing this file"); return; } try { ByteArrayInputStream byteArrayInputStream2 = new ByteArrayInputStream(decode); try { byte[] bArr = new byte[100]; while (true) { int read = byteArrayInputStream2.read(bArr, 0, 100); if (read >= 0) { fileOutputStream.write(bArr, 0, read); } else { fileOutputStream.getFD().sync(); try { byteArrayInputStream2.close(); return; } catch (IOException e3) { e3.printStackTrace(); return; } } } } catch (IOException e4) { e2 = e4; byteArrayInputStream = byteArrayInputStream2; try { ALog.e(c, "error in write files", e2, new Object[0]); fileOutputStream.getFD().sync(); if (byteArrayInputStream == null) { } } catch (Throwable th2) { th = th2; fileOutputStream.getFD().sync(); if (byteArrayInputStream != null) { try { byteArrayInputStream.close(); } catch (IOException e5) { e5.printStackTrace(); } } throw th; } } catch (Throwable th3) { th = th3; byteArrayInputStream = byteArrayInputStream2; fileOutputStream.getFD().sync(); if (byteArrayInputStream != null) { } throw th; } } catch (IOException e6) { e2 = e6; ALog.e(c, "error in write files", e2, new Object[0]); fileOutputStream.getFD().sync(); if (byteArrayInputStream == null) { byteArrayInputStream.close(); } } } } private void a(String str) { StringBuilder sb = new StringBuilder(); a("", "chmod 500 " + str, sb); a("", str + " " + e(), sb); String str2 = c; ALog.w(str2, str + " " + e(), new Object[0]); } private String e() { StringBuilder sb = new StringBuilder(); String str = "/data/data/" + this.d.getPackageName(); sb.append("-s \""); sb.append(str); sb.append("/lib/"); sb.append("\" "); sb.append("-n \"runServer\" "); sb.append("-p \""); sb.append(g()); sb.append("\" "); sb.append("-f \""); sb.append(str); sb.append("\" "); sb.append("-t \""); sb.append(this.f); sb.append("\" "); sb.append("-c \"agoo.pid\" "); if (this.m != null) { sb.append("-P "); sb.append(this.m); sb.append(" "); } if (this.k != null) { sb.append("-K "); sb.append(this.k); sb.append(" "); } if (this.l != null) { sb.append("-U "); sb.append(this.l); sb.append(" "); } if (this.q != null) { sb.append("-L "); sb.append(this.q); sb.append(" "); } sb.append("-D "); sb.append(f()); sb.append(" "); if (this.p != null) { sb.append("-I "); sb.append(this.p); sb.append(" "); } if (this.r > 0) { sb.append("-O "); sb.append(this.r); sb.append(" "); } String a2 = UtilityImpl.a(this.d); int b2 = UtilityImpl.b(this.d); if (a2 != null && b2 > 0) { sb.append("-X "); sb.append(a2); sb.append(" "); sb.append("-Y "); sb.append(b2); sb.append(" "); } if (this.s) { sb.append("-T "); } sb.append("-Z "); return sb.toString(); } private String f() { String l2 = UtilityImpl.l(this.d); if (TextUtils.isEmpty(l2)) { l2 = "null"; } String str = "{\"package\":\"" + this.d.getPackageName() + "\",\"appKey\":\"" + this.n + "\",\"utdid\":\"" + l2 + "\",\"sdkVersion\":\"" + this.o + "\"}"; try { return URLEncoder.encode(str, "UTF-8"); } catch (Throwable unused) { ALog.e(c, "getReportData failed for url encode, data:" + str, new Object[0]); return str; } } private String g() { StringBuilder sb = new StringBuilder(); sb.append(f11377a.replaceAll("\\{packname\\}", this.d.getApplicationContext().getPackageName())); if (Build.VERSION.SDK_INT > 15) { sb.append(" --user 0"); } return sb.toString(); } private void a(String str, int i2, int i3, String str2, String str3, int i4) { String str4 = "AndroidVer=" + Build.VERSION.RELEASE + "&Model=" + Build.MODEL + "&AndroidSdk=" + Build.VERSION.SDK_INT + "&AccsVer=" + Constants.SDK_VERSION_CODE + "&Appkey=" + this.n + "&PullCount=" + str2 + "&Pid=" + str + "&StartTime=" + i2 + "&EndTime=" + i3 + "&ExitCode=" + str3 + "&AliveTime=" + i4; Log.d(c, "EUDEMON_ENDSTAT doReportDaemonStat:" + str4); UTMini.getInstance().commitEvent(66001, "EUDEMON_ENDSTAT", str4); } /* JADX WARNING: Code restructure failed: missing block: B:35:0x00f5, code lost: r0 = move-exception; */ /* JADX WARNING: Code restructure failed: missing block: B:36:0x00f6, code lost: r1 = r0; r2 = r11; */ /* JADX WARNING: Code restructure failed: missing block: B:68:0x015a, code lost: r0 = e; */ /* JADX WARNING: Code restructure failed: missing block: B:69:0x015b, code lost: r2 = r11; */ /* JADX WARNING: Failed to process nested try/catch */ /* JADX WARNING: Removed duplicated region for block: B:101:0x01a1 A[SYNTHETIC, Splitter:B:101:0x01a1] */ /* JADX WARNING: Removed duplicated region for block: B:106:0x01ae */ /* JADX WARNING: Removed duplicated region for block: B:112:0x01bf A[SYNTHETIC, Splitter:B:112:0x01bf] */ /* JADX WARNING: Removed duplicated region for block: B:117:0x01cc A[SYNTHETIC, Splitter:B:117:0x01cc] */ /* JADX WARNING: Removed duplicated region for block: B:122:0x01d9 A[SYNTHETIC, Splitter:B:122:0x01d9] */ /* JADX WARNING: Removed duplicated region for block: B:127:0x01e6 A[SYNTHETIC, Splitter:B:127:0x01e6] */ /* JADX WARNING: Removed duplicated region for block: B:137:? A[RETURN, SYNTHETIC] */ /* JADX WARNING: Removed duplicated region for block: B:68:0x015a A[ExcHandler: Exception (e java.lang.Exception), PHI: r11 PHI: (r11v1 java.lang.String) = (r11v0 java.lang.String), (r11v2 java.lang.String) binds: [B:9:0x0044, B:14:0x0051] A[DONT_GENERATE, DONT_INLINE], Splitter:B:9:0x0044] */ /* JADX WARNING: Removed duplicated region for block: B:91:0x0187 A[SYNTHETIC, Splitter:B:91:0x0187] */ /* JADX WARNING: Removed duplicated region for block: B:96:0x0194 A[SYNTHETIC, Splitter:B:96:0x0194] */ private void h() { FileOutputStream fileOutputStream; BufferedReader bufferedReader; InputStreamReader inputStreamReader; FileInputStream fileInputStream; String str; Throwable th; BufferedReader bufferedReader2; Exception e2; Throwable th2; FileOutputStream fileOutputStream2; String str2; String str3; String str4; String str5 = "error in close input file"; String str6 = "/data/data/" + this.d.getPackageName() + MqttTopic.TOPIC_LEVEL_SEPARATOR + "eudemon"; File file = new File(str6); if (file.exists()) { try { fileInputStream = new FileInputStream(file); try { inputStreamReader = new InputStreamReader(fileInputStream); try { bufferedReader = new BufferedReader(inputStreamReader); } catch (Exception e3) { e2 = e3; str = str5; bufferedReader2 = null; fileOutputStream = null; try { Log.e(c, "report daemon stat exp:", e2); if (bufferedReader2 != null) { } if (inputStreamReader != null) { } if (fileInputStream != null) { } if (fileOutputStream != null) { } } catch (Throwable th3) { th = th3; bufferedReader = bufferedReader2; if (bufferedReader != null) { } if (inputStreamReader != null) { } if (fileInputStream != null) { } if (fileOutputStream != null) { } throw th; } } catch (Throwable th4) { str = str5; th = th4; bufferedReader = null; fileOutputStream = null; if (bufferedReader != null) { } if (inputStreamReader != null) { } if (fileInputStream != null) { } if (fileOutputStream != null) { } throw th; } } catch (Exception e4) { e2 = e4; str = str5; bufferedReader2 = null; inputStreamReader = null; fileOutputStream = null; Log.e(c, "report daemon stat exp:", e2); if (bufferedReader2 != null) { } if (inputStreamReader != null) { } if (fileInputStream != null) { } if (fileOutputStream != null) { } } catch (Throwable th5) { str = str5; th = th5; inputStreamReader = null; bufferedReader = null; fileOutputStream = null; if (bufferedReader != null) { } if (inputStreamReader != null) { } if (fileInputStream != null) { } if (fileOutputStream != null) { } throw th; } try { StringBuilder sb = new StringBuilder(); while (true) { String readLine = bufferedReader.readLine(); if (readLine == null) { break; } String[] split = readLine.split("\\|"); if (split.length == 5) { String trim = split[0].trim(); int intValue = Integer.valueOf(split[1].trim()).intValue(); int intValue2 = Integer.valueOf(split[2].trim()).intValue(); int i2 = intValue2 - intValue; String trim2 = split[3].trim(); String trim3 = split[4].trim(); if (trim3.equals("0")) { File file2 = new File("/proc", trim); String str7 = c; str4 = trim3; StringBuilder sb2 = new StringBuilder(); str3 = str5; try { sb2.append("pidfile:"); sb2.append(file2); Log.e(str7, sb2.toString()); if (file2.exists()) { sb.append(readLine); sb.append("\n"); str5 = str3; } else { i2 += this.f / 2; } } catch (Exception e5) { e2 = e5; bufferedReader2 = bufferedReader; str = str3; fileOutputStream = null; Log.e(c, "report daemon stat exp:", e2); if (bufferedReader2 != null) { } if (inputStreamReader != null) { } if (fileInputStream != null) { } if (fileOutputStream != null) { } } catch (Throwable th6) { th = th6; str = str3; fileOutputStream = null; if (bufferedReader != null) { } if (inputStreamReader != null) { } if (fileInputStream != null) { } if (fileOutputStream != null) { } throw th; } } else { str4 = trim3; str3 = str5; } a(trim, intValue, intValue2, trim2, str4, i2); sb = sb; str5 = str3; } } try { fileOutputStream2 = new FileOutputStream(new File(str6)); } catch (Exception e6) { e2 = e6; str = str5; bufferedReader2 = bufferedReader; fileOutputStream = null; Log.e(c, "report daemon stat exp:", e2); if (bufferedReader2 != null) { } if (inputStreamReader != null) { } if (fileInputStream != null) { } if (fileOutputStream != null) { } } catch (Throwable th7) { th2 = th7; str = str5; th = th2; fileOutputStream = null; if (bufferedReader != null) { } if (inputStreamReader != null) { } if (fileInputStream != null) { } if (fileOutputStream != null) { } throw th; } try { fileOutputStream2.write(sb.toString().getBytes()); bufferedReader.close(); try { bufferedReader.close(); } catch (Throwable th8) { Log.e(c, "error in close buffreader stream", th8); } try { inputStreamReader.close(); } catch (Throwable th9) { Log.e(c, "error in close reader stream", th9); } try { fileInputStream.close(); str2 = str5; } catch (IOException e7) { str2 = str5; Log.e(c, str2, e7); } try { fileOutputStream2.close(); } catch (IOException e8) { Log.e(c, str2, e8); } } catch (Exception e9) { e2 = e9; str = str5; fileOutputStream = fileOutputStream2; bufferedReader2 = bufferedReader; Log.e(c, "report daemon stat exp:", e2); if (bufferedReader2 != null) { } if (inputStreamReader != null) { } if (fileInputStream != null) { } if (fileOutputStream != null) { } } catch (Throwable th10) { str = str5; th = th10; fileOutputStream = fileOutputStream2; if (bufferedReader != null) { } if (inputStreamReader != null) { } if (fileInputStream != null) { } if (fileOutputStream != null) { } throw th; } } catch (Exception e10) { } catch (Throwable th11) { th2 = th11; str = str5; th = th2; fileOutputStream = null; if (bufferedReader != null) { } if (inputStreamReader != null) { } if (fileInputStream != null) { } if (fileOutputStream != null) { } throw th; } } catch (Exception e11) { e2 = e11; str = str5; bufferedReader2 = null; fileInputStream = null; inputStreamReader = null; fileOutputStream = null; Log.e(c, "report daemon stat exp:", e2); if (bufferedReader2 != null) { try { bufferedReader2.close(); } catch (Throwable th12) { Log.e(c, "error in close buffreader stream", th12); } } if (inputStreamReader != null) { try { inputStreamReader.close(); } catch (Throwable th13) { Log.e(c, "error in close reader stream", th13); } } if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e12) { Log.e(c, str, e12); } } if (fileOutputStream != null) { fileOutputStream.close(); } } catch (Throwable th14) { str = str5; th = th14; fileInputStream = null; inputStreamReader = null; bufferedReader = null; fileOutputStream = null; if (bufferedReader != null) { try { bufferedReader.close(); } catch (Throwable th15) { Log.e(c, "error in close buffreader stream", th15); } } if (inputStreamReader != null) { try { inputStreamReader.close(); } catch (Throwable th16) { Log.e(c, "error in close reader stream", th16); } } if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e13) { Log.e(c, str, e13); } } if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e14) { Log.e(c, str, e14); } } throw th; } } } public void a() { Log.d(c, "start SoManager"); Message obtain = Message.obtain(); obtain.what = 0; this.t.sendMessage(obtain); } private void i() { String str = c; ALog.d(str, "api level is:" + Build.VERSION.SDK_INT, new Object[0]); b(this.d); if (Build.VERSION.SDK_INT < 20) { try { String d2 = d(); h(); a(d2); } catch (IOException e2) { e2.printStackTrace(); } } UTMini.getInstance().commitEvent(66001, "EUDEMON_START", ""); } private void j() { File file = new File("/data/data/" + this.d.getPackageName(), "daemonserver.pid"); if (file.exists()) { file.delete(); } } public static final PendingIntent a(Context context) { Intent intent = new Intent(); intent.setAction(context.getApplicationContext().getPackageName() + ".intent.action.COCKROACH"); intent.putExtra("cockroach", "cockroach-PPreotect"); intent.putExtra("pack", context.getApplicationContext().getPackageName()); return PendingIntent.getService(context, 0, intent, 134217728); } public static void b(Context context) { int i2 = Calendar.getInstance().get(11); AlarmManager alarmManager = (AlarmManager) context.getSystemService("alarm"); if (alarmManager != null) { PendingIntent a2 = a(context); long elapsedRealtime = SystemClock.elapsedRealtime(); if (i2 > 23 || i2 < 6) { ALog.w(c, "time is night, do not wakeup cpu", new Object[0]); b(alarmManager, a2, elapsedRealtime); return; } ALog.w(c, "time is daytime, wakeup cpu for keeping connecntion", new Object[0]); a(alarmManager, a2, elapsedRealtime); } } private static void a(AlarmManager alarmManager, PendingIntent pendingIntent, long j2) { if (pendingIntent != null) { alarmManager.cancel(pendingIntent); int i2 = h; alarmManager.setRepeating(2, ((long) (i2 * 1000)) + j2, (long) (i2 * 1000), pendingIntent); } } private static void b(AlarmManager alarmManager, PendingIntent pendingIntent, long j2) { alarmManager.cancel(pendingIntent); int i2 = g; alarmManager.setRepeating(3, j2 + ((long) (i2 * 1000)), (long) (i2 * 1000), pendingIntent); } /* JADX WARNING: Removed duplicated region for block: B:21:0x0063 */ /* JADX WARNING: Removed duplicated region for block: B:26:0x0073 A[SYNTHETIC, Splitter:B:26:0x0073] */ /* JADX WARNING: Removed duplicated region for block: B:35:? A[RETURN, SYNTHETIC] */ public static void c(Context context) { Throwable th; FileWriter fileWriter; Throwable th2; try { File file = new File(context.getFilesDir(), AGOO_PID); ALog.d(c, "pid path:" + file.getAbsolutePath(), new Object[0]); if (file.exists()) { file.delete(); } file.createNewFile(); try { int myPid = Process.myPid(); fileWriter = new FileWriter(file); try { fileWriter.write(String.valueOf(myPid).toCharArray()); try { fileWriter.close(); } catch (Throwable th3) { ALog.e(c, "error", th3, new Object[0]); } } catch (Throwable th4) { th2 = th4; try { ALog.e(c, "save pid error", th2, new Object[0]); if (fileWriter == null) { fileWriter.close(); } } catch (Throwable th5) { th = th5; if (fileWriter != null) { try { fileWriter.close(); } catch (Throwable th6) { ALog.e(c, "error", th6, new Object[0]); } } throw th; } } } catch (Throwable th7) { fileWriter = null; th = th7; if (fileWriter != null) { } throw th; } } catch (Throwable th8) { ALog.e(c, "error in create file", th8, new Object[0]); } } public boolean handleMessage(Message message) { try { if (message.what == 0) { i(); return true; } else if (message.what != -1) { return true; } else { j(); return true; } } catch (Throwable th) { ALog.e(c, "handleMessage error", th, new Object[0]); return true; } } public static boolean a(String str, String str2, StringBuilder sb) { Log.w("TAG.", str2); try { Process exec = Runtime.getRuntime().exec("sh"); DataInputStream dataInputStream = new DataInputStream(exec.getInputStream()); DataOutputStream dataOutputStream = new DataOutputStream(exec.getOutputStream()); if (str != null && !"".equals(str.trim())) { dataOutputStream.writeBytes("cd " + str + "\n"); } dataOutputStream.writeBytes(str2 + " &\n"); dataOutputStream.writeBytes("exit \n"); dataOutputStream.flush(); exec.waitFor(); byte[] bArr = new byte[dataInputStream.available()]; dataInputStream.read(bArr); String str3 = new String(bArr); if (str3.length() == 0) { return true; } sb.append(str3); return true; } catch (Exception e2) { sb.append("Exception:"); sb.append(e2.getMessage()); return false; } } }