huangcm
2025-04-09 02d4ce54b909bd733f12e9f3fa4c1b03cf2d6f45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package com.DeviceTest;
 
import static android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN;
import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
 
import com.DeviceTest.helper.ControlButtonUtil;
import com.DeviceTest.view.GsensorBall;
 
import android.app.Activity;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
 
/**
 * @author LanBinYuan
 * @date 2011-06-11
 * 
 */
 
public class GsensorTestActivity extends Activity {
   /** Called when the activity is first created. */
   private final static int MAX_NUM = 8;
   private SensorManager sensorManager;
   private SensorEventListener lsn = null;
   boolean stop = false;
 
   private static enum TEST_AXIS {
       X, Y, Z, D
   };
 
   private TEST_AXIS testAxis;
   private GsensorBall mGsensorBall;
   private Button save_btn;
   private double x_offset=0;
   private double y_offset=0;
   private double z_offset=0;
   
   protected void onCreate(Bundle paramBundle) {
       super.onCreate(paramBundle);
       setContentView(R.layout.gsensortest);
       stop = false;
       setTitle(getTitle() + "----("
               + getIntent().getStringExtra(DeviceTest.EXTRA_TEST_PROGRESS) + ")");
       // requestWindowFeature(Window.FEATURE_NO_TITLE);
       getWindow().addFlags(FLAG_FULLSCREEN | FLAG_KEEP_SCREEN_ON);
 
       ControlButtonUtil.initControlButtonView(this);
       sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
       //mGsensorBall = (GsensorBall)findViewById(R.id.gsensorball);
       setTestAxis(TEST_AXIS.X);
//        findViewById(R.id.btn_Pass).setVisibility(View.INVISIBLE);
         save_btn = (Button) findViewById(R.id.save_calibration_button);
   }
 
   TextView X_textView, Y_textView, Z_textView;
 
   
   protected void onResume() {
       super.onResume();
 
       stop = false;
       final TextView subTitle = (TextView) findViewById(R.id.Accelerometer);
       subTitle.setTextColor(Color.rgb(255, 0, 0));
 
       X_textView = (TextView) findViewById(R.id.gsensorTestX);
       X_textView.setTextColor(android.graphics.Color.GREEN);
 
       Y_textView = (TextView) findViewById(R.id.gsensorTestY);
       Y_textView.setTextColor(android.graphics.Color.GREEN);
 
       Z_textView = (TextView) findViewById(R.id.gsensorTestZ);
       Z_textView.setTextColor(android.graphics.Color.GREEN);
 
 
       lsn = new SensorEventListener() {
           public void onAccuracyChanged(Sensor sensor, int accuracy) {
 
           }
 
           public void onSensorChanged(SensorEvent e) {
               if(stop) {
                   return;
               }
               subTitle.setText("x:" + (double) e.values[0] + ", \ny:"
                       + (double) e.values[1] + ",\nz:" + (double) e.values[2]);
               x_offset=e.values[0]-0;
               y_offset=e.values[1]-0;
               z_offset=e.values[2]-9.8;
               doTest(e);
               //mGsensorBall.setXYZ(e.values[0], e.values[1], e.values[2]);
           }
 
       };
 
       Sensor sensors = sensorManager
               .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
 
       sensorManager.registerListener(lsn, sensors,
               SensorManager.SENSOR_DELAY_NORMAL);
 
       save_btn.setOnClickListener(new OnClickListener()
       {
 
           @Override
           public void onClick(View v)
           {
               // TODO Auto-generated method stub
               File savefile = new File("/cache/Gsensor.txt");
               if (savefile.exists()) {
                   savefile.delete();
               }
               if (!savefile.exists())
               {
                   try
                   {
                       savefile.createNewFile();
                   } catch (IOException e)
                   {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                   }
               }
               StringBuilder sb = new StringBuilder();
               sb.append("x_offset:" + x_offset).append("\n").append("y_offset:" + y_offset).append("\n").append("z_offset:" + x_offset);
               String sbStirng = sb.toString();
               FileOutputStream fos = null;
               try
               {
                   fos = new FileOutputStream(savefile, false);
                   fos.write(sbStirng.getBytes());
                   fos.close();
               } catch (FileNotFoundException e)
               {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               } catch (IOException e)
               {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               } finally
               {
                   if (fos != null)
                   {
                       try
                       {
                           fos.close();
                       } catch (IOException e)
                       {
                           e.printStackTrace();
                       }
                   }
               }
               Toast.makeText(getApplicationContext(), getString(R.string.save_isok), Toast.LENGTH_SHORT).show();
           }
       });
   }
 
   private void doTest(SensorEvent e) {
       switch (testAxis) {
       case X:
           if ((int) e.values[0] >= MAX_NUM && (int) e.values[1] == 0
                   && (int) e.values[2] == 0) {
               setTestAxis(TEST_AXIS.Y);
               X_textView.setText(X_textView.getText() + ":Pass");
           }
           break;
       case Y:
           if ((int) e.values[0] == 0 && (int) e.values[1] >= MAX_NUM
                   && (int) e.values[2] == 0) {
               setTestAxis(TEST_AXIS.Z);
               Y_textView.setText(Y_textView.getText() + ":Pass");
           }
           break;
       case Z:
           if ((int) e.values[0] == 0 && (int) e.values[1] == 0
                   && (int) e.values[2] >= MAX_NUM) {
               setTestAxis(TEST_AXIS.D);
               Z_textView.setText(Z_textView.getText() + ":Pass");
//                findViewById(R.id.btn_Pass).performClick();
           }
           break;
       default:
           break;
       }
   }
 
   private void setTestAxis(TEST_AXIS testAxis) {
       this.testAxis = testAxis;
       switch (testAxis) {
       case X:
           findViewById(R.id.gsensorTestX).setVisibility(View.VISIBLE);
           break;
       case Y:
           findViewById(R.id.gsensorTestY).setVisibility(View.VISIBLE);
           break;
       case Z:
           findViewById(R.id.gsensorTestZ).setVisibility(View.VISIBLE);
           break;
       default:
           break;
       }
   }
 
   //
   
   protected void onPause() {
       super.onPause();
       sensorManager.unregisterListener(lsn);
       stop = true;
   }
 
   public boolean dispatchKeyEvent(KeyEvent event) {
       if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
           return false;
       }
       return super.dispatchKeyEvent(event);
   }
}