com.bruce.study.demo.base.BaseFragmentActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.bruce.study.demo.base.BaseFragmentActivity.java

Source

/*
 * BruceHurrican
 * Copyright (c) 2016.
 *  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.
 *
 *    This document is Bruce's individual learning the android demo, wherein the use of the code from the Internet, only to use as a learning exchanges.
 *   And where any person can download and use, but not for commercial purposes.
 *   Author does not assume the resulting corresponding disputes.
 *   If you have good suggestions for the code, you can contact BurrceHurrican@foxmail.com
 *   Bruce'sandroiddemo, ?????
 *   ?, ?
 *   ??
 *   ???BurrceHurrican@foxmail.com
 */

package com.bruce.study.demo.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.*;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;
import com.bruce.study.demo.DemoApplication;
import com.bruce.study.demo.R;
import com.bruce.study.demo.log.Logs;

import java.lang.ref.WeakReference;

/**
 * FragmentActivity
 * Created by BruceHurrican on 2015/9/13.
 */
public abstract class BaseFragmentActivity extends FragmentActivity {
    private final String TAG = getTAG();
    private Context context;
    private String logsTag;
    private DemoApplication application;
    /**
     * ?
     */
    private ProgressDialog pd_waiting;
    private UIHandler mUIHandler;
    private WorkerHandler mWorkerHandler;
    private HandlerThread mHandlerThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = BaseFragmentActivity.this;
        //        TAG = getTAG();
        logsTag = getLocalClassName() + "-->";
        application = (DemoApplication) getApplication();
        application.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        application.delActivity(this);
        super.onDestroy();
    }

    public abstract String getTAG();

    public final void logV(String text) {
        Logs.v(logsTag, text);
    }

    public final void logD(String text) {
        Logs.d(logsTag, text);
    }

    public final void logI(String text) {
        Logs.i(logsTag, text);
    }

    public final void logW(String text) {
        Logs.w(logsTag, text);
    }

    public final void logE(String text) {
        Logs.e(logsTag, text);
    }

    public void showToastShort(String text) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (null != context) {
                    Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
                } else {
                    Logs.e(TAG, "?");
                }
            }
        });
    }

    public void showToastLong(String text) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (null != context) {
                    Toast.makeText(context, text, Toast.LENGTH_LONG).show();
                } else {
                    Logs.e(TAG, "print log error");
                }
            }
        });
    }

    /**
     * @param msg ???
     * @return
     */
    public ProgressDialog initProgressDialog(String msg) {
        pd_waiting = new ProgressDialog(this);
        pd_waiting.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pd_waiting.setTitle("??");
        pd_waiting.setMessage(msg);
        pd_waiting.setIcon(R.drawable.icon_workdemo);
        pd_waiting.setIndeterminate(false);
        pd_waiting.setCancelable(false);
        pd_waiting.setCanceledOnTouchOutside(false);
        return pd_waiting;
    }

    public void showProgressDialog() {
        if (null != pd_waiting) {
            pd_waiting.show();
        } else {
            logE("--pd_waiting->" + pd_waiting);
        }
    }

    public void cancelProgressDialog() {
        if (null != pd_waiting) {
            getUIHandler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    pd_waiting.cancel();
                }
            }, 2000);
        } else {
            logE("--pd_waiting->" + pd_waiting);
        }
    }

    /**
     * ?sendUIMessage??initUIHandler??mUIHandler
     */
    public void initUIHandler() {
        if (null == mUIHandler) {
            mUIHandler = new UIHandler(this);
        }
    }

    /**
     * UIHandlerhandler
     *
     * @return
     */
    public UIHandler getUIHandler() {
        if (null == mUIHandler) {
            logE("UIHandler ");
        }
        return mUIHandler;
    }

    /**
     * ?UIHandler????
     *
     * @param msg
     */
    public void handleUIMessage(Message msg) {
        // super ?????
    }

    // ????sendUIMessage?handler???getUIHandler??
    public void sendUIMessage(Message msg) {
        if (null != mUIHandler) {
            mUIHandler.sendMessage(msg);
        } else {
            uiHandlerNotInit();
        }
    }

    public void sendUIMessageEmpty(int what) {
        sendUIMessageEmptyDelayed(what, 0);
    }

    public void sendUIMessageEmptyDelayed(int what, long delayMillis) {
        if (null != mUIHandler) {
            mUIHandler.sendEmptyMessageDelayed(what, delayMillis);
        } else {
            uiHandlerNotInit();
        }
    }

    /**
     * ?onDestroy???
     */
    public void recycleUIHandler() {
        if (null != mUIHandler) {
            mUIHandler.removeCallbacksAndMessages(null);
        }
    }

    /**
     * ?handleWorkerMessage??initWorkerHandler??mWorkerHandlermHandlerThread
     *
     * @param name
     */
    public void initWorkerHandler(String name) {
        if (mHandlerThread == null && mWorkerHandler == null) {
            mHandlerThread = new HandlerThread(name);
            mHandlerThread.start();
            mWorkerHandler = new WorkerHandler(mHandlerThread.getLooper(), this);
        } else {
            logE("initWorkerHandler is called ,don't called again!");
        }
    }

    public void initWorkerHandler() {
        initWorkerHandler("workThread");
    }

    /**
     * UIHandler ?
     */
    public void uiHandlerNotInit() {
        showToastShort("UIHandler ?");
        logE("UIHandler ?");
    }

    /**
     * mWorkerHandler,?WorkerHandler
     *
     * @return
     */
    public WorkerHandler getWorkerHandler() {
        if (null == mWorkerHandler) {
            logE("?WorkerHandler");
        }
        return mWorkerHandler;
    }

    /**
     * ?WorkerHandler????
     *
     * @param msg
     */
    public void handleWorkerMessage(Message msg) {
        // super ?????
    }

    // ????sendWorkerMessage?handler???getmUIHandler??
    public void sendWorkderMessage(Message msg) {
        if (null != mWorkerHandler) {
            mWorkerHandler.sendMessage(msg);
        } else {
            workerHandlerNotInit();
        }
    }

    public void sendWorkerMessageEmpty(int what) {
        if (null != mWorkerHandler) {
            mWorkerHandler.sendEmptyMessage(what);
        } else {
            workerHandlerNotInit();
        }
    }

    /**
     * WorkerHandler ?
     */
    private void workerHandlerNotInit() {
        showToastShort("WorkerHandler ?");
        logE("WorkerHandler ?");
    }

    public void recycleWorkerHandler() {
        if (null != mHandlerThread && null != mWorkerHandler) {
            mHandlerThread.quit();
            mWorkerHandler.removeCallbacksAndMessages(null);
        }
    }

    public static class UIHandler extends Handler {
        WeakReference<BaseFragmentActivity> weakReference;

        /**
         *  Handler ????Handler ????? Handler  TLS(Thread Local Storage)?? Activity ?
         * Handler  Activity 
         *
         * @param activity
         */
        public UIHandler(BaseFragmentActivity activity) {
            super(Looper.getMainLooper());
            this.weakReference = new WeakReference<BaseFragmentActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            final BaseFragmentActivity activity = weakReference.get();
            if (null != activity) {
                activity.handleUIMessage(msg);
            }
        }
    }

    /**
     * Handler??AsyncTask???
     */
    public static class WorkerHandler extends Handler {
        WeakReference<BaseFragmentActivity> weakReference;

        public WorkerHandler(Looper looper, BaseFragmentActivity activity) {
            super(looper);
            this.weakReference = new WeakReference<BaseFragmentActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            final BaseFragmentActivity activity = weakReference.get();
            if (null != activity) {
                activity.handleWorkerMessage(msg);
            }
        }
    }
}