mobile/android/base/SessionParser.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

     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 ***** */
    10 package org.mozilla.gecko;
    12 import java.util.LinkedList;
    14 import org.json.JSONArray;
    15 import org.json.JSONException;
    16 import org.json.JSONObject;
    18 import android.util.Log;
    20 public abstract class SessionParser {
    21     private static final String LOGTAG = "GeckoSessionParser";
    23     public class SessionTab {
    24         final private String mTitle;
    25         final private String mUrl;
    26         final private JSONObject mTabObject;
    27         private boolean mIsSelected;
    29         private SessionTab(String title, String url, boolean isSelected, JSONObject tabObject) {
    30             mTitle = title;
    31             mUrl = url;
    32             mIsSelected = isSelected;
    33             mTabObject = tabObject;
    34         }
    36         public String getTitle() {
    37             return mTitle;
    38         }
    40         public String getUrl() {
    41             return mUrl;
    42         }
    44         public boolean isSelected() {
    45             return mIsSelected;
    46         }
    48         public JSONObject getTabObject() {
    49             return mTabObject;
    50         }
    51     };
    53     abstract public void onTabRead(SessionTab tab);
    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);
    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");
    71                     String title = entry.optString("title");
    72                     if (title.length() == 0) {
    73                         title = url;
    74                     }
    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         }
    90         // If no selected index was found, select the first tab.
    91         if (selectedIndex == -1 && sessionTabs.size() > 0) {
    92             sessionTabs.getFirst().mIsSelected = true;
    93         }
    95         for (SessionTab tab : sessionTabs) {
    96             onTabRead(tab);
    97         }
    98     }
    99 }

mercurial