package com.ifpdos.multiuser;

import a.a.a.a.e.e;
import android.app.Service;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.IBinder;
import android.util.Log;
import e.s.c.i;
import e.w.h;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/* JADX INFO: loaded from: classes.dex */
public final class MultiUserService extends Service {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final ConcurrentHashMap<String, ByteArrayOutputStream> f5486b = new ConcurrentHashMap<>();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final e.a f5487c = new a();

    public static final class a extends e.a {
        public a() {
        }

        @Override // a.a.a.a.e.e
        public boolean k(byte[] bArr, String str) {
            ByteArrayOutputStream byteArrayOutputStreamPutIfAbsent;
            i.e(bArr, "chunk");
            i.e(str, "path");
            try {
                Log.d("MultiUserService", i.j("processBitmapChunk: ", str));
                ConcurrentHashMap<String, ByteArrayOutputStream> concurrentHashMap = MultiUserService.this.f5486b;
                ByteArrayOutputStream byteArrayOutputStream = concurrentHashMap.get(str);
                if (byteArrayOutputStream == null && (byteArrayOutputStreamPutIfAbsent = concurrentHashMap.putIfAbsent(str, (byteArrayOutputStream = new ByteArrayOutputStream()))) != null) {
                    byteArrayOutputStream = byteArrayOutputStreamPutIfAbsent;
                }
                ByteArrayOutputStream byteArrayOutputStream2 = byteArrayOutputStream;
                i.d(byteArrayOutputStream2, "outputStream");
                synchronized (byteArrayOutputStream2) {
                    byteArrayOutputStream2.write(bArr);
                }
                return true;
            } catch (Exception e2) {
                Log.e("MultiUserService", "processBitmapChunk: error", e2);
                MultiUserService.this.f5486b.remove(str);
                return false;
            }
        }

        @Override // a.a.a.a.e.e
        public boolean v(String str) {
            Bitmap bitmapDecodeByteArray;
            i.e(str, "path");
            ByteArrayOutputStream byteArrayOutputStreamRemove = MultiUserService.this.f5486b.remove(str);
            if (byteArrayOutputStreamRemove == null) {
                return false;
            }
            byte[] byteArray = byteArrayOutputStreamRemove.toByteArray();
            try {
                bitmapDecodeByteArray = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
            } catch (Exception e2) {
                Log.e("MultiUserService", "finalizeBitmap error", e2);
            }
            if (bitmapDecodeByteArray == null) {
                throw new IllegalArgumentException("Invalid bitmap data");
            }
            MultiUserService.a(MultiUserService.this, bitmapDecodeByteArray, str);
            return false;
        }
    }

    public static final boolean a(MultiUserService multiUserService, Bitmap bitmap, String str) {
        FileOutputStream fileOutputStream;
        Objects.requireNonNull(multiUserService);
        Bitmap.CompressFormat compressFormat = h.d(str, ".jpg", false, 2) ? Bitmap.CompressFormat.JPEG : Bitmap.CompressFormat.PNG;
        try {
            try {
                File parentFile = new File(str).getParentFile();
                if (parentFile != null && !parentFile.exists()) {
                    parentFile.mkdirs();
                }
                fileOutputStream = new FileOutputStream(str);
            } catch (Exception e2) {
                Log.e("MultiUserService", "saveBitmapToFile: error", e2);
            }
            try {
                if (bitmap.compress(compressFormat, 100, fileOutputStream)) {
                    fileOutputStream.flush();
                    d.a.u.a.f(fileOutputStream, null);
                    Log.d("MultiUserService", i.j("saveBitmapToFile: finished saving to ", str));
                    bitmap.recycle();
                    return true;
                }
                Log.e("MultiUserService", "Bitmap compress failed");
                fileOutputStream.flush();
                new File(str).delete();
                d.a.u.a.f(fileOutputStream, null);
                return false;
            } catch (Throwable th) {
                try {
                    throw th;
                } catch (Throwable th2) {
                    d.a.u.a.f(fileOutputStream, th);
                    throw th2;
                }
            }
        } finally {
            bitmap.recycle();
        }
    }

    @Override // android.app.Service
    public IBinder onBind(Intent intent) {
        return this.f5487c;
    }

    @Override // android.app.Service
    public int onStartCommand(Intent intent, int i2, int i3) {
        return super.onStartCommand(intent, i2, i3);
    }
}
