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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package com.DeviceTest;
 
import static android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN;
import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
 
import java.util.Iterator;
 
import com.DeviceTest.GpsTestActivity.MystatusListener;
import com.DeviceTest.helper.ControlButtonUtil;
import com.DeviceTest.helper.SystemUtil;
 
import android.app.Activity;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.bluetooth.BluetoothAdapter;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.provider.Settings;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
 
public class GpsLocationTestActivity extends Activity {
   private static final String TAG = GpsLocationTestActivity.class
           .getSimpleName();
   LocationManager mLocatManager;
   LocationListener mLocationListener = new LocationListener() {
       public void onLocationChanged(Location location) {
           // mLocatManager.getGpsStatus(gpsStatus);
           // mResult.setText("Debug:get location data...");
           // Log.e("Jeffy", "!!!!!!!!!!!!!!!!Debug:get location data...");
           // if (gpsStatus != null && gpsStatus.getTimeToFirstFix() != 0) {
           // mResult.setText("Debug:TTFF: " + gpsStatus.getTimeToFirstFix());
           // Log.e("Jeffy", "!!!!!!!!!!!!!!!!Debug:TTFF:" +
           // gpsStatus.getTimeToFirstFix());
           // return;
           // }
       }
 
       public void onProviderDisabled(String provider) {
       }
 
       public void onProviderEnabled(String provider) {
       }
 
       public void onStatusChanged(String provider, int status, Bundle extras) {
       }
   };
   TextView mResult;
   TextView mText;
   TextView mTitle;
 
   private GpsStatus.Listener statusListener = new MystatusListener();
   private static final int step = 1000; // msecs
   private static final int MSG_RUN = 0;
 
   boolean stop = false;
   Handler mHandler = new Handler() {
       public void handleMessage(Message msg) {
           removeMessages(MSG_RUN);
           switch (msg.what) {
           case MSG_RUN:
               ((Runnable) msg.obj).run();
               break;
           default:
               break;
           }
       }
   };
   Runnable mFailedRunnable = new Runnable() {
 
       public void run() {
           if (stop) {
               return;
           }
           findViewById(R.id.btn_Fail).performClick();
       }
   };
   Runnable mSkipRunnable = new Runnable() {
 
       public void run() {
           if (stop) {
               return;
           }
           findViewById(R.id.btn_Skip).performClick();
       }
   };
 
   Runnable mPassRunnable = new Runnable() {
 
       public void run() {
           if (stop) {
               return;
           }
           findViewById(R.id.btn_Pass).performClick();
       }
   };
 
   protected void onCreate(Bundle savedInstanceState) {
 
       super.onCreate(savedInstanceState);
 
       setTitle(getTitle() + "----("
               + getIntent().getStringExtra(DeviceTest.EXTRA_TEST_PROGRESS)
               + ")");
       // requestWindowFeature(Window.FEATURE_NO_TITLE);
       getWindow().addFlags(FLAG_FULLSCREEN | FLAG_KEEP_SCREEN_ON);
 
       setContentView(R.layout.gpslocationtest);
 
       this.mResult = (TextView) findViewById(R.id.gpslocationresultText);
       this.mResult.setVisibility(View.VISIBLE);
       this.mResult.setGravity(Gravity.CENTER);
       ControlButtonUtil.initControlButtonView(this);
       this.mResult.setText("Wait for location data...");
 
       //findViewById(R.id.btn_Pass).setVisibility(View.INVISIBLE);
 
       this.mLocatManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
 
       // mLocatManager.getGpsStatus(gpsStatus);
       // if(gpsStatus != null && gpsStatus.getTimeToFirstFix() != 0) {
       // mResult.setText("TTFF already been set, please turn off & turn on GPS..");
       //
       // mHandler.postDelayed(mSkipRunnable, 5000);
       // return;
       // }
 
       WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
       wifiManager.setWifiEnabled(false);
       if(BluetoothAdapter.getDefaultAdapter() != null){
           BluetoothAdapter.getDefaultAdapter().disable();
       }
       
       /*Settings.Secure.setLocationProviderEnabled(getContentResolver(),
               LocationManager.GPS_PROVIDER, true);*/
 
       if (!mLocatManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
           this.mResult.setText("Gps off,gps location was not enabled!");
           //mHandler.postDelayed(mSkipRunnable, 5000);
           //return;
       }else{
 
            TextView nmeaView = (TextView) findViewById(R.id.nmealocationresultText);
            nmeaView.setText("Wait for NMEA information");
            
            mLocatManager.addGpsStatusListener(this.statusListener);
            mLocatManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                    step, 0, mLocationListener);
            mLocatManager.addNmeaListener(nmeaListener);
            stop = true;
            mHandler.postDelayed(new Runnable() {
                
                public void run() {
                    SystemUtil.execRootCmd(DeviceTest.GPS_COLD_START_PATH);
                    SystemUtil.execRootCmd(DeviceTest.GPS_COLD_START_PATH);
                }
            }, 2000);
            stop = false;
            mHandler.postDelayed(mFailedRunnable, 120 * 1000);
       }
   }
   public boolean onTouchEvent(MotionEvent paramMotionEvent) {
        if (paramMotionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            if (!mLocatManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                Intent intent = new Intent();
                intent .setAction("android.settings.LOCATION_SOURCE_SETTINGS");   
                intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.Settings$LocationSettingsActivity"));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        }
        return super.onTouchEvent(paramMotionEvent);
    }
   protected void onStop() {
       super.onStop();
       this.mLocatManager.removeGpsStatusListener(this.statusListener);
       this.mLocatManager.removeUpdates(this.mLocationListener);
       stop = true;
   }
 
   public boolean dispatchKeyEvent(KeyEvent event) {
       if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
           return false;
       }
       return super.dispatchKeyEvent(event);
   }
 
   GpsStatus gpsStatus;
   
   private void updateNmeaStatus(String nmea) {
       TextView nmeaView = (TextView) findViewById(R.id.nmealocationresultText);
       nmeaView.setText(nmea);
   }
   
   private final GpsStatus.NmeaListener nmeaListener = new GpsStatus.NmeaListener() {
       public void onNmeaReceived(long timestamp, String nmea) {
 
           updateNmeaStatus(nmea);
       }
   };
   
 
   class MystatusListener implements GpsStatus.Listener {
 
       public void onGpsStatusChanged(int event) {
           gpsStatus = mLocatManager.getGpsStatus(null);
           if(stop) {
               return;
           }
           switch (event) {
           case GpsStatus.GPS_EVENT_FIRST_FIX:
 
               Log.e("Jeffy",
                       "GPS_EVENT_FIRST_FIX:" + gpsStatus.getTimeToFirstFix());
               String ttff = ((int) (gpsStatus.getTimeToFirstFix() / 100)) / 10.0 + "s";
               mResult.setText("TTFF: "
                       + ttff);
               ControlButtonUtil.setResult(DeviceTest.RESULT_INFO_HEAD
                       + ttff);
               mHandler.removeMessages(MSG_RUN);
               if(gpsStatus.getTimeToFirstFix() > 90 * 1000 ) {
                   mHandler.postDelayed(mFailedRunnable, 2 * 1000);
               } else {
                   mHandler.postDelayed(mPassRunnable, 2 * 1000);
               }
               findViewById(R.id.btn_Pass).setVisibility(View.VISIBLE);
               findViewById(R.id.btn_Fail).setVisibility(View.INVISIBLE);
               break;
           default:
               break;
           }
       }
 
   }
}