Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Commit

Permalink
Make add word dialog autofocus text field, input word after adding an…
Browse files Browse the repository at this point in the history
…d assign max priority to newly added word
  • Loading branch information
akasaka committed Feb 22, 2021
1 parent 44c56e2 commit 4eff06a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
25 changes: 24 additions & 1 deletion src/org/nyanya/android/traditionalt9/AddWordAct.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import org.nyanya.android.traditionalt9.T9DB.DBSettings.SETTING;

public class AddWordAct extends Activity {
public static final String WORD_ADDED_NOTIFICATION = "TT9_WORD_ADDED_NOTIFICATION";
public static final String WORD_ADDED_NOTIFICATION_WORD_VALUE = "TT9_WORD_ADDED_NOTIFICATION_WORD_VALUE";

View main;
int lang;
String origword;


@Override
protected void onCreate(Bundle savedData) {
super.onCreate(savedData);
Expand All @@ -37,11 +43,28 @@ protected void onCreate(Bundle savedData) {
main = v;
}

@Override
protected void onResume() {
super.onResume();
EditText et = (EditText) findViewById(R.id.add_word_text);
if(et != null) {
et.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
}
}

public void addWordButton(View v) {
EditText et = (EditText) main.findViewById(R.id.add_word_text);
String word = et.getText().toString();
// Log.d("AddWordAct", "adding word: " + et.getText());
doAddWord(et.getText().toString());
doAddWord(word);
this.finish();

Intent intent = new Intent();
intent.setAction(WORD_ADDED_NOTIFICATION);
intent.putExtra(WORD_ADDED_NOTIFICATION_WORD_VALUE, word);
sendBroadcast(intent);
}

public void doAddWord(String text) {
Expand Down
7 changes: 5 additions & 2 deletions src/org/nyanya/android/traditionalt9/T9DB.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,10 @@ protected void addWord(String iword, LANGUAGE lang, int freq) throws DBException
return;
}
try {
db.insertOrThrow(WORD_TABLE_NAME, null, values);
long insertedWordId = db.insertOrThrow(WORD_TABLE_NAME, null, values);
if(insertedWordId > 0) {
incrementWord(insertedWordId);
}
} catch (SQLiteConstraintException e) {
String msg = r.getString(R.string.add_word_exist2, iword, lang.name());
Log.w("T9DB.addWord", msg);
Expand All @@ -365,7 +368,7 @@ protected void addWord(String iword, LANGUAGE lang) throws DBException {
addWord(iword, lang, 1);
}

protected void incrementWord(int id) {
protected void incrementWord(long id) {
if (!checkReady()) {
Log.e("T9DB.incrementWord", "not ready");
Toast.makeText(mContext, R.string.database_notready, Toast.LENGTH_SHORT).show();
Expand Down
28 changes: 28 additions & 0 deletions src/org/nyanya/android/traditionalt9/TraditionalT9.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.nyanya.android.traditionalt9;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.KeyboardView;
Expand Down Expand Up @@ -96,6 +99,25 @@ public void run() {

private Toast modeNotification = null;

private BroadcastReceiver mWordAddedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(AddWordAct.WORD_ADDED_NOTIFICATION)) {
String word = intent.getStringExtra(AddWordAct.WORD_ADDED_NOTIFICATION_WORD_VALUE);
if(word != null) {
if (currentInputConnection == null)
return;
currentInputConnection.beginBatchEdit();
if (mComposing.length() > 0 || mComposingI.length() > 0) {
commitTyped();
}
currentInputConnection.commitText(word, 1);
currentInputConnection.endBatchEdit();
}
}
}
};

/**
* Main initialization of the input method component. Be sure to call to
* super class.
Expand All @@ -111,6 +133,10 @@ public void onCreate() {
interfacehandler = new InterfaceHandler(getLayoutInflater().inflate(R.layout.mainview,
null), this);
}

IntentFilter wordAddedIFlt = new IntentFilter();
wordAddedIFlt.addAction(AddWordAct.WORD_ADDED_NOTIFICATION);
registerReceiver(mWordAddedReceiver, wordAddedIFlt);
}

@Override
Expand Down Expand Up @@ -220,6 +246,7 @@ protected void showAddWord() {
awintent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
awintent.putExtra("org.nyanya.android.traditionalt9.word", template);
awintent.putExtra("org.nyanya.android.traditionalt9.lang", mLang.id);

clearState();
currentInputConnection.setComposingText("", 0);
currentInputConnection.finishComposingText();
Expand Down Expand Up @@ -462,6 +489,7 @@ private void finish() {
@Override
public void onDestroy() {
db.close();
unregisterReceiver(mWordAddedReceiver);
super.onDestroy();
}

Expand Down

0 comments on commit 4eff06a

Please sign in to comment.