/*****
 *
 */
package com.hiroshica.android.input.nicownn2;
import android.inputmethodservice.InputMethodService;
import android.view.WindowManager;
import android.content.Context;
import android.view.View;
import android.view.KeyEvent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import android.content.pm.PackageManager;
import android.util.Log;
import android.os.*;
import android.view.inputmethod.*;
import android.content.res.Configuration;
import android.graphics.*;
import android.graphics.drawable.*;
import android.view.MotionEvent;

import android.app.Activity;
import android.content.Intent;

import java.lang.Object;
import java.util.List;

public class NicoWnnMushUp extends Activity {
	private static final String ACTION_INTERCEPT = "com.adamrocker.android.simeji.ACTION_INTERCEPT";
	private static final String CATEGORY_KEY = "com.adamrocker.android.simeji.REPLACE";
	private static final String REPLACE_KEY = "replace_key";
	private static final String INPUTCONNECTION_KEY = "inputconnection_key";

	private static final String GETACTION_INTERCEPT = "com.hiroshica.android.input.nicownn2.ACTION_INTERCEPT";

	private String mGetString;
	private String mReturnString;

	protected InputConnection mInputConnection = null;


	@Override public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent getintent = getIntent();
		String action = getintent.getAction();
		if (action != null && GETACTION_INTERCEPT.equals(action)) {
			mGetString = getintent.getStringExtra(REPLACE_KEY);
//			mInputConnection = (InputConnection)getintent.getExtra(INPUTCONNECTION_KEY);
			// push mushroom
			Intent intent = new Intent(ACTION_INTERCEPT);
			intent.addCategory(CATEGORY_KEY);
			intent.putExtra(REPLACE_KEY, mGetString);
			List info = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
			if (info.size() != 0) {
				startActivityForResult(intent, 0);
			}
		}
	}

	@Override public void onActivityResult(int req, int result, Intent intent) {
		if (req == 0 && result == RESULT_OK) {
			mReturnString = intent.getStringExtra("replace_key");
			//mInputConnection = getCurrentInputConnection();
			if (mInputConnection != null) {
				mInputConnection.commitText(mReturnString.subSequence(0,-1), -1);
			}
		}
	}
}
