package org.wikipedia.json;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.google.gson.reflect.TypeToken;

import org.wikipedia.crash.RemoteLogException;
import org.wikipedia.page.tabs.Tab;
import org.wikipedia.util.log.L;

import java.util.Collections;
import java.util.List;

public final class TabUnmarshaller {
    private static final TypeToken<List<Tab>> TYPE_TOKEN = new TypeToken<List<Tab>>() { };

    @NonNull public static List<Tab> unmarshal(@Nullable String json) {
        List<Tab> object = null;
        try {
            object = GsonUnmarshaller.unmarshal(TYPE_TOKEN, json);
        } catch (Exception e) {
            // Catch all. Any Exception can be thrown when unmarshalling.
            L.logRemoteErrorIfProd(new RemoteLogException(e).put("json", json));
        }
        if (object == null) {
            object = Collections.emptyList();
        }
        return object;
    }

    private TabUnmarshaller() { }
}

@@ -108,16 +108,47 @@
 ullable;
+%0Aimport android.text.TextUtils;
 %0A%0Aimport
@@ -231,16 +231,61 @@
 eption;%0A
+import org.wikipedia.page.PageBackStackItem;%0A
 import o
@@ -988,16 +988,16 @@
 List();%0A
-
         
@@ -990,32 +990,1050 @@
 st();%0A        %7D%0A
+%0A        // T152980%0A        // When upgrading from much older versions (namely, 2.1.141 or earlier), the serialized%0A        // tab list may be in a format that causes WikiSite objects to have null Uri components.%0A        // If we encounter one of these occurrences, just clear out the entire tab list.%0A        // to be on the safe side.%0A        // TODO: Remove when the incidence of this is sufficiently diminished (April 2017?)%0A        boolean bad = false;%0A        for (Tab tab : object) %7B%0A            for (PageBackStackItem item : tab.getBackStack()) %7B%0A                if (TextUtils.isEmpty(item.getTitle().getWikiSite().authority())%0A                        %7C%7C TextUtils.isEmpty(item.getHistoryEntry().getTitle().getWikiSite().authority())) %7B%0A                    L.logRemoteErrorIfProd(new IllegalArgumentException(%22Format error in serialized tab list.%22));%0A                    bad = true;%0A                    break;%0A                %7D%0A            %7D%0A        %7D%0A        if (bad) %7B%0A            object.clear();%0A        %7D%0A%0A
         return o

Fix possible crash when upgrading from older app versions.