Skip to content

Commit

Permalink
Implement roles for volume buttons: none, text size, send history
Browse files Browse the repository at this point in the history
Keeps the default setting to "text size" for backwards compatibility.
  • Loading branch information
zopieux committed Jul 22, 2016
1 parent 9426987 commit 31dc9ae
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public class BufferFragment extends Fragment implements BufferEye, OnKeyListener
private Buffer buffer;

private ChatLinesAdapter linesAdapter;
private int historyScrollIndex;
private String historyUserLine;
private boolean uiInputSetProgrammatically = false;

private Logger logger = LoggerFactory.getLogger(toString());

Expand All @@ -76,6 +79,7 @@ public static BufferFragment newInstance(String tag) {
Bundle args = new Bundle();
args.putString(TAG, tag);
fragment.setArguments(args);
fragment.resetHistory();
return fragment;
}

Expand Down Expand Up @@ -410,7 +414,7 @@ public boolean onKey(View v, int keycode, KeyEvent event) {
if (DEBUG_TAB_COMPLETE) logger.debug("onKey(..., {}, ...)", keycode);
int action = event.getAction();
return checkSendMessage(keycode, action) ||
checkVolumeButtonResize(keycode, action) ||
checkVolumeButtons(keycode, action) ||
checkForTabCompletion(keycode, action);

}
Expand All @@ -432,27 +436,58 @@ private boolean checkForTabCompletion(int keycode, int action) {
return false;
}

