Android Open Source - android-gskbyte-utils Load Dialog Fragment






From Project

Back to project page android-gskbyte-utils.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project android-gskbyte-utils 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 org.gskbyte.dialog;
//w ww  .  j  a  va2s .c  o  m
import java.lang.ref.WeakReference;

import org.gskbyte.R;

import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class LoadDialogFragment
extends DialogFragment
{

public static final String DEFAULT_TAG = "LOAD_DIALOG";
public static final String KEY_ARG_SIZE_MODE = "SIZE_MODE";

protected int windowGravity = Gravity.CENTER;

protected boolean dimsBackground = true;

protected float displayedRate;
protected boolean showsRate;

protected Drawable customBackground;

protected String customTitle, subtitleText;
protected CancelMode cancelMode = CancelMode.Invisible;
protected SizeMode sizeMode = SizeMode.Normal;
protected WeakReference<LoadDialogCancelListener> cancelListener = null;
protected boolean dismissOnCancel = true;

protected ViewGroup rootView;
protected TextView title, subtitle, progressText;
protected ProgressBar horizontalProgressBar;
protected Button cancel;


public static enum CancelMode
{
    Invisible,  // button not visible, can't cancel pressing back
    Disabled,   // button visible but disabled, can't cancel pressing back
    Enabled     // button visible and enabled, can cancel pressing back
}

public static enum SizeMode
{
    Normal,
    Compact
}

public static interface LoadDialogCancelListener
{
    public void onLoadDialogCanceled(LoadDialogFragment dialog);
}

@Deprecated
public static LoadDialogFragment newInstance()
{
    return newInstance(SizeMode.Normal);
}

public static LoadDialogFragment newInstance(SizeMode sizeMode)
{
    LoadDialogFragment fragment = new LoadDialogFragment();
    
    Bundle args = new Bundle();
    args.putSerializable(KEY_ARG_SIZE_MODE, sizeMode);
    fragment.setArguments(args);
    
    return fragment;
}

public void dismissAfter(long millis, Runnable runAfterDismiss)
{
    final Runnable finalRunnable = runAfterDismiss;
    getView().postDelayed(new Runnable() {
        @Override
        public void run()
        {
            dismiss();
            if(finalRunnable != null) {
                finalRunnable.run();
            }
        }
    }, millis);
}

@Override
public void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  
  // Disable title
  setStyle(STYLE_NO_TITLE, 0);
  
  Bundle args = getArguments();
  if(args != null) {
      SizeMode szm = (SizeMode) args.getSerializable(KEY_ARG_SIZE_MODE);
      if(szm != null) {
          this.sizeMode = szm;
      }
  }
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
    Dialog d = super.onCreateDialog(savedInstanceState);

    // Disable user cancellation
    d.setCanceledOnTouchOutside(false);
    setCancelable(false);
    
    updateDialog(d);
    
    return d;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    final int layoutRes = (sizeMode==SizeMode.Compact) ? R.layout.dialog_load_compact : R.layout.dialog_load;
    
    View v = inflater.inflate(layoutRes, container, false);
    
    rootView = (ViewGroup) v.findViewById(R.id.root);

    title = (TextView) v.findViewById(R.id.loading_title);
    subtitle = (TextView) v.findViewById(R.id.loading_subtitle);
    progressText = (TextView) v.findViewById(R.id.progressText);
    horizontalProgressBar = (ProgressBar) v.findViewById(R.id.determinateProgressBar);
    cancel = (Button) v.findViewById(R.id.cancel);
    cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        { onCancel( getDialog() );}
    });
    
    updateView();
    
    return v;
}

protected void updateDialog(Dialog d)
{
    if(d == null)
        return;
    
    Window w = d.getWindow();
    
    // TODO implement the positive case, when you have time!!!!
    if(!dimsBackground)
        w.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    
    if(customBackground != null)
        d.getWindow().setBackgroundDrawable(customBackground);
    
    WindowManager.LayoutParams wlp = w.getAttributes();

    wlp.gravity = windowGravity;
    wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
    
    w.setAttributes(wlp);
}

protected void updateView()
{
    if(progressText == null) // getView() is still null in onCreateView
        return;
    
    title.setText( getTitle() );
    
    if(subtitleText == null || subtitleText.isEmpty()) {
        subtitle.setVisibility(View.GONE);
    } else {
        subtitle.setText(subtitleText);
        subtitle.setVisibility(View.VISIBLE);
    }
    
    updateDialog( getDialog() );
    
    progressText.setVisibility( showsRate ? View.VISIBLE : View.GONE);
    horizontalProgressBar.setVisibility( showsRate ? View.VISIBLE : View.GONE );
    
    cancel.setEnabled( (cancelMode==CancelMode.Enabled) );
    cancel.setVisibility( (cancelMode!=CancelMode.Invisible) ? View.VISIBLE : View.GONE );
    
    if(showsRate) {
        float percentRate = displayedRate * 100;
        horizontalProgressBar.setProgress( Math.round( percentRate ) );
        progressText.setText( String.format("%.2f %%", percentRate) );
    }
}

@Override
public void onCancel(DialogInterface dialog)
{
    super.onCancel(dialog);
    if(cancelListener != null && cancelListener.get() != null) {
        cancelListener.get().onLoadDialogCanceled(this);
    }
    
    if(dismissOnCancel) {
        dismiss();
    }
}

public CancelMode getCancelMode()
{ return cancelMode; }

