package com.battlelancer.seriesguide.extensions;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.text.TextUtils;
import java.util.List;
import timber.log.Timber;

/**
 * Broadcast receiver watching for changes to installed packages on the device. Removes uninstalled
 * extensions or clears caches triggering a data refresh if an extension was updated.
 *
 * <p> Adapted from <a href="https://github.com/romannurik/muzei">muzei's</a>
 * SourcePackageChangeReceiver.
 */
public class ExtensionPackageChangeReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null || intent.getData() == null) {
            return;
        }
        String action = intent.getAction();
        if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)
                && !Intent.ACTION_PACKAGE_REPLACED.equals(action)
                && !Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
            return; // ignore the intent
        }

        String changedPackage = intent.getData().getSchemeSpecificPart();
        ExtensionManager extensionManager = ExtensionManager.get();
        List<ComponentName> enabledExtensions = extensionManager.getEnabledExtensions(context);
        int affectedExtensionIndex = -1;
        for (int i = 0; i < enabledExtensions.size(); i++) {
            ComponentName componentName = enabledExtensions.get(i);
            if (TextUtils.equals(changedPackage, componentName.getPackageName())) {
                affectedExtensionIndex = i;
                break;
            }
        }
        if (affectedExtensionIndex == -1) {
            return;
        }

        // temporarily unsubscribe from extension
        ComponentName changedExtension = enabledExtensions.remove(affectedExtensionIndex);
        extensionManager.setEnabledExtensions(context, enabledExtensions);

        try {
            context.getPackageManager().getServiceInfo(changedExtension, 0);
        } catch (PackageManager.NameNotFoundException e) {
            Timber.i("Extension %s no longer available: removed",
                    changedExtension.toShortString());
            return;
        }

        // changed or updated
        Timber.i("Extension %s changed or replaced: re-subscribing",
                changedExtension.toShortString());
        enabledExtensions.add(affectedExtensionIndex, changedExtension);
        extensionManager.setEnabledExtensions(context, enabledExtensions);
    }
}

@@ -879,295 +879,8 @@
    %7D
-%0A        String action = intent.getAction();%0A        if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)%0A                && !Intent.ACTION_PACKAGE_REPLACED.equals(action)%0A                && !Intent.ACTION_PACKAGE_REMOVED.equals(action)) %7B%0A            return; // ignore the intent%0A        %7D
 %0A%0A  

Drop ExtensionPackageChangeReceiver action check: registered at runtime.