メインルーチン
code:Gyaim.java
//
// Gyaim for Android
//
// Slimeを改造し、キーボード専用にする 2017/06/27 12:17:19 (masui)
//
package com.pitecan.gyaim;
import android.inputmethodservice.InputMethodService;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.CursorAnchorInfo;
import android.view.ViewGroup;
import android.view.View;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.content.Context;
import android.text.ClipboardManager;
// Android3.0以上の場合こちらを使うらしい
//import android.content.ClipData;
//import android.content.ClipboardManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.graphics.Matrix;
import android.graphics.Rect;
ここから本体
各種のonXxxYyy()の機能や順番がよくわからない
code:Gyaim.java
public class Gyaim extends InputMethodService
{
private CandView candView;
private KeyController keyController;
private Search search;
private ClipboardManager clipboardManager;
private String oldClipboardText = "";
static final int MAXCANDS = 50;
/**
* Main initialization of the input method component. Be sure to call
* to super class.
*/
@Override
public void onCreate()
{
super.onCreate(); // 必須
search = new Search(this);
clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
}
/**
* This is the point where you can do all of your UI initialization. It
* is called after creation and any configuration change.
*/
/*
@Override public void onInitializeInterface() {
super.onInitializeInterface(); // 必要??
}
*/
/**
* This is the main point where we do our initialization of the input method
* to begin operating on an application. At this point we have been
* bound to the client, and are now receiving all of the detailed information
* about the target of our edits.
*/
@Override public void onStartInput(EditorInfo attribute, boolean restarting) {
super.onStartInput(attribute, restarting);
InputConnection ic = getCurrentInputConnection();
if(ic != null){
ic.requestCursorUpdates(InputConnection.CURSOR_UPDATE_MONITOR);
}
}
/**
* Called by the framework when your view for creating input needs to
* be generated. This will be called the first time your input method
* is displayed, and every time it needs to be re-created such as due to
* a configuration change.
*/
/*
InputViewはキーボードを表示するViewなのだが、これが出ているとき
アプリの大きさはそのぶんだけ減る。なので入力枠が最下部にある
場合などは候補を出すためにこれが定義されていると良いのかもしれないのだが...
*/
@Override public View onCreateInputView(){
// return super.onCreateInputView(); // 必要??
CandView dummyCandView = (CandView) getLayoutInflater().inflate(R.layout.candidate, null);
Message.message("Gyaim","CANDVIEW = " + candView);
// candView.setY(100);
// candView.setTop(100);
// setCandidatesViewShown(true); // これが無いと表示されない
keyController = new KeyController(this,candView); // この場所?
return candView;
}
/*
候補を表示するView。
*/
@Override public View onCreateCandidatesView(){
super.onCreateCandidatesView(); // 必要??
candView = (CandView) getLayoutInflater().inflate(R.layout.candidate, null);
Message.message("Gyaim","CANDVIEW = " + candView);
// candView.setY(100);
// candView.setTop(100);
setCandidatesViewShown(true); // これが無いと表示されない
keyController = new KeyController(this,candView); // この場所?
return candView;
}
public void commitText(String s){
InputConnection ic = getCurrentInputConnection();
if(ic != null) ic.commitText(s,1); // 入力貼り付け
}
public void showComposingText(String text){
InputConnection ic = getCurrentInputConnection();
if(ic != null){
ic.setComposingText(text,1);
}
}
//
// 新規登録用にクリップボードの単語を返す
//
public void setOldClipboardText(){
CharSequence seq = clipboardManager.getText();
oldClipboardText = (seq == null ? "" : seq.toString());
}
public String getNewClipboardText(){
CharSequence seq = clipboardManager.getText();
String cilpboardText = (seq == null ? "" : seq.toString());
if(cilpboardText.equals(oldClipboardText)){
return "";
}
else {
return cilpboardText;
}
}
//
// KeyイベントをすべてKeyControllerに投げる
//
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Message.message("Gyaim","onKeyDown - keyCode = "+keyCode);
return keyController.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
Message.message("Gyaim","onKeyUp - keyCode = "+keyCode);
return keyController.onKeyUp(keyCode, event);
}
// こういうものはないようだが
//public boolean onTouchEvent(MotionEvent event) {
// Message.message("Gyaim","onTouchEvent - ");
// return false;
//}
//
// ConnectivityManagerというのを使って、ネットが使えるかどうかを判断し、
// ネットがあるときは常にGoogleIMEを使うようにしてみる
// AndroidManifest.xmlに以下の追加が必要である
// <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
//
// メソッドがActivityの中でしか使えないのでここで定義する。
//
public Boolean isConnected(){
ConnectivityManager connManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
Message.message("Gyaim","networkInfo = " + networkInfo);
return networkInfo != null && networkInfo.isConnected();
}
public void logWord(String word){
}
void updateCursor (Rect newCursor){
Message.message("Gyaim","updateCursor: rect = " + newCursor);
}
void updateCursorAnchorInfo (View view, CursorAnchorInfo cursorAnchorInfo){
Message.message("Gyaim","updateCursorAnchorInfo: view = " + view);
}
//
// キャレットの位置を取得する方法?
//
@Override
public void onUpdateCursorAnchorInfo(CursorAnchorInfo cursorAnchorInfo){
/*
InputConnection ic = getCurrentInputConnection();
ic.requestCursorUpdates(int cursorUpdateMode)
cursorAnchorInfo.getInsertionMarkerHorizontal() (x)
and cursorAnchorInfo.getInsertionMarkerTop() (y).
*/
Message.message("Gyaim","insertionMarkerTop = " + cursorAnchorInfo.getInsertionMarkerTop()); // NaNになることあり
Message.message("Gyaim","insertionMarkerHorizontal = " + cursorAnchorInfo.getInsertionMarkerHorizontal()); // NaNになることあり
Matrix matrix = cursorAnchorInfo.getMatrix();
matrix.getValues(f);
Message.message("Gyaim","matrix = "+f0); Message.message("Gyaim","matrix = "+f1); Message.message("Gyaim","matrix = "+f2); Message.message("Gyaim","matrix = "+f3); Message.message("Gyaim","matrix = "+f4); Message.message("Gyaim","matrix = "+f5); Message.message("Gyaim","matrix = "+f6); Message.message("Gyaim","matrix = "+f7); Message.message("Gyaim","matrix = "+f8); candView.getLocationInWindow(loc);
Message.message("Gyaim","location x= "+loc0); Message.message("Gyaim","location y= "+loc1); float floc[] = new float2; floc1 = (float)cursorAnchorInfo.getInsertionMarkerTop(); matrix.mapPoints(floc);
Message.message("Gyaim","flocation x= "+floc0); Message.message("Gyaim","flocation y= "+floc1); LayoutParams lp = candView.getLayoutParams();
MarginLayoutParams mlp = (MarginLayoutParams)lp;
//AbsoluteLayoutParams alp = (AbsoluteLayoutParams)lp; こんなのないか
Message.message("Gyaim","topmargin = "+mlp.topMargin);
Message.message("Gyaim","bottommargin = "+mlp.bottomMargin);
Message.message("Gyaim","leftmargin = "+mlp.leftMargin);
Message.message("Gyaim","rightmargin = "+mlp.rightMargin);
int markerTop = (int)cursorAnchorInfo.getInsertionMarkerTop();
int margin;
margin = 0;
}
else {
//margin = 1400 - (int)(f2 + f5 + markerTop); margin = 1400 - (int)(floc1); // + markerTop); }
if(margin < 0) margin = 0;
mlp.setMargins(mlp.leftMargin, mlp.topMargin, mlp.rightMargin, margin); // これで表示場所を変えられる
//マージンを設定
candView.setLayoutParams(mlp);
}
}