Пример #1
0
    @Override
    @SuppressWarnings("unchecked")
    public Void doInBackground(Object... args) {
      List<HistoricalRecord> historicalRecords = (List<HistoricalRecord>) args[0];
      String hostoryFileName = (String) args[1];

      FileOutputStream fos = null;

      try {
        fos = mContext.openFileOutput(hostoryFileName, Context.MODE_PRIVATE);
      } catch (FileNotFoundException fnfe) {
        Log.e(LOG_TAG, "Error writing historical recrod file: " + hostoryFileName, 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 iae) {
        Log.e(LOG_TAG, "Error writing historical recrod file: " + mHistoryFileName, iae);
      } catch (IllegalStateException ise) {
        Log.e(LOG_TAG, "Error writing historical recrod file: " + mHistoryFileName, ise);
      } catch (IOException ioe) {
        Log.e(LOG_TAG, "Error writing historical recrod file: " + mHistoryFileName, ioe);
      } finally {
        mCanReadHistoricalData = true;
        if (fos != null) {
          try {
            fos.close();
          } catch (IOException e) {
            /* ignore */
          }
        }
      }
      return null;
    }
  void readHistoricalDataFromStream(FileInputStream fis) {
    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 | IOException xppe) {
      Log.e(LOG_TAG, "Error reading historical record file: " + mHistoryFileName, xppe);
    } finally {
      if (fis != null) {
        try {
          fis.close();
        } catch (IOException ioe) {
          /* ignore */
        }
      }
    }
  }
  /** 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 */
        }
      }
    }
  }