// Connects to the online service and puts the latest statuses into DB.
 // Returns the count of new statuses
 public synchronized int fetchStatusUpdates() {
   Log.d(TAG, "Fetching status updates");
   YambaClient yamba = this.getYambaClient();
   if (yamba == null) {
     Log.d(TAG, "Yamba connection info not initialized");
     return 0;
   }
   try {
     List<Status> statusUpdates = yamba.getTimeline(20);
     long latestStatusCreatedAtTime = this.getStatusData().getLatestStatusCreatedAtTime();
     int count = 0;
     ContentValues values = new ContentValues();
     for (Status status : statusUpdates) {
       values.put(StatusData.C_ID, status.getId());
       long createdAt = status.getCreatedAt().getTime();
       values.put(StatusData.C_CREATED_AT, createdAt);
       values.put(StatusData.C_TEXT, status.getMessage());
       values.put(StatusData.C_USER, status.getUser());
       Log.d(TAG, "Got update with id " + status.getId() + ". Saving");
       this.getStatusData().insertOrIgnore(values);
       if (latestStatusCreatedAtTime < createdAt) {
         count++;
       }
     }
     Log.d(TAG, count > 0 ? "Got " + count + " status updates" : "No new status updates");
     return count;
   } catch (YambaClientException yce) {
     Log.e(TAG, "Failed to fetch status updates", yce);
     return 0;
   } catch (RuntimeException e) {
     Log.e(TAG, "Failed to fetch status updates", e);
     return 0;
   }
 }
Example #2
0
  @Override
  public void onHandleIntent(Intent intent) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    final String username = prefs.getString("username", "");
    final String password = prefs.getString("password", "");

    // Check that username and password are not empty
    if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {

      Toast.makeText(this, "Please update your username and password", Toast.LENGTH_LONG).show();
      return;
    }
    Log.d(TAG, "onStarted");

    YambaClient cloud = new YambaClient(username, password);
    try {
      List<YambaClient.Status> timeline = cloud.getTimeline(20);
      for (YambaClient.Status status : timeline) {
        Log.d(TAG, String.format("%s: %s", status.getUser(), status.getMessage()));
      }
    } catch (YambaClientException e) {
      Log.e(TAG, "Failed to fetch the timeline", e);
      e.printStackTrace();
    }
  }
  /** Executes on a worker thread. */
  @Override
  protected void onHandleIntent(Intent intent) {
    Log.d(TAG, "onHandleIntent");

    // Do we have yamba client?
    if (yamba == null) {
      noYamba();
      return;
    }

    // Pull the data from the cloud
    try {
      ContentValues values = new ContentValues();

      List<Status> timeline = yamba.getTimeline(20);
      for (Status status : timeline) {
        // Insert into db
        values.clear();
        values.put(StatusContract.Columns.ID, status.getId());
        values.put(StatusContract.Columns.USER, status.getUser());
        values.put(StatusContract.Columns.MESSAGE, status.getMessage());
        values.put(StatusContract.Columns.CREATED_AT, status.getCreatedAt().getTime());
        getContentResolver().insert(StatusContract.CONTENT_URI, values);

        Log.d(TAG, String.format("%s: %s", status.getUser(), status.getMessage()));
      }
    } catch (YambaClientException e) {
      e.printStackTrace();
      noYamba();
    }
  }