cn.org.eshow.framwork.activity.AbActivity.java Source code

Java tutorial

Introduction

Here is the source code for cn.org.eshow.framwork.activity.AbActivity.java

Source

/*
 * Copyright (C) 2012 www.amsoft.cn
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.org.eshow.framwork.activity;

import android.app.Application;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.lang.reflect.Field;

import cn.org.eshow.framwork.global.AbActivityManager;
import cn.org.eshow.framwork.view.ioc.AbIocEventListener;
import cn.org.eshow.framwork.view.ioc.AbIocSelect;
import cn.org.eshow.framwork.view.ioc.AbIocView;
import cn.org.eshow.framwork.view.titlebar.AbBottomBar;
import cn.org.eshow.framwork.view.titlebar.AbTitleBar;

// TODO: Auto-generated Javadoc
/**
 * ActivityActivity?.
 */

public abstract class AbActivity extends FragmentActivity {

    /** LayoutInflater???. */
    public LayoutInflater mInflater;

    /** Application???. */
    public Application abApplication = null;

    /** . */
    public RelativeLayout ab_base = null;

    /** ?. */
    private AbTitleBar mAbTitleBar = null;

    /** ?. */
    private AbBottomBar mAbBottomBar = null;

    /** . */
    protected RelativeLayout contentLayout = null;

