package com.parse;

import android.content.Context;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import com.parse.ParseObject;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import p137.C2742;
import p137.InterfaceC2741;

/* JADX INFO: loaded from: classes.dex */
@ParseClassName("_Installation")
public class ParseInstallation extends ParseObject {
    public static final List<String> READ_ONLY_FIELDS = Collections.unmodifiableList(Arrays.asList("deviceType", "installationId", "deviceToken", "pushType", "timeZone", "localeIdentifier", "appVersion", "appName", "parseVersion", "appIdentifier", "objectId"));

    public ParseInstallation() {
        super("_Automatic");
    }

    public static ParseCurrentInstallationController getCurrentInstallationController() {
        ParseCorePlugins parseCorePlugins = ParseCorePlugins.INSTANCE;
        if (parseCorePlugins.currentInstallationController.get() == null) {
            ParseObjectStore fileObjectStore = new FileObjectStore(ParseInstallation.class, new File(ParsePlugins.get().getParseDir(), "currentInstallation"), ParseObjectCurrentCoder.INSTANCE);
            if (Parse.isLocalDatastoreEnabled) {
                fileObjectStore = new OfflineObjectStore(ParseInstallation.class, "_currentInstallation", fileObjectStore);
            }
            parseCorePlugins.currentInstallationController.compareAndSet(null, new CachedCurrentInstallationController(fileObjectStore, ParsePlugins.get().installationId()));
        }
        return parseCorePlugins.currentInstallationController.get();
    }

    @Override // com.parse.ParseObject
    public C2742<Void> handleSaveResultAsync(ParseObject.State state, ParseOperationSet parseOperationSet) {
        C2742<Void> c2742HandleSaveResultAsync = super.handleSaveResultAsync(state, parseOperationSet);
        return state == null ? c2742HandleSaveResultAsync : c2742HandleSaveResultAsync.m4817(new InterfaceC2741<Void, C2742<Void>>() { // from class: com.parse.ParseInstallation.3
            @Override // p137.InterfaceC2741
            public C2742<Void> then(C2742<Void> c2742) {
                return ParseInstallation.getCurrentInstallationController().setAsync(ParseInstallation.this);
            }
        }, C2742.f8455);
    }

    @Override // com.parse.ParseObject
    public boolean isKeyMutable(String str) {
        return !READ_ONLY_FIELDS.contains(str);
    }

    @Override // com.parse.ParseObject
    public boolean needsDefaultACL() {
        return false;
    }

    @Override // com.parse.ParseObject
    public C2742<Void> saveAsync(final String str, final C2742<Void> c2742) {
        return super.saveAsync(str, c2742).m4815(new InterfaceC2741<Void, C2742<Void>>() { // from class: com.parse.ParseInstallation.2
            @Override // p137.InterfaceC2741
            public C2742<Void> then(C2742<Void> c27422) {
                int i;
                C2742<Void> c2742SaveAsync;
                if (c27422.m4806() == null || !(c27422.m4806() instanceof ParseException) || ((i = ((ParseException) c27422.m4806()).code) != 101 && (i != 135 || ParseInstallation.this.getObjectId() != null))) {
                    return c27422;
                }
                synchronized (ParseInstallation.this.mutex) {
                    ParseInstallation parseInstallation = ParseInstallation.this;
                    ParseObject.State.Builder builderObjectId = new ParseObject.State.Builder(ParseInstallation.this.getState()).objectId(null);
                    if (builderObjectId == null) {
                        throw null;
                    }
                    parseInstallation.setState(new ParseObject.State(builderObjectId));
                    ParseInstallation.this.markAllFieldsDirty();
                    c2742SaveAsync = ParseInstallation.super.saveAsync(str, c2742);
                }
                return c2742SaveAsync;
            }
        }, C2742.f8455, null);
    }

    @Override // com.parse.ParseObject
    public void updateBeforeSave() {
        if (getCurrentInstallationController().isCurrent(this)) {
            String id = TimeZone.getDefault().getID();
            if ((id.indexOf(47) > 0 || id.equals("GMT")) && !id.equals(get("timeZone"))) {
                performPut("timeZone", id);
            }
            updateVersionInfo();
            updateDeviceInfo(ParsePlugins.get().installationId());
            Locale locale = Locale.getDefault();
            String language = locale.getLanguage();
            String country = locale.getCountry();
            if (TextUtils.isEmpty(language)) {
                return;
            }
            if (language.equals("iw")) {
                language = "he";
            }
            if (language.equals("in")) {
                language = "id";
            }
            if (language.equals("ji")) {
                language = "yi";
            }
            if (!TextUtils.isEmpty(country)) {
                language = String.format(Locale.US, "%s-%s", language, country);
            }
            if (language.equals(get("localeIdentifier"))) {
                return;
            }
            performPut("localeIdentifier", language);
        }
    }

    public void updateDeviceInfo(InstallationId installationId) {
        if (!containsKey("installationId")) {
            performPut("installationId", installationId.get());
        }
        if ("android".equals(get("deviceType"))) {
            return;
        }
        performPut("deviceType", "android");
    }

    public final void updateVersionInfo() {
        String str;
        synchronized (this.mutex) {
            try {
                Context applicationContext = Parse.getApplicationContext();
                String packageName = applicationContext.getPackageName();
                PackageManager packageManager = applicationContext.getPackageManager();
                str = packageManager.getPackageInfo(packageName, 0).versionName;
                String string = packageManager.getApplicationLabel(packageManager.getApplicationInfo(packageName, 0)).toString();
                if (packageName != null && !packageName.equals(get("appIdentifier"))) {
                    performPut("appIdentifier", packageName);
                }
                if (string != null && !string.equals(get("appName"))) {
                    performPut("appName", string);
                }
            } catch (PackageManager.NameNotFoundException unused) {
                PLog.w("com.parse.ParseInstallation", "Cannot load package info; will not be saved to installation");
            }
            if (str != null && !str.equals(get("appVersion"))) {
                performPut("appVersion", str);
            }
        }
    }
}
