Android Open Source - LearningAndroid2edYamba2 Refresh Service






From Project

Back to project page LearningAndroid2edYamba2.

License

The source code is released under:

Apache License

If you think the Android project LearningAndroid2edYamba2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.marakana.android.yamba;
//w w w .ja v a 2 s.  com
import java.util.List;

import android.app.IntentService;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.marakana.android.yamba.clientlib.YambaClient;
import com.marakana.android.yamba.clientlib.YambaClient.Status;
import com.marakana.android.yamba.clientlib.YambaClientException;

public class RefreshService extends IntentService {
    //static final String YAMBA_API_ROOT = "http://yamba.newcircle.com/api";
    static final String YAMBA_API_ROOT = YambaClient.DEFAULT_API_ROOT; // "http://yamba.marakana.com/api"
  private static final String TAG = RefreshService.class.getSimpleName();

  public RefreshService() {
    super(TAG);
  }

  @Override
  public void onCreate() {
    super.onCreate();
    Log.d(TAG, "onCreated");
  }

  // Executes on a worker thread
  @Override
  protected 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");

    ContentValues values = new ContentValues();

    YambaClient cloud = new YambaClient(username, password,
            YAMBA_API_ROOT); // clk: instead of always using built-in default api-root
    try {
      int count = 0;
      List<Status> timeline = cloud.getTimeline(20);
      for (Status status : timeline) {
        values.clear();
        values.put(StatusContract.Column.ID, status.getId());
        values.put(StatusContract.Column.USER, status.getUser());
        values.put(StatusContract.Column.MESSAGE, status.getMessage());
        values.put(StatusContract.Column.CREATED_AT, status
            .getCreatedAt().getTime());

        Uri uri = getContentResolver().insert(
            StatusContract.CONTENT_URI, values);
        if (uri != null) {
          count++;
          Log.d(TAG,
              String.format("%s: %s", status.getUser(),
                  status.getMessage()));
        }
      }

      if (count > 0) {
        sendBroadcast(new Intent(
            "com.marakana.android.yamba.action.NEW_STATUSES").putExtra(
            "count", count));
      }

    } catch (YambaClientException e) {
      Log.e(TAG, "Failed to fetch the timeline", e);
      e.printStackTrace();
    }

    return;
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "onDestroyed");
  }

}




Java Source Code List

com.marakana.android.yamba.BootReceiver.java
com.marakana.android.yamba.DbHelper.java
com.marakana.android.yamba.DetailsActivity.java
com.marakana.android.yamba.DetailsFragment.java
com.marakana.android.yamba.FreshnessView.java
com.marakana.android.yamba.MainActivity.java
com.marakana.android.yamba.NotificationReceiver.java
com.marakana.android.yamba.RefreshService.java
com.marakana.android.yamba.SettingsActivity.java
com.marakana.android.yamba.SettingsFragment.java
com.marakana.android.yamba.StatusActivity.java
com.marakana.android.yamba.StatusContract.java
com.marakana.android.yamba.StatusFragment.java
com.marakana.android.yamba.StatusProvider.java
com.marakana.android.yamba.SubActivity.java
com.marakana.android.yamba.TimelineFragment.java
com.marakana.android.yamba.YambaWidget.java