Android Open Source - mobilib Mbl Activity Plugin






From Project

Back to project page mobilib.

License

The source code is released under:

MIT License

If you think the Android project mobilib 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 com.datdo.mobilib.base;
//from w ww.  j  a va2s  . c om
import com.datdo.mobilib.event.MblCommonEvents;
import com.datdo.mobilib.event.MblEventCenter;
import com.datdo.mobilib.event.MblEventListener;
import com.datdo.mobilib.util.MblUtils;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;

/**
 * <pre>
 * Plug an object of this class into an activity to make this library works.
 * </pre>
 */
public class MblActivityPlugin {

    // current status
    private int mOrientation;

    // wrapper views
    private MblDecorView mDecorView;

    // for background/foreground detecting
    private static long sLastOnPause = 0;
    private static Runnable sBackgroundStatusCheckTask = new Runnable() {
        @Override
        public void run() {
            MblEventCenter.postEvent(this, MblCommonEvents.GO_TO_BACKGROUND);
        }
    };
    private static final long DEFAULT_MAX_ALLOWED_TRASITION_BETWEEN_ACTIVITY = 2000;
    protected long mMaxAllowedTrasitionBetweenActivity = DEFAULT_MAX_ALLOWED_TRASITION_BETWEEN_ACTIVITY;

    /**
     * Extends Activity#onCreate(Bundle)
     * @param activity targeted activity
     * @param savedInstanceState
     */
    public void onCreate(Activity activity, Bundle savedInstanceState) {
        Context context = MblUtils.getCurrentContext();
        if (context == null || !(context instanceof Activity)) {
            MblUtils.setCurrentContext(activity);
        }

        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        mOrientation = activity.getResources().getConfiguration().orientation;
    }

    /**
     * Extends Activity#onResume()
     * @param activity targeted activity
     */
    public void onResume(Activity activity) {
        MblUtils.setCurrentContext(activity);

        MblUtils.getMainThreadHandler().removeCallbacks(sBackgroundStatusCheckTask);
        long now = getNow();
        if (now - sLastOnPause > mMaxAllowedTrasitionBetweenActivity) {
            MblEventCenter.postEvent(this, MblCommonEvents.GO_TO_FOREGROUND);
        }

        MblEventCenter.postEvent(this, MblCommonEvents.ACTIVITY_RESUMED, activity);
    }

    /**
     * Extends Activity#onPause()
     */
    public void onPause() {
        MblUtils.hideKeyboard();

        sLastOnPause = getNow();
        MblUtils.getMainThreadHandler().postDelayed(sBackgroundStatusCheckTask, mMaxAllowedTrasitionBetweenActivity);
    }

    /**
     * Extends Activity#onConfigurationChanged(Configuration)
     * @param activity targeted activity
     * @param newConfig
     */
    public void onConfigurationChanged(Activity activity, Configuration newConfig) {
        if (newConfig.orientation != mOrientation) {
            mOrientation = newConfig.orientation;
            waitForWindowOrientationReallyChanged(activity, new Runnable() {
                @Override
                public void run() {
                    MblEventCenter.postEvent(this, MblCommonEvents.ORIENTATION_CHANGED);
                }
            });
        }
    }

