zuo.biao.library.base.BaseFragmentActivity.java Source code

Java tutorial

Introduction

Here is the source code for zuo.biao.library.base.BaseFragmentActivity.java

Source

/*Copyright 2015 TommyLemon(https://github.com/TommyLemon)
    
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 zuo.biao.library.base;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import zuo.biao.library.R;
import zuo.biao.library.interfaces.OnPageReturnListener;
import zuo.biao.library.ui.EditTextManager;

/**android.support.v4.app.FragmentActivity?? ?   ;FragmentActivity
 * @author Lemon
 * @use extends BaseActivity
 */
public abstract class BaseFragmentActivity extends FragmentActivity implements OnGestureListener, OnTouchListener {
    private static final String TAG = "BaseFragmentActivity";

    protected View view = null;//activity?View?contentView
    protected BaseFragmentActivity context = null;//onCreate??Activity
    protected FragmentManager fragmentManager = null;//onCreate??Activity
    protected boolean isActivityAlive = false;//FragmentActivity??onCreatetrue??Activity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //setContentView?ActivityonCreate

        gestureDetector = new GestureDetector(this, this);//??
    }

    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    private OnPageReturnListener onPageReturnListener;
    private GestureDetector gestureDetector;

    public void setContentView(int layoutResID, OnPageReturnListener listener) {
        super.setContentView(layoutResID);

        onPageReturnListener = listener;
        view = LayoutInflater.from(this).inflate(layoutResID, null);
        view.setOnTouchListener(this);
    };
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    public static final String INTENT_TITLE = "INTENT_TITLE";

    protected Intent intent = null;//? activity?activity????
    protected int enterAnim = R.anim.fade;//??,?finish();????
    protected int exitAnim = R.anim.right_push_out;//?,?finish();????

    protected ProgressDialog progressDialog = null;//
    protected View toGetWindowTokenView = null;//activity????finish?

    protected List<Handler> handlerList = new ArrayList<Handler>();
    protected List<Runnable> runnableList = new ArrayList<Runnable>();

    public abstract void initView();//UIsetContentView?

    public abstract void initData();//data?setContentView?

    public abstract void initListener();//listener?setContentView?

    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    /**?,
     * @param stringResId
     */
    public void showProgressDialog(int stringResId) {
        try {
            showProgressDialog(null, context.getResources().getString(stringResId));
        } catch (Exception e) {
            Log.e(TAG,
                    "showProgressDialog  showProgressDialog(null, context.getResources().getString(stringResId));");
        }
    }

    /**?,
     * @param dialogMessage
     */
    public void showProgressDialog(String dialogMessage) {
        showProgressDialog(null, dialogMessage);
    }

    /**?
     * @param dialogTitle 
     * @param dialogMessage ?
     */
    public void showProgressDialog(final String dialogTitle, final String dialogMessage) {
        if (isActivityAlive == false) {
            return;
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                if (progressDialog == null) {
                    progressDialog = new ProgressDialog(context);
                }
                if (progressDialog.isShowing() == true) {
                    progressDialog.dismiss();
                }
                if (dialogTitle != null && !"".equals(dialogTitle.trim())) {
                    progressDialog.setTitle(dialogTitle);
                }
                if (dialogMessage != null && !"".equals(dialogMessage.trim())) {
                    progressDialog.setMessage(dialogMessage);
                }
                progressDialog.setCanceledOnTouchOutside(false);
                progressDialog.show();
            }
        });
    }

    /** ??
     */
    public void dismissProgressDialog() {
        if (isActivityAlive && progressDialog != null && progressDialog.isShowing() == true) {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    progressDialog.dismiss();
                }
            });
        }
    }
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //?Activity<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**Activity?
     * @param intent
     */
    public void toActivity(final Intent intent) {
        toActivity(intent, true);
    }

    /**Activity
     * @param intent
     * @param showAnimation
     */
    public void toActivity(final Intent intent, final boolean showAnimation) {
        toActivity(intent, -1, showAnimation);
    }

    /**Activity?
     * @param intent
     * @param requestCode
     */
    public void toActivity(final Intent intent, final int requestCode) {
        toActivity(intent, requestCode, true);
    }

    /**Activity
     * @param intent
     * @param requestCode
     * @param showAnimation
     */
    public void toActivity(final Intent intent, final int requestCode, final boolean showAnimation) {
        if (isActivityAlive == false) {
            return;
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                if (requestCode < 0) {
                    startActivity(intent);
                } else {
                    startActivityForResult(intent, requestCode);
                }
                if (showAnimation) {
                    overridePendingTransition(R.anim.right_push_in, R.anim.hold);
                } else {
                    overridePendingTransition(R.anim.null_anim, R.anim.null_anim);
                }
            }
        });
    }
    //?Activity>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //show short toast <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    /**?short toast?long toast Toast.makeText(string, Toast.LENGTH_LONG).show(); ---??
     * @param stringResId
     */
    public void showShortToast(int stringResId) {
        try {
            showShortToast(context.getResources().getString(stringResId));
        } catch (Exception e) {
            Log.e(TAG, "showShortToast  context.getResources().getString(resId) >>  catch (Exception e) {"
                    + e.getMessage());
        }
    }

    /**?short toast?long toast Toast.makeText(string, Toast.LENGTH_LONG).show(); ---??
     * @param string
     */
    public void showShortToast(final String string) {
        showShortToast(string, false);
    }

    /**?short toast?long toast Toast.makeText(string, Toast.LENGTH_LONG).show(); ---??
     * @param string
     * @param isForceDismissProgressDialog
     */
    public void showShortToast(final String string, final boolean isForceDismissProgressDialog) {
        if (isActivityAlive == false) {
            return;
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (isForceDismissProgressDialog == true) {
                    dismissProgressDialog();
                }

                Toast.makeText(context, "" + string, Toast.LENGTH_SHORT).show();
            }
        });
    }
    //show short toast >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    /**?
     * @param threadName
     * @param runnable
     * @return
     */
    public Handler runThread(String threadName, Runnable runnable) {
        if (runnable == null) {
            Log.e(TAG, "runThread  runnable == null >> return");
            return null;
        }
        HandlerThread handlerThread = new HandlerThread("" + threadName);
        handlerThread.start();//HandlerThread?
        Handler handler = new Handler(handlerThread.getLooper());//HandlerThreadlooperHandler
        handler.post(runnable);//postHandler

        handlerList.add(handler);
        runnableList.add(runnable);

        return handler;
    }

    public void onPageReturn() {
        finish();
    }

    @Override
    public void finish() {
        super.finish();//??
        //?????super.finish();??<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        if (isActivityAlive == false) {
            return;
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                if (toGetWindowTokenView != null) {
                    EditTextManager.hideKeyboard(context, toGetWindowTokenView);
                }
                if (enterAnim > 0 && exitAnim > 0) {
                    try {
                        overridePendingTransition(enterAnim, exitAnim);
                    } catch (Exception e) {
                        Log.e(TAG,
                                "finish overridePendingTransition(enterAnim, exitAnim); >> catch (Exception e) {  "
                                        + e.getMessage());
                    }
                }
            }
        });
        //?????super.finish();??>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //?????super.onDestroy();??<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        dismissProgressDialog();
        isActivityAlive = false;
        if (handlerList != null && runnableList != null) {
            for (int i = 0; i < handlerList.size(); i++) {
                try {
                    (handlerList.get(i)).removeCallbacks(runnableList.get(i));
                } catch (Exception e) {
                    Log.e(TAG,
                            "onDestroy try { handler.removeCallbacks(runnable);...  >> catch  : " + e.getMessage());
                }
            }
        }
        //?????super.onDestroy();??>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }

    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    private boolean isOnKeyLongPress = false;

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        isOnKeyLongPress = true;
        return true;
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (isOnKeyLongPress) {
            isOnKeyLongPress = false;
            return true;
        }

        switch (keyCode) {
        case KeyEvent.KEYCODE_BACK:
            if (onPageReturnListener != null) {
                onPageReturnListener.onPageReturn();
                return true;
            }
            break;
        default:
            break;
        }

        return super.onKeyUp(keyCode, event);
    }

    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

        if (onPageReturnListener != null) {

            float maxDragHeight = getResources().getDimension(R.dimen.page_drag_max_height);
            float distanceY = e2.getRawY() - e1.getRawY();
            if (distanceY < maxDragHeight && distanceY > -maxDragHeight) {

                float minDragWidth = getResources().getDimension(R.dimen.page_drag_min_width);
                float distanceX = e2.getRawX() - e1.getRawX();
                if (distanceX > minDragWidth) {
                    onPageReturnListener.onPageReturn();
                    return true;
                }
            }
        }

        return false;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        gestureDetector.onTouchEvent(ev);
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

}