package com.mobileer.midisynthexample;
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * 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.
 */

import android.app.Instrumentation;
import android.util.Log;
import android.view.KeyEvent;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Generate fake key events so that the CPU speed will not get lowered.
 * This is useful if you are using a MIDI keyboard and no touching the screen.
 */
public class FakeKeyGenerator {
    public static final String TAG = "FakeKeyGenerator";
    private Timer mFakeKeyTimer;
    private FakeKeyTimerTask mFakeKeyTask;

    static class FakeKeyTimerTask extends TimerTask {
        @Override
        public void run() {
            sendFakeKeyEvent();
        }
    };

    /**
     * Post fake key event to keep CPU from throttling down.
     * This should be called at least once per second.
     * It should NOT be called on the UI thread!
     */
    public static void sendFakeKeyEvent() {
        try {
            Instrumentation instrumentation = new Instrumentation();
            instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_BACKSLASH);
        } catch(SecurityException e) {
            // Even though I was honoring window focus, I was still getting these exceptions.
            Log.e(TAG, "sendFakeKeyEvent() was out of focus");
        }
    }

    /**
     * Start a timer task that periodically generates a fake key input event.
     */
    public void start() {
        stop(); // just in case start() is called twice in a row
        mFakeKeyTimer = new Timer();
        mFakeKeyTask = new FakeKeyTimerTask();
        mFakeKeyTimer.schedule(mFakeKeyTask, 1000, 1000);
    }

    /**
     * Stop the fake key timer task if running.
     */
    public void stop() {
        // Cancel the fake key events.
        if (mFakeKeyTimer != null) {
            mFakeKeyTimer.cancel();
            mFakeKeyTimer.purge();
            mFakeKeyTimer = null;
        }
        if (mFakeKeyTask != null){
            mFakeKeyTask.cancel();
            mFakeKeyTask = null;
        }
    }

}

@@ -931,16 +931,17 @@
 d and no
+t
  touchin
@@ -1047,16 +1047,83 @@
 rator%22;%0A
+    public static final int FAKE_KEY = KeyEvent.KEYCODE_BACKSLASH;%0A
     priv
@@ -1189,16 +1189,92 @@
 KeyTask;
+%0A    private static Instrumentation instrumentation = new Instrumentation();
 %0A%0A    st
@@ -1660,77 +1660,8 @@
 y %7B%0A
-            Instrumentation instrumentation = new Instrumentation();%0A
     
@@ -1706,34 +1706,16 @@
 ync(
-KeyEvent.KEYCODE_BACKSLASH
+FAKE_KEY
 );%0A 

Use FAKE_KEY constant.