haijiang
2018-05-10 800cf637391b4b237149907ef3b3323966d971da
app/src/main/java/com/moral/yunfushao/MainActivity.java
@@ -1,10 +1,13 @@
package com.moral.yunfushao;
import android.Manifest;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
@@ -14,6 +17,8 @@
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
@@ -23,6 +28,7 @@
import com.lzy.okgo.cache.CacheMode;
import com.moral.andbrickslib.utils.FastJsonTools;
import com.moral.andbrickslib.utils.NetworkUtil;
import com.moral.andbrickslib.utils.TimeUtil;
import com.moral.yunfushao.base.BaseActivity;
import com.moral.yunfushao.ble.BluetoothService;
import com.moral.yunfushao.common.API;
@@ -32,11 +38,14 @@
import com.moral.yunfushao.fragment.FragmentShoppongMall;
import com.moral.yunfushao.httputils.HttpCallBack;
import com.moral.yunfushao.httputils.HttpUtils;
import com.moral.yunfushao.imageload.ImageLoader;
import com.moral.yunfushao.model.EventMessage;
import com.moral.yunfushao.model.User;
import com.moral.yunfushao.update.UpdateManager;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.List;
@@ -133,6 +142,7 @@
        }else{
            mToatUtils.showSingletonToast("对不起,您的手机不支持BLE!");
        }
        getAd();
    }
    @Override
@@ -324,16 +334,18 @@
        public void onConnectFail() {
            progressDialog.dismiss();
            mToatUtils.showSingletonToast("连接失败");
            EventMessage msg = new EventMessage();
            msg.setType(AppConfig.GET_BLE_DISCONNECT);
            EventBus.getDefault().postSticky(msg);
        }
        @Override
        public void onDisConnected() {
            progressDialog.dismiss();
            mToatUtils.showSingletonToast("连接断开");
            mToatUtils.showSingletonToast("孕妇哨连接断开");
            EventMessage msg = new EventMessage();
            msg.setType(AppConfig.GET_BLE_DISCONNECT);
            EventBus.getDefault().post(msg);
            EventBus.getDefault().postSticky(msg);
        }
        @Override
@@ -341,57 +353,80 @@
            progressDialog.dismiss();
            EventMessage msg = new EventMessage();
            msg.setType(AppConfig.GET_BLE_SERVICE);
            EventBus.getDefault().post(msg);
            EventBus.getDefault().postSticky(msg);
        }
    };
//    private void registerBoradcastReceiver() {
//        IntentFilter stateChangeFilter = new IntentFilter(
//                BluetoothAdapter.ACTION_STATE_CHANGED);
//        IntentFilter connectedFilter = new IntentFilter(
//                BluetoothDevice.ACTION_ACL_CONNECTED);
//        IntentFilter disConnectedFilter = new IntentFilter(
//                BluetoothDevice.ACTION_ACL_DISCONNECTED);
//        registerReceiver(stateChangeReceiver, stateChangeFilter);
//        registerReceiver(stateChangeReceiver, connectedFilter);
//        registerReceiver(stateChangeReceiver, disConnectedFilter);
//    }
//
//    private BroadcastReceiver stateChangeReceiver = new BroadcastReceiver() {
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            String action = intent.getAction();
//            if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
//                mToatUtils.showSingletonToast("xxx连接OK");
//            }
//            if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
//                mToatUtils.showSingletonToast("xxx连接断开");
//            }
//            if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
//                mToatUtils.showSingletonToast("xxx连接变化");
//            }
//        }
//    };
    private AlertDialog adDialog;
    private View adView;
    private TextView tv_content,tv_name;
    private ImageView iv_logo;
    private Button bt_cancel,bt_ok;
    private void showAd(String content,String logo,String name){
        adView = LayoutInflater.from(this).inflate(R.layout.dialog_ad_layout,null);
        tv_content = (TextView) adView.findViewById(R.id.tv_content);
        tv_name = (TextView) adView.findViewById(R.id.tv_name);
        bt_cancel = (Button) adView.findViewById(R.id.bt_cancel);
        bt_ok = (Button) adView.findViewById(R.id.bt_ok);
        iv_logo = (ImageView) adView.findViewById(R.id.iv_logo);
        tv_content.setText(content);
        tv_name.setText(name);
        ImageLoader.setCircleImageView(this,logo,iv_logo,R.mipmap.circle);
        bt_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                adDialog.dismiss();
            }
        });
        adDialog = new AlertDialog.Builder(this).setView(adView).create();
        Window window=adDialog.getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        adDialog.show();
    }
//    private boolean isFinish = false;
//    Timer timer;
//
//    private void bleHeart() {
//        timer = new Timer();
//        timer.schedule(new TimerTask() {
//            @Override
//            public void run() {
//                if (!isFinish) {
//                    if(!MainApp.theApp.bleManager.isConnected()){
//                        runOnUiThread(new Runnable() {
//                            @Override
//                            public void run() {
//                                mToatUtils.showSingletonToast("TMD连接断了");
//                            }
//                        });
//                    }
//                }
//            }
//        }, 0, 10000);
//    }
    private void getAd(){
        HttpUtils.doPost(API.GETAD, null, CacheMode.DEFAULT, false, new HttpCallBack() {
            @Override
            public void onSuccess(String res, String msg) {
                if(TextUtils.isEmpty(res)){
                    return;
                }
                try {
                    JSONObject json = new JSONObject(res);
                    String time  = MainApp.theApp.sharedPreferencesUtil.getShowAd();
                    if(TextUtils.isEmpty(time)){
                        showAd(json.optString("content"),json.optString("logo"),json.optString("name"));
                        MainApp.theApp.sharedPreferencesUtil.saveShowAd(TimeUtil.getCurrentDate("yyyy-MM-dd"));
                    }else{
                        if(time.equals(TimeUtil.getCurrentDate("yyyy-MM-dd"))){
                            return;
                        }else{
                            showAd(json.optString("content"),json.optString("logo"),json.optString("name"));
                            MainApp.theApp.sharedPreferencesUtil.saveShowAd(TimeUtil.getCurrentDate("yyyy-MM-dd"));
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void showLoadingDialog() {
            }
            @Override
            public void onFail(int errno, String s) {
            }
        });
    }
    public void resetBLECallBack(){
        if(mBluetoothService!=null){
            mBluetoothService.setScanCallback(callback);
        }
    }
}