The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 android.test; |
| 18 | |
| 19 | |
| 20 | import android.location.Criteria; |
Mike Lockwood | 4e50b78 | 2009-04-03 08:24:43 -0700 | [diff] [blame^] | 21 | import android.location.ILocationManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.location.Location; |
| 23 | import android.location.LocationProviderImpl; |
| 24 | import android.os.Bundle; |
| 25 | import android.os.SystemClock; |
| 26 | |
| 27 | /** |
| 28 | * @hide - This is part of a framework that is under development and should not be used for |
| 29 | * active development. |
| 30 | */ |
| 31 | public class TestLocationProvider extends LocationProviderImpl { |
| 32 | |
| 33 | public static final String PROVIDER_NAME = "test"; |
| 34 | public static final double LAT = 0; |
| 35 | public static final double LON = 1; |
| 36 | public static final double ALTITUDE = 10000; |
| 37 | public static final float SPEED = 10; |
| 38 | public static final float BEARING = 1; |
| 39 | public static final int STATUS = AVAILABLE; |
Mike Lockwood | 4e50b78 | 2009-04-03 08:24:43 -0700 | [diff] [blame^] | 40 | private static final long LOCATION_INTERVAL = 1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
| 42 | private Location mLocation; |
| 43 | private boolean mEnabled; |
Mike Lockwood | 4e50b78 | 2009-04-03 08:24:43 -0700 | [diff] [blame^] | 44 | private TestLocationProviderThread mThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | |
Mike Lockwood | 4e50b78 | 2009-04-03 08:24:43 -0700 | [diff] [blame^] | 46 | private class TestLocationProviderThread extends Thread { |
| 47 | |
| 48 | private boolean mDone = false; |
| 49 | |
| 50 | public TestLocationProviderThread() { |
| 51 | super("TestLocationProviderThread"); |
| 52 | } |
| 53 | |
| 54 | public void run() { |
| 55 | // thread exits after disable() is called |
| 56 | synchronized (this) { |
| 57 | while (!mDone) { |
| 58 | try { |
| 59 | wait(LOCATION_INTERVAL); |
| 60 | } catch (InterruptedException e) { |
| 61 | } |
| 62 | |
| 63 | if (!mDone) { |
| 64 | TestLocationProvider.this.updateLocation(); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | synchronized void setDone() { |
| 71 | mDone = true; |
| 72 | notify(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | public TestLocationProvider(ILocationManager locationManager) { |
| 77 | super(PROVIDER_NAME, locationManager); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | mLocation = new Location(PROVIDER_NAME); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | //LocationProvider methods |
| 82 | |
| 83 | @Override |
| 84 | public int getAccuracy() { |
| 85 | return Criteria.ACCURACY_COARSE; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public int getPowerRequirement() { |
| 90 | return Criteria.NO_REQUIREMENT; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public boolean hasMonetaryCost() { |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public boolean requiresCell() { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public boolean requiresNetwork() { |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public boolean requiresSatellite() { |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public boolean supportsAltitude() { |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public boolean supportsBearing() { |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public boolean supportsSpeed() { |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | //LocationProviderImpl methods |
| 129 | @Override |
Mike Lockwood | 4e50b78 | 2009-04-03 08:24:43 -0700 | [diff] [blame^] | 130 | public synchronized void disable() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | mEnabled = false; |
Mike Lockwood | 4e50b78 | 2009-04-03 08:24:43 -0700 | [diff] [blame^] | 132 | if (mThread != null) { |
| 133 | mThread.setDone(); |
| 134 | try { |
| 135 | mThread.join(); |
| 136 | } catch (InterruptedException e) { |
| 137 | } |
| 138 | mThread = null; |
| 139 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
Mike Lockwood | 4e50b78 | 2009-04-03 08:24:43 -0700 | [diff] [blame^] | 143 | public synchronized void enable() { |
| 144 | mEnabled = true; |
| 145 | mThread = new TestLocationProviderThread(); |
| 146 | mThread.start(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public boolean isEnabled() { |
| 151 | return mEnabled; |
| 152 | } |
| 153 | |
| 154 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | public int getStatus(Bundle extras) { |
| 156 | return STATUS; |
| 157 | } |
| 158 | |
| 159 | private void updateLocation() { |
| 160 | long time = SystemClock.uptimeMillis(); |
| 161 | long multiplier = (time/5000)%500000; |
| 162 | mLocation.setLatitude(LAT*multiplier); |
| 163 | mLocation.setLongitude(LON*multiplier); |
| 164 | mLocation.setAltitude(ALTITUDE); |
| 165 | mLocation.setSpeed(SPEED); |
| 166 | mLocation.setBearing(BEARING*multiplier); |
| 167 | |
| 168 | Bundle extras = new Bundle(); |
| 169 | extras.putInt("extraTest", 24); |
| 170 | mLocation.setExtras(extras); |
| 171 | mLocation.setTime(time); |
Mike Lockwood | 4e50b78 | 2009-04-03 08:24:43 -0700 | [diff] [blame^] | 172 | reportLocationChanged(mLocation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | } |