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