1 /* 2 * Copyright 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.mobileer.oboetester; 18 19 import android.app.Activity; 20 import android.os.Bundle; 21 import android.os.Handler; 22 import android.os.Looper; 23 import android.view.View; 24 import android.widget.TextView; 25 26 public class TestErrorCallbackActivity extends Activity { 27 28 private TextView mStatusDeleteCallback; 29 // This must match the value in TestErrorCallback.h 30 private static final int MAGIC_GOOD = 0x600DCAFE; 31 private MyStreamSniffer mStreamSniffer; 32 33 @Override onCreate(Bundle savedInstanceState)34 protected void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 setContentView(R.layout.activity_error_callback); 37 38 mStreamSniffer = new MyStreamSniffer(); 39 mStatusDeleteCallback = (TextView) findViewById(R.id.text_callback_status); 40 } 41 onTestDeleteCrash(View view)42 public void onTestDeleteCrash(View view) { 43 mStatusDeleteCallback.setText(getString(R.string.plug_or_unplug)); 44 mStreamSniffer.startStreamSniffer(); 45 testDeleteCrash(); 46 } 47 48 49 // Periodically query the status of the streams. 50 protected class MyStreamSniffer { 51 public static final int SNIFFER_UPDATE_PERIOD_MSEC = 150; 52 public static final int SNIFFER_UPDATE_DELAY_MSEC = 300; 53 54 private Handler mHandler; 55 56 // Display status info for the stream. 57 private Runnable runnableCode = new Runnable() { 58 @Override 59 public void run() { 60 int magic = getCallbackMagic(); 61 updateMagicDisplay(magic); 62 mHandler.postDelayed(runnableCode, SNIFFER_UPDATE_PERIOD_MSEC); 63 } 64 }; 65 startStreamSniffer()66 private void startStreamSniffer() { 67 stopStreamSniffer(); 68 mHandler = new Handler(Looper.getMainLooper()); 69 // Start the initial runnable task by posting through the handler 70 mHandler.postDelayed(runnableCode, SNIFFER_UPDATE_DELAY_MSEC); 71 } 72 stopStreamSniffer()73 private void stopStreamSniffer() { 74 if (mHandler != null) { 75 mHandler.removeCallbacks(runnableCode); 76 } 77 } 78 } 79 updateMagicDisplay(int magic)80 private void updateMagicDisplay(int magic) { 81 if (magic != 0) { 82 String text = getString(R.string.report_magic_pass, MAGIC_GOOD); 83 if (magic != MAGIC_GOOD) { 84 text = getString(R.string.report_magic_fail, 85 magic, MAGIC_GOOD); 86 } 87 mStatusDeleteCallback.setText(text); 88 } 89 } 90 91 @Override onPause()92 public void onPause() { 93 super.onPause(); 94 mStreamSniffer.stopStreamSniffer(); 95 } 96 testDeleteCrash()97 private native void testDeleteCrash(); getCallbackMagic()98 private native int getCallbackMagic(); 99 100 } 101