com.darly.im.ui.CCPFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.darly.im.ui.CCPFragment.java

Source

/*
 *  Copyright (c) 2013 The CCP project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license
 *  that can be found in the LICENSE file in the root of the web site.
 *
 *   http://www.yuntongxun.com
 *
 *  An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
package com.darly.im.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.SpannableString;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.darly.dlclent.R;
import com.darly.im.common.utils.LogUtil;
import com.darly.im.common.view.SwipeBackLayout;
import com.darly.im.common.view.TopBarView;
import com.umeng.analytics.MobclickAgent;

/**
 * ?View?View ?
 * Created by Jorstin on 2015/3/18.
 */
public abstract class CCPFragment extends Fragment {

    private static final String TAG = "ECSDK_Demo.CCPFragment";

    /***/
    private InternalReceiver internalReceiver;
    /**?????*/
    private boolean isFinish = false;
    /**
     * ?ActionBar
     */
    private CCPActivityBase mBaseActivity = new CCPFragmentImpl(this);
    public SwipeBackLayout mSwipeBackLayout;

    final Handler mSupperHandler = new Handler() {

        @Override
        public void dispatchMessage(Message msg) {
            // TODO Auto-generated method stub
            super.dispatchMessage(msg);
        }

    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBaseActivity.init(getActivity().getBaseContext(), getActivity());
        onFragmentInit();
        abstracrRegist();
    }

    public void abstracrRegist() {
        registerReceiver(new String[] { SDKCoreHelper.ACTION_KICK_OFF });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.mBaseActivity.setRootConsumeWatcher(null);
        return mBaseActivity.mBaseLayoutView;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        try {
            // ?
            getActivity().unregisterReceiver(internalReceiver);
        } catch (Exception e) {
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mBaseActivity.onDestroy();
    }

    /**
     * ?
     * @param keyCode
     * @param event
     * @return
     */
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (mBaseActivity.onKeyDown(keyCode, event)) {
            return true;
        }
        if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK) && event.getAction() == KeyEvent.ACTION_DOWN) {
            finish();
            return true;
        }
        return false;
    }

    /**
     * Action????
     * Action
     * @param actionArray
     */
    protected final void registerReceiver(String[] actionArray) {
        if (actionArray == null) {
            return;
        }
        IntentFilter intentfilter = new IntentFilter();
        for (String action : actionArray) {
            intentfilter.addAction(action);
        }
        //intentfilter.addAction(CASIntent.ACTION_SERVICE_DESTORY);
        //intentfilter.addAction(CASIntent.ACTION_FORCE_DEACTIVE);
        if (internalReceiver == null) {
            internalReceiver = new InternalReceiver();
        }
        getActivity().registerReceiver(internalReceiver, intentfilter);
    }

    /**
     * Handler 
     * @return
     */
    public android.os.Handler getSupperHandler() {
        return mSupperHandler;
    }

    public boolean isFinish() {
        return isFinish;
    }

    public void setIsFinish(boolean isFinish) {
        this.isFinish = isFinish;
    }

    /**
     * ??
     */
    public void finish() {
        if (getActivity() == null) {
            return;
        }
        if (isFinish) {
            getActivity().finish();
            return;
        }

        getActivity().getSupportFragmentManager().popBackStack();
    }

    @Override
    public void onResume() {
        long currentTimeMillis = System.currentTimeMillis();
        super.onResume();
        MobclickAgent.onPageStart("MainScreen"); //?
        this.mBaseActivity.onResume();
        LogUtil.d(TAG, "KEVIN MMActivity onPause:" + (System.currentTimeMillis() - currentTimeMillis));
    }

    @Override
    public void onStart() {
        this.mBaseActivity.onStart();
        super.onStart();
    }

    public final void setScreenEnable(boolean screenEnable) {
        this.mBaseActivity.setScreenEnable(screenEnable);
    }

    @Override
    public void onPause() {
        long currentTimeMillis = System.currentTimeMillis();
        super.onPause();
        MobclickAgent.onPageEnd("MainScreen");
        this.mBaseActivity.onPause();
        LogUtil.d(TAG, "KEVIN MMActivity onPause:" + (System.currentTimeMillis() - currentTimeMillis));
    }

    /**
     *    View
     * @param paramInt
     * @return
     */
    public final View findViewById(int paramInt) {
        return getView().findViewById(paramInt);
    }

    /**
     * ????
     * ?
     * @param context
     * @param intent
     */
    protected void handleReceiver(Context context, Intent intent) {
        // ?
    }

    /**
     * ?????ID
     * @return ??ID
     */
    protected abstract int getLayoutId();

    /**
     * ????
     * @return
     */
    public int getTitleLayoutId() {
        return R.layout.ec_title_view_base;
    }

    protected void onFragmentInit() {

    }

    public void onBaseContentViewAttach(View contentView) {

    }

    public TopBarView getTopBarView() {
        return mBaseActivity.getTopBarView();
    }

    /**
     *
     * @param mute
     */
    public SpannableString setNewMessageMute(boolean mute) {
        mBaseActivity.setMute(mute);
        return mBaseActivity.buildActionTitle();
    }

    /**
     * ActionBar
     * @param resid
     */
    public void setActionBarTitle(int resid) {
        mBaseActivity.setActionBarTitle(getString(resid));
    }

    /**
     * ActionBar
     * @param text
     */
    public void setActionBarTitle(CharSequence text) {
        mBaseActivity.setActionBarTitle(text);
    }

    /**
     * ActionBar 
     * @return
     */
    public final CharSequence getActionBarTitle() {
        return mBaseActivity.getActionBarTitle();
    }

    public void toggleSoftInput() {
        mBaseActivity.toggleSoftInput();
    }

    public void hideSoftKeyboard() {
        mBaseActivity.hideSoftKeyboard();
    }

    /**
     * ??
     * @author ?
     * @date 2014-12-4
     * @version 4.0
     */
    private class InternalReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent == null || intent.getAction() == null) {
                return;
            }
            handleReceiver(context, intent);
        }

    }
}