lwb
2020-11-19 7d62c45726746fd3a4e9360642e81d7da6d76757
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.secondworld.sdk.utils;
 
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
 
import com.secondworld.sdk.GameApp;
import com.secondworld.sdk.UnityMsgHandler;
 
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
 
/**
 * Created by Administrator on 2018/7/15 0015.
 */
 
public class BatteryUtil {
    private static BatteryUtil s_Instance = null;
 
    public static BatteryUtil getInstance() {
        if (s_Instance == null) {
            s_Instance = new BatteryUtil();
        }
        return s_Instance;
    }
 
    private BatteryBroadCastReceiver m_BatteryBroadCastReceiver;
 
    private BatteryUtil() {
    }
 
    public void start() {
        m_BatteryBroadCastReceiver = new BatteryBroadCastReceiver();
        IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        GameApp.I.registerReceiver(m_BatteryBroadCastReceiver, filter);
    }
 
    public void stop() {
        if (m_BatteryBroadCastReceiver != null) {
            try {
                GameApp.I.unregisterReceiver(m_BatteryBroadCastReceiver);
                m_BatteryBroadCastReceiver = null;
            } catch (IllegalArgumentException e) {
                if (!Objects.requireNonNull(e.getMessage()).contains("Receiver not registered")) {
                    throw e;
                }
            }
        }
    }
 
    private static class BatteryBroadCastReceiver extends BroadcastReceiver {
        private int m_BatteryLevel;
        private int m_Status;
 
        public BatteryBroadCastReceiver() {
            m_BatteryLevel = 0;
        }
 
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
                int _batteryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
 
                Map<String, Object> _msg = new HashMap<>();
 
                if (_batteryLevel != m_BatteryLevel) {
                    try {
                        _msg.put("code", CodeA2U.BatteryLevel);
                        _msg.put("level", _batteryLevel);
 
                        UnityMsgHandler.sendMessageToUnity(_msg);
 
                        m_BatteryLevel = _batteryLevel;
 
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
 
                int _status = intent.getIntExtra("status", 0);
 
                if (_status != m_Status) {
                    _msg.clear();
 
                    int _transCode = 0;
                    if (_status == BatteryManager.BATTERY_STATUS_CHARGING) {
                        _transCode = 2;
                    } else if (_status == BatteryManager.BATTERY_STATUS_NOT_CHARGING ||
                            _status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
                        _transCode = 1;
                    } else if (_status == BatteryManager.BATTERY_STATUS_FULL) {
                        _transCode = 3;
                    }
 
                    _msg.put("code", CodeA2U.BatteryCharging);
                    _msg.put("status", _transCode);
 
                    UnityMsgHandler.sendMessageToUnity(_msg);
 
                    m_Status = _status;
                }
            }
        }
    }
}