package com.kofax.mobile.sdk.extract.id.bundle; import android.content.Context; import com.kofax.mobile.sdk.extract.id.ProjectProviderException; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Enumeration; import java.util.Scanner; import o.C15191gjl; import o.C15193gjn; import o.C15201gjv; import o.InterfaceC13390fZB; import o.InterfaceC13391fZD; import o.gjD; /* loaded from: classes3.dex */ public class BundleCacheProvider implements IBundleCacheProvider { public static final String ODE_CACHE = "_COM_KOFAX_MOBILE_SDK_EXTRACT_ID_ODE_CACHE_"; private static final String afG = "BuildInfo.txt"; private File afH; public BundleCacheProvider(Context context) { this.afH = new File(context.getFilesDir(), ODE_CACHE); } @InterfaceC13391fZD public BundleCacheProvider(@InterfaceC13390fZB(a = "_COM_KOFAX_MOBILE_SDK_EXTRACT_ID_ODE_CACHE_") File file) { this.afH = new File(file, ODE_CACHE); } @Override // com.kofax.mobile.sdk.extract.id.bundle.IBundleCacheProvider public String getLatestModelDataBuildInfo(String str) { String latestVersionForProject = getLatestVersionForProject(str); if (latestVersionForProject == null) { return null; } try { Scanner scanner = new Scanner(new File(getProject(str, latestVersionForProject), afG)); if (scanner.hasNextLine()) { return scanner.nextLine(); } return null; } catch (FileNotFoundException unused) { return null; } } @Override // com.kofax.mobile.sdk.extract.id.bundle.IBundleCacheProvider public File getProject(String str, String str2) { File T = T(str, str2); if (i(T)) { return T; } return null; } @Override // com.kofax.mobile.sdk.extract.id.bundle.IBundleCacheProvider public File cacheProject(String str, IBundle iBundle, String str2) { File T = T(str, str2); try { Enumeration list = iBundle.list(); while (list.hasMoreElements()) { IBundleFile nextElement = list.nextElement(); if (!nextElement.isDirectory()) { a(nextElement, T); } } return T; } catch (IOException e) { throw new ProjectProviderException((Exception) e); } } @Override // com.kofax.mobile.sdk.extract.id.bundle.IBundleCacheProvider public File getVariant(String str, String str2, String str3) { File file = new File(getProject(str, str3), str2); if (i(file)) { return file; } return null; } @Override // com.kofax.mobile.sdk.extract.id.bundle.IBundleCacheProvider public File cacheVariant(String str, String str2, IBundle iBundle, String str3) { try { File file = new File(T(str, str3), str2); Enumeration list = iBundle.list(); while (list.hasMoreElements()) { IBundleFile nextElement = list.nextElement(); if (!nextElement.isDirectory()) { a(nextElement, file); } } return file; } catch (Exception e) { throw new ProjectProviderException(e); } } @Override // com.kofax.mobile.sdk.extract.id.bundle.IBundleCacheProvider public String getLatestVersionForProject(String str) { File file = this.afH; String str2 = null; if (file != null && file.exists()) { for (File file2 : C15191gjl.d(this.afH, C15201gjv.e, C15193gjn.c)) { if (file2.getName().equalsIgnoreCase(str)) { if (str2 != null) { throw new ProjectProviderException("More than one cache directory exists!"); } str2 = file2.getParentFile().getName(); } } } return str2; } protected void clearCache() { File file = this.afH; if (file == null || !file.exists()) { return; } g(this.afH); } private void g(File file) { synchronized (this) { try { h(file); } catch (SecurityException unused) { StringBuilder sb = new StringBuilder("Could not clean cache of file/directory: "); sb.append(this.afH.getAbsolutePath()); throw new ProjectProviderException(sb.toString()); } } } private void h(File file) { if (file.isDirectory()) { for (File file2 : file.listFiles()) { h(file2); } } if (file.delete()) { return; } StringBuilder sb = new StringBuilder("Could not clean cache of file/directory: "); sb.append(file.getAbsolutePath()); throw new ProjectProviderException(sb.toString()); } private File T(String str, String str2) { if (this.afH.exists() && this.afH.list().length > 0) { for (File file : this.afH.listFiles()) { if (!file.getName().equals(str2) && gjD.e(file.list(), str)) { g(new File(file, str)); if (file.list().length <= 0) { file.delete(); } } } } return new File(new File(this.afH, str2), str); } private void a(IBundleFile iBundleFile, File file) throws IOException { File file2 = new File(file, new File(iBundleFile.getName()).getName()); if (file2.exists()) { return; } File parentFile = file2.getParentFile(); if (!parentFile.exists() && !parentFile.mkdirs()) { throw new ProjectProviderException("Unable to save file to cache"); } iBundleFile.getFile(file2); } private boolean i(File file) { if (file != null && file.exists() && file.list() != null && file.list().length > 0) { for (File file2 : file.listFiles()) { if (!file2.isDirectory()) { return true; } } } return false; } }