package com.getkeepsafe.relinker; import android.content.Context; import android.util.Log; import com.getkeepsafe.relinker.ReLinker; import com.getkeepsafe.relinker.elf.ElfParser; import java.io.File; import java.io.FilenameFilter; import java.io.IOException; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Set; /* loaded from: classes.dex */ public class ReLinkerInstance { protected final Set a; protected final ReLinker.LibraryLoader b; protected final ReLinker.LibraryInstaller c; protected boolean d; protected boolean e; protected ReLinker.Logger f; protected ReLinkerInstance() { this(new SystemLibraryLoader(), new ApkLibraryInstaller()); } /* JADX INFO: Access modifiers changed from: private */ public void c(Context context, String str, String str2) { ElfParser elfParser; if (this.a.contains(str) && !this.d) { a("%s already loaded previously!", str); return; } try { this.b.loadLibrary(str); this.a.add(str); a("%s (%s) was loaded normally!", str, str2); } catch (UnsatisfiedLinkError e) { a("Loading the library normally failed: %s", Log.getStackTraceString(e)); a("%s (%s) was not loaded normally, re-linking...", str, str2); File b = b(context, str, str2); if (!b.exists() || this.d) { if (this.d) { a("Forcing a re-link of %s (%s)...", str, str2); } a(context, str, str2); this.c.a(context, this.b.a(), this.b.a(str), b, this); } try { if (this.e) { ElfParser elfParser2 = null; try { elfParser = new ElfParser(b); } catch (Throwable th) { th = th; } try { List b2 = elfParser.b(); elfParser.close(); Iterator it = b2.iterator(); while (it.hasNext()) { a(context, this.b.b(it.next())); } } catch (Throwable th2) { th = th2; elfParser2 = elfParser; elfParser2.close(); throw th; } } } catch (IOException unused) { } this.b.c(b.getAbsolutePath()); this.a.add(str); a("%s (%s) was re-linked!", str, str2); } } protected File b(Context context, String str, String str2) { String a = this.b.a(str); if (TextUtils.a(str2)) { return new File(a(context), a); } return new File(a(context), a + "." + str2); } protected ReLinkerInstance(ReLinker.LibraryLoader libraryLoader, ReLinker.LibraryInstaller libraryInstaller) { this.a = new HashSet(); if (libraryLoader == null) { throw new IllegalArgumentException("Cannot pass null library loader"); } if (libraryInstaller == null) { throw new IllegalArgumentException("Cannot pass null library installer"); } this.b = libraryLoader; this.c = libraryInstaller; } public void a(Context context, String str) { a(context, str, (String) null, (ReLinker.LoadListener) null); } public void a(final Context context, final String str, final String str2, final ReLinker.LoadListener loadListener) { if (context != null) { if (!TextUtils.a(str)) { a("Beginning load of %s...", str); if (loadListener == null) { c(context, str, str2); return; } else { new Thread(new Runnable() { // from class: com.getkeepsafe.relinker.ReLinkerInstance.1 @Override // java.lang.Runnable public void run() { try { ReLinkerInstance.this.c(context, str, str2); loadListener.a(); } catch (MissingLibraryException e) { loadListener.a(e); } catch (UnsatisfiedLinkError e2) { loadListener.a(e2); } } }).start(); return; } } throw new IllegalArgumentException("Given library is either null or empty"); } throw new IllegalArgumentException("Given context is null"); } protected File a(Context context) { return context.getDir("lib", 0); } protected void a(Context context, String str, String str2) { File a = a(context); File b = b(context, str, str2); final String a2 = this.b.a(str); File[] listFiles = a.listFiles(new FilenameFilter(this) { // from class: com.getkeepsafe.relinker.ReLinkerInstance.2 @Override // java.io.FilenameFilter public boolean accept(File file, String str3) { return str3.startsWith(a2); } }); if (listFiles == null) { return; } for (File file : listFiles) { if (this.d || !file.getAbsolutePath().equals(b.getAbsolutePath())) { file.delete(); } } } public void a(String str, Object... objArr) { a(String.format(Locale.US, str, objArr)); } public void a(String str) { ReLinker.Logger logger = this.f; if (logger != null) { logger.log(str); } } }