michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * ***** BEGIN LICENSE BLOCK ***** michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: * michael@0: * ***** END LICENSE BLOCK ***** */ michael@0: michael@0: package org.mozilla.gecko; michael@0: michael@0: import java.util.LinkedList; michael@0: michael@0: import org.json.JSONArray; michael@0: import org.json.JSONException; michael@0: import org.json.JSONObject; michael@0: michael@0: import android.util.Log; michael@0: michael@0: public abstract class SessionParser { michael@0: private static final String LOGTAG = "GeckoSessionParser"; michael@0: michael@0: public class SessionTab { michael@0: final private String mTitle; michael@0: final private String mUrl; michael@0: final private JSONObject mTabObject; michael@0: private boolean mIsSelected; michael@0: michael@0: private SessionTab(String title, String url, boolean isSelected, JSONObject tabObject) { michael@0: mTitle = title; michael@0: mUrl = url; michael@0: mIsSelected = isSelected; michael@0: mTabObject = tabObject; michael@0: } michael@0: michael@0: public String getTitle() { michael@0: return mTitle; michael@0: } michael@0: michael@0: public String getUrl() { michael@0: return mUrl; michael@0: } michael@0: michael@0: public boolean isSelected() { michael@0: return mIsSelected; michael@0: } michael@0: michael@0: public JSONObject getTabObject() { michael@0: return mTabObject; michael@0: } michael@0: }; michael@0: michael@0: abstract public void onTabRead(SessionTab tab); michael@0: michael@0: public void parse(String... sessionStrings) { michael@0: final LinkedList sessionTabs = new LinkedList(); michael@0: int totalCount = 0; michael@0: int selectedIndex = -1; michael@0: try { michael@0: for (String sessionString : sessionStrings) { michael@0: final JSONObject window = new JSONObject(sessionString).getJSONArray("windows").getJSONObject(0); michael@0: final JSONArray tabs = window.getJSONArray("tabs"); michael@0: final int optSelected = window.optInt("selected", -1); michael@0: michael@0: for (int i = 0; i < tabs.length(); i++) { michael@0: final JSONObject tab = tabs.getJSONObject(i); michael@0: final int index = tab.getInt("index"); michael@0: final JSONObject entry = tab.getJSONArray("entries").getJSONObject(index - 1); michael@0: final String url = entry.getString("url"); michael@0: michael@0: String title = entry.optString("title"); michael@0: if (title.length() == 0) { michael@0: title = url; michael@0: } michael@0: michael@0: totalCount++; michael@0: boolean selected = false; michael@0: if (optSelected == i+1) { michael@0: selected = true; michael@0: selectedIndex = totalCount; michael@0: } michael@0: sessionTabs.add(new SessionTab(title, url, selected, tab)); michael@0: } michael@0: } michael@0: } catch (JSONException e) { michael@0: Log.e(LOGTAG, "JSON error", e); michael@0: return; michael@0: } michael@0: michael@0: // If no selected index was found, select the first tab. michael@0: if (selectedIndex == -1 && sessionTabs.size() > 0) { michael@0: sessionTabs.getFirst().mIsSelected = true; michael@0: } michael@0: michael@0: for (SessionTab tab : sessionTabs) { michael@0: onTabRead(tab); michael@0: } michael@0: } michael@0: }