    private void waitForWindowOrientationReallyChanged(final Activity activity, final Runnable callback) {
        if (MblUtils.isPortraitDisplay() != isPortraitWindow(activity)) {
            MblUtils.getMainThreadHandler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    waitForWindowOrientationReallyChanged(activity, callback);
                }
            }, 10);
        } else {
            callback.run();
        }
    }

    public boolean isPortraitWindow(Activity activity) {
        View root = activity.getWindow().getDecorView();
        return root.getWidth() <= root.getHeight();
    }

    private long getNow() {
        return System.currentTimeMillis();
    }

    /**
     * Determine whether targeted activity is top activity in current task
     * @param activity
     * @return
     */
    public boolean isTopActivity(Activity activity) {
        return MblUtils.getCurrentContext() == activity;
    }

    private View createDecorViewAndAddContent(Activity activity, int layoutResId, LayoutParams params) {
        View content = activity.getLayoutInflater().inflate(layoutResId, null);
        return createDecorViewAndAddContent(activity, content, params);
    }

    private View createDecorViewAndAddContent(Activity activity, View layout, LayoutParams params) {
        if (params == null) {
            params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        }
        layout.setLayoutParams(params);
        MblDecorView decorView = new MblDecorView(activity);
        decorView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        decorView.addView(layout);

        mDecorView = decorView;

        return decorView;
    }

    /**
     * Create wrapping content view for {@link Activity#setContentView(int)}
     * @param activity targeted activity
     * @param layoutResID
     * @return wrapping content view
     */
    public View getContentView(Activity activity, int layoutResID) {
        return createDecorViewAndAddContent(activity, layoutResID, null);
    }

    /**
     * Create wrapping content view for {@link Activity#setContentView(View)}
     * @param activity targeted activity
     * @param view
     * @return wrapping content view
     */
    public View getContentView(Activity activity, View view) {
        return createDecorViewAndAddContent(activity, view, null);
    }

    /**
     * Create wrapping content view for {@link Activity#setContentView(View, LayoutParams)}
     * @param activity targeted activity
     * @param view
     * @param params
     * @return wrapping content view
     */
    public View getContentView(Activity activity, View view, LayoutParams params) {
        return createDecorViewAndAddContent(activity, view, params);
    }

    /**
     * Extends Activity#onDestroy()
     * @param activity targeted activity
     */
    public void onDestroy(Activity activity) {
        if (activity instanceof MblEventListener) {
            MblEventCenter.removeListenerFromAllEvents((MblEventListener) activity);
        }
    }

    /**
     * Get root view of activity
     * @param activity targeted activity
     * @return root view
     */
    public MblDecorView getDecorView(Activity activity) {
        return mDecorView;
    }

    /**
     * <pre>
     * Change max-allowed-transition-between-activities (abbreviation MATBA). Default value is 2 seconds.
     * This will effect {@link MblCommonEvents#GO_TO_BACKGROUND} and {@link MblCommonEvents#GO_TO_FOREGROUND}
     * 
     * The following is how this library detect changes between background (BG) and foreground (FG):
     * 1) Transition between activity A and activity B:
     *        A#onPaused() at t1 ------> B#onResumed() at t2
     *    In this case, t2-t1 < MTBA. FG status is not changed
     * 2) Go to BG
     *        A#onPaused() at t1 ------> Home screen ------> t2
     *    At t2 where t2-t1 > MTBA, FG status is changed to BG. {@link MblCommonEvents#GO_TO_BACKGROUND} event is fired
     * 3) Go back to FG
     *        A#onPaused() at t1 ------> Home screen ------> t2 (BG) ------> A#onResumed() at t3
     *    At t3 where t3-t1 > MTBA, BG status is changed to FG {@link MblCommonEvents#GO_TO_FOREGROUND} event is fired
     * </pre>
     * @param activity targeted activity
     * @param duration
     */
    public void setMaxAllowedTrasitionBetweenActivity(Activity activity, long duration) {
        mMaxAllowedTrasitionBetweenActivity = duration;
    }

    /**
     * @see #setMaxAllowedTrasitionBetweenActivity(Activity, long)
     */
    public void resetDefaultMaxAllowedTrasitionBetweenActivity(Activity activity) {
        mMaxAllowedTrasitionBetweenActivity = DEFAULT_MAX_ALLOWED_TRASITION_BETWEEN_ACTIVITY;
    }
}




Java Source Code List

com.datdo.mobilib.api.DBBase.java
com.datdo.mobilib.api.DBHelper.java
com.datdo.mobilib.api.MblApi.java
com.datdo.mobilib.api.MblCache.java
com.datdo.mobilib.api.MblException.java
com.datdo.mobilib.api.MblSSLCertificateUtils.java
com.datdo.mobilib.base.MblActivityPlugin.java
com.datdo.mobilib.base.MblBaseActionBarActivity.java
com.datdo.mobilib.base.MblBaseActivity.java
com.datdo.mobilib.base.MblBaseAdapter.java
com.datdo.mobilib.base.MblBaseApplication.java
com.datdo.mobilib.base.MblBaseFragmentActivity.java
com.datdo.mobilib.base.MblDecorView.java
com.datdo.mobilib.base.MblNetworkStatusChangedReceiver.java
com.datdo.mobilib.event.MblCommonEvents.java
com.datdo.mobilib.event.MblEventCenter.java
com.datdo.mobilib.event.MblEventListener.java
com.datdo.mobilib.event.MblStrongEventListener.java
com.datdo.mobilib.event.MblWeakArrayList.java
com.datdo.mobilib.imageinput.MblAutoResizeSquareImageView.java
com.datdo.mobilib.imageinput.MblDataInputActivity.java
com.datdo.mobilib.imageinput.MblImageFolderScanner.java
com.datdo.mobilib.imageinput.MblImageInput.java
com.datdo.mobilib.imageinput.MblImagePickingScanEngine.java
com.datdo.mobilib.imageinput.MblPickImageActivity.java
com.datdo.mobilib.imageinput.MblPickImageGridViewAdapter.java
com.datdo.mobilib.imageinput.MblTakeImageActivity.java
com.datdo.mobilib.util.MblAsyncTask.java
com.datdo.mobilib.util.MblImageLoader.java
com.datdo.mobilib.util.MblLinkMovementMethod.java
com.datdo.mobilib.util.MblUrlRecognizer.java
com.datdo.mobilib.util.MblUtils.java
com.datdo.mobilib.widget.MblHorizontalViewPager.java
com.datdo.mobilib.widget.MblListViewWithScrollableItems.java
com.datdo.mobilib.widget.MblSequenceImage.java
com.datdo.mobilib.widget.MblSideMenuEnabledLayout.java
com.datdo.mobilib.widget.MblTouchImageView.java