-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBattery.java
More file actions
73 lines (56 loc) · 1.95 KB
/
Battery.java
File metadata and controls
73 lines (56 loc) · 1.95 KB
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
package com.kasilov.andrew.solidrobot;
import android.os.AsyncTask;
import com.kasilov.andrew.solidrobot.interfaces.IBatteryCharging;
import com.kasilov.andrew.solidrobot.interfaces.IBatteryStatus;
import com.kasilov.andrew.solidrobot.utils.LogUtil;
import java.util.concurrent.TimeUnit;
abstract class Battery implements IBatteryCharging, IBatteryStatus {
protected IBatteryStatus iBatteryStatus;
protected BatteryCharging batteryCharging;
private boolean isChargeable = true;
private boolean charged = false;
Battery() {
this.batteryCharging = new BatteryCharging();
}
@Override
public void onBatteryCharged() {
this.setCharged(true);
this.setChargeable(false);
this.iBatteryStatus.onBatteryCharged();
}
public abstract int getChargeLevel();
public boolean isChargeable() {
return isChargeable;
}
private void setChargeable(boolean chargeable) {
isChargeable = chargeable;
}
public boolean isCharged() {
return charged;
}
protected void setCharged(boolean charged) {
this.charged = charged;
}
abstract void notifyBatteriesChargingIsFinished(Integer chargeLevel);
protected class BatteryCharging extends AsyncTask<Integer, Integer, Integer> {
@Override
protected Integer doInBackground(Integer... integers) {
LogUtil.makeLog("Charging started");
int chargeLevel = 0;
while (chargeLevel < 100) {
chargeLevel += integers[0];
try {
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return chargeLevel;
}
@Override
protected void onPostExecute(Integer integer) {
Battery.this.notifyBatteriesChargingIsFinished(integer);
Battery.this.onBatteryCharged();
}
}
}