cn.bingoogolapple.scaffolding.util.UMAnalyticsUtil.java Source code

Java tutorial

Introduction

Here is the source code for cn.bingoogolapple.scaffolding.util.UMAnalyticsUtil.java

Source

/**
 * Copyright 2016 bingoogolapple
 * <p/>
 * 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
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * 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.bingoogolapple.scaffolding.util;

import android.app.Activity;
import android.support.v4.app.Fragment;

import com.umeng.analytics.MobclickAgent;

/**
 * : :bingoogolapple@gmail.com
 * :16/11/3 ?11:13
 * ??:? SDK 
 */
public class UMAnalyticsUtil {
    /**
     * ???
     */
    private static boolean sIsInit = false;

    private UMAnalyticsUtil() {
    }

    /**
     * ?? SDK Application  onCreate 
     *
     * @param appKey    ? AppKey
     * @param channelId ??
     */
    public static void initSdk(String appKey, String channelId) {
        initSdk(new MobclickAgent.UMAnalyticsConfig(AppManager.getApp(), appKey, channelId));
    }

    /**
     * ?? SDK Application  onCreate 
     *
     * @param analyticsConfig
     */
    public static void initSdk(MobclickAgent.UMAnalyticsConfig analyticsConfig) {
        sIsInit = true;

        MobclickAgent.startWithConfigure(analyticsConfig);
        MobclickAgent.setDebugMode(AppManager.getInstance().isBuildDebug());
        // ?????Activity
        MobclickAgent.openActivityDurationTrack(false);
        MobclickAgent.setSessionContinueMillis(AppManager.getInstance().isBuildDebug() ? 3000 : 30000);
    }

    /**
     * ?Process.killSystem.exit??MobclickAgent.onKillProcess(Context context)???
     */
    public static void onKillProcess() {
        if (sIsInit) {
            MobclickAgent.onKillProcess(AppManager.getApp());
        }
    }

    // ======================== ? START ========================

    /**
     *  MvcFragment  handleOnVisibilityChangedToUser 
     *
     * @param fragment
     * @param isVisibleToUser
     */
    public static void onVisibilityChangedToUser(Fragment fragment, boolean isVisibleToUser) {
        if (sIsInit) {
            // ?
            if (isVisibleToUser) {
                MobclickAgent.onPageStart(fragment.getClass().getSimpleName());
            } else {
                MobclickAgent.onPageEnd(fragment.getClass().getSimpleName());
            }
        }
    }

    /**
     *  ActivityLifecycleCallbacks  onActivityResumed 
     *
     * @param activity
     */
    public static void onActivityResumed(Activity activity) {
        if (sIsInit) {
            // ?
            if (AppManager.getInstance().isActivityNotContainFragment(activity)) {
                MobclickAgent.onPageStart(activity.getClass().getSimpleName());
            }
            // 
            MobclickAgent.onResume(activity);
        }
    }

    /**
     *  ActivityLifecycleCallbacks  onActivityPaused 
     *
     * @param activity
     */
    public static void onActivityPaused(Activity activity) {
        if (sIsInit) {
            // ?
            if (AppManager.getInstance().isActivityNotContainFragment(activity)) {
                // ?? onPageEnd  onPause ?, onPause ??
                MobclickAgent.onPageEnd(activity.getClass().getSimpleName());
            }
            // 
            MobclickAgent.onPause(activity);
        }
    }
    // ======================== ? START ========================
}