Skip to content

Commit 7aaec18

Browse files
committed
better support for copy&paste (automatic display)
1 parent 9468e2c commit 7aaec18

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

project/src/main/java/de/earthlingz/oerszebra/DroidZebra.java

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import android.app.AlertDialog;
2222
import android.app.Dialog;
2323
import android.app.ProgressDialog;
24+
import android.content.ClipData;
25+
import android.content.ClipboardManager;
2426
import android.content.DialogInterface;
2527
import android.content.Intent;
2628
import android.content.SharedPreferences;
@@ -112,7 +114,8 @@ public class DroidZebra extends FragmentActivity
112114
SETTINGS_KEY_DISPLAY_ENABLE_ANIMATIONS = "settings_ui_display_enable_animations";
113115

114116
private final CandidateMoves mCandidateMoves = new CandidateMoves();
115-
public int mSettingFunction = DEFAULT_SETTING_FUNCTION;
117+
private ClipboardManager clipboard;
118+
public int mSettingFunction = DEFAULT_SETTING_FUNCTION;
116119
public boolean mSettingAutoMakeForcedMoves = DEFAULT_SETTING_AUTO_MAKE_FORCED_MOVES;
117120
public int mSettingZebraRandomness = DEFAULT_SETTING_RANDOMNESS;
118121
public String mSettingZebraForceOpening = DEFAULT_SETTING_FORCE_OPENING;
@@ -143,8 +146,10 @@ public class DroidZebra extends FragmentActivity
143146
private DroidZebraHandler mDroidZebraHandler = null;
144147

145148
public DroidZebra() {
149+
146150
initBoard();
147-
}
151+
152+
}
148153

149154
public boolean isThinking() {
150155
return mZebraThread.isThinking();
@@ -330,6 +335,9 @@ public void onCreate(Bundle savedInstanceState)
330335
{
331336
super.onCreate(savedInstanceState);
332337

338+
339+
clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
340+
333341
setContentView(R.layout.spash_layout);
334342
if(android.os.Build.VERSION.SDK_INT >= 11) {
335343
new ActionBarHelper().hide();
@@ -686,17 +694,17 @@ public void showQuitDialog() {
686694
}
687695

688696
private void enterMoves() {
689-
DialogFragment newFragment = DialogMoves.newInstance();
690-
showDialog(newFragment, "dialog_moves");
697+
DialogFragment newFragment = DialogMoves.newInstance(clipboard);
698+
showDialog(newFragment, "dialog_moves");
691699
}
692700

693701
private void setUpBoard(String s) {
694702
final LinkedList<Move> moves = makeMoveList(s);
695703
mZebraThread.sendReplayMoves(moves);
696704
}
697705

698-
private LinkedList<Move> makeMoveList(String s) {
699-
LinkedList<Move> moves = new LinkedList<Move>();
706+
private static LinkedList<Move> makeMoveList(String s) {
707+
LinkedList<Move> moves = new LinkedList<Move>();
700708
Pattern p = Pattern.compile("([ABCDEFGH]{1}[12345678]{1})+");
701709
Matcher matcher = p.matcher(s.toUpperCase());
702710
if (!matcher.matches()) {
@@ -972,8 +980,12 @@ public void onClick(DialogInterface dialog, int id) {
972980
// Moves as Text
973981
public static class DialogMoves extends DialogFragment {
974982

975-
public static DialogMoves newInstance() {
976-
return new DialogMoves();
983+
private ClipboardManager clipBoard;
984+
985+
public static DialogMoves newInstance(ClipboardManager clipBoard) {
986+
DialogMoves moves = new DialogMoves();
987+
moves.setClipBoard(clipBoard);
988+
return moves;
977989
}
978990

979991
public DroidZebra getDroidZebra() {
@@ -991,6 +1003,24 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
9911003
input.setInputType(InputType.TYPE_CLASS_TEXT);
9921004
builder.setView(input);
9931005

1006+
if (clipBoard != null) {
1007+
String possibleMatch = null;
1008+
ClipData primaryClip = clipBoard.getPrimaryClip();
1009+
for (int i = 0; i < primaryClip.getItemCount(); i++) {
1010+
ClipData.Item clip = primaryClip.getItemAt(i);
1011+
CharSequence charSequence = clip.coerceToText(getActivity().getBaseContext());
1012+
if (charSequence != null) {
1013+
possibleMatch = charSequence.toString();
1014+
break;
1015+
}
1016+
}
1017+
1018+
if (possibleMatch != null && !DroidZebra.makeMoveList(possibleMatch).isEmpty()) {
1019+
input.setText(possibleMatch);
1020+
}
1021+
}
1022+
1023+
9941024
// Set up the buttons
9951025
builder.setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
9961026
@Override
@@ -1007,6 +1037,10 @@ public void onClick(DialogInterface dialog, int which) {
10071037

10081038
return builder.create();
10091039
}
1040+
1041+
public void setClipBoard(ClipboardManager clipBoard) {
1042+
this.clipBoard = clipBoard;
1043+
}
10101044
}
10111045

10121046
//-------------------------------------------------------------------------

0 commit comments

Comments
 (0)