Advertisement
GeorgeIoak

TSCalibration.java

Feb 10th, 2013
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. /* Copyright (C) 2010 0xlab.org
  2. * Authored by: Kan-Ru Chen <[email protected]>
  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. package org.zeroxlab.util.tscal;
  17.  
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.FileNotFoundException;
  21. import java.io.IOException;
  22. import android.view.WindowManager;
  23. import android.content.Context;
  24. import android.app.Activity;
  25. import android.os.Bundle;
  26. import android.util.Log;
  27. import android.view.Display;
  28. import android.view.MotionEvent;
  29. import android.view.KeyEvent;
  30. import android.util.Log;
  31. import android.os.ServiceManager;
  32. import android.view.WindowManager;
  33. import android.view.IWindowManager;
  34.  
  35. public class TSCalibration extends Activity {
  36.  
  37. final private static String TAG = "TSCalibration";
  38. final private static String POINTERCAL = "/data/pointercal";
  39. final private static String defaultPointercalValues = "1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1\n";
  40. final private static File FILE = new File(POINTERCAL);
  41. boolean mSetContentView = false;
  42.  
  43. private TSCalibrationView mTSCalibrationView;
  44.  
  45. @Override
  46. public void onCreate(Bundle savedInstanceState) {
  47. super.onCreate(savedInstanceState);
  48.  
  49. Display display = getWindowManager().getDefaultDisplay();
  50. mTSCalibrationView = new TSCalibrationView(this,
  51. display.getHeight(),
  52. display.getWidth(),FILE.exists());
  53. setContentView(R.layout.intro);
  54. }
  55.  
  56. @Override
  57. public void onResume() {
  58. super.onResume();
  59. mTSCalibrationView.reset();
  60. //reset();
  61. }
  62.  
  63. @Override
  64. public boolean dispatchTouchEvent(MotionEvent event)
  65. {
  66. boolean ret;
  67.  
  68. if(mSetContentView == false)
  69. {
  70. return onTouchEvent(event);
  71. }
  72.  
  73. ret = mTSCalibrationView.onTouchEvent(event);
  74. if(ret == false || mTSCalibrationView.isFinished())
  75. {
  76. return onTouchEvent(event);
  77. }
  78.  
  79. return true;
  80. }
  81.  
  82. public boolean onTouchEvent(MotionEvent event)
  83. {
  84. //Log.d(TAG,"onTouchEvent" + event);
  85. if (mTSCalibrationView.isFinished()) {
  86. if(!FILE.exists())
  87. {
  88. //Log.d("Result","requestCode15" + "\n");
  89. mTSCalibrationView.dumpCalData(FILE);
  90. }
  91. else
  92. {
  93. //Log.d("Result","requestCode15" + "\n");
  94. mTSCalibrationView.dumpCalData(FILE);
  95.  
  96. try
  97. {
  98. IWindowManager mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService(Context.WINDOW_SERVICE));
  99.  
  100. mWindowManager.resetInputCalibration();
  101. }
  102. catch (Exception e)
  103. {
  104. Log.d(TAG,"mWindowManager resetInputCalibration failed!");
  105. }
  106.  
  107. }
  108. setResult(0);
  109. //Log.d("Result","requestCode13" + "\n");
  110. finish();
  111. //Log.d("Result","requestCode14" + "\n");
  112. } else {
  113. if (event.getAction() != MotionEvent.ACTION_UP)
  114. {
  115. return true;
  116. }
  117. mSetContentView = true;
  118. Log.d(TAG, "setContentView");
  119. setContentView(mTSCalibrationView);
  120. }
  121. return true;
  122. }
  123.  
  124. public boolean dispatchKeyEvent(KeyEvent e)
  125. {
  126. return true;
  127. }
  128.  
  129. private void reset() {
  130. try {
  131. FileOutputStream fos = new FileOutputStream(FILE);
  132. fos.write(defaultPointercalValues.getBytes());
  133. fos.flush();
  134. fos.getFD().sync();
  135. fos.close();
  136. } catch (FileNotFoundException e) {
  137. } catch (IOException e) {
  138. }
  139. }
  140.  
  141. public void onCalTouchEvent(MotionEvent ev) {
  142. mTSCalibrationView.invalidate();
  143. if (mTSCalibrationView.isFinished()) {
  144. setContentView(R.layout.done);
  145. }
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement