/*
 * Copyright 2012 Kulikov Dmitriy
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package javax.microedition.lcdui.pointer;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.event.CanvasEvent;

public class KeyRepeater implements Runnable {
	public static long[] INTERVALS =
			{
					400,
					200,
					400,
					128,
					128,
					128,
					128,
					128,
					80
			};

	protected Canvas target;

	protected Thread thread;
	protected Object waiter;
	protected boolean isrunning;

	protected boolean enabled;
	protected int position;

	protected int keyCode;
	protected int secondKeyCode;

	public KeyRepeater() {
		waiter = new Object();

		thread = new Thread(this);
		thread.start();
	}

	public void setTarget(Canvas canvas) {
		if (canvas == null) {
			stop();
		}

		target = canvas;
	}

	public void start(int keyCode) {
		start(keyCode, 0);
	}

	public void start(int keyCode, int secondKeyCode) {
		if (target == null) {
			return;
		}

		synchronized (waiter) {
			if (isrunning) {
				return;
			}

			this.keyCode = keyCode;
			this.secondKeyCode = secondKeyCode;

			enabled = true;
			position = 0;

			waiter.notifyAll();
		}
	}

	public void stop() {
		enabled = false;
		thread.interrupt();
	}

	public boolean isRunning() {
		return isrunning;
	}

	public void run() {
		while (true) {
			try {
				synchronized (waiter) {
					isrunning = false;
					waiter.wait();

					isrunning = true;
				}

				while (enabled) {
					Thread.sleep(INTERVALS[position]);

					target.postEvent(CanvasEvent.getInstance(target, CanvasEvent.KEY_REPEATED, keyCode));

					if (secondKeyCode != 0) {
						target.postEvent(CanvasEvent.getInstance(target, CanvasEvent.KEY_REPEATED, secondKeyCode));
					}

					if (position < INTERVALS.length - 1) {
						position++;
					}
				}
			} catch (InterruptedException ie) {
				ie.printStackTrace();
			}
		}
	}
}
@@ -30,19 +30,53 @@
 Dmitriy%0A
+ * Copyright 2018 Nikita Shakarun%0A
  *%0A
-
  * Licen
@@ -811,21 +811,22 @@
 ble %7B%0A%09p
-ublic
+rivate
  static 
@@ -1000,23 +1000,27 @@
 ead;%0A%09pr
-otected
+ivate final
  Object 
@@ -1026,31 +1026,29 @@
  waiter;%0A%09pr
-otec
+iva
 te
-d
  boolean isr
@@ -1109,31 +1109,29 @@
 sition;%0A%0A%09pr
-otec
+iva
 te
-d
  int keyCode
@@ -1131,31 +1131,29 @@
 keyCode;%0A%09pr
-otec
+iva
 te
-d
  int secondK
@@ -1369,67 +1369,8 @@
 %09%7D%0A%0A
-%09public void start(int keyCode) %7B%0A%09%09start(keyCode, 0);%0A%09%7D%0A%0A
 %09pub
@@ -1728,62 +1728,8 @@
 %09%7D%0A%0A
-%09public boolean isRunning() %7B%0A%09%09return isrunning;%0A%09%7D%0A%0A
 %09pub
@@ -2288,29 +2288,46 @@
 %09%09%09%09
-ie.
+// Don't need to 
 print
-S
+ s
 tack
-T
+t
 race
-();
+ here
 %0A%09%09%09

Refactor KeyRepeater