1 /* 2 * Copyright (C) 2018 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.googlecode.android_scripting.facade.bluetooth; 18 19 import android.view.KeyEvent; 20 import android.view.MotionEvent; 21 import android.view.View; 22 23 import com.googlecode.android_scripting.Log; 24 import com.googlecode.android_scripting.facade.ui.ViewInflater; 25 import com.googlecode.android_scripting.future.FutureActivityTask; 26 27 import org.xmlpull.v1.XmlPullParser; 28 29 import java.io.StringReader; 30 import java.util.concurrent.CountDownLatch; 31 32 public class BluetoothHidInputCounterTask extends FutureActivityTask<Object> { 33 private static final String BLANKLAYOUT = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" 34 + "<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"" 35 + "android:id=\"@+id/background\" android:orientation=\"vertical\"" 36 + "android:layout_width=\"match_parent\" android:layout_height=\"match_parent\"" 37 + "android:background=\"#ff000000\">" 38 + "</LinearLayout>"; 39 private static final String TITLE = "Blank"; 40 41 protected final CountDownLatch mShowLatch = new CountDownLatch(1); 42 43 private long mFirstTime = 0; 44 private long mLastTime = 0; 45 private int mInputCount = 0; 46 BluetoothHidInputCounterTask()47 public BluetoothHidInputCounterTask() { 48 super(); 49 } 50 51 @Override onCreate()52 public void onCreate() { 53 ViewInflater inflater = new ViewInflater(); 54 inflater.getErrors().clear(); 55 try { 56 StringReader sr = new StringReader(BLANKLAYOUT); 57 XmlPullParser xml = ViewInflater.getXml(sr); 58 View view = inflater.inflate(getActivity(), xml); 59 getActivity().setContentView(view); 60 } catch (Exception e) { 61 Log.e("fail with exception " + e); 62 } 63 getActivity().setTitle(TITLE); 64 mShowLatch.countDown(); 65 } 66 67 @Override onDestroy()68 public void onDestroy() { 69 super.onDestroy(); 70 } 71 getShowLatch()72 public CountDownLatch getShowLatch() { 73 return mShowLatch; 74 } 75 getCount()76 public int getCount() { 77 return mInputCount; 78 } 79 80 @Override onKeyDown(int keyCode, KeyEvent event)81 public boolean onKeyDown(int keyCode, KeyEvent event) { 82 mInputCount++; 83 return false; 84 } 85 86 @Override onKeyUp(int keyCode, KeyEvent event)87 public boolean onKeyUp(int keyCode, KeyEvent event) { 88 mInputCount++; 89 return false; 90 } 91 92 @Override onGenericMotionEvent(MotionEvent event)93 public boolean onGenericMotionEvent(MotionEvent event) { 94 mInputCount++; 95 return false; 96 } 97 } 98