Android Open Source - android-002 Main Activity






From Project

Back to project page android-002.

License

The source code is released under:

Apache License

If you think the Android project android-002 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 course.labs.notificationslab;
/*w w w .  ja  v  a2  s . c o m*/
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends Activity implements SelectionListener {

  public static final String TWEET_FILENAME = "tweets.txt";
  public static final String[] FRIENDS = { "taylorswift13", "msrebeccablack",
      "ladygaga" };
  public static final int IS_ALIVE = Activity.RESULT_FIRST_USER;
  public static final String DATA_REFRESHED_ACTION = "course.labs.notificationslab.DATA_REFRESHED";
  
  private static final int NUM_FRIENDS = 3;
  
  // Actual URLs are unused in this offline version of the app
  private static final String URL_TSWIFT = "https://d396qusza40orc.cloudfront.net/android%2FLabs%2FUserNotifications%2Ftaylorswift.txt";
  private static final String URL_RBLACK = "https://d396qusza40orc.cloudfront.net/android%2FLabs%2FUserNotifications%2Frebeccablack.txt";
  private static final String URL_LGAGA = "https://d396qusza40orc.cloudfront.net/android%2FLabs%2FUserNotifications%2Fladygaga.txt";
  private static final String TAG = "Lab-Notifications";
  private static final long TWO_MIN = 2 * 60 * 1000;
  private static final int UNSELECTED = -1;


  private FragmentManager mFragmentManager;
  private FriendsFragment mFriendsFragment;
  private boolean mIsFresh;
  private BroadcastReceiver mRefreshReceiver;
  private int mFeedSelected = UNSELECTED;
  private FeedFragment mFeedFragment;
  private String[] mRawFeeds = new String[3];
  private String[] mProcessedFeeds = new String[3];

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mFragmentManager = getFragmentManager();
    addFriendsFragment();

    // The feed is fresh if it was downloaded less than 2 minutes ago
    mIsFresh = (System.currentTimeMillis() - getFileStreamPath(
        TWEET_FILENAME).lastModified()) < TWO_MIN;

    ensureData();

  }

  // Add Friends Fragment to Activity
  private void addFriendsFragment() {

    mFriendsFragment = new FriendsFragment();
    mFriendsFragment.setArguments(getIntent().getExtras());

    FragmentTransaction transaction = mFragmentManager.beginTransaction();
    transaction.add(R.id.fragment_container, mFriendsFragment);

    transaction.commit();
  }

  // If stored Tweets are not fresh, reload them from network
  // Otherwise, load them from file
  private void ensureData() {

    Log.i(TAG,"In ensureData(), mIsFresh:" + mIsFresh);

    if (!mIsFresh) {

      // TODO:
      // Show a Toast Notification to inform user that 
      // the app is "Downloading Tweets from Network"
      Log.i (TAG,"Issuing Toast Message");
      Toast.makeText(getApplicationContext(), 
              "Downloading Tweets from Network", 
              Toast.LENGTH_SHORT).show();

      new DownloaderTask(this).execute(URL_TSWIFT, URL_RBLACK, URL_LGAGA);

      
      // Set up a BroadcastReceiver to receive an Intent when download
      // finishes. 
      mRefreshReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

          Log.i(TAG,"BroadcastIntent received in MainActivity");
          
          // TODO:        
          // Check to make sure this is an ordered broadcast
          // Let sender know that the Intent was received
          // by setting result code to MainActivity.IS_ALIVE
          if ( isOrderedBroadcast() ) {
            Log.i(TAG,"isOrderedBroadcast is true...");
            setResult(MainActivity.IS_ALIVE, null, null);
          }
        }
      };

    } else {

      loadTweetsFromFile();
      parseJSON();
      updateFeed();

    }
  }

  // Called when new Tweets have been downloaded 
  public void setRefreshed(String[] feeds) {

    mRawFeeds[0] = feeds[0];
    mRawFeeds[1] = feeds[1];
    mRawFeeds[2] = feeds[2];

    parseJSON();
    updateFeed();
    mIsFresh = true;

  };

  // Called when a Friend is clicked on
  @Override
  public void onItemSelected(int position) {

    mFeedSelected = position;
    mFeedFragment = addFeedFragment();

    if (mIsFresh) {
      updateFeed();
    }
  }

  // Calls FeedFragement.update, passing in the 
  // the tweets for the currently selected friend
 
  void updateFeed() {

    if (null != mFeedFragment)

      mFeedFragment.update(mProcessedFeeds[mFeedSelected]);

  }

  // Add FeedFragment to Activity
  private FeedFragment addFeedFragment() {
    FeedFragment feedFragment;
    feedFragment = new FeedFragment();

    FragmentTransaction transaction = mFragmentManager.beginTransaction();

    transaction.replace(R.id.fragment_container, feedFragment);
    transaction.addToBackStack(null);

    transaction.commit();
    mFragmentManager.executePendingTransactions();
    return feedFragment;

  }

  // Register the BroadcastReceiver
  @Override
  protected void onResume() {
    super.onResume();

    // TODO:
    // Register the BroadcastReceiver to receive a 
    // DATA_REFRESHED_ACTION broadcast
    this.registerReceiver(mRefreshReceiver, 
              new IntentFilter(DATA_REFRESHED_ACTION));
  }

  @Override
  protected void onPause() {
    
    super.onPause();

    // TODO:
    // Unregister the BroadcastReceiver if it has been registered
        // Note: To work around a Robotium issue - check that the BroadcastReceiver
        // is not null before you try to unregister it
        if (mRefreshReceiver != null) {
          this.unregisterReceiver(mRefreshReceiver);
        }

  }

  // Convert raw Tweet data (in JSON format) into text for display

  public void parseJSON() {

    JSONArray[] JSONFeeds = new JSONArray[NUM_FRIENDS];

    for (int i = 0; i < NUM_FRIENDS; i++) {
      try {
        JSONFeeds[i] = new JSONArray(mRawFeeds[i]);
      } catch (JSONException e) {
        e.printStackTrace();
      }

      String name = "";
      String tweet = "";

      JSONArray tmp = JSONFeeds[i];

      // string buffer for twitter feeds
      StringBuffer tweetRec = new StringBuffer("");

      for (int j = 0; j < tmp.length(); j++) {
        try {
          tweet = tmp.getJSONObject(j).getString("text");
          JSONObject user = (JSONObject) tmp.getJSONObject(j).get(
              "user");
          name = user.getString("name");

        } catch (JSONException e) {
          e.printStackTrace();
        }

        tweetRec.append(name + " - " + tweet + "\n\n");
      }

      mProcessedFeeds[i] = tweetRec.toString();

    }
  }

  // Retrieve feeds text from a file
  // Store them in mRawTextFeed[]

  private void loadTweetsFromFile() {
    BufferedReader reader = null;

    try {
      FileInputStream fis = openFileInput(TWEET_FILENAME);
      reader = new BufferedReader(new InputStreamReader(fis));
      String s = null;
      int i = 0;
      while (null != (s = reader.readLine()) && i < NUM_FRIENDS) {
        mRawFeeds[i] = s;
        i++;
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (null != reader) {
        try {
          reader.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }

}




Java Source Code List

com.michaelfitzmaurice.android.modernart.MainActivity.java
com.michaelfitzmaurice.dailyselfie.AlarmReceiver.java
com.michaelfitzmaurice.dailyselfie.AlarmTimeInterval.java
com.michaelfitzmaurice.dailyselfie.Alarms.java
com.michaelfitzmaurice.dailyselfie.SelfieDetailActivity.java
com.michaelfitzmaurice.dailyselfie.SelfieListActivity.java
com.michaelfitzmaurice.dailyselfie.SelfieListViewAdapter.java
com.michaelfitzmaurice.dailyselfie.SelfieRecord.java
com.michaelfitzmaurice.dailyselfie.settings.ReminderIntervalDialogPreference.java
com.michaelfitzmaurice.dailyselfie.settings.SettingsActivity.java
com.michaelfitzmaurice.dailyselfie.settings.SettingsFragment.java
course.labs.contentproviderlab.MockLocationProvider.java
course.labs.contentproviderlab.PlaceDownloaderTask.java
course.labs.contentproviderlab.PlaceRecord.java
course.labs.contentproviderlab.PlaceViewActivity.java
course.labs.contentproviderlab.PlaceViewAdapter.java
course.labs.contentproviderlab.provider.PlaceBadgeContentProvider.java
course.labs.contentproviderlab.provider.PlaceBadgesContract.java
course.labs.contentproviderlab.provider.PlaceBadgesContract.java
course.labs.graphicslab.BubbleActivity.java
course.labs.locationlab.MockLocationProvider.java
course.labs.locationlab.PlaceDownloaderTask.java
course.labs.locationlab.PlaceRecord.java
course.labs.locationlab.PlaceViewActivity.java
course.labs.locationlab.PlaceViewAdapter.java
course.labs.notificationslab.DownloaderTask.java
course.labs.notificationslab.FeedFragment.java
course.labs.notificationslab.FriendsFragment.java
course.labs.notificationslab.MainActivity.java
course.labs.notificationslab.SelectionListener.java
course.labs.notificationslab.TestFrontEndActivity.java