import android.app.Activity;
import android.os.Bundle;
-import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
-
+import android.view.View;
import android.util.Log;
public class TSCalibration extends Activity {
final private static String TAG = "TSCalibration";
final private static String POINTERCAL = "/data/misc/tscal/pointercal";
final private static String defaultPointercalValues = "1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1\n";
- final private static File FILE = new File(POINTERCAL);
+ final protected static File FILE = new File(POINTERCAL);
private TSCalibrationView mTSCalibrationView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- Display display = getWindowManager().getDefaultDisplay();
- mTSCalibrationView = new TSCalibrationView(this,
- display.getHeight(),
- display.getWidth());
+ mTSCalibrationView = new TSCalibrationView(this);
setContentView(R.layout.intro);
}
private void reset() {
try {
FileOutputStream fos = new FileOutputStream(FILE);
- fos.write(defaultPointercalValues.getBytes());
fos.flush();
fos.getFD().sync();
fos.close();
setContentView(R.layout.done);
}
}
+
+ static protected void setImmersiveMode(View v) {
+ v.setSystemUiVisibility(v.SYSTEM_UI_FLAG_LAYOUT_STABLE
+ | v.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
+ | v.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
+ | v.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
+ | v.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
+ | v.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
+ }
}
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
+import android.provider.Settings;
+import android.util.Log;
import java.io.File;
+//import org.zeroxlab.util.tscal.TSCalibration;
public class TSCalibrationStartup extends Activity {
- private static String cal_path = "/data/misc/tscal/pointercal";
-
-
@Override
public void onCreate(Bundle savedInstanceState) {
+ Settings.Secure.putString(getContentResolver(),
+ Settings.Secure.IMMERSIVE_MODE_CONFIRMATIONS,
+ getPackageName());
super.onCreate(savedInstanceState);
- File calFile = new File(cal_path);
- if (!calFile.exists()) {
+ if (TSCalibration.FILE.exists() && TSCalibration.FILE.length() == 0) {
Intent starterIntent = new Intent(this, TSCalibration.class);
startActivityForResult(starterIntent, 0);
} else {
private TargetPoint mTargetPoints[];
private TSCalibration mContext;
- public TSCalibrationView(TSCalibration context, int h, int w) {
+ public TSCalibrationView(TSCalibration context) {
super(context);
mContext = context;
- mTargetPoints = new TargetPoint[5];
- mTargetPoints[0] = new TargetPoint(50, 50);
- mTargetPoints[1] = new TargetPoint(w - 50, 50);
- mTargetPoints[2] = new TargetPoint(w - 50, h - 50);
- mTargetPoints[3] = new TargetPoint(50, h - 50);
- mTargetPoints[4] = new TargetPoint(w/2, h/2);
+ context.setImmersiveMode(this);
}
public void reset() {
}
public void dumpCalData(File file) {
- StringBuilder sb = new StringBuilder();
- for (TargetPoint point : mTargetPoints) {
- sb.append(point.calx);
- sb.append(" ");
- sb.append(point.caly);
- sb.append(" ");
- }
- for (TargetPoint point : mTargetPoints) {
- sb.append(point.x);
- sb.append(" ");
- sb.append(point.y);
- sb.append(" ");
- }
+ String cal = performCalibration();
try {
FileOutputStream fos = new FileOutputStream(file);
- fos.write(sb.toString().getBytes());
+ fos.write(cal.getBytes());
fos.flush();
fos.getFD().sync();
fos.close();
@Override
protected void onDraw(Canvas canvas) {
+ if (mTargetPoints == null) {
+ int w = getWidth(), h = getHeight();
+ mTargetPoints = new TargetPoint[5];
+ mTargetPoints[0] = new TargetPoint(50, 50);
+ mTargetPoints[1] = new TargetPoint(w - 50, 50);
+ mTargetPoints[2] = new TargetPoint(w - 50, h - 50);
+ mTargetPoints[3] = new TargetPoint(50, h - 50);
+ mTargetPoints[4] = new TargetPoint(w / 2, h / 2);
+ }
if (isFinished())
return;
canvas.drawColor(Color.BLACK);
c.drawCircle(x, y, 5, white);
c.drawCircle(x, y, 1, red);
}
+
+ private String performCalibration() {
+ int cal[] = new int[7];
+ double n, x, y, x2, y2, xy, z, zx, zy;
+ double det, a, b, c, e, f, i;
+ double scaling = 65536.0;
+
+ // Get sums for matrix
+ n = x = y = x2 = y2 = xy = 0;
+ for (TargetPoint point : mTargetPoints) {
+ n += 1;
+ x += (double)point.calx;
+ y += (double)point.caly;
+ x2 += (double)(point.calx * point.calx);
+ y2 += (double)(point.caly * point.caly);
+ xy += (double)(point.calx * point.caly);
+ Log.v(TAG, n + "x=" + point.x + " y=" + point.y + " calx=" + point.calx + " caly=" + point.caly);
+ }
+
+ // Get determinant of matrix -- check if determinant is too small
+ det = n*(x2*y2 - xy*xy) + x*(xy*y - x*y2) + y*(x*xy - y*x2);
+ if (det > -0.1 && det < 0.1) {
+ Log.w(TAG, "determinant is too small -- " + det);
+ }
+
+ // Get elements of inverse matrix
+ a = (x2*y2 - xy*xy);
+ b = (xy*y - x*y2);
+ c = (x*xy - y*x2);
+ e = (n*y2 - y*y);
+ f = (x*y - n*xy);
+ i = (n*x2 - x*x);
+
+ // Get sums for x calibration
+ z = zx = zy = 0;
+ for (TargetPoint point : mTargetPoints) {
+ z += (double)point.x;
+ zx += (double)(point.x * point.calx);
+ zy += (double)(point.x * point.caly);
+ }
+
+ // Now multiply out to get the calibration for framebuffer x coord
+ cal[2] = (int)((a*z + b*zx + c*zy) * scaling / det);
+ cal[0] = (int)((b*z + e*zx + f*zy) * scaling / det);
+ cal[1] = (int)((c*z + f*zx + i*zy) * scaling / det);
+
+ // Get sums for y calibration
+ z = zx = zy = 0;
+ for (TargetPoint point : mTargetPoints) {
+ z += (double)point.y;
+ zx += (double)(point.y * point.calx);
+ zy += (double)(point.y * point.caly);
+ }
+
+ // Now multiply out to get the calibration for framebuffer y coord
+ cal[5] = (int)((a*z + b*zx + c*zy) * scaling / det);
+ cal[3] = (int)((b*z + e*zx + f*zy) * scaling / det);
+ cal[4] = (int)((c*z + f*zx + i*zy) * scaling / det);
+
+ // If we got here, we're OK, so assign scaling to a[6] and return
+ cal[6] = (int)scaling;
+
+ StringBuilder sb = new StringBuilder();
+ for (int s : cal) {
+ sb.append(s);
+ sb.append(" ");
+ }
+ sb.append(getWidth());
+ sb.append(" ");
+ sb.append(getHeight());
+ sb.append("\n");
+ String ret = sb.toString();
+ Log.i(TAG, "pointercal = " + ret);
+ return ret;
+ }
}