NotifyManager.java 10.2 KB
package com.lotus.town.notify;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.ad.lib.AdInfo;
import com.controller.IADController;
import com.controller.NotifyType;
import com.lotus.town.MainActivity;
import com.lotus.town.locker.LockerActivity;
import com.manager.AdCache;
import com.manager.CacheSuccess;
import com.sdk.Sdk;
import com.sdk.SharedPref;
import com.sdk.utils.NetworkUtils;
import com.lotus.town.utils.NotificationUtils;
import java.util.List;
import java.util.Random;

public class NotifyManager {

    public static boolean isPhoneStatus = false;
    public static long PhoneStartTime = 0;
    public static long PhoneEndTime = 0;
//    public static boolean isSCREEN_OFF = false;

    public static void NotifyChange(final Context context, final int type){
//        if(!isApplicationBroughtToBackground(context)){
//            return;
//        }

        if(!IADController.getInstance().isShowOuter()){
            return;
        }
        if(type == NotifyType.WIFI_CONNECTED_NOTIFY) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    triggerScenes(context,type);
                }
            }, 6000);
        } else {
            triggerScenes(context,type);
        }

    }
    private static void triggerScenes(final Context context, final int type){
        if(type == NotifyType.WIFI_CONNECTED_NOTIFY || type == NotifyType.POP_NOTIFY_EVERYWHERE ){
            AdCache.getInstance().getNativeAdInfo(context, type, new CacheSuccess() {
                @Override
                public void success(AdInfo info) {
                    gotoPage(context, type);
                }
            });
            return;
        } else if(type == NotifyType.SCREEN_ON_NOTIRY
                || type == NotifyType.SCREEN_OFF_NOTIRY
                || NotifyType.BATTERY_PLUG_NOTIFY == type
                || NotifyType.WORNING_NOTIRY_BOOSTER == type
                || NotifyType.WORNING_NOTIRY_CLEAN == type
                || NotifyType.PACKAGE_UNINSTALL_NOTIFY == type
                || NotifyType.AD_NOTIFY == type
                || NotifyType.CONTACT_NOTIFY == type
                || NotifyType.CPU_NOTIFY == type
                || NotifyType.BATTERY_NOTIFY == type
                ||NotifyType.WORNING_NOTIRY_VIRUS == type){

            gotoPage(context, type);
            return;
        }

        AdCache.getInstance().getNativeAdInfo(context, type, new CacheSuccess() {
            @Override
            public void success(AdInfo info) {
                gotoPage(context, type);
            }
        });
    }

    public static boolean isApplicationBroughtToBackground(final Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            ComponentName topActivity = tasks.get(0).topActivity;
            if (!topActivity.getPackageName().equals(context.getPackageName())) {
                return true;
            }
        }
        return false;
    }

    private static Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            int time = new Random().nextInt(120);
            int delayTime = 120;
            if(msg.what == 0) {
                NotifyManager.NotifyChange(Sdk.app(), NotifyType.POP_NOTIFY_EVERYWHERE);
                handler.sendEmptyMessageDelayed(0, (time + delayTime) * 1000);
            } else if(msg.what == 1){
                handler.sendEmptyMessageDelayed(0, (delayTime) * 1000);
            } else if(msg.what == 2){
                if(SharedPref.needWorked(Sdk.app(),"v_w_n")){
                    NotifyManager.NotifyChange(Sdk.app(), NotifyType.WORNING_NOTIRY_VIRUS);
                } else if(SharedPref.needWorked(Sdk.app(),"b_w_n")){
                    NotifyManager.NotifyChange(Sdk.app(), NotifyType.WORNING_NOTIRY_BOOSTER);
                } else if(SharedPref.needWorked(Sdk.app(),"c_w_n")){
                    NotifyManager.NotifyChange(Sdk.app(), NotifyType.WORNING_NOTIRY_CLEAN);
                } else if(SharedPref.needWorked(Sdk.app(),"a_d_n")){
                    NotifyManager.NotifyChange(Sdk.app(), NotifyType.AD_NOTIFY);
                } else if(SharedPref.needWorked(Sdk.app(),"b_t_n")){
                    NotifyManager.NotifyChange(Sdk.app(), NotifyType.BATTERY_NOTIFY);
                } else if(SharedPref.needWorked(Sdk.app(),"c_p_n")){
                    NotifyManager.NotifyChange(Sdk.app(), NotifyType.CPU_NOTIFY);
                } else if(SharedPref.needWorked(Sdk.app(),"c_t_n")){
                    NotifyManager.NotifyChange(Sdk.app(), NotifyType.CONTACT_NOTIFY);
                }
            }
        }
    };

    private static long lastShowTime = 0;
    private static  void gotoPage(final Context context, int type){
        if(!NetworkUtils.isNetworkConnected(context)) {
            return;
        }
        if(isPhoneStatus){ //打电话期间都不能弹出来
            return;
        }
//        if(isSCREEN_OFF){//灭屏到解锁之间,只有锁屏可以弹
//            if(type != NotifyType.SCREEN_ON_NOTIRY){
//                return;
//            }
//        }

//        if(!IADController.getInstance().isShowOuter()){
//            return;
//        }


        if(type != NotifyType.SCREEN_ON_NOTIRY && type != NotifyType.UNLOCK_NOTIRY && NotifyType.SCREEN_OFF_NOTIRY != type&& NotifyType.BATTERY_PLUG_NOTIFY != type){//锁屏和解锁没有时间间隔
            if(System.currentTimeMillis() - lastShowTime < 10 * 1000){//其他应用外锁屏需要至少间隔最小间隔才能显示
                return;
            }
        }
        Intent i = new Intent();
        Class clz = null;
        Log.v("XPC","NotifyManager  gotoPag e  type="+type);

        switch (type){
            case NotifyType.WIFI_CONNECTED_NOTIFY:
                if(Sdk.isInApp||Sdk.isInMain){
                    return;
                }
                if(!"COMPLETED".equals(getConnectWifiState(context))){
                    return;
                }
                clz = wn.class;
                i.setClass(context,wn.class);
                break;
            case NotifyType.WIFI_DISCONNECT_NOTIFY:
                break;
            case NotifyType.PHONE_OVER_NOTIRY:
                if(Sdk.isInApp||Sdk.isInMain){
                    return;
                }
                clz = po.class;
                i.setClass(context,po.class);
                break;
            case NotifyType.BATTERY_PLUG_NOTIFY:
                if(Sdk.isInApp||Sdk.isInMain){
                    return;
                }
                LockerActivity.startActivity(context);
                return;
            case NotifyType.BATTERY_UNPLUG_NOTIFY:
                if(Sdk.isInApp||Sdk.isInMain){
                    return;
                }
                i.setClass(context,bu.class);
                clz = bu.class;
                break;
            case NotifyType.POP_NOTIFY_EVERYWHERE:
                if(Sdk.isInApp||Sdk.isInMain||Sdk.isInSplash){
                    return;
                }
                clz = ev.class;
                i.setClass(context,ev.class);
                break;
            case NotifyType.UNLOCK_NOTIRY:
                removeHandleMessage();
                handler.sendEmptyMessage(1);
                handler.sendEmptyMessageDelayed(2,60000);
                if(Sdk.isInApp||Sdk.isInMain||Sdk.isInSplash){
                    return;
                }
                clz = un.class;
                i.setClass(context,un.class);
                break;
            case NotifyType.SCREEN_OFF_NOTIRY:
                removeHandleMessage();
//                lastShowTime = System.currentTimeMillis();
                LockerActivity.startActivity(context);
                return;
            case NotifyType.SCREEN_ON_NOTIRY:
                lastShowTime = System.currentTimeMillis();
                LockerActivity.startActivity(context);
                return;
            case NotifyType.WORNING_NOTIRY_BOOSTER:
                clz = bw.class;
                i.setClass(context,bw.class);
                break;
            case NotifyType.WORNING_NOTIRY_CLEAN:
                clz = cw.class;
                i.setClass(context,cw.class);
                break;
            case NotifyType.WORNING_NOTIRY_VIRUS:
                clz = vw.class;
                i.setClass(context,vw.class);
                break;
            case NotifyType.PACKAGE_UNINSTALL_NOTIFY:
                clz = pr.class;
                i.setClass(context,pr.class);
                break;
            case NotifyType.AD_NOTIFY:
                clz = ad.class;
                i.setClass(context,ad.class);
                break;
            case NotifyType.CONTACT_NOTIFY:
                clz = contact.class;
                i.setClass(context,contact.class);
                break;
            case NotifyType.BATTERY_NOTIFY:
                clz = battery.class;
                i.setClass(context,battery.class);
                break;
            case NotifyType.CPU_NOTIFY:
                clz = cpu.class;
                i.setClass(context,cpu.class);
                break;

        }
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        lastShowTime = System.currentTimeMillis();
        context.startActivity(i);
        if(clz == null){
            clz = MainActivity.class;
        }
        NotificationUtils.sendNotification(context,clz);
    }

    private static void removeHandleMessage(){
        if(handler.hasMessages(0)){
            handler.removeMessages(0);
        }
        if(handler.hasMessages(1)){
            handler.removeMessages(1);
        }

        if(handler.hasMessages(2)){
            handler.removeMessages(2);
        }
    }
    private static String getConnectWifiState(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        return wifiInfo.getSupplicantState().name();
    }

}