1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/SessionParser.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,99 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * ***** BEGIN LICENSE BLOCK ***** 1.6 + * 1.7 + * This Source Code Form is subject to the terms of the Mozilla Public 1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.9 + * You can obtain one at http://mozilla.org/MPL/2.0/. 1.10 + * 1.11 + * ***** END LICENSE BLOCK ***** */ 1.12 + 1.13 +package org.mozilla.gecko; 1.14 + 1.15 +import java.util.LinkedList; 1.16 + 1.17 +import org.json.JSONArray; 1.18 +import org.json.JSONException; 1.19 +import org.json.JSONObject; 1.20 + 1.21 +import android.util.Log; 1.22 + 1.23 +public abstract class SessionParser { 1.24 + private static final String LOGTAG = "GeckoSessionParser"; 1.25 + 1.26 + public class SessionTab { 1.27 + final private String mTitle; 1.28 + final private String mUrl; 1.29 + final private JSONObject mTabObject; 1.30 + private boolean mIsSelected; 1.31 + 1.32 + private SessionTab(String title, String url, boolean isSelected, JSONObject tabObject) { 1.33 + mTitle = title; 1.34 + mUrl = url; 1.35 + mIsSelected = isSelected; 1.36 + mTabObject = tabObject; 1.37 + } 1.38 + 1.39 + public String getTitle() { 1.40 + return mTitle; 1.41 + } 1.42 + 1.43 + public String getUrl() { 1.44 + return mUrl; 1.45 + } 1.46 + 1.47 + public boolean isSelected() { 1.48 + return mIsSelected; 1.49 + } 1.50 + 1.51 + public JSONObject getTabObject() { 1.52 + return mTabObject; 1.53 + } 1.54 + }; 1.55 + 1.56 + abstract public void onTabRead(SessionTab tab); 1.57 + 1.58 + public void parse(String... sessionStrings) { 1.59 + final LinkedList<SessionTab> sessionTabs = new LinkedList<SessionTab>(); 1.60 + int totalCount = 0; 1.61 + int selectedIndex = -1; 1.62 + try { 1.63 + for (String sessionString : sessionStrings) { 1.64 + final JSONObject window = new JSONObject(sessionString).getJSONArray("windows").getJSONObject(0); 1.65 + final JSONArray tabs = window.getJSONArray("tabs"); 1.66 + final int optSelected = window.optInt("selected", -1); 1.67 + 1.68 + for (int i = 0; i < tabs.length(); i++) { 1.69 + final JSONObject tab = tabs.getJSONObject(i); 1.70 + final int index = tab.getInt("index"); 1.71 + final JSONObject entry = tab.getJSONArray("entries").getJSONObject(index - 1); 1.72 + final String url = entry.getString("url"); 1.73 + 1.74 + String title = entry.optString("title"); 1.75 + if (title.length() == 0) { 1.76 + title = url; 1.77 + } 1.78 + 1.79 + totalCount++; 1.80 + boolean selected = false; 1.81 + if (optSelected == i+1) { 1.82 + selected = true; 1.83 + selectedIndex = totalCount; 1.84 + } 1.85 + sessionTabs.add(new SessionTab(title, url, selected, tab)); 1.86 + } 1.87 + } 1.88 + } catch (JSONException e) { 1.89 + Log.e(LOGTAG, "JSON error", e); 1.90 + return; 1.91 + } 1.92 + 1.93 + // If no selected index was found, select the first tab. 1.94 + if (selectedIndex == -1 && sessionTabs.size() > 0) { 1.95 + sessionTabs.getFirst().mIsSelected = true; 1.96 + } 1.97 + 1.98 + for (SessionTab tab : sessionTabs) { 1.99 + onTabRead(tab); 1.100 + } 1.101 + } 1.102 +}