Exemplo n.º 1
0
    @Override
    public void onPostSyncItem(
        Context context, Account account, Uri uri, JSONObject item, boolean updated)
        throws SyncException, IOException {
      if (!mStartChildSync) {
        return;
      }
      final Uri childDir = mRelationship.getChildDirUri(uri);
      try {
        final String childPubUri = item.getString(remoteKey);
        // TODO optimize so it doesn't need to create a whole new instance
        final NetworkClient nc = NetworkClient.getInstance(context, account);
        final Uri serverUri = nc.getFullUrl(childPubUri);

        LocastSync.startSync(context, serverUri, childDir, false);

      } catch (final JSONException e) {
        final IOException ioe = new IOException("JSON encoding error");
        ioe.initCause(e);
        throw ioe;
      }
    }
Exemplo n.º 2
0
    @Override
    public ContentValues fromJSON(Context context, Uri localItem, JSONObject item, String lProp)
        throws JSONException, NetworkProtocolException, IOException {
      final ContentValues cv = new ContentValues();

      switch (getType()) {
        case SyncFieldMap.STRING:
          cv.put(lProp, item.getString(remoteKey));
          break;

        case SyncFieldMap.INTEGER:
          cv.put(lProp, item.getInt(remoteKey));
          break;

        case SyncFieldMap.DOUBLE:
          cv.put(lProp, item.getDouble(remoteKey));
          break;

        case SyncFieldMap.BOOLEAN:
          cv.put(lProp, item.getBoolean(remoteKey));
          break;

        case SyncFieldMap.LIST_INTEGER:
        case SyncFieldMap.LIST_STRING:
        case SyncFieldMap.LIST_DOUBLE:
          {
            final JSONArray ar = item.getJSONArray(remoteKey);
            final List<String> l = new Vector<String>(ar.length());
            for (int i = 0; i < ar.length(); i++) {
              switch (getType()) {
                case SyncFieldMap.LIST_STRING:
                  l.add(ar.getString(i));
                  break;

                case SyncFieldMap.LIST_DOUBLE:
                  l.add(String.valueOf(ar.getDouble(i)));
                  break;

                case SyncFieldMap.LIST_INTEGER:
                  l.add(String.valueOf(ar.getInt(i)));
                  break;
              }
            }
            cv.put(lProp, TextUtils.join(LIST_DELIM, l));
          }
          break;

        case SyncFieldMap.DATE:
          try {
            cv.put(lProp, NetworkClient.parseDate(item.getString(remoteKey)).getTime());
          } catch (final ParseException e) {
            final NetworkProtocolException ne = new NetworkProtocolException("bad date format");
            ne.initCause(e);
            throw ne;
          }
          break;

        case SyncFieldMap.DURATION:
          {
            final Matcher m = durationPattern.matcher(item.getString(remoteKey));
            if (!m.matches()) {
              throw new NetworkProtocolException("bad duration format");
            }
            final int durationSeconds =
                1200 * Integer.parseInt(m.group(1))
                    + 60 * Integer.parseInt(m.group(2))
                    + Integer.parseInt(m.group(3));
            cv.put(lProp, durationSeconds);
          }
          break;
      }
      return cv;
    }