Android Open Source - ixming-android-inject Themed Injector Utils






From Project

Back to project page ixming-android-inject.

License

The source code is released under:

Apache License

If you think the Android project ixming-android-inject 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.ixming.android.inject.themed;
/*  w  w w . j  a va 2  s  .  co m*/
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.ixming.android.inject.core.BaseInjectLoader;
import org.ixming.android.inject.core.InjectLoaderFactory;

import android.app.Activity;
import android.content.Context;
import android.view.View;

/**
 * ????{@link org.ixming.android.inject}?????
 * <p>
 * ?????????????????????????????????????????
 * ??????????????????????????????...??
 * <br/>
 * ?????????????????????????????????????????
 * </p>
 * @author Yin Yong
 * @version 1.0
 */
public class ThemedInjectorUtils {

  final String TAG = ThemedInjectorUtils.class.getSimpleName();

  private static ThemedInjectorUtils mSingleton = null;
  
  /**
   * ??????????????????????????????????????ThemedInjectConfigure??
   */
  public static synchronized ThemedInjectorUtils buildAsSingleton(ThemedInjectConfigure configure) {
    if (null == mSingleton) {
      mSingleton = new ThemedInjectorUtils(configure);
    }
    return mSingleton;
  }
  /**
   * ?????????????
   * 
   * <p>
   * Tips:?????{@link ThemedInjectorUtils#buildAsSingleton(ThemedInjectConfigure)}???????????
   * ?????????<br/>
   * ????????????????????????
   * </p>
   */
  public static ThemedInjectorUtils defaultInstance() {
    return new ThemedInjectorUtils();
  }

  /**
   * ???????????configure????????????
   * 
   * <p>
   * <strong><i>Tips:</i></strong><br/>
   * ?????{@link ThemedInjectorUtils#buildAsSingleton(ThemedInjectConfigure)}???????????
   * ?????????<br/>
   * ?????????????????????????configure is ignored?
   * </p>
   */
  public static ThemedInjectorUtils instanceBuildFrom(
      ThemedInjectConfigure configure) {
    return new ThemedInjectorUtils(configure);
  }

  private final ThemedInjectConfigure mConfigure;

  protected ThemedInjectorUtils() {
    this(new ThemedInjectConfigure());
  }

  protected ThemedInjectorUtils(ThemedInjectConfigure configure) {
    mConfigure = configure;
  }

  /**
   * ?????Activity????????
   * 
   * @param context
   *            ???????????????????Context
   * @param themedContext
   *            ???????Context
   */
  public void inject(Activity context, Context themedContext) {
    inject(context, context.getApplicationContext(), themedContext);
  }

  /**
   * ?????Activity????????
   * 
   * @param context
   *            ???????????
   * @param localContext
   *            ??????Context
   * @param themedContext
   *            ???????Context
   */
  public void inject(Activity context, Context localContext,
      Context themedContext) {
    BaseInjectLoader baseLoader = InjectLoaderFactory.createFrom(context,
        localContext, themedContext);
    innerInject(context, baseLoader);
  }

  /**
   * ????????View??????????????????????
   * <p>
   * ?????{@link #inject(View, Context)}??View.getContext()??????????
   * </p>
   * 
   * <p>
   * ???????????View???Context???????{@link #inject(View, Context)}
   * </p>
   * 
   * @param view
   *            ???????????????Context
   * @param themedContext
   *            ???????Context
   * 
   * @see {@link #inject(View, Context)}
   */
  public void inject(View view, Context themedContext) {
    inject(view, view.getContext(), themedContext);
  }

  /**
   * ????????View??????????????????????
   * <p>
   * ?????{@link #inject(View, Context)}??View.getContext()??????????
   * </p>
   * 
   * <p>
   * ???????????View???Context???????{@link #inject(View, Context)}
   * </p>
   * 
   * @param view
   *            ???????????
   * @param localContext
   *            ??????Context
   * @param themedContext
   *            ???????Context
   * 
   * @see {@link #inject(View, Context)}
   */
  public void inject(View view, Context localContext, Context themedContext) {
    BaseInjectLoader baseLoader = InjectLoaderFactory.createFrom(view,
        localContext, themedContext);
    innerInject(view, baseLoader);
  }

  /**
   * ??????????????????????????Activity??View????????Fragment
   * 
   * @param target
   *            ?????????????????
   * @param contentView
   *            ????????View???????????Context??
   * @param themedContext
   *            ???????????Context??
   */
  public void inject(Object target, View contentView, Context themedContext) {
    inject(target, contentView, contentView.getContext(), themedContext);
  }

  /**
   * ??????????????????????????Activity??View????????Fragment
   * 
   * @param target
   *            ?????????????????
   * @param contentView
   *            ????????View
   * @param localContext
   *            ?????????Context??
   * @param themedContext
   *            ???????????Context??
   */
  public void inject(Object target, View contentView, Context localContext,
      Context themedContext) {
    BaseInjectLoader baseLoader = InjectLoaderFactory.createFrom(
        contentView, localContext, themedContext);
    innerInject(target, baseLoader);
  }

  private void innerInject(Object target, BaseInjectLoader baseLoader) {
    boolean isInjectReses = mConfigure.isInjectReses();
    boolean isInjectViews = mConfigure.isInjectViews();
    boolean isInjectOnClickMethods = mConfigure.isInjectOnClickMethods();
    // inject object
    if (isInjectReses || isInjectViews) Inject1: {
      Field[] fields = target.getClass().getDeclaredFields();
      if (null == fields || fields.length == 0) {
        break Inject1;
      }
      for (Field field : fields) {
        if (isInjectReses && baseLoader.injectThemedRes(target, field)) {
          continue;
        }
        // next
        if (isInjectViews && baseLoader.injectView(target, field)) {
          continue;
        }
        // to be continued
      }
    }
    
    if (isInjectOnClickMethods) Inject2: {
      Method[] methods = target.getClass().getDeclaredMethods();
      if (null == methods || methods.length == 0) {
        break Inject2;
      }
      for (Method method : methods) {
        if (baseLoader.injectOnClickMethodListener(target, method)) {
          continue;
        }
      }
    }
    
    // force GC
    System.gc();
  }
}




Java Source Code List

org.ixming.android.MainActivity.java
org.ixming.android.inject.InjectConfigure.java
org.ixming.android.inject.InjectorUtils.java
org.ixming.android.inject.ResTargetType.java
org.ixming.android.inject.annotation.OnClickMethodInject.java
org.ixming.android.inject.annotation.ResInject.java
org.ixming.android.inject.annotation.ViewInject.java
org.ixming.android.inject.annotation.ViewOnClickInject.java
org.ixming.android.inject.annotation.package-info.java
org.ixming.android.inject.core.ActivityInjectLoader.java
org.ixming.android.inject.core.BaseInjectLoader.java
org.ixming.android.inject.core.IOnClickListenerInjector.java
org.ixming.android.inject.core.IResInjector.java
org.ixming.android.inject.core.IViewFinder.java
org.ixming.android.inject.core.IViewInjector.java
org.ixming.android.inject.core.InjectLoaderFactory.java
org.ixming.android.inject.core.ResLoader.java
org.ixming.android.inject.core.ViewInjectLoader.java
org.ixming.android.inject.core.WrappedViewFinder.java
org.ixming.android.inject.themed.ThemedInjectConfigure.java
org.ixming.android.inject.themed.ThemedInjectorUtils.java
org.ixming.android.inject.themed.ThemedResInject.java
org.ixming.android.inject.themed.package-info.java
org.ixming.android.inject.package-info.java