Android Open Source - bad Bad Fragment






From Project

Back to project page bad.

License

The source code is released under:

Copyright (C) 2013 Madis Pink Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Softwa...

If you think the Android project bad 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.madisp.bad.lib;
//  w  w w. j  a  v a 2 s.  c  om
import android.app.Fragment;
import android.os.*;
import android.os.Process;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.madisp.bad.decor.CheckableDecorator;
import com.madisp.bad.decor.EditTextDecorator;
import com.madisp.bad.decor.ListViewDecorator;
import com.madisp.bad.decor.TextViewDecorator;
import com.madisp.bad.decor.ViewDecorator;
import com.madisp.bad.decor.WebViewDecorator;
import com.madisp.bad.eval.BadScope;
import com.madisp.bad.eval.Scope;
import com.madisp.bad.eval.ScopeWrapper;
import com.madisp.bad.expr.BlockExpression;
import com.madisp.bad.expr.ExpressionFactory;

/**
 * Created with IntelliJ IDEA.
 * User: madis
 * Date: 3/20/13
 * Time: 9:04 AM
 */
public abstract class BadFragment extends Fragment {
  private final int layout;
  private Scope scope;
  protected final Handler fg, bg;
  private final HandlerThread bgThread;

  private ExpressionFactory exprFactory = new ExpressionFactory();

  protected BadFragment(int layoutRes) {
    this.layout = layoutRes;
    fg = new Handler(Looper.getMainLooper());
    bgThread = new HandlerThread("badBgThread", Process.THREAD_PRIORITY_BACKGROUND);
    bgThread.start();
    bg = new Handler(bgThread.getLooper());
  }

  protected final Scope getScope() {
    return scope == null ? (scope = createScope()) : scope;
  }

  protected Scope createScope() {
    return new BadScope(new BadScope(null, new BadStdLib(getActivity())), this);
  }

  public void bg(final BlockExpression bgExpr) {
    bg(bgExpr, null);
  }

  public void bg(final BlockExpression bgExpr, final BlockExpression fgExpr) {
    // it's a possible leak
    bg.post(new Runnable() {
      @Override
      public void run() {
        bgExpr.yield(new ScopeWrapper(getScope()) {
          @Override
          public void setVar(final Object base, final String var, final Object newValue) {
            fg.post(new Runnable() {
              @Override
              public void run() {
                scope.setVar(base, var, newValue);
              }
            });
          }
        });
        if (fgExpr != null) {
          fg.post(new Runnable() {
            @Override
            public void run() {
              fgExpr.yield(getScope());
            }
          });
        }
      }
    });
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    long start = System.nanoTime();
    BadLayoutFactory factory = new BadLayoutFactory(inflater, getScope());
    // register standard decorators
    factory.addDecorator(new CheckableDecorator(exprFactory));
    factory.addDecorator(new EditTextDecorator(exprFactory));
    factory.addDecorator(new TextViewDecorator(exprFactory));
    factory.addDecorator(new ViewDecorator(exprFactory));
    factory.addDecorator(new ListViewDecorator(exprFactory));
    factory.addDecorator(new WebViewDecorator(exprFactory));
    // hook to add more decorators before inflation
    beforeInflate(factory);
    View v = factory.getInflater().inflate(layout, null);
    long end = System.nanoTime();
    Log.d("BadLib", "inflation took " + (end-start) + "ns");
    return v;
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    bgThread.quit();
  }

  protected void beforeInflate(BadLayoutFactory factory) {}
}




Java Source Code List

com.madisp.bad.ConsoleRepl.java
com.madisp.bad.Freezer.java
com.madisp.bad.Persist.java
com.madisp.bad.decor.BadDecorator.java
com.madisp.bad.decor.BaseDecorator.java
com.madisp.bad.decor.CheckableDecorator.java
com.madisp.bad.decor.EditTextDecorator.java
com.madisp.bad.decor.ListViewDecorator.java
com.madisp.bad.decor.TextViewDecorator.java
com.madisp.bad.decor.ViewDecorator.java
com.madisp.bad.decor.WebViewDecorator.java
com.madisp.bad.demo.ListFragment.java
com.madisp.bad.demo.LoginFragment.java
com.madisp.bad.demo.MainActivity.java
com.madisp.bad.demo.PersistFragment.java
com.madisp.bad.demo.ReplFragment.java
com.madisp.bad.demo.ShopFragment.java
com.madisp.bad.demo.TwoWayFragment.java
com.madisp.bad.eval.BadCollections.java
com.madisp.bad.eval.BadConverter.java
com.madisp.bad.eval.BadScope.java
com.madisp.bad.eval.BadVar.java
com.madisp.bad.eval.ScopeWrapper.java
com.madisp.bad.eval.Scope.java
com.madisp.bad.eval.Watcher.java
com.madisp.bad.expr.AndExpression.java
com.madisp.bad.expr.AssignExpression.java
com.madisp.bad.expr.BasableExpression.java
com.madisp.bad.expr.BlockExpression.java
com.madisp.bad.expr.ConstantExpression.java
com.madisp.bad.expr.DivisionExpression.java
com.madisp.bad.expr.ExpressionFactory.java
com.madisp.bad.expr.Expression.java
com.madisp.bad.expr.MethodExpression.java
com.madisp.bad.expr.MinusExpression.java
com.madisp.bad.expr.MultiplyExpression.java
com.madisp.bad.expr.NotExpression.java
com.madisp.bad.expr.OrExpression.java
com.madisp.bad.expr.PlusExpression.java
com.madisp.bad.expr.StatementList.java
com.madisp.bad.expr.UnaryMinusExpression.java
com.madisp.bad.expr.VarExpression.java
com.madisp.bad.lib.BadFragment.java
com.madisp.bad.lib.BadLayoutFactory.java
com.madisp.bad.lib.BadStdLib.java
com.madisp.bad.lib.ui.BadAdapter.java
com.madisp.bad.lib.ui.BadTextWatcher.java
com.madisp.bad.parse.ExprBaseVisitor.java
com.madisp.bad.parse.ExprLexer.java
com.madisp.bad.parse.ExprParser.java
com.madisp.bad.parse.ExprVisitor.java