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