Android Open Source - Aviary-Android-SDK Aviary Toast






From Project

Back to project page Aviary-Android-SDK.

License

The source code is released under:

AVIARY API TERMS OF USE Full Legal Agreement The following terms and conditions and the terms and conditions at http://www.aviary.com/terms (collectively, the ?Terms??) govern your use of any and ...

If you think the Android project Aviary-Android-SDK 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.aviary.android.feather.widget;
/*  w  w  w .  ja  va 2s  .c o  m*/
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

/**
 * Custom {@link Toast} with arbitrary duration
 * 
 * @author alessandro
 */
public class AviaryToast {

  final String LOG_TAG = "toast";

  Context mContext;

  WindowManager mWindowManager;

  View mNextView;

  View mView;

  int mDuration;

  int mGravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;

  int mX, mY;

  final TN mTN;

  float mHorizontalMargin;

  float mVerticalMargin;

  final Handler mHandler = new Handler();

  public static interface LayoutListener {

    public void onShown( View currentView );

    public void onHidden();
  }

  private LayoutListener mLayoutListener;

  public AviaryToast ( Context context ) {
    mContext = context;
    mWindowManager = (WindowManager) context.getSystemService( Context.WINDOW_SERVICE );

    mTN = new TN();
    mTN.mWm = mWindowManager;
    mY = 0;
    mX = 0;
  }

  public void setLayoutListener( LayoutListener listener ) {
    mLayoutListener = listener;
  }

  public void setView( View v ) {
    mNextView = v;
  }

  public View getView() {
    return mNextView;
  }

  public void show() {
    if ( mNextView == null ) throw new RuntimeException( "setView must be called first" );
    mTN.show();
  }

  public void hide() {
    mTN.hide();
  }

  public void update() {}

  /**
   * Create a new AviaryToast
   * 
   * @param context
   *            the current context
   * @param resid
   *            the view resource id
   * @param duration
   *            the toast duration ( -1 for infinite duration )
   * @return
   */
  public static AviaryToast make( Context context, int resid, int duration ) {
    AviaryToast result = new AviaryToast( context );
    LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
    View v = inflater.inflate( resid, null );
    result.mNextView = v;
    result.mDuration = duration;
    return result;
  }

  private class TN {

    final Runnable mShow = new Runnable() {

      @Override
      public void run() {
        handleShow();
      }
    };

    final Runnable mHide = new Runnable() {

      @Override
      public void run() {
        handleHide();
      }
    };

    WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();

    WindowManager mWm;

    TN () {
      final WindowManager.LayoutParams params = mParams;
      params.height = WindowManager.LayoutParams.WRAP_CONTENT;
      params.width = WindowManager.LayoutParams.WRAP_CONTENT;
      params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
          | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
      params.format = PixelFormat.TRANSLUCENT;
      params.type = WindowManager.LayoutParams.TYPE_TOAST;
      params.windowAnimations = com.aviary.android.feather.R.style.AviaryAnimations_AviaryToast;
      params.setTitle( "Toast" );
    }

    public void show() {
      mHandler.post( mShow );
    }

    public void hide() {
      mHandler.post( mHide );
    }

    public void handleShow() {

      if ( mView != mNextView ) {
        handleHide();
        mView = mNextView;
        final int gravity = mGravity;
        mParams.gravity = gravity;
        if ( ( gravity & Gravity.HORIZONTAL_GRAVITY_MASK ) == Gravity.FILL_HORIZONTAL ) {
          mParams.horizontalWeight = 1.0f;
        }
        if ( ( gravity & Gravity.VERTICAL_GRAVITY_MASK ) == Gravity.FILL_VERTICAL ) {
          mParams.verticalWeight = 1.0f;
        }
        mParams.x = mX;
        mParams.y = mY;
        mParams.verticalMargin = mVerticalMargin;
        mParams.horizontalMargin = mHorizontalMargin;

        if ( mView.getParent() != null ) {
          mView.setVisibility( View.GONE );
          mWm.removeView( mView );
        }

        mWm.addView( mView, mParams );
        mView.setVisibility( View.VISIBLE );

        if ( mLayoutListener != null ) {
          mLayoutListener.onShown( mView );
        }
      }
    }

    public void handleHide() {
      removeView();
      if ( mLayoutListener != null ) {
        mLayoutListener.onHidden();
      }
    }

    void removeView() {
      if ( mView != null ) {
        if ( mView.getParent() != null ) {
          mView.setVisibility( View.GONE );
          mWm.removeView( mView );
        }
        mView = null;
      }
    }
  };
}




Java Source Code List

