package com.example.oboe.megadrone;

/*
 * Copyright 2018 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 androidx.appcompat.app.AppCompatActivity;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;

public class MainActivity extends AppCompatActivity {

    private final String TAG = MainActivity.class.toString();

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("megadrone");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    protected void onStart(){
        super.onStart();
        startEngine(getExclusiveCores());
    }

    protected void onStop(){
        super.onStop();
        stopEngine();
    }

    private native void stopEngine();

    private native void startEngine(int[] cpuIds);

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN){
            tap(true);
        } else if (event.getAction() == MotionEvent.ACTION_UP){
            tap(false);
        }


        return super.onTouchEvent(event);
    }

    // Obtain CPU cores which are reserved for the foreground app. The audio thread can be
    // bound to these cores to avoids the risk of it being migrated to slower or more contended
    // core(s).
    private int[] getExclusiveCores(){
        int exclusiveCores[] = {};

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            Log.w(TAG, "getExclusiveCores() not supported. Only available on API " +
                    Build.VERSION_CODES.N + "+");
        } else {
            exclusiveCores = android.os.Process.getExclusiveCores();

        }
        return exclusiveCores;
    }

    private native void tap(boolean b);


}



@@ -2291,16 +2291,38 @@
  else %7B%0A
+            try %7B%0A    
         
@@ -2378,24 +2378,163 @@
 iveCores();%0A
+            %7D catch (RuntimeException e)%7B%0A                Log.w(TAG, %22getExclusiveCores() is not supported on this device.%22);%0A            %7D
 %0A        %7D%0A 

Fix #221 Wrap getExclusiveCores in try/catch