Skip to content

Commit 1a40f78

Browse files
committed
Deprecating last settings constant
1 parent ebe65db commit 1a40f78

File tree

3 files changed

+60
-14
lines changed

3 files changed

+60
-14
lines changed

dfu/src/main/java/no/nordicsemi/android/dfu/DfuBaseService.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
* The service will show its progress on the notification bar and will send local broadcasts to the
9494
* application.
9595
*/
96+
@SuppressWarnings("deprecation")
9697
public abstract class DfuBaseService extends IntentService implements DfuProgressInfo.ProgressListener {
9798
private static final String TAG = "DfuBaseService";
9899

@@ -214,6 +215,12 @@ public abstract class DfuBaseService extends IntentService implements DfuProgres
214215
* <a href="https://github.com/NordicSemiconductor/Android-DFU-Library/issues/71">#71</a>.
215216
*/
216217
public static final String EXTRA_DISABLE_RESUME = "no.nordicsemi.android.dfu.extra.EXTRA_DISABLE_RESUME";
218+
/**
219+
* The MBR size.
220+
*
221+
* @see DfuServiceInitiator#setMbrSize(int)
222+
*/
223+
public static final String EXTRA_MBR_SIZE = "no.nordicsemi.android.dfu.extra.EXTRA_MBR_SIZE";
217224
/**
218225
* This extra allows you to control the MTU that will be requested (on Lollipop or newer devices).
219226
* If the field is null, the service will not request higher MTU and will use MTU = 23
@@ -1108,14 +1115,20 @@ protected void onHandleIntent(final Intent intent) {
11081115
// Applications and bootloader starts from bigger address. However, in custom DFU
11091116
// implementations, user may want to transmit the whole whole data, even from address 0x0000.
11101117
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
1111-
final String value = preferences.getString(DfuSettingsConstants.SETTINGS_MBR_SIZE, String.valueOf(DfuSettingsConstants.SETTINGS_DEFAULT_MBR_SIZE));
1112-
int mbrSize;
1113-
try {
1114-
mbrSize = Integer.parseInt(value);
1118+
int mbrSize = DfuServiceInitiator.DEFAULT_MBR_SIZE;
1119+
if (preferences.contains(DfuSettingsConstants.SETTINGS_MBR_SIZE)) {
1120+
final String value = preferences.getString(DfuSettingsConstants.SETTINGS_MBR_SIZE, String.valueOf(DfuServiceInitiator.DEFAULT_MBR_SIZE));
1121+
try {
1122+
mbrSize = Integer.parseInt(value);
1123+
if (mbrSize < 0)
1124+
mbrSize = 0;
1125+
} catch (final NumberFormatException e) {
1126+
// ignore, default value will be used
1127+
}
1128+
} else {
1129+
mbrSize = intent.getIntExtra(EXTRA_MBR_SIZE, DfuServiceInitiator.DEFAULT_MBR_SIZE);
11151130
if (mbrSize < 0)
11161131
mbrSize = 0;
1117-
} catch (final NumberFormatException e) {
1118-
mbrSize = DfuSettingsConstants.SETTINGS_DEFAULT_MBR_SIZE;
11191132
}
11201133

11211134
if (foregroundService) {

dfu/src/main/java/no/nordicsemi/android/dfu/DfuServiceInitiator.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@
4848
* parameters to the service. The DfuServiceInitiator class may be used to make this process easier.
4949
* It provides simple API that covers all low lever operations.
5050
*/
51-
@SuppressWarnings({"WeakerAccess", "unused"})
52-
public class DfuServiceInitiator {
51+
@SuppressWarnings({"WeakerAccess", "unused", "deprecation"})
52+
public final class DfuServiceInitiator {
5353
public static final int DEFAULT_PRN_VALUE = 12;
54+
public static final int DEFAULT_MBR_SIZE = 0x1000;
5455

5556
/** Constant used to narrow the scope of the update to system components (SD+BL) only. */
5657
public static final int SCOPE_SYSTEM_COMPONENTS = 1;
@@ -80,6 +81,7 @@ public class DfuServiceInitiator {
8081
private boolean enableUnsafeExperimentalButtonlessDfu = false;
8182
private boolean disableResume = false;
8283
private int numberOfRetries = 0; // 0 to be backwards compatible
84+
private int mbrSize = DEFAULT_MBR_SIZE;
8385

8486
private Boolean packetReceiptNotificationsEnabled;
8587
private int numberOfPackets = 12;
@@ -374,6 +376,32 @@ else if (scope == (SCOPE_APPLICATION | SCOPE_SYSTEM_COMPONENTS))
374376
return this;
375377
}
376378

379+
/**
380+
* This method sets the size of an MBR (Master Boot Record). It should be used only
381+
* when updating a file from a HEX file. If you use BIN or ZIP, value set here will
382+
* be ignored.
383+
* <p>
384+
* The MBR size is important for the HEX parser, which has to cut it from the Soft Device's
385+
* HEX before sending it to the DFU target. The MBR can't be updated using DFU, and the
386+
* bootloader expects only the Soft Device bytes. Usually, the Soft Device HEX provided
387+
* by Nordic contains an MBR at addresses 0x0000 to 0x1000.
388+
* 0x1000 is the default size of MBR which will be used.
389+
* <p>
390+
* If you have a HEX file which address start from 0 and want to send the whole BIN content
391+
* from it, you have to set the MBR size to 0, otherwise first 4096 bytes will be cut off.
392+
* <p>
393+
* The value set here will not be used if the {@link DfuSettingsConstants#SETTINGS_MBR_SIZE}
394+
* is set in Shared Preferences.
395+
*
396+
* @param mbrSize the MBR size in bytes. Defaults to 4096 (0x1000) bytes.
397+
* @return the builder
398+
* @see DfuSettingsConstants#SETTINGS_MBR_SIZE
399+
*/
400+
public DfuServiceInitiator setMbrSize(@IntRange(from = 0) final int mbrSize) {
401+
this.mbrSize = mbrSize;
402+
return this;
403+
}
404+
377405
/**
378406
* Set this flag to true to enable experimental buttonless feature in Secure DFU. When the
379407
* experimental Buttonless DFU Service is found on a device, the service will use it to
@@ -730,6 +758,7 @@ public DfuServiceController start(@NonNull final Context context, @NonNull final
730758
intent.putExtra(DfuBaseService.EXTRA_FORCE_DFU, forceDfu);
731759
intent.putExtra(DfuBaseService.EXTRA_DISABLE_RESUME, disableResume);
732760
intent.putExtra(DfuBaseService.EXTRA_MAX_DFU_ATTEMPTS, numberOfRetries);
761+
intent.putExtra(DfuBaseService.EXTRA_MBR_SIZE, mbrSize);
733762
if (mtu > 0)
734763
intent.putExtra(DfuBaseService.EXTRA_MTU, mtu);
735764
intent.putExtra(DfuBaseService.EXTRA_CURRENT_MTU, currentMtu);

dfu/src/main/java/no/nordicsemi/android/dfu/DfuSettingsConstants.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@
2424

2525
import android.bluetooth.BluetoothGattCharacteristic;
2626

27+
/**
28+
* A collection of constants used for reading DFU constants from
29+
* {@link android.preference.PreferenceManager} in previous versions of DFU Library.
30+
*
31+
* @deprecated Use {@link DfuServiceInitiator} methods instead.
32+
*/
2733
@SuppressWarnings("DeprecatedIsStillUsed")
34+
@Deprecated
2835
public interface DfuSettingsConstants {
2936
/**
3037
* This property must contain a boolean value.
@@ -91,15 +98,12 @@ public interface DfuSettingsConstants {
9198
* If you are using the PC nrf util tool to create a ZIP Distribution Packet with the
9299
* firmware and Init Packet this option does not apply as the nrf tool will convert
93100
* HEX to BIN itself.
101+
*
102+
* @deprecated Use {@link DfuServiceInitiator#setMbrSize(int)} instead.
94103
*/
104+
@Deprecated
95105
String SETTINGS_MBR_SIZE = "settings_mbr_size";
96106

97-
/**
98-
* The default value of the MBR size.
99-
* @see #SETTINGS_DEFAULT_MBR_SIZE
100-
*/
101-
int SETTINGS_DEFAULT_MBR_SIZE = 0x1000;
102-
103107
/**
104108
* This property must contain a boolean value.
105109
* <p>

0 commit comments

Comments
 (0)