Android Open Source - checkin Check In Handler






From Project

Back to project page checkin.

License

The source code is released under:

# License Copyright ? 2014 Sam Whited Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ?Software??), to deal ...

If you think the Android project checkin 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.samwhited.checkin;
/*w w  w. ja  v  a  2s . co m*/
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import java.lang.ref.WeakReference;


public final class CheckInHandler extends Handler {

  public static interface Handlable {
    void updateViews();
  }

  /**
   * Messages which can be sent to the handler.
   */
  public final static int SHOW_TOAST = 0;
  public final static int UPDATE_VIEW = 1;
  public final static int DISABLE_UPLOAD = 2;
  public final static int ENABLE_UPLOAD = 3;

  /**
   * Hold a weak reference to the activity to prevent a memory leak where the activity would not
   * be garbage collected if a long running thread was using this handler.
   */
  private final WeakReference<Activity> mActivity;

  /**
   * Constructor for our custom handler that takes in the outer activity.
   *
   * @param activity The activity to handle events for.
   */
  public CheckInHandler(final Activity activity) {
    mActivity = new WeakReference<>(activity);
  }

  public Context getContext() {
    return mActivity.get();
  }

  /**
   * Handle a message sent from the networking thread.
   *
   * @param msg The message object.
   */
  @Override
  public void handleMessage(final Message msg) {
    if (msg == null) {
      return;
    }

    final Activity activity = mActivity.get();
    switch (msg.arg1) {
      case SHOW_TOAST:
        if (msg.obj != null) {

          final String text = msg.obj.toString();
          if (text != null && !text.isEmpty() && activity != null) {
            Toast.makeText(activity, text, Toast.LENGTH_LONG).show();
          }
        }
        break;
      case UPDATE_VIEW:
        if (activity != null) {
          try {
             ((Handlable)activity).updateViews();
          } catch (final ClassCastException ignored) {
          }
        }
        break;
    }
  }
}




Java Source Code List

com.samwhited.checkin.CheckInActivity.java
com.samwhited.checkin.CheckInDB.java
com.samwhited.checkin.CheckInFragment.java
com.samwhited.checkin.CheckInHandler.java
com.samwhited.checkin.CheckInListAdapter.java
com.samwhited.checkin.CheckInListFragment.java
com.samwhited.checkin.CheckInListItem.java
com.samwhited.checkin.CheckInListLoader.java
com.samwhited.checkin.CheckInList.java
com.samwhited.checkin.CheckInOpenHelper.java
com.samwhited.checkin.IconSpinnerAdapter.java
com.samwhited.checkin.SettingsActivity.java
com.samwhited.checkin.SettingsFragment.java
com.samwhited.checkin.database.CheckInDB.java
com.samwhited.checkin.database.CheckInOpenHelper.java
com.samwhited.checkin.model.CheckIn.java
com.samwhited.checkin.util.CheckInPreferences.java
com.samwhited.checkin.util.Formatting.java
com.samwhited.checkin.util.GeoJSON.java
com.samwhited.checkin.util.NetworkUtils.java