    /**
     * ??.
     *
     * @param savedInstanceState the saved instance state
     * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mInflater = LayoutInflater.from(this);

        //?
        mAbTitleBar = new AbTitleBar(this);

        //
        ab_base = new RelativeLayout(this);
        ab_base.setBackgroundColor(Color.rgb(255, 255, 255));

        //
        contentLayout = new RelativeLayout(this);
        contentLayout.setPadding(0, 0, 0, 0);

        //?
        mAbBottomBar = new AbBottomBar(this);

        //View
        ab_base.addView(mAbTitleBar,
                new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        mAbTitleBar.setVisibility(View.GONE);

        RelativeLayout.LayoutParams layoutParamsBottomBar = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParamsBottomBar.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        ab_base.addView(mAbBottomBar, layoutParamsBottomBar);

        RelativeLayout.LayoutParams layoutParamsContent = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParamsContent.addRule(RelativeLayout.BELOW, mAbTitleBar.getId());
        layoutParamsContent.addRule(RelativeLayout.ABOVE, mAbBottomBar.getId());
        ab_base.addView(contentLayout, layoutParamsContent);

        //Application?
        abApplication = getApplication();

        //ContentView
        setContentView(ab_base,
                new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        AbActivityManager.getInstance().addActivity(this);
    }

    /**
     * ??View?.
     * @param contentView  View
     */
    public void setAbContentView(View contentView) {
        contentLayout.removeAllViews();
        contentLayout.addView(contentView,
                new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        initIocView();
    }

    /**
     * ???IDView?.
     * @param resId  View?ID
     */
    public void setAbContentView(int resId) {
        setAbContentView(mInflater.inflate(resId, null));
    }

    /**
     * ??.
     * @return the title layout
     */
    public AbTitleBar getTitleBar() {
        mAbTitleBar.setVisibility(View.VISIBLE);
        return mAbTitleBar;
    }

    /**
     * ??.
     * @return the bottom layout
     */
    public AbBottomBar getBottomBar() {
        return mAbBottomBar;
    }

    /**
     * ?????.
     *
     * @param overlay the new title bar overlay
     */
    public void setTitleBarOverlay(boolean overlay) {
        ab_base.removeAllViews();
        if (overlay) {
            RelativeLayout.LayoutParams layoutParamsFW1 = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParamsFW1.addRule(RelativeLayout.ABOVE, mAbBottomBar.getId());
            ab_base.addView(contentLayout, layoutParamsFW1);
            RelativeLayout.LayoutParams layoutParamsFW2 = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParamsFW2.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
            ab_base.addView(mAbTitleBar, layoutParamsFW2);

            RelativeLayout.LayoutParams layoutParamsFW3 = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParamsFW3.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            ab_base.addView(mAbBottomBar, layoutParamsFW3);

        } else {
            ab_base.addView(mAbTitleBar,
                    new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

            RelativeLayout.LayoutParams layoutParamsFW2 = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParamsFW2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            ab_base.addView(mAbBottomBar, layoutParamsFW2);

            RelativeLayout.LayoutParams layoutParamsFW1 = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParamsFW1.addRule(RelativeLayout.BELOW, mAbTitleBar.getId());
            layoutParamsFW1.addRule(RelativeLayout.ABOVE, mAbBottomBar.getId());
            ab_base.addView(contentLayout, layoutParamsFW1);
        }
    }

    /**
     * ????.
     *
     * @param layoutResID the new content view
     * @see android.app.Activity#setContentView(int)
     */
    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        initIocView();
    }

    /**
     * ????.
     *
     * @param view the view
     * @param params the params
     * @see android.app.Activity#setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
     */
    @Override
    public void setContentView(View view, android.view.ViewGroup.LayoutParams params) {
        super.setContentView(view, params);
    }

    /**
     * ????.
     *
     * @param view the new content view
     * @see android.app.Activity#setContentView(android.view.View)
     */
    public void setContentView(View view) {
        super.setContentView(view);
    }

    /**
     * ?IOCView.
     */
    private void initIocView() {
        Field[] fields = getClass().getDeclaredFields();
        if (fields != null && fields.length > 0) {
            for (Field field : fields) {
                try {
                    field.setAccessible(true);

                    if (field.get(this) != null)
                        continue;

                    AbIocView viewInject = field.getAnnotation(AbIocView.class);
                    if (viewInject != null) {

                        int viewId = viewInject.id();
                        field.set(this, findViewById(viewId));

                        setListener(field, viewInject.click(),
                                cn.org.eshow.framwork.view.ioc.AbIocEventListener.CLICK);
                        setListener(field, viewInject.longClick(), AbIocEventListener.LONGCLICK);
                        setListener(field, viewInject.itemClick(), AbIocEventListener.ITEMCLICK);
                        setListener(field, viewInject.itemLongClick(), AbIocEventListener.ITEMLONGCLICK);

                        AbIocSelect select = viewInject.select();
                        if (!TextUtils.isEmpty(select.selected())) {
                            setViewSelectListener(field, select.selected(), select.noSelected());
                        }

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * view?.
     *
     * @param field the field
     * @param select the select
     * @param noSelect the no select
     * @throws Exception the exception
     */
    private void setViewSelectListener(Field field, String select, String noSelect) throws Exception {
        Object obj = field.get(this);
        if (obj instanceof View) {
            ((AbsListView) obj)
                    .setOnItemSelectedListener(new AbIocEventListener(this).select(select).noSelect(noSelect));
        }
    }

    /**
     * view?.
     *
     * @param field the field
     * @param methodName the method name
     * @param method the method
     * @throws Exception the exception
     */
    private void setListener(Field field, String methodName, int method) throws Exception {
        if (methodName == null || methodName.trim().length() == 0)
            return;

        Object obj = field.get(this);

        switch (method) {
        case AbIocEventListener.CLICK:
            if (obj instanceof View) {
                ((View) obj).setOnClickListener(new AbIocEventListener(this).click(methodName));
            }
            break;
        case AbIocEventListener.ITEMCLICK:
            if (obj instanceof AbsListView) {
                ((AbsListView) obj).setOnItemClickListener(new AbIocEventListener(this).itemClick(methodName));
            }
            break;
        case AbIocEventListener.LONGCLICK:
            if (obj instanceof View) {
                ((View) obj).setOnLongClickListener(new AbIocEventListener(this).longClick(methodName));
            }
            break;
        case AbIocEventListener.ITEMLONGCLICK:
            if (obj instanceof AbsListView) {
                ((AbsListView) obj)
                        .setOnItemLongClickListener(new AbIocEventListener(this).itemLongClick(methodName));
            }
            break;
        default:
            break;
        }
    }

    /**
     * 
     * @param view
     */
    public void back(View view) {
        finish();
    }

    /**
     * ??Activity?.
     *
     * @see android.app.Activity#finish()
     */
    @Override
    public void finish() {
        AbActivityManager.getInstance().removeActivity(this);
        super.finish();
    }

    /**
     * Toast short ??
     *
     * @param content
     */
    public void showShortToast(Context mContext, String content) {
        if (null != mContext && null != content && content.length() > 1) {
            Toast.makeText(mContext, content, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * Toast long ??
     *
     * @param content
     */
    public void showLongToast(Context mContext, String content) {
        Toast.makeText(mContext, content, Toast.LENGTH_LONG).show();
    }
}