mobile/android/base/health/SessionInformation.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  * This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 package org.mozilla.gecko.health;
     8 import android.content.SharedPreferences;
     9 import android.util.Log;
    11 import org.mozilla.gecko.GeckoApp;
    13 import org.json.JSONException;
    14 import org.json.JSONObject;
    16 public class SessionInformation {
    17     private static final String LOG_TAG = "GeckoSessInfo";
    19     public static final String PREFS_SESSION_START = "sessionStart";
    21     public final long wallStartTime;    // System wall clock.
    22     public final long realStartTime;    // Realtime clock.
    24     private final boolean wasOOM;
    25     private final boolean wasStopped;
    27     private volatile long timedGeckoStartup = -1;
    28     private volatile long timedJavaStartup = -1;
    30     // Current sessions don't (right now) care about wasOOM/wasStopped.
    31     // Eventually we might want to lift that logic out of GeckoApp.
    32     public SessionInformation(long wallTime, long realTime) {
    33         this(wallTime, realTime, false, false);
    34     }
    36     // Previous sessions do...
    37     public SessionInformation(long wallTime, long realTime, boolean wasOOM, boolean wasStopped) {
    38         this.wallStartTime = wallTime;
    39         this.realStartTime = realTime;
    40         this.wasOOM = wasOOM;
    41         this.wasStopped = wasStopped;
    42     }
    44     /**
    45      * Initialize a new SessionInformation instance from the supplied prefs object.
    46      *
    47      * This includes retrieving OOM/crash data, as well as timings.
    48      *
    49      * If no wallStartTime was found, that implies that the previous
    50      * session was correctly recorded, and an object with a zero
    51      * wallStartTime is returned.
    52      */
    53     public static SessionInformation fromSharedPrefs(SharedPreferences prefs) {
    54         boolean wasOOM = prefs.getBoolean(GeckoApp.PREFS_OOM_EXCEPTION, false);
    55         boolean wasStopped = prefs.getBoolean(GeckoApp.PREFS_WAS_STOPPED, true);
    56         long wallStartTime = prefs.getLong(PREFS_SESSION_START, 0L);
    57         long realStartTime = 0L;
    58         Log.d(LOG_TAG, "Building SessionInformation from prefs: " +
    59                        wallStartTime + ", " + realStartTime + ", " +
    60                        wasStopped + ", " + wasOOM);
    61         return new SessionInformation(wallStartTime, realStartTime, wasOOM, wasStopped);
    62     }
    64     /**
    65      * Initialize a new SessionInformation instance to 'split' the current
    66      * session.
    67      */
    68     public static SessionInformation forRuntimeTransition() {
    69         final boolean wasOOM = false;
    70         final boolean wasStopped = true;
    71         final long wallStartTime = System.currentTimeMillis();
    72         final long realStartTime = android.os.SystemClock.elapsedRealtime();
    73         Log.v(LOG_TAG, "Recording runtime session transition: " +
    74                        wallStartTime + ", " + realStartTime);
    75         return new SessionInformation(wallStartTime, realStartTime, wasOOM, wasStopped);
    76     }
    78     public boolean wasKilled() {
    79         return wasOOM || !wasStopped;
    80     }
    82     /**
    83      * Record the beginning of this session to SharedPreferences by
    84      * recording our start time. If a session was already recorded, it is
    85      * overwritten (there can only be one running session at a time). Does
    86      * not commit the editor.
    87      */
    88     public void recordBegin(SharedPreferences.Editor editor) {
    89         Log.d(LOG_TAG, "Recording start of session: " + this.wallStartTime);
    90         editor.putLong(PREFS_SESSION_START, this.wallStartTime);
    91     }
    93     /**
    94      * Record the completion of this session to SharedPreferences by
    95      * deleting our start time. Does not commit the editor.
    96      */
    97     public void recordCompletion(SharedPreferences.Editor editor) {
    98         Log.d(LOG_TAG, "Recording session done: " + this.wallStartTime);
    99         editor.remove(PREFS_SESSION_START);
   100     }
   102     /**
   103      * Return the JSON that we'll put in the DB for this session.
   104      */
   105     public JSONObject getCompletionJSON(String reason, long realEndTime) throws JSONException {
   106         long durationSecs = (realEndTime - this.realStartTime) / 1000;
   107         JSONObject out = new JSONObject();
   108         out.put("r", reason);
   109         out.put("d", durationSecs);
   110         if (this.timedGeckoStartup > 0) {
   111             out.put("sg", this.timedGeckoStartup);
   112         }
   113         if (this.timedJavaStartup > 0) {
   114             out.put("sj", this.timedJavaStartup);
   115         }
   116         return out;
   117     }
   119     public JSONObject getCrashedJSON() throws JSONException {
   120         JSONObject out = new JSONObject();
   121         // We use ints here instead of booleans, because we're packing
   122         // stuff into JSON, and saving bytes in the DB is a worthwhile
   123         // goal.
   124         out.put("oom", this.wasOOM ? 1 : 0);
   125         out.put("stopped", this.wasStopped ? 1 : 0);
   126         out.put("r", "A");
   127         return out;
   128     }
   130     public void setTimedGeckoStartup(final long duration) {
   131         timedGeckoStartup = duration;
   132     }
   134     public void setTimedJavaStartup(final long duration) {
   135         timedJavaStartup = duration;
   136     }
   137 }

mercurial