Skip to content

Commit 1faf218

Browse files
committed
- Fixed battery consumption due to unnecessary CPU usage
- Implemented a safe cast and type-checking mechanism to avoid unchecked cast warnings during deserialization of ShutterLogEntry list from shared preferences. - Dependencies updated
1 parent 58377ad commit 1faf218

File tree

11 files changed

+60
-63
lines changed

11 files changed

+60
-63
lines changed

.idea/compiler.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ android {
1010
applicationId 'com.sepp89117.goeasypro_android'
1111
minSdk 27
1212
targetSdk 34
13-
versionCode 173
14-
versionName '1.7.3'
13+
versionCode 174
14+
versionName '1.7.4'
1515

1616
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1717
vectorDrawables.useSupportLibrary = true
@@ -35,12 +35,12 @@ dependencies {
3535
implementation 'com.google.android.exoplayer:exoplayer:2.19.1'
3636
implementation 'com.arthenica:ffmpeg-kit-min:6.0.LTS'
3737
implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.11'
38-
implementation 'androidx.appcompat:appcompat:1.6.1'
38+
implementation 'androidx.appcompat:appcompat:1.7.0'
3939
implementation 'com.google.android.material:material:1.9.0' /* do not update 1.9.0 */
4040
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
41-
implementation 'com.google.android.gms:play-services-location:21.0.1'
41+
implementation 'com.google.android.gms:play-services-location:21.3.0'
4242
implementation 'androidx.preference:preference:1.2.1'
4343
testImplementation 'junit:junit:4.13.2'
44-
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
45-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
44+
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
45+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
4646
}

app/src/main/java/com/sepp89117/goeasypro_android/gopro/GoProDevice.java

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.util.Pair;
2424
import android.widget.Toast;
2525

26+
import androidx.annotation.NonNull;
2627
import androidx.appcompat.app.AlertDialog;
2728

2829
import com.google.protobuf.InvalidProtocolBufferException;
@@ -59,7 +60,6 @@ public class GoProDevice {
5960

6061
public static final int UNK_MODEL = 0;
6162
public static final int FUSION = 23;
62-
public static final int HERO6_BLACK = 24;
6363
public static final int HERO2018 = 34;
6464
public static final int HERO8_BLACK = 50;
6565
public static final int HERO_MAX = 51;
@@ -142,7 +142,7 @@ public class GoProDevice {
142142
public int btConnectionStage = BT_NOT_CONNECTED;
143143
public String btDeviceName;
144144
public String displayName;
145-
public String btMacAddress = "";
145+
public String btMacAddress;
146146
public String modelName;
147147
public int modelID = UNK_MODEL;
148148
public String boardType = "";
@@ -391,7 +391,7 @@ public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic
391391
}
392392

393393
@Override
394-
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, byte[] value, int status) {
394+
public void onCharacteristicRead(@NonNull BluetoothGatt gatt, @NonNull BluetoothGattCharacteristic characteristic, @NonNull byte[] value, int status) {
395395
if (status == BluetoothGatt.GATT_SUCCESS) {
396396
parseBtData(characteristic.getUuid().toString(), characteristic.getValue(), false);
397397
} else if (status == BluetoothGatt.GATT_FAILURE) {
@@ -577,12 +577,13 @@ private void parseBtData(String characteristicUuid, byte[] valueBytes, boolean i
577577
}
578578
break;
579579
case settingsRespUUID:
580-
if ((valueBytes.length >= 4 && valueBytes[3] == 91) || (valueBytes.length >= 3 && valueBytes[1] == 91)) {
581-
// ignore keepAlive response
582-
} else {
580+
if (!((valueBytes.length >= 4 && valueBytes[3] == 91) || (valueBytes.length >= 3 && valueBytes[1] == 91))) {
583581
// A setting was changed. Get an update of the current settings
584-
if (providesAvailableOptions) getAvailableOptions();
585-
else getAllSettings();
582+
if (providesAvailableOptions) {
583+
getAvailableOptions();
584+
} else {
585+
getAllSettings();
586+
}
586587
}
587588
break;
588589
case queryRespUUID:
@@ -1419,9 +1420,7 @@ public void setNewCamName(String newName) {
14191420
if (btActionHelper != null) btActionHelper.queueAction(() -> {
14201421
byte[] msg = newName.getBytes(StandardCharsets.UTF_8);
14211422

1422-
if (wifiSsidCharacteristic != null && wifiSsidCharacteristic.setValue(msg) && bluetoothGatt.writeCharacteristic(wifiSsidCharacteristic)) {
1423-
// wait for onWrite
1424-
} else {
1423+
if (!(wifiSsidCharacteristic != null && wifiSsidCharacteristic.setValue(msg) && bluetoothGatt.writeCharacteristic(wifiSsidCharacteristic))) {
14251424
Log.e("setCamName", "not sent");
14261425
if (btActionHelper != null) {
14271426
btActionHelper.resetGattInProgress();
@@ -1433,9 +1432,7 @@ public void setNewCamName(String newName) {
14331432
if (btActionHelper != null) btActionHelper.queueAction(() -> {
14341433
byte[] msg = wifiPSK.getBytes(StandardCharsets.UTF_8);
14351434

1436-
if (wifiPwCharacteristic != null && wifiPwCharacteristic.setValue(msg) && bluetoothGatt.writeCharacteristic(wifiPwCharacteristic)) {
1437-
// wait for onWrite
1438-
} else {
1435+
if (!(wifiPwCharacteristic != null && wifiPwCharacteristic.setValue(msg) && bluetoothGatt.writeCharacteristic(wifiPwCharacteristic))) {
14391436
Log.e("setCamName", "not sent");
14401437
if (btActionHelper != null) {
14411438
btActionHelper.resetGattInProgress();
@@ -1664,8 +1661,7 @@ public void setSubMode(int mode, int subMode) {
16641661
});
16651662
}
16661663

1667-
1668-
public void getSettingsJson() {
1664+
/*public void getSettingsJson() {
16691665
if (btActionHelper != null) btActionHelper.queueAction(() -> {
16701666
if (btConnectionStage < 2) {
16711667
if (btActionHelper != null) {
@@ -1683,8 +1679,7 @@ public void getSettingsJson() {
16831679
}
16841680
}
16851681
});
1686-
}
1687-
1682+
}*/
16881683

16891684
public void shutterOn() {
16901685
if (btActionHelper != null) btActionHelper.queueAction(() -> {
@@ -2269,10 +2264,7 @@ public interface WifiConnectionChangedInterface {
22692264
void onWifiConnectionChanged();
22702265
}
22712266

2272-
private WifiConnectionChangedInterface wifiConnectionChangedCallback;
2273-
22742267
public void connectWifi(WifiConnectionChangedInterface _wifiConnectionChangedCallback) {
2275-
wifiConnectionChangedCallback = _wifiConnectionChangedCallback;
22762268
if (wifiApState != 1 && !isWifiConnected()) {
22772269
wifiApOn();
22782270
try {
@@ -2283,7 +2275,7 @@ public void connectWifi(WifiConnectionChangedInterface _wifiConnectionChangedCal
22832275
}
22842276

22852277
try {
2286-
wiFiHelper = new WiFiHelper(_context, wifiConnectionChangedCallback);
2278+
wiFiHelper = new WiFiHelper(_context, _wifiConnectionChangedCallback);
22872279
} catch (Exception e) {
22882280
e.printStackTrace();
22892281
showToast("WiFi error: " + e.getMessage(), Toast.LENGTH_SHORT);
@@ -2340,7 +2332,7 @@ public void run() {
23402332
return;
23412333
}
23422334

2343-
if (new Date().getTime() - btActionHelper.getLastExecStartDate() /*gattProgressStart.getTime()*/ >= 10000 && btActionHelper.isGattInProgress() /*gattInProgress*/) {
2335+
if (System.currentTimeMillis() - btActionHelper.getLastExecMillis() >= 10000 && btActionHelper.isGattInProgress()) {
23442336
Log.e("ExecWatchdog", "'Gatt in progress' timeout triggered!");
23452337
btActionHelper.resetGattInProgress();
23462338
}
@@ -2457,10 +2449,5 @@ private void showToast(String text, int duration) {
24572449
});
24582450
}
24592451

2460-
public static String getMethodName() {
2461-
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
2462-
StackTraceElement currentMethod = stackTrace[3];
2463-
return currentMethod.getMethodName();
2464-
}
24652452
//endregion
24662453
}

app/src/main/java/com/sepp89117/goeasypro_android/helpers/BtActionHelper.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import android.util.Log;
44

5-
import java.util.Date;
65
import java.util.LinkedList;
76

87
public class BtActionHelper {
@@ -13,29 +12,30 @@ public class BtActionHelper {
1312
private final LinkedList<Runnable> btActionList = new LinkedList<>();
1413
private boolean gattInProgress;
1514
private boolean execShouldStop;
16-
private Date btActionStart;
15+
private long btActionStart;
1716
private Thread executionThread;
1817

1918
public BtActionHelper() {
2019
this.gattInProgress = false;
2120
this.execShouldStop = false;
22-
this.btActionStart = new Date();
21+
this.btActionStart = System.currentTimeMillis();
2322
}
2423

25-
public boolean queueAction(Runnable runnable) {
24+
public void queueAction(Runnable runnable) {
2625
synchronized (this.btActionList) {
2726
if (this.btActionList.size() > MAX_FIFO_SIZE) {
28-
return false;
27+
return;
2928
}
3029
this.btActionList.add(runnable);
3130
}
3231

33-
return true;
3432
}
3533

36-
public boolean queueActionIfNotQueued(Runnable runnable) {
34+
public void queueActionIfNotQueued(Runnable runnable) {
3735
synchronized (this.btActionList) {
38-
return this.btActionList.contains(runnable) || queueAction(runnable);
36+
if (!this.btActionList.contains(runnable)) {
37+
queueAction(runnable);
38+
}
3939
}
4040
}
4141

@@ -59,8 +59,8 @@ public boolean isGattInProgress() {
5959
return this.gattInProgress;
6060
}
6161

62-
public long getLastExecStartDate() {
63-
return btActionStart.getTime();
62+
public long getLastExecMillis() {
63+
return btActionStart;
6464
}
6565

6666
public boolean isExecutionAlive() {
@@ -92,7 +92,7 @@ private Thread getNewExecThread() {
9292
private void executeNext() {
9393
Runnable toExec;
9494

95-
if (!this.gattInProgress) {
95+
if (!this.gattInProgress && this.btActionList.size() > 0) {
9696
this.gattInProgress = true;
9797

9898
synchronized (this.btActionList) {
@@ -102,18 +102,22 @@ private void executeNext() {
102102
if (toExec == null) {
103103
this.gattInProgress = false;
104104
} else {
105-
Date now = new Date();
106-
while (now.getTime() - btActionStart.getTime() < BT_ACTION_DELAY) {
105+
while (System.currentTimeMillis() - btActionStart < BT_ACTION_DELAY) {
107106
try {
108-
Thread.sleep(BT_ACTION_DELAY - (now.getTime() - btActionStart.getTime()));
107+
Thread.sleep(BT_ACTION_DELAY - (System.currentTimeMillis() - btActionStart));
109108
} catch (InterruptedException e) {
110109
e.printStackTrace();
111110
}
112-
now = new Date();
113111
}
114-
btActionStart = now;
112+
btActionStart = System.currentTimeMillis();
115113
toExec.run();
116114
}
115+
} else {
116+
try {
117+
Thread.sleep(BT_ACTION_DELAY);
118+
} catch (InterruptedException e) {
119+
e.printStackTrace();
120+
}
117121
}
118122
}
119123

app/src/main/java/com/sepp89117/goeasypro_android/helpers/ShutterLog.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public static void logShutter(Context context, String cameraName, String shutter
2727
saveShutterLog(context, logEntries);
2828
}
2929

30+
@SuppressWarnings("unchecked")
3031
public static List<ShutterLogEntry> getShutterLog(Context context) {
3132
SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
3233
String serializedLog = prefs.getString(LOG_KEY, "");
@@ -35,9 +36,15 @@ public static List<ShutterLogEntry> getShutterLog(Context context) {
3536
if (!serializedLog.isEmpty()) {
3637
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(Base64.decode(serializedLog, Base64.DEFAULT));
3738
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
38-
List<ShutterLogEntry> logEntries = (List<ShutterLogEntry>) objectInputStream.readObject();
39+
Object object = objectInputStream.readObject();
3940
objectInputStream.close();
40-
return logEntries;
41+
42+
if (object instanceof List<?>) {
43+
List<?> tempList = (List<?>) object;
44+
if (!tempList.isEmpty() && tempList.get(0) instanceof ShutterLogEntry) {
45+
return (List<ShutterLogEntry>) tempList;
46+
}
47+
}
4148
}
4249
} catch (IOException | ClassNotFoundException e) {
4350
e.printStackTrace();

app/src/main/res/layout/activity_storage_browser.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@
100100

101101
</androidx.constraintlayout.widget.ConstraintLayout>
102102

103-
104-
105103
<ListView
106104
android:id="@+id/file_listView"
107105
android:layout_width="match_parent"

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22
plugins {
3-
id 'com.android.application' version '7.3.1' apply false
4-
id 'com.android.library' version '7.3.1' apply false
3+
id 'com.android.application' version '8.2.0' apply false
4+
id 'com.android.library' version '8.2.0' apply false
55
}

gradle.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ android.useAndroidX=true
1818
# Enables namespacing of each library's R class so that its R class includes only the
1919
# resources declared in the library itself and none from the library's dependencies,
2020
# thereby reducing the size of the R class for that library
21-
android.nonTransitiveRClass=true
21+
android.nonTransitiveRClass=true
22+
android.defaults.buildfeatures.buildconfig=true
23+
android.nonFinalResIds=false

0 commit comments

Comments
 (0)