@Override
    @SuppressWarnings("unchecked")
    public Void doInBackground(Object... args) {
      List<HistoricalRecord> historicalRecords = (List<HistoricalRecord>) args[0];
      String historyFileName = (String) args[1];

      FileOutputStream fos = null;

      try {
        // Mozilla - Update the location we save files to
        GeckoProfile profile = GeckoProfile.get(mContext);
        File file = profile.getFile(historyFileName);
        fos = new FileOutputStream(file);
      } catch (FileNotFoundException fnfe) {
        Log.e(LOG_TAG, "Error writing historical record file: " + historyFileName, fnfe);
        return null;
      }

      XmlSerializer serializer = Xml.newSerializer();

      try {
        serializer.setOutput(fos, null);
        serializer.startDocument("UTF-8", true);
        serializer.startTag(null, TAG_HISTORICAL_RECORDS);

        final int recordCount = historicalRecords.size();
        for (int i = 0; i < recordCount; i++) {
          HistoricalRecord record = historicalRecords.remove(0);
          serializer.startTag(null, TAG_HISTORICAL_RECORD);
          serializer.attribute(null, ATTRIBUTE_ACTIVITY, record.activity.flattenToString());
          serializer.attribute(null, ATTRIBUTE_TIME, String.valueOf(record.time));
          serializer.attribute(null, ATTRIBUTE_WEIGHT, String.valueOf(record.weight));
          serializer.endTag(null, TAG_HISTORICAL_RECORD);
          if (DEBUG) {
            Log.i(LOG_TAG, "Wrote " + record.toString());
          }
        }

        serializer.endTag(null, TAG_HISTORICAL_RECORDS);
        serializer.endDocument();

        if (DEBUG) {
          Log.i(LOG_TAG, "Wrote " + recordCount + " historical records.");
        }
      } catch (IllegalArgumentException | IOException | IllegalStateException e) {
        Log.e(LOG_TAG, "Error writing historical record file: " + mHistoryFileName, e);
      } finally {
        mCanReadHistoricalData = true;
        if (fos != null) {
          try {
            fos.close();
          } catch (IOException e) {
            /* ignore */
          }
        }
      }
      return null;
    }
  /** Command for reading the historical records from a file off the UI thread. */
  private void readHistoricalDataImpl() {
    try {
      GeckoProfile profile = GeckoProfile.get(mContext);
      File f = profile.getFile(mHistoryFileName);
      if (!f.exists()) {
        // Fall back to the non-profile aware file if it exists...
        File oldFile = new File(mHistoryFileName);
        oldFile.renameTo(f);
      }
      readHistoricalDataFromStream(new FileInputStream(f));
    } catch (FileNotFoundException fnfe) {
      final Distribution dist = Distribution.getInstance(mContext);
      dist.addOnDistributionReadyCallback(
          new Runnable() {
            @Override
            public void run() {
              Log.d(LOGTAG, "Running post-distribution task: quickshare.");

              if (!dist.exists()) {
                return;
              }

              try {
                File distFile = dist.getDistributionFile("quickshare/" + mHistoryFileName);
                if (distFile == null) {
                  if (DEBUG) {
                    Log.i(LOG_TAG, "Could not open historical records file: " + mHistoryFileName);
                  }
                  return;
                }
                readHistoricalDataFromStream(new FileInputStream(distFile));
              } catch (Exception ex) {
                if (DEBUG) {
                  Log.i(LOG_TAG, "Could not open historical records file: " + mHistoryFileName);
                }
                return;
              }
            }
          });
    }
  }
  /** Command for reading the historical records from a file off the UI thread. */
  private void readHistoricalDataImpl() {
    FileInputStream fis = null;
    try {
      GeckoProfile profile = GeckoProfile.get(mContext);
      File f = profile.getFile(mHistoryFileName);
      if (!f.exists()) {
        // Fall back to the non-profile aware file if it exists...
        File oldFile = new File(mHistoryFileName);
        oldFile.renameTo(f);
      }
      fis = new FileInputStream(f);
    } catch (FileNotFoundException fnfe) {
      try {
        Distribution dist = new Distribution(mContext);
        File distFile = dist.getDistributionFile("quickshare/" + mHistoryFileName);
        if (distFile == null) {
          if (DEBUG) {
            Log.i(LOG_TAG, "Could not open historical records file: " + mHistoryFileName);
          }
          return;
        }
        fis = new FileInputStream(distFile);
      } catch (Exception ex) {
        if (DEBUG) {
          Log.i(LOG_TAG, "Could not open historical records file: " + mHistoryFileName);
        }
        return;
      }
    }

    try {
      XmlPullParser parser = Xml.newPullParser();
      parser.setInput(fis, null);

      int type = XmlPullParser.START_DOCUMENT;
      while (type != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
        type = parser.next();
      }

      if (!TAG_HISTORICAL_RECORDS.equals(parser.getName())) {
        throw new XmlPullParserException(
            "Share records file does not start with " + TAG_HISTORICAL_RECORDS + " tag.");
      }

      List<HistoricalRecord> historicalRecords = mHistoricalRecords;
      historicalRecords.clear();

      while (true) {
        type = parser.next();
        if (type == XmlPullParser.END_DOCUMENT) {
          break;
        }
        if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
          continue;
        }
        String nodeName = parser.getName();
        if (!TAG_HISTORICAL_RECORD.equals(nodeName)) {
          throw new XmlPullParserException("Share records file not well-formed.");
        }

        String activity = parser.getAttributeValue(null, ATTRIBUTE_ACTIVITY);
        final long time = Long.parseLong(parser.getAttributeValue(null, ATTRIBUTE_TIME));
        final float weight = Float.parseFloat(parser.getAttributeValue(null, ATTRIBUTE_WEIGHT));
        HistoricalRecord readRecord = new HistoricalRecord(activity, time, weight);
        historicalRecords.add(readRecord);

        if (DEBUG) {
          Log.i(LOG_TAG, "Read " + readRecord.toString());
        }
      }

      if (DEBUG) {
        Log.i(LOG_TAG, "Read " + historicalRecords.size() + " historical records.");
      }
    } catch (XmlPullParserException xppe) {
      Log.e(LOG_TAG, "Error reading historical recrod file: " + mHistoryFileName, xppe);
    } catch (IOException ioe) {
      Log.e(LOG_TAG, "Error reading historical recrod file: " + mHistoryFileName, ioe);
    } finally {
      if (fis != null) {
        try {
          fis.close();
        } catch (IOException ioe) {
          /* ignore */
        }
      }
    }
  }