Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
michael@0 | 2 | * ***** BEGIN LICENSE BLOCK ***** |
michael@0 | 3 | * |
michael@0 | 4 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 6 | * You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 7 | * |
michael@0 | 8 | * ***** END LICENSE BLOCK ***** */ |
michael@0 | 9 | |
michael@0 | 10 | package org.mozilla.gecko; |
michael@0 | 11 | |
michael@0 | 12 | import java.util.LinkedList; |
michael@0 | 13 | |
michael@0 | 14 | import org.json.JSONArray; |
michael@0 | 15 | import org.json.JSONException; |
michael@0 | 16 | import org.json.JSONObject; |
michael@0 | 17 | |
michael@0 | 18 | import android.util.Log; |
michael@0 | 19 | |
michael@0 | 20 | public abstract class SessionParser { |
michael@0 | 21 | private static final String LOGTAG = "GeckoSessionParser"; |
michael@0 | 22 | |
michael@0 | 23 | public class SessionTab { |
michael@0 | 24 | final private String mTitle; |
michael@0 | 25 | final private String mUrl; |
michael@0 | 26 | final private JSONObject mTabObject; |
michael@0 | 27 | private boolean mIsSelected; |
michael@0 | 28 | |
michael@0 | 29 | private SessionTab(String title, String url, boolean isSelected, JSONObject tabObject) { |
michael@0 | 30 | mTitle = title; |
michael@0 | 31 | mUrl = url; |
michael@0 | 32 | mIsSelected = isSelected; |
michael@0 | 33 | mTabObject = tabObject; |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | public String getTitle() { |
michael@0 | 37 | return mTitle; |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | public String getUrl() { |
michael@0 | 41 | return mUrl; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | public boolean isSelected() { |
michael@0 | 45 | return mIsSelected; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | public JSONObject getTabObject() { |
michael@0 | 49 | return mTabObject; |
michael@0 | 50 | } |
michael@0 | 51 | }; |
michael@0 | 52 | |
michael@0 | 53 | abstract public void onTabRead(SessionTab tab); |
michael@0 | 54 | |
michael@0 | 55 | public void parse(String... sessionStrings) { |
michael@0 | 56 | final LinkedList<SessionTab> sessionTabs = new LinkedList<SessionTab>(); |
michael@0 | 57 | int totalCount = 0; |
michael@0 | 58 | int selectedIndex = -1; |
michael@0 | 59 | try { |
michael@0 | 60 | for (String sessionString : sessionStrings) { |
michael@0 | 61 | final JSONObject window = new JSONObject(sessionString).getJSONArray("windows").getJSONObject(0); |
michael@0 | 62 | final JSONArray tabs = window.getJSONArray("tabs"); |
michael@0 | 63 | final int optSelected = window.optInt("selected", -1); |
michael@0 | 64 | |
michael@0 | 65 | for (int i = 0; i < tabs.length(); i++) { |
michael@0 | 66 | final JSONObject tab = tabs.getJSONObject(i); |
michael@0 | 67 | final int index = tab.getInt("index"); |
michael@0 | 68 | final JSONObject entry = tab.getJSONArray("entries").getJSONObject(index - 1); |
michael@0 | 69 | final String url = entry.getString("url"); |
michael@0 | 70 | |
michael@0 | 71 | String title = entry.optString("title"); |
michael@0 | 72 | if (title.length() == 0) { |
michael@0 | 73 | title = url; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | totalCount++; |
michael@0 | 77 | boolean selected = false; |
michael@0 | 78 | if (optSelected == i+1) { |
michael@0 | 79 | selected = true; |
michael@0 | 80 | selectedIndex = totalCount; |
michael@0 | 81 | } |
michael@0 | 82 | sessionTabs.add(new SessionTab(title, url, selected, tab)); |
michael@0 | 83 | } |
michael@0 | 84 | } |
michael@0 | 85 | } catch (JSONException e) { |
michael@0 | 86 | Log.e(LOGTAG, "JSON error", e); |
michael@0 | 87 | return; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | // If no selected index was found, select the first tab. |
michael@0 | 91 | if (selectedIndex == -1 && sessionTabs.size() > 0) { |
michael@0 | 92 | sessionTabs.getFirst().mIsSelected = true; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | for (SessionTab tab : sessionTabs) { |
michael@0 | 96 | onTabRead(tab); |
michael@0 | 97 | } |
michael@0 | 98 | } |
michael@0 | 99 | } |