com.aviary.android.feather.AlertActivity.java
com.aviary.android.feather.AviaryMainController.java
com.aviary.android.feather.FeatherActivity.java
com.aviary.android.feather.async_tasks.AsyncImageManager.java
com.aviary.android.feather.async_tasks.DownloadImageAsyncTask.java
com.aviary.android.feather.async_tasks.ExifTask.java
com.aviary.android.feather.effects.AbstractContentPanel.java
com.aviary.android.feather.effects.AbstractOptionPanel.java
com.aviary.android.feather.effects.AbstractPanelLoaderService.java
com.aviary.android.feather.effects.AbstractPanel.java
com.aviary.android.feather.effects.AdjustEffectPanel.java
com.aviary.android.feather.effects.BordersPanel.java
com.aviary.android.feather.effects.ColorSplashPanel.java
com.aviary.android.feather.effects.CropPanel.java
com.aviary.android.feather.effects.DelayedSpotDrawPanel.java
com.aviary.android.feather.effects.DrawingPanel.java
com.aviary.android.feather.effects.EffectsPanel.java
com.aviary.android.feather.effects.EnhanceEffectPanel.java
com.aviary.android.feather.effects.MemePanel.java
com.aviary.android.feather.effects.NativeEffectRangePanel.java
com.aviary.android.feather.effects.SimpleStatusMachine.java
com.aviary.android.feather.effects.SliderEffectPanel.java
com.aviary.android.feather.effects.StickersPanel.java
com.aviary.android.feather.effects.TextPanel.java
com.aviary.android.feather.effects.TiltShiftPanel.java
com.aviary.android.feather.graphics.CdsPreviewTransformer.java
com.aviary.android.feather.graphics.GalleryBottomIndicatorDrawable.java
com.aviary.android.feather.graphics.GalleryTopIndicatorDrawable.java
com.aviary.android.feather.graphics.GlowBitmapDrawable.java
com.aviary.android.feather.graphics.GlowDrawable.java
com.aviary.android.feather.graphics.PluginDividerDrawable.java
com.aviary.android.feather.graphics.PreviewFillColorDrawable.java
com.aviary.android.feather.graphics.PreviewSpotDrawable.java
com.aviary.android.feather.graphics.RepeatableHorizontalDrawable.java
com.aviary.android.feather.opengl.AviaryGLSurfaceView.java
com.aviary.android.feather.utils.PackIconCallable.java
com.aviary.android.feather.utils.SimpleBitmapCache.java
com.aviary.android.feather.utils.ThreadUtils.java
com.aviary.android.feather.utils.TypefaceUtils.java
com.aviary.android.feather.utils.UIUtils.java
com.aviary.android.feather.widget.AdjustImageView.java
com.aviary.android.feather.widget.AviaryAbsSpinner.java
com.aviary.android.feather.widget.AviaryAdapterView.java
com.aviary.android.feather.widget.AviaryBadgeToolLayout.java
com.aviary.android.feather.widget.AviaryBottomBarViewFlipper.java
com.aviary.android.feather.widget.AviaryButton.java
com.aviary.android.feather.widget.AviaryEdgeEffect.java
com.aviary.android.feather.widget.AviaryGalleryTopIndicatorView.java
com.aviary.android.feather.widget.AviaryGallery.java
com.aviary.android.feather.widget.AviaryHighlightImageButton.java
com.aviary.android.feather.widget.AviaryImageRestoreSwitcher.java
com.aviary.android.feather.widget.AviaryImageSwitcher.java
com.aviary.android.feather.widget.AviaryNavBarViewFlipper.java
com.aviary.android.feather.widget.AviarySeekBar.java
com.aviary.android.feather.widget.AviaryTextView.java
com.aviary.android.feather.widget.AviaryToast.java
com.aviary.android.feather.widget.AviaryToggleButton.java
com.aviary.android.feather.widget.AviaryWheel.java
com.aviary.android.feather.widget.AviaryWorkspaceIndicator.java
com.aviary.android.feather.widget.AviaryWorkspace.java
com.aviary.android.feather.widget.CellLayout.java
com.aviary.android.feather.widget.CropImageView.java
com.aviary.android.feather.widget.DrawableHighlightView.java
com.aviary.android.feather.widget.EffectThumbLayout.java
com.aviary.android.feather.widget.HighlightView.java
com.aviary.android.feather.widget.IAPBuyButton.java
com.aviary.android.feather.widget.IAPDialogDetail.java
com.aviary.android.feather.widget.IAPDialogList.java
com.aviary.android.feather.widget.IAPDialogMain.java
com.aviary.android.feather.widget.ImageViewDrawableOverlay.java
com.aviary.android.feather.widget.ImageViewSpotDraw.java
com.aviary.android.feather.widget.ImageViewTiltiShiftTouch.java
com.aviary.android.feather.widget.ImageViewTouchAndDraw.java
com.aviary.android.feather.widget.PointCloud.java
com.aviary.android.feather.widget.ScrollerRunnable.java
com.aviary.android.feather.widget.VibrationHelper.java
com.aviary.android.feather.widget.VibrationWidget.java