package com.facebook.internal; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.os.Messenger; import android.os.RemoteException; /* loaded from: classes.dex */ public abstract class PlatformServiceClient implements ServiceConnection { private final Context a; private final Handler b; private CompletedListener c; private boolean d; private Messenger e; private int f; private int g; private final String h; private final int i; public interface CompletedListener { void a(Bundle bundle); } public PlatformServiceClient(Context context, int i, int i2, int i3, String str) { Context applicationContext = context.getApplicationContext(); this.a = applicationContext != null ? applicationContext : context; this.f = i; this.g = i2; this.h = str; this.i = i3; this.b = new Handler() { // from class: com.facebook.internal.PlatformServiceClient.1 @Override // android.os.Handler public void handleMessage(Message message) { PlatformServiceClient.this.a(message); } }; } private void c() { Bundle bundle = new Bundle(); bundle.putString("com.facebook.platform.extra.APPLICATION_ID", this.h); a(bundle); Message obtain = Message.obtain((Handler) null, this.f); obtain.arg1 = this.i; obtain.setData(bundle); obtain.replyTo = new Messenger(this.b); try { this.e.send(obtain); } catch (RemoteException unused) { b(null); } } protected abstract void a(Bundle bundle); public void a(CompletedListener completedListener) { this.c = completedListener; } public boolean b() { Intent a; if (this.d || NativeProtocol.a(this.i) == -1 || (a = NativeProtocol.a(this.a)) == null) { return false; } this.d = true; this.a.bindService(a, this, 1); return true; } @Override // android.content.ServiceConnection public void onServiceConnected(ComponentName componentName, IBinder iBinder) { this.e = new Messenger(iBinder); c(); } @Override // android.content.ServiceConnection public void onServiceDisconnected(ComponentName componentName) { this.e = null; try { this.a.unbindService(this); } catch (IllegalArgumentException unused) { } b(null); } public void a() { this.d = false; } protected void a(Message message) { if (message.what == this.g) { Bundle data = message.getData(); if (data.getString("com.facebook.platform.status.ERROR_TYPE") != null) { b(null); } else { b(data); } try { this.a.unbindService(this); } catch (IllegalArgumentException unused) { } } } private void b(Bundle bundle) { if (this.d) { this.d = false; CompletedListener completedListener = this.c; if (completedListener != null) { completedListener.a(bundle); } } } }