Home | History | Annotate | Download | only in location
      1 /*
      2  * Copyright (C) 2012 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.android.cts.verifier.location;
     18 
     19 import com.android.cts.verifier.PassFailButtons;
     20 import com.android.cts.verifier.R;
     21 
     22 import android.content.Context;
     23 import android.graphics.Color;
     24 import android.graphics.Typeface;
     25 import android.location.LocationManager;
     26 import android.os.Bundle;
     27 import android.text.Layout;
     28 import android.text.Spannable;
     29 import android.text.style.ForegroundColorSpan;
     30 import android.util.Log;
     31 import android.view.View;
     32 import android.widget.ScrollView;
     33 import android.widget.TextView;
     34 
     35 interface PassFailLog {
     36     public void log(String s);
     37     public void pass();
     38     public void fail(String s);
     39 }
     40 
     41 /**
     42  * CTS Verifier case for verifying GPS.
     43  */
     44 public class GpsTestActivity extends PassFailButtons.Activity implements PassFailLog {
     45     private LocationManager mLocationManager;
     46     private TextView mTextView;
     47     private ScrollView mScrollView;
     48 
     49     LocationVerifier mLocationVerifier;
     50     private int mTestNumber;
     51 
     52     @Override
     53     public void onCreate(Bundle savedInstanceState) {
     54         super.onCreate(savedInstanceState);
     55 
     56         mLocationManager = (LocationManager) getApplicationContext().getSystemService(
     57                 Context.LOCATION_SERVICE);
     58 
     59         setContentView(R.layout.pass_fail_text);
     60         setPassFailButtonClickListeners();
     61         setInfoResources(R.string.location_gps_test, R.string.location_gps_test_info, -1);
     62         mTextView = (TextView) findViewById(R.id.text);
     63         mTextView.setTypeface(Typeface.MONOSPACE);
     64         mTextView.setTextSize(15.0f);
     65         mScrollView = (ScrollView) findViewById(R.id.scroll);
     66     }
     67 
     68     @Override
     69     public void onResume() {
     70         super.onResume();
     71 
     72         mTextView.setText("");
     73         getPassButton().setEnabled(false);
     74         mTestNumber = 0;
     75         nextTest();
     76     }
     77 
     78     @Override
     79     public void onPause() {
     80         super.onPause();
     81 
     82         if (mLocationVerifier != null) {
     83             mLocationVerifier.stop();
     84         }
     85     }
     86 
     87     @Override
     88     public String getTestDetails() {
     89         return mTextView.getText().toString();
     90     }
     91 
     92     private void nextTest() {
     93         mTestNumber++;
     94         switch (mTestNumber) {
     95             case 1:
     96                 // Test GPS with minTime = 0
     97                 mLocationVerifier = new LocationVerifier(this, mLocationManager,
     98                         LocationManager.GPS_PROVIDER, 0, 10);
     99                 mLocationVerifier.start();
    100                 break;
    101             case 2:
    102                 // Test GPS with minTime = 1s
    103                 mLocationVerifier = new LocationVerifier(this, mLocationManager,
    104                         LocationManager.GPS_PROVIDER, 1000, 10);
    105                 mLocationVerifier.start();
    106                 break;
    107             case 3:
    108                 // Test GPS with minTime = 5s
    109                 mLocationVerifier = new LocationVerifier(this, mLocationManager,
    110                         LocationManager.GPS_PROVIDER, 5 * 1000, 4);
    111                 mLocationVerifier.start();
    112                 break;
    113             case 4:
    114                 // Test GPS with minTime = 15s
    115                 mLocationVerifier = new LocationVerifier(this, mLocationManager,
    116                         LocationManager.GPS_PROVIDER, 15 * 1000, 4);
    117                 mLocationVerifier.start();
    118                 break;
    119             case 5:
    120                 log("All GPS tests complete", Color.GREEN);
    121                 getPassButton().setEnabled(true);
    122                 break;
    123         }
    124     }
    125 
    126     @Override
    127     public void log(String s) {
    128         mTextView.append(s);
    129         mTextView.append("\n");
    130 
    131         // Scroll to bottom
    132         mScrollView.post(new Runnable() {
    133             @Override
    134             public void run() {
    135                 mScrollView.fullScroll(View.FOCUS_DOWN);
    136             }
    137         });
    138     }
    139 
    140     private void log(String s, int color) {
    141         int start = mTextView.getText().length();
    142         mTextView.append(s);
    143         mTextView.append("\n");
    144         int end = mTextView.getText().length();
    145         Spannable spanText = (Spannable) mTextView.getText();
    146         spanText.setSpan(new ForegroundColorSpan(color), start, end, 0);
    147     }
    148 
    149     @Override
    150     public void fail(String s) {
    151         log(s, Color.RED);
    152         mLocationVerifier = null;
    153     }
    154 
    155     @Override
    156     public void pass() {
    157         log("OK!\n", Color.GREEN);
    158         mLocationVerifier = null;
    159         nextTest();
    160     }
    161 }
    162