com.cwp.android.baidutest.MyApplication.java Source code

Java tutorial

Introduction

Here is the source code for com.cwp.android.baidutest.MyApplication.java

Source

package com.cwp.android.baidutest;

import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.baidu.mapapi.SDKInitializer;

import org.json.JSONObject;

import java.util.List;
import java.util.concurrent.Semaphore;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobRealTimeData;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.ValueEventListener;
import just.beans.AutoInfo;
import just.beans.MaInfo;
import just.constants.AutoInfoConstants;
import just.operations.AutoInfoLocalDBOperation;
import just.operations.MaInfoLocalDBOperation;
import just.services.InfoSyncToCloudService;
import just.services.InfoSyncToLocalService;

/**
 * Created by Just on 2016/5/4.
 */
public class MyApplication extends Application {
    public static final String NOT_LANDING = "NOT_LANDING";
    public static final String NULL_NAME = "NULL_NAME";
    public static final String FILE_NAME = "LoginInfo";

    public static final String USERNAME_INFO = "username";
    public static final String NAME_INFO = "name";

    private static Context mContext;
    public static Semaphore mSyncSemaphore = new Semaphore(1);
    private static String USERNAME;
    private static String NAME;

    private static Handler mHandler = new Handler(Looper.getMainLooper()) {
        String result = "";
        String auto = "";

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case 1:
                result += "20%,?\n";
                Log.d("->MyApplication", "onDataChange->++++");
                break;
            case 2:
                result += "" + ((int) msg.obj * 15000) + "km,?\n";
                Log.d("->MyApplication", "onDataChange->++++");
                break;
            case 3:
                result += "?\n";
                Log.d("->MyApplication", "onDataChange->?++++");
                break;
            case 4:
                result += "?\n";
                Log.d("->MyApplication", "onDataChange->?++++");
                break;
            case 5:
                result += "?\n";
                Log.d("->MyApplication", "onDataChange->?++++");
                break;
            //?,??
            case 0:
                Log.d("->MyApplication", "result=" + result);
                //??????
                if (!result.equals("")) {
                    Log.d("->MyApplication", "onDataChange->??");
                    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext);
                    mBuilder.setWhen(System.currentTimeMillis())// ?
                            .setTicker("??").setContentTitle(auto).setContentText(result)
                            .setSmallIcon(R.drawable.ic_logo);
                    Notification notify = mBuilder.build();
                    notify.flags = Notification.FLAG_AUTO_CANCEL;
                    NotificationManager manager = (NotificationManager) mContext
                            .getSystemService(Context.NOTIFICATION_SERVICE);
                    manager.notify(3, notify);
                    result = "";
                }
                Log.d("->MyApplication", "onDataChange->??");
                auto = "";
                break;
            //??
            case -1:
                Log.d("->MyApplication", "onDataChange->??");
                auto += msg.obj;
                break;
            default:
                break;
            }
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();
        init();
        autoLogin();
    }

    public static void init() {
        Bmob.initialize(mContext, "11c50a59fafd8add5a2c19107b769f9d"); //Bmob?
        SDKInitializer.initialize(mContext);//?
    }

    //?????
    public static void startSyncService() {
        Intent intentService = new Intent(mContext, InfoSyncToCloudService.class);
        mContext.startService(intentService);
    }

    public static void setUsername(String username) {
        USERNAME = username;
    }

    public static String getUsername() {
        return USERNAME;
    }

    public static void setName(String name) {
        NAME = name;
    }

    public static String getName() {
        return NAME;
    }

    public static boolean isLanded() {
        return !USERNAME.equals(NOT_LANDING);
    }

    public static void logoutCurrentAccount() {
        USERNAME = NOT_LANDING;
        NAME = NULL_NAME;
        SharedPreferences.Editor editor = mContext.getSharedPreferences(FILE_NAME, MODE_PRIVATE).edit();
        editor.putString(USERNAME_INFO, NOT_LANDING);
        editor.putString(NAME_INFO, NULL_NAME);
        editor.apply();
    }

    public static void saveLoginInfo(String username, String name) {
        SharedPreferences.Editor editor = mContext.getSharedPreferences(MyApplication.FILE_NAME, MODE_PRIVATE)
                .edit();
        editor.putString(MyApplication.USERNAME_INFO, username);
        editor.putString(MyApplication.NAME_INFO, name);
        editor.commit();
        USERNAME = username;
        NAME = name;
    }

    private static void autoLogin() {
        SharedPreferences pref = mContext.getSharedPreferences(FILE_NAME, MODE_PRIVATE);
        String username = pref.getString(USERNAME_INFO, NOT_LANDING);
        String name = pref.getString(NAME_INFO, NULL_NAME);
        MyApplication.setUsername(username);
        MyApplication.setName(name);
    }

    public static void startMaInfoMonitoring() {

        //?MaInfo?
        BmobRealTimeData rtd = new BmobRealTimeData();

        //start?
        //??
        rtd.start(mContext, new ValueEventListener() {
            @Override
            public void onConnectCompleted() {
                //?
                Log.d("->MyApplication", "BmobRealTimeData->currentThread=" + Thread.currentThread());
                Log.d("->MyApplication", "BmobRealTimeData->?:" + rtd.isConnected());

                //?
                if (rtd.isConnected()) {
                    rtd.subTableUpdate("MaInfo");
                }
            }

            @Override
            public void onDataChange(JSONObject jsonObject) {
                //

                new Thread(() -> {
                    Log.d("->MyApplication", "onDataChange->currentThread=" + Thread.currentThread());
                    Log.d("->MyApplication",
                            "onDataChange->(" + jsonObject.optString("action") + ")\n?" + jsonObject);

                    if (BmobRealTimeData.ACTION_UPDATETABLE.equals(jsonObject.optString("action"))) {
                        JSONObject data = jsonObject.optJSONObject("data");

                        Log.d("->MyApplication", "onDataChange->?");
                        String username = data.optString("username");
                        String vin = data.optString("vin");

                        try {
                            mSyncSemaphore.acquire();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        //?????????????
                        List<AutoInfo> list = AutoInfoLocalDBOperation
                                .queryBy(mContext,
                                        AutoInfoConstants.COLUMN_USERNAME + " = ? and "
                                                + AutoInfoConstants.COLUMN_VIN + " = ?",
                                        new String[] { username, vin });
                        if (list.size() != 0) {
                            Log.d("->MyApplication", "onDataChange->?");
                            //?
                            AutoInfo autoInfo = list.get(0);
                            String brand = autoInfo.getBrand();
                            String model = autoInfo.getModel();
                            String plateNum = autoInfo.getLicensePlateNum();
                            Message message = Message.obtain();
                            message.what = -1;
                            message.obj = brand + " " + model + " " + plateNum;
                            //???????
                            if (data.optInt("gasolineVolume") < 20) {//?<20%
                                //
                                mHandler.sendEmptyMessage(1);
                            }

                            //?15000?
                            //?1500?(??)?????

                            //???

                            //??5.E.a-c,????
                            //????
                            Semaphore semaphore = new Semaphore(1);
                            double mileage = data.optDouble("mileage");
                            //mileage?15000??
                            if (mileage >= 15000) {
                                try {
                                    semaphore.acquire();
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                String scanTime = data.optString("scanTime");
                                BmobQuery<MaInfo> query = new BmobQuery<MaInfo>();
                                query.addWhereEqualTo("username", username);
                                query.addWhereEqualTo("vin", vin);
                                query.addWhereLessThan("scanTime", scanTime);
                                query.addQueryKeys("scanTime");
                                query.order("-scanTime");
                                final Semaphore finalSemaphore = semaphore;
                                query.findObjects(mContext, new FindListener<MaInfo>() {
                                    @Override
                                    public void onSuccess(List<MaInfo> list) {
                                        int current = (int) mileage / 15000;
                                        Log.d("", "current=" + current + ",mileage=" + mileage);
                                        if (list.size() == 0) {
                                            message.obj = current;
                                            message.what = 2;
                                            mHandler.sendMessage(message);
                                        } else {
                                            double lastMileage = list.get(0).getMileage();
                                            int last = (int) lastMileage / 15000;
                                            if (current > last) {
                                                Message message = Message.obtain();
                                                message.obj = current;
                                                message.what = 2;
                                                mHandler.sendMessage(message);
                                            }
                                        }
                                        finalSemaphore.release();
                                    }

                                    @Override
                                    public void onError(int i, String s) {
                                        finalSemaphore.release();
                                    }
                                });
                            }

                            try {
                                semaphore.acquire();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            //???????????
                            if (data.optString("enginePerfor").equals("")) {//?
                                mHandler.sendEmptyMessage(3);
                            }
                            if (data.optString("lamp").equals("")) {//?
                                mHandler.sendEmptyMessage(4);
                            }
                            if (data.optString("transmissionPerfor").equals("")) {//?
                                mHandler.sendEmptyMessage(5);
                            }
                            semaphore.release();

                            //????
                            mHandler.sendEmptyMessage(0);
                            mSyncSemaphore.release();
                            Log.d("++++++++++++++++++++", "++++++++++++++++");
                        } else {
                            mSyncSemaphore.release();
                        }
                    }
                }).start();
            }
        });
    }
}