public void setCancelMode(CancelMode m)
{
    this.cancelMode = m;
    setCancelable((m == CancelMode.Enabled));
    updateView();
}

public SizeMode getSizeMode()
{ return this.sizeMode; }

public void setCancelListener(LoadDialogCancelListener l)
{ this.cancelListener = new WeakReference<LoadDialogCancelListener>(l); }

// dismiss the dialog automatically when canceled
public void setDismissOnCancel(boolean d)
{ dismissOnCancel = d; }

public int getWindowGravity()
{ return windowGravity; }

public void setGravity(int g)
{
    if(g != windowGravity) {
        windowGravity = g;
        updateView();
    }
}

public boolean dimsBackground()
{ return dimsBackground; }

public void setDimsBackground(boolean d)
{
    if(d != dimsBackground) {
        dimsBackground = d;
        updateView();
    }
}


public float getProgressRate()
{ return displayedRate; }

public void setProgressRate(float rate)
{
    displayedRate = rate;
    updateView();
}


public boolean showsRate()
{ return showsRate; }

public void setShowsRate(boolean b)
{
    if(showsRate != b) {
        showsRate = b;
        updateView();
    }
}


public Drawable getBackground()
{ return customBackground; }

public void setBackground(Drawable d)
{
    if(d != customBackground) {
        customBackground = d;
        updateView();
    }
}

public String getDefaultTitle()
{ return getString(R.string.load_dialog_loading); }

public String getTitle()
{
    if(customTitle == null)
        return getDefaultTitle();
    else
        return customTitle;
}

public void setTitle(String customTitle)
{
    this.customTitle = customTitle;
    updateView();
}

public String getSubtitle()
{ return customTitle; }

public void setSubtitle(String subtitle)
{
    this.subtitleText = subtitle;
    updateView();
}
  
}




Java Source Code List

com.woozzu.android.widget.IndexScroller.java
com.woozzu.android.widget.IndexableListView.java
org.gskbyte.FragmentWrapperActivity.java
org.gskbyte.animation.ExpandAnimation.java
org.gskbyte.bitmap.AbstractBitmapManager.java
org.gskbyte.bitmap.BitmapColorizer.java
org.gskbyte.bitmap.BitmapManager.java
org.gskbyte.bitmap.CachedBitmapColorizer.java
org.gskbyte.bitmap.IndexedBitmaps.java
org.gskbyte.bitmap.LRUBitmapCache.java
org.gskbyte.bitmap.LRUBitmapManager.java
org.gskbyte.bitmap.PrivateBitmapManager.java
org.gskbyte.bitmap.ReferencedBitmaps.java
org.gskbyte.collection.ArrayHashMap.java
org.gskbyte.collection.DoubleSparseArray.java
org.gskbyte.collection.ListHashMap.java
org.gskbyte.dialog.DownloadDialogFragment.java
org.gskbyte.dialog.LoadDialogFragment.java
org.gskbyte.dialog.OpenLinkDialogBuilder.java
org.gskbyte.dialog.PickerDialogFragment.java
org.gskbyte.download.DiskDownload.java
org.gskbyte.download.DownloadManager.java
org.gskbyte.download.Download.java
org.gskbyte.download.MemoryDownload.java
org.gskbyte.drawable.AutoBackgroundButtonDrawable.java
org.gskbyte.listener.IListenable.java
org.gskbyte.listener.ListenableNG.java
org.gskbyte.listener.Listenable.java
org.gskbyte.preferences.DialogSeekBarPreference.java
org.gskbyte.preferences.InlineSeekBarPreference.java
org.gskbyte.remote.AsyncURLRequest.java
org.gskbyte.remote.URLRequest.java
org.gskbyte.tasks.QueuedTaskExecutor.java
org.gskbyte.tasks.TaskStep.java
org.gskbyte.tasks.Task.java
org.gskbyte.ui.ArrayAdapterWithDefaultValue.java
org.gskbyte.ui.ListAdapter.java
org.gskbyte.ui.ColorDialog.ColorDialog.java
org.gskbyte.ui.ColorDialog.ColorPreference.java
org.gskbyte.ui.iconifiedMainMenuList.EntryView.java
org.gskbyte.ui.iconifiedMainMenuList.MainMenuAdapter.java
org.gskbyte.ui.iconifiedMainMenuList.MenuEntry.java
org.gskbyte.util.FrequentIntents.java
org.gskbyte.util.IOUtils.java
org.gskbyte.util.Logger.java
org.gskbyte.util.OpenFileHandlerFactory.java
org.gskbyte.util.OpenFileHandler.java
org.gskbyte.util.XmlUtils.java
org.gskbyte.view.AsyncImageView.java
org.gskbyte.view.AutoBackgroundButton.java
org.gskbyte.view.AutoBackgroundImageButton.java
org.gskbyte.view.AutoHeightImageView.java
org.gskbyte.view.ExpandedGridView.java
org.gskbyte.view.ExpandedListView.java
org.gskbyte.view.FontUtil.java
org.gskbyte.view.FontableButton.java
org.gskbyte.view.FontableCheckBox.java
org.gskbyte.view.FontableEditText.java
org.gskbyte.view.FontableTextView.java
org.gskbyte.view.FullWidthImageView.java
org.gskbyte.view.ProportionalHeightLayout.java
org.gskbyte.view.PullToRefreshListView.java
org.gskbyte.view.SquaredLayout.java
org.gskbyte.view.StepSeekBar.java
org.gskbyte.view.TextViewUtil.java
org.gskbyte.view.ViewUtils.java