private boolean checkVolumeButtonResize(int keycode, int action) {
if (keycode == KeyEvent.KEYCODE_VOLUME_DOWN || keycode == KeyEvent.KEYCODE_VOLUME_UP) {
if (P.volumeBtnSize) {
if (action == KeyEvent.ACTION_UP) {
float textSize = P.textSize;
switch (keycode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (textSize < 30) textSize += 1;
break;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (textSize > 5) textSize -= 1;
break;
}
P.setTextSizeAndLetterWidth(textSize);
private boolean checkVolumeButtons(int keycode, int action) {
if (!(keycode == KeyEvent.KEYCODE_VOLUME_DOWN || keycode == KeyEvent.KEYCODE_VOLUME_UP))
return false;
if (P.volumeRole == P.VolumeRole.NONE)
return false;
if (action != KeyEvent.ACTION_UP)
// consume DOWN & MULTIPLE volume button events
return true;
switch(P.volumeRole) {
case TEXT_SIZE:
float textSize = P.textSize;
switch (keycode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (textSize < 30) textSize += 1;
break;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (textSize > 5) textSize -= 1;
break;
}
P.setTextSizeAndLetterWidth(textSize);
return true;
case SEND_HISTORY:
switch (keycode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (historyScrollIndex >= P.sentMessages.size() - 1)
return false;
historyScrollIndex++;
break;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (historyScrollIndex < 0)
return false;
historyScrollIndex--;
break;
}
uiInputSetProgrammatically = true;
if (historyScrollIndex == -1) {
uiInput.setText(historyUserLine);
uiInput.setSelection(uiInput.length());
return true;
}
uiInput.setText(P.sentMessages.get(P.sentMessages.size() - historyScrollIndex - 1));
uiInput.setSelection(uiInput.length());
return true;
}
}
return false;
}

private void resetHistory() {
historyScrollIndex = -1;
historyUserLine = "";
}

/** the only OnClickListener's method
** our own send button or tab button pressed */
@Override
Expand Down Expand Up @@ -488,6 +523,7 @@ private void sendMessage() {
EventBus.getDefault().post(new SendMessageEvent("input " + fullName + " " + line));
}
uiInput.setText(""); // this will reset tab completion
resetHistory();
}

////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -568,6 +604,10 @@ public void setText(String text) {
public void afterTextChanged(Editable s) {
if (DEBUG_TAB_COMPLETE) logger.debug("afterTextChanged(...)");
tcInProgress = false;
if (!uiInputSetProgrammatically) {
historyUserLine = uiInput.getText().toString();
}
uiInputSetProgrammatically = false;
}

////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public static void init(@NonNull Context context) {
p.registerOnSharedPreferenceChangeListener(instance = new P());
}

public enum VolumeRole {NONE, TEXT_SIZE, SEND_HISTORY}

///////////////////////////////////////////////////////////////////////////////////////////// ui

public static boolean sortBuffers, showTitle, filterBuffers, optimizeTraffic;
Expand All @@ -69,7 +71,8 @@ public static void init(@NonNull Context context) {
public static boolean notificationEnable, notificationTicker, notificationLight, notificationVibrate;
public static String notificationSound;

public static boolean showSend, showTab, hotlistSync, volumeBtnSize;
public static boolean showSend, showTab, hotlistSync;
public static VolumeRole volumeRole;
public static String bufferFont;

public static boolean showBufferFilter;
Expand Down Expand Up @@ -102,7 +105,7 @@ public static void loadUIPreferences() {
showSend = p.getBoolean(PREF_SHOW_SEND, PREF_SHOW_SEND_D);
showTab = p.getBoolean(PREF_SHOW_TAB, PREF_SHOW_TAB_D);
hotlistSync = p.getBoolean(PREF_HOTLIST_SYNC, PREF_HOTLIST_SYNC_D);
volumeBtnSize = p.getBoolean(PREF_VOLUME_BTN_SIZE, PREF_VOLUME_BTN_SIZE_D);
setVolumeBtnRole();

// buffer list filter
showBufferFilter = p.getBoolean(PREF_SHOW_BUFFER_FILTER, PREF_SHOW_BUFFER_FILTER_D);
Expand Down Expand Up @@ -216,7 +219,7 @@ public static void loadConnectionPreferences() {
case PREF_SHOW_SEND: showSend = p.getBoolean(key, PREF_SHOW_SEND_D); break;
case PREF_SHOW_TAB: showTab = p.getBoolean(key, PREF_SHOW_TAB_D); break;
case PREF_HOTLIST_SYNC: hotlistSync = p.getBoolean(key, PREF_HOTLIST_SYNC_D); break;
case PREF_VOLUME_BTN_SIZE: volumeBtnSize = p.getBoolean(key, PREF_VOLUME_BTN_SIZE_D); break;
case PREF_VOLUME_BTN_ROLE: setVolumeBtnRole(); break;

// buffer list fragment
case PREF_SHOW_BUFFER_FILTER: showBufferFilter = p.getBoolean(key, PREF_SHOW_BUFFER_FILTER_D); break;
Expand All @@ -240,6 +243,16 @@ private static void setAlignment() {
}
}

private static void setVolumeBtnRole() {
String role = p.getString(PREF_VOLUME_BTN_ROLE, PREF_VOLUME_BTN_ROLE_D);
switch (role) {
case "none": volumeRole = VolumeRole.NONE; break;
case "text-size": volumeRole = VolumeRole.TEXT_SIZE; break;
case "send-history": volumeRole = VolumeRole.SEND_HISTORY; break;
default: volumeRole = VolumeRole.TEXT_SIZE; break;
}
}

private static void setTextSizeAndLetterWidth() {
textSize = Float.parseFloat(p.getString(PREF_TEXT_SIZE, PREF_TEXT_SIZE_D));
bufferFont = p.getString(PREF_BUFFER_FONT, PREF_BUFFER_FONT_D);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class Constants {
// buttons
public final static String PREF_SHOW_SEND = "sendbtn_show"; final public static boolean PREF_SHOW_SEND_D = true;
public final static String PREF_SHOW_TAB = "tabbtn_show"; final public static boolean PREF_SHOW_TAB_D = true;
public final static String PREF_VOLUME_BTN_SIZE = "volumebtn_size"; final public static boolean PREF_VOLUME_BTN_SIZE_D = true;
public final static String PREF_VOLUME_BTN_ROLE = "volumebtn_role"; final public static String PREF_VOLUME_BTN_ROLE_D = "text-size";

// notifications
final static public String PREF_NOTIFICATION_ENABLE = "notification_enable"; final public static boolean PREF_NOTIFICATION_ENABLE_D = true;
Expand Down
8 changes: 6 additions & 2 deletions weechat-android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,12 @@
<string name="pref_button_group">Buttons</string>
<string name="pref_tabbtn_show">Show tab button</string>
<string name="pref_sendbtn_show">Show send button</string>
<string name="pref_volumebtn_size">Volume buttons change text size</string>
<string name="pref_volumebtn_size_summary">If set, volume buttons will change text size instead of volume</string>
<string name="pref_volumebtn_role">Volume button role</string>
<string-array name="pref_volumebtn_role_names">
<item>Do nothing</item>
<item>Change text size</item>
<item>Navigate send history</item>
</string-array>

<!-- notifications -->

Expand Down
5 changes: 5 additions & 0 deletions weechat-android/src/main/res/values/values.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
<item >websocket</item>
<item >websocket-ssl</item>
</string-array>
<string-array name="settings_volumebtn_role_values">
<item>none</item>
<item>text-size</item>
<item>send-history</item>
</string-array>

<dimen name="dialog_item_padding_vertical">8dp</dimen>
<dimen name="drawer_width">320dp</dimen>
Expand Down
2 changes: 1 addition & 1 deletion weechat-android/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<PreferenceScreen android:key="button_group" android:title="@string/pref_button_group">
<CheckBoxPreference android:key="tabbtn_show" android:title="@string/pref_tabbtn_show" android:defaultValue="true" />
<CheckBoxPreference android:key="sendbtn_show" android:title="@string/pref_sendbtn_show" android:defaultValue="true" />
<CheckBoxPreference android:key="volumebtn_size" android:title="@string/pref_volumebtn_size" android:summary="@string/pref_volumebtn_size_summary" android:defaultValue="true" />
<ListPreference android:key="volumebtn_role" android:title="@string/pref_volumebtn_role" android:summary="%s" android:defaultValue="text-size" android:entries="@array/pref_volumebtn_role_names" android:entryValues="@array/settings_volumebtn_role_values" />
</PreferenceScreen>

<!-- notifications -->
Expand Down

0 comments on commit 31dc9ae

Please sign in to comment.