package com.tencent.mm.opensdk.diffdev.a; import android.os.AsyncTask; import android.os.Build; import android.os.Environment; import android.util.Base64; import com.tencent.mm.opensdk.diffdev.OAuthErrCode; import com.tencent.mm.opensdk.diffdev.OAuthListener; import com.tencent.mm.opensdk.utils.Log; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.json.JSONObject; public final class d extends AsyncTask<Void, Void, a> { private static final boolean h = (Environment.getExternalStorageState().equals("mounted") && new File(Environment.getExternalStorageDirectory().getAbsolutePath()).canWrite()); private static final String i = (Environment.getExternalStorageDirectory().getAbsolutePath() + "/tencent/MicroMsg/oauth_qrcode.png"); private static String j; private String appId; private String k; private String l; private OAuthListener m; private f n; private String scope; private String signature; static class a { public OAuthErrCode o; public String p; public String q; public String r; public int s; public String t; public byte[] u; private a() { } public static a a(byte[] bArr) { OAuthErrCode oAuthErrCode; Object[] objArr; String str; a aVar = new a(); if (bArr == null || bArr.length == 0) { Log.e("MicroMsg.SDK.GetQRCodeResult", "parse fail, buf is null"); oAuthErrCode = OAuthErrCode.WechatAuth_Err_NetworkErr; } else { try { try { JSONObject jSONObject = new JSONObject(new String(bArr, "utf-8")); int i = jSONObject.getInt("errcode"); if (i != 0) { Log.e("MicroMsg.SDK.GetQRCodeResult", String.format("resp errcode = %d", Integer.valueOf(i))); aVar.o = OAuthErrCode.WechatAuth_Err_NormalErr; aVar.s = i; aVar.t = jSONObject.optString("errmsg"); return aVar; } String string = jSONObject.getJSONObject("qrcode").getString("qrcodebase64"); if (string != null) { if (string.length() != 0) { byte[] decode = Base64.decode(string, 0); if (decode != null) { if (decode.length != 0) { if (d.h) { File file = new File(d.i); file.mkdirs(); if (file.exists()) { file.delete(); } if (!a(d.i, decode)) { Log.e("MicroMsg.SDK.GetQRCodeResult", String.format("writeToFile fail, qrcodeBuf length = %d", Integer.valueOf(decode.length))); aVar.o = OAuthErrCode.WechatAuth_Err_NormalErr; return aVar; } aVar.o = OAuthErrCode.WechatAuth_Err_OK; aVar.r = d.i; aVar.p = jSONObject.getString("uuid"); aVar.q = jSONObject.getString("appname"); Log.d("MicroMsg.SDK.GetQRCodeResult", String.format("parse succ, save in external storage, uuid = %s, appname = %s, imgPath = %s", aVar.p, aVar.q, aVar.r)); return aVar; } aVar.o = OAuthErrCode.WechatAuth_Err_OK; aVar.u = decode; aVar.p = jSONObject.getString("uuid"); aVar.q = jSONObject.getString("appname"); Log.d("MicroMsg.SDK.GetQRCodeResult", String.format("parse succ, save in memory, uuid = %s, appname = %s, imgBufLength = %d", aVar.p, aVar.q, Integer.valueOf(aVar.u.length))); return aVar; } } Log.e("MicroMsg.SDK.GetQRCodeResult", "parse fail, qrcodeBuf is null"); aVar.o = OAuthErrCode.WechatAuth_Err_JsonDecodeErr; return aVar; } } Log.e("MicroMsg.SDK.GetQRCodeResult", "parse fail, qrcodeBase64 is null"); aVar.o = OAuthErrCode.WechatAuth_Err_JsonDecodeErr; return aVar; } catch (Exception e) { objArr = new Object[]{e.getMessage()}; str = "parse json fail, ex = %s"; Log.e("MicroMsg.SDK.GetQRCodeResult", String.format(str, objArr)); oAuthErrCode = OAuthErrCode.WechatAuth_Err_NormalErr; aVar.o = oAuthErrCode; return aVar; } } catch (Exception e2) { objArr = new Object[]{e2.getMessage()}; str = "parse fail, build String fail, ex = %s"; Log.e("MicroMsg.SDK.GetQRCodeResult", String.format(str, objArr)); oAuthErrCode = OAuthErrCode.WechatAuth_Err_NormalErr; aVar.o = oAuthErrCode; return aVar; } } aVar.o = oAuthErrCode; return aVar; } /* JADX WARNING: Removed duplicated region for block: B:20:0x004f A[SYNTHETIC, Splitter:B:20:0x004f] */ /* JADX WARNING: Removed duplicated region for block: B:25:0x006b A[SYNTHETIC, Splitter:B:25:0x006b] */ /* JADX WARNING: Removed duplicated region for block: B:31:? A[RETURN, SYNTHETIC] */ private static boolean a(String str, byte[] bArr) { Throwable th; Exception e; FileOutputStream fileOutputStream = null; try { FileOutputStream fileOutputStream2 = new FileOutputStream(str); try { fileOutputStream2.write(bArr); fileOutputStream2.flush(); try { fileOutputStream2.close(); } catch (IOException e2) { Log.e("MicroMsg.SDK.GetQRCodeResult", "fout.close() exception:" + e2.getMessage()); } Log.d("MicroMsg.SDK.GetQRCodeResult", "writeToFile ok!"); return true; } catch (Exception e3) { e = e3; fileOutputStream = fileOutputStream2; try { Log.w("MicroMsg.SDK.GetQRCodeResult", "write to file error, exception:" + e.getMessage()); if (fileOutputStream != null) { return false; } try { fileOutputStream.close(); return false; } catch (IOException e4) { Log.e("MicroMsg.SDK.GetQRCodeResult", "fout.close() exception:" + e4.getMessage()); return false; } } catch (Throwable th2) { th = th2; if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e5) { Log.e("MicroMsg.SDK.GetQRCodeResult", "fout.close() exception:" + e5.getMessage()); } } throw th; } } catch (Throwable th3) { th = th3; fileOutputStream = fileOutputStream2; if (fileOutputStream != null) { } throw th; } } catch (Exception e6) { e = e6; Log.w("MicroMsg.SDK.GetQRCodeResult", "write to file error, exception:" + e.getMessage()); if (fileOutputStream != null) { } } } } static { j = null; j = "http://open.weixin.qq.com/connect/sdk/qrconnect?appid=%s&noncestr=%s×tamp=%s&scope=%s&signature=%s"; } public d(String str, String str2, String str3, String str4, String str5, OAuthListener oAuthListener) { this.appId = str; this.scope = str2; this.k = str3; this.l = str4; this.signature = str5; this.m = oAuthListener; } public final boolean a() { Log.i("MicroMsg.SDK.GetQRCodeTask", "cancelTask"); f fVar = this.n; return fVar == null ? cancel(true) : fVar.cancel(true); } /* Return type fixed from 'java.lang.Object' to match base method */ /* JADX DEBUG: Method arguments types fixed to match base method, original types: [java.lang.Object[]] */ /* access modifiers changed from: protected */ @Override // android.os.AsyncTask public final /* synthetic */ a doInBackground(Void[] voidArr) { Log.i("MicroMsg.SDK.GetQRCodeTask", "external storage available = " + h); String format = String.format(j, this.appId, this.k, this.l, this.scope, this.signature); long currentTimeMillis = System.currentTimeMillis(); byte[] a2 = e.a(format, -1); Log.d("MicroMsg.SDK.GetQRCodeTask", String.format("doInBackground, url = %s, time consumed = %d(ms)", format, Long.valueOf(System.currentTimeMillis() - currentTimeMillis))); return a.a(a2); } /* JADX DEBUG: Method arguments types fixed to match base method, original types: [java.lang.Object] */ /* access modifiers changed from: protected */ @Override // android.os.AsyncTask public final /* synthetic */ void onPostExecute(a aVar) { a aVar2 = aVar; if (aVar2.o == OAuthErrCode.WechatAuth_Err_OK) { Log.d("MicroMsg.SDK.GetQRCodeTask", "onPostExecute, get qrcode success"); this.m.onAuthGotQrcode(aVar2.r, aVar2.u); this.n = new f(aVar2.p, this.m); f fVar = this.n; if (Build.VERSION.SDK_INT >= 11) { fVar.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Void[0]); } else { fVar.execute(new Void[0]); } } else { Log.e("MicroMsg.SDK.GetQRCodeTask", String.format("onPostExecute, get qrcode fail, OAuthErrCode = %s", aVar2.o)); this.m.onAuthFinish(aVar2.o, null); } } }