Skip to content

Commit a0220a1

Browse files
Refactor SoftButtonState & SoftButtonObject
1 parent a915ec3 commit a0220a1

2 files changed

Lines changed: 34 additions & 21 deletions

File tree

base/src/main/java/com/smartdevicelink/managers/screen/SoftButtonObject.java

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
package com.smartdevicelink.managers.screen;
3333

3434
import android.support.annotation.NonNull;
35-
import android.util.Log;
3635

36+
import com.smartdevicelink.managers.file.filetypes.SdlArtwork;
3737
import com.smartdevicelink.proxy.rpc.OnButtonEvent;
3838
import com.smartdevicelink.proxy.rpc.OnButtonPress;
3939
import com.smartdevicelink.proxy.rpc.SoftButton;
@@ -44,14 +44,13 @@
4444

4545
/**
4646
* <strong>SoftButtonObject</strong> <br>
47-
* SoftButtonObject define a button that can have multiple SoftButtonState values.<br>
47+
* SoftButtonObject defines a button that can have multiple SoftButtonState values.<br>
4848
* The states of SoftButtonObject allow the developer to not have to manage multiple SoftButtons that have very similar functionality.<br>
4949
* For example, a repeat button in a music app can be thought of as one SoftButtonObject with three typical states: repeat off, repeat 1, and repeat on.<br>
5050
* @see SoftButtonState
5151
*/
5252
public class SoftButtonObject {
5353

54-
private static final String TAG = "SoftButtonObject";
5554
static int SOFT_BUTTON_ID_NOT_SET_VALUE = -1;
5655
static int SOFT_BUTTON_ID_MIN_VALUE = 0;
5756
static int SOFT_BUTTON_ID_MAX_VALUE = 65535;
@@ -74,13 +73,13 @@ public SoftButtonObject(@NonNull String name, @NonNull List<SoftButtonState> sta
7473

7574
// Make sure there aren't two states with the same name
7675
if (hasTwoStatesOfSameName(states)) {
77-
Log.e(TAG, "Two states have the same name in states list for soft button object");
76+
DebugTool.logError("Two states have the same name in states list for soft button object");
7877
return;
7978
}
8079

8180
this.name = name;
8281
this.states = states;
83-
currentStateName = initialStateName;
82+
this.currentStateName = initialStateName;
8483
this.buttonId = SOFT_BUTTON_ID_NOT_SET_VALUE;
8584
this.onEventListener = onEventListener;
8685
}
@@ -95,6 +94,16 @@ public SoftButtonObject(@NonNull String name, @NonNull SoftButtonState state, On
9594
this(name, Collections.singletonList(state), state.getName(), onEventListener);
9695
}
9796

97+
/**
98+
* Create a new instance of the SoftButtonObject with one state
99+
* @param name a String value represents name of the object
100+
* @param artwork a SdlArtwork to be displayed on the button
101+
* @param onEventListener a listener that has a callback that will be triggered when a button event happens
102+
*/
103+
public SoftButtonObject(@NonNull String name, @NonNull String text, SdlArtwork artwork, OnEventListener onEventListener) {
104+
this(name, Collections.singletonList(new SoftButtonState(name, text, artwork)), name, onEventListener);
105+
}
106+
98107
/**
99108
* Transition the SoftButtonObject to a specific state
100109
* @param newStateName a String value represents the name fo the state that we want to transition the SoftButtonObject to
@@ -103,17 +112,23 @@ public SoftButtonObject(@NonNull String name, @NonNull SoftButtonState state, On
103112
public boolean transitionToStateByName(@NonNull String newStateName) {
104113
SoftButtonState newState = getStateByName(newStateName);
105114
if (newState == null) {
106-
Log.e(TAG, String.format("Attempted to transition to state: %s on soft button object: %s but no state with that name was found", newStateName, this.name));
115+
DebugTool.logError(String.format("Attempted to transition to state: %s on soft button object: %s but no state with that name was found", newStateName, this.name));
116+
return false;
117+
}
118+
119+
if (states.size() == 1) {
120+
DebugTool.logWarning("There's only one state, so no transitioning is possible!");
107121
return false;
108122
}
123+
109124
DebugTool.logInfo(String.format("Transitioning soft button object %s to state %s", this.name, newStateName));
110125
currentStateName = newStateName;
111126

112127
// Send a new Show RPC because the state has changed which means the actual SoftButton has changed
113128
if (updateListener != null) {
114129
updateListener.onUpdate();
115130
} else {
116-
Log.e(TAG, String.format("SoftButtonManager is not set for soft button object: %s. Update cannot be triggered", this.name));
131+
DebugTool.logError(String.format("SoftButtonManager is not set for soft button object: %s. Update cannot be triggered", this.name));
117132
}
118133

119134
return true;
@@ -126,16 +141,13 @@ public void transitionToNextState() {
126141
String nextStateName = null;
127142
for (int i = 0; i < states.size(); i++) {
128143
if (states.get(i).getName().equals(currentStateName)) {
129-
if (i == (states.size() - 1)) {
130-
nextStateName = states.get(0).getName();
131-
} else {
132-
nextStateName = states.get(i + 1).getName();
133-
}
144+
int nextStateNumber = (i == states.size() - 1) ? 0 : (i + 1);
145+
nextStateName = states.get(nextStateNumber).getName();
134146
break;
135147
}
136148
}
137149
if (nextStateName == null) {
138-
Log.e(TAG, String.format("Current state name : %s cannot be found for soft button object %s", currentStateName, this.name));
150+
DebugTool.logError(String.format("Current state name : %s cannot be found for soft button object %s", currentStateName, this.name));
139151
return;
140152
}
141153
transitionToStateByName(nextStateName);
@@ -148,7 +160,7 @@ public void transitionToNextState() {
148160
public SoftButtonState getCurrentState() {
149161
SoftButtonState state = getStateByName(currentStateName);
150162
if (state == null) {
151-
Log.e(TAG, String.format("Current state name : %s cannot be found for soft button object %s", currentStateName, this.name));
163+
DebugTool.logError(String.format("Current state name : %s cannot be found for soft button object %s", currentStateName, this.name));
152164
}
153165
return state;
154166
}
@@ -273,7 +285,7 @@ public int getButtonId() {
273285
*/
274286
public void setButtonId(int buttonId) {
275287
if (buttonId < SOFT_BUTTON_ID_MIN_VALUE){
276-
Log.e(TAG, "buttonId has to be equal or more than " + SOFT_BUTTON_ID_MIN_VALUE);
288+
DebugTool.logError("buttonId has to be equal or more than " + SOFT_BUTTON_ID_MIN_VALUE);
277289
return;
278290
}
279291
this.buttonId = buttonId;
@@ -303,7 +315,7 @@ public interface OnEventListener{
303315
/**
304316
* A listener interface that is used by SoftButtonObject to request an update from SoftButtonManager
305317
*/
306-
interface UpdateListener{
318+
interface UpdateListener {
307319
/**
308320
* Requests an update from SoftButtonManager
309321
*/

base/src/main/java/com/smartdevicelink/managers/screen/SoftButtonState.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
3232
package com.smartdevicelink.managers.screen;
3333

3434
import android.support.annotation.NonNull;
35-
import android.util.Log;
3635

3736
import com.smartdevicelink.managers.file.filetypes.SdlArtwork;
3837
import com.smartdevicelink.proxy.rpc.SoftButton;
3938
import com.smartdevicelink.proxy.rpc.enums.SoftButtonType;
4039
import com.smartdevicelink.proxy.rpc.enums.SystemAction;
40+
import com.smartdevicelink.util.DebugTool;
4141

4242
/**
4343
* <strong>SoftButtonState</strong> <br>
@@ -48,7 +48,6 @@
4848
*/
4949
public class SoftButtonState {
5050

51-
private static final String TAG = "SoftButtonState";
5251
private String name;
5352
private SdlArtwork artwork;
5453
private final SoftButton softButton;
@@ -62,7 +61,7 @@ public class SoftButtonState {
6261
*/
6362
public SoftButtonState(@NonNull String name, String text, SdlArtwork artwork) {
6463
if (text == null && artwork == null) {
65-
Log.e(TAG, "Attempted to create an invalid soft button state: text and artwork are both null");
64+
DebugTool.logError("Attempted to create an invalid soft button state: text and artwork are both null");
6665
softButton = null;
6766
return;
6867
}
@@ -84,13 +83,15 @@ public SoftButtonState(@NonNull String name, String text, SdlArtwork artwork) {
8483

8584
// Set the SoftButton's image
8685
if (artwork != null) {
87-
softButton.setImage(artwork.getImageRPC());
86+
this.softButton.setImage(artwork.getImageRPC());
8887
}
8988

9089
// Set the SoftButton's text
9190
if (text != null) {
92-
softButton.setText(text);
91+
this.softButton.setText(text);
9392
}
93+
94+
this.softButton.setSystemAction(SystemAction.DEFAULT_ACTION);
9495
}
9596

9697
/**

0 commit comments

Comments
 (0)