/*
 * Copyright 2012 Kulikov Dmitriy
 * Copyright 2018 Nikita Shakarun
 *
 * 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 {
	private static long[] INTERVALS =
			{
					400,
					200,
					400,
					128,
					128,
					128,
					128,
					128,
					80
			};

	protected Canvas target;

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

	protected boolean enabled;
	protected int position;

	private int keyCode;
	private 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, 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();
	}

	@Override
	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) {
				// Don't need to print stacktrace here
			}
		}
	}
}
@@ -829,14 +829,18 @@
 tic 
+final 
 long
-%5B%5D
  INT
@@ -848,109 +848,14 @@
 RVAL
-S
  =
-%0A%09%09%09%7B%0A%09%09%09%09%09400,%0A%09%09%09%09%09200,%0A%09%09%09%09%09400,%0A%09%09%09%09%09128,%0A%09%09%09%09%09128,%0A%09%09%09%09%09128,%0A%09%09%09%09%09128,%0A%09%09%09%09%09128,%0A%09%09%09%09%0980%0A%09%09%09%7D
+ 500
 ;%0A%0A%09
@@ -996,33 +996,8 @@
 led;
-%0A%09protected int position;
 %0A%0A%09p
@@ -1495,25 +1495,8 @@
 rue;
-%0A%09%09%09position = 0;
 %0A%0A%09%09
@@ -1755,32 +1755,8 @@
 %09%09%7D%0A
-%0A%09%09%09%09while (enabled) %7B%0A%09
 %09%09%09%09
@@ -1780,24 +1780,12 @@
 RVAL
-S%5Bposition%5D
 );%0A%0A
-%09
 %09%09%09%09
@@ -1870,22 +1870,20 @@
 Code));%0A
-%0A
 %09%09%09%09
-%09
 if (seco
@@ -1896,25 +1896,24 @@
 ode != 0) %7B%0A
-%09
 %09%09%09%09%09target.
@@ -2001,85 +2001,8 @@
 ));%0A
-%09%09%09%09%09%7D%0A%0A%09%09%09%09%09if (position %3C INTERVALS.length - 1) %7B%0A%09%09%09%09%09%09position++;%0A%09%09%09%09%09%7D%0A
 %09%09%09%09

Simplify KeyRepeater