package com.studio4plus.homerplayer;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.provider.MediaStore;

import androidx.core.content.pm.PackageInfoCompat;
import androidx.multidex.MultiDexApplication;

import com.studio4plus.homerplayer.analytics.AnalyticsTracker;
import com.studio4plus.homerplayer.crashreporting.CrashReporting;
import com.studio4plus.homerplayer.ui.HomeActivity;
import com.studio4plus.homerplayer.service.NotificationUtil;

import javax.inject.Inject;

public class HomerPlayerApplication extends MultiDexApplication {

    private static final String AUDIOBOOKS_DIRECTORY = "AudioBooks";

    private ApplicationComponent component;


    @Inject public MediaStoreUpdateObserver mediaStoreUpdateObserver;
    @Inject public GlobalSettings globalSettings;
    @Inject public AnalyticsTracker analyticsTracker;  // Force creation of the tracker early.

    @Override
    public void onCreate() {
        super.onCreate();

        CrashReporting.init(this);

        component = DaggerApplicationComponent.builder()
                .applicationModule(new ApplicationModule(this))
                .audioBookManagerModule(new AudioBookManagerModule(AUDIOBOOKS_DIRECTORY))
                .build();
        component.inject(this);

        long currentVersionCode = getVersionCode();
        if (isUpdate(globalSettings.lastVersionCode(), currentVersionCode)) {
            onUpdate(globalSettings.lastVersionCode(), currentVersionCode);
        }

        getContentResolver().registerContentObserver(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, true, mediaStoreUpdateObserver);

        HomeActivity.setEnabled(this, globalSettings.isAnyKioskModeEnabled());

        if (Build.VERSION.SDK_INT >= 26)
            NotificationUtil.API26.registerPlaybackServiceChannel(this);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        getContentResolver().unregisterContentObserver(mediaStoreUpdateObserver);
        mediaStoreUpdateObserver = null;
    }

    public static ApplicationComponent getComponent(Context context) {
        return ((HomerPlayerApplication) context.getApplicationContext()).component;
    }

    private boolean isUpdate(long previousVersionCode, long currentVersionCode) {
        if (previousVersionCode == 0) {
            // previousVersionCode is 0 for versions up to 55 so it can't be used to distinguish
            // between an update and a new installation. browsingHintShown is a good approximation.
            return globalSettings.browsingHintShown();
        } else {
            return previousVersionCode < currentVersionCode;
        }
    }

    private void onUpdate(long previousVersionCode, long currentVersionCode) {
        if (previousVersionCode < 56) {
            globalSettings.setVolumeControlsEnabled(false);
        }
        globalSettings.setLastVersionCode(currentVersionCode);
    }

    private long getVersionCode() {
        try {
            PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), 0);
            return PackageInfoCompat.getLongVersionCode(info);
        } catch (PackageManager.NameNotFoundException e) {
            // Should never happen.
            CrashReporting.logException(e);
            return 0L;
        }
    }
}

@@ -173,35 +173,8 @@
 ld;%0A
-import android.os.Handler;%0A
 impo
@@ -205,16 +205,16 @@
 aStore;%0A
+
 %0Aimport 
@@ -1415,21 +1415,35 @@
     
-if (isUpdate(
+long previousVersionCode = 
 glob
@@ -1462,32 +1462,74 @@
 astVersionCode()
+;%0A        if (isUpdate(previousVersionCode
 , currentVersion
@@ -1558,16 +1558,186 @@
 nUpdate(
+previousVersionCode);%0A        %7D%0A        if (previousVersionCode %3C currentVersionCode) %7B%0A            // This is true both for fresh installations and updates.%0A            
 globalSe
@@ -1735,33 +1735,36 @@
  globalSettings.
-l
+setL
 astVersionCode()
@@ -1762,19 +1762,16 @@
 ionCode(
-), 
 currentV
@@ -3035,33 +3035,8 @@
 Code
-, long currentVersionCode
 ) %7B%0A
@@ -3131,24 +3131,24 @@
 led(false);%0A
+
         %7D%0A  
@@ -3149,71 +3149,8 @@
   %7D%0A
-        globalSettings.setLastVersionCode(currentVersionCode);%0A
     

Fix volume button disappearing on second start.