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 17package com.android.location.fused; 18 19 20import java.io.FileDescriptor; 21import java.io.PrintWriter; 22 23import com.android.location.provider.LocationProviderBase; 24import com.android.location.provider.ProviderPropertiesUnbundled; 25import com.android.location.provider.ProviderRequestUnbundled; 26 27import android.content.Context; 28import android.location.Criteria; 29import android.location.LocationProvider; 30import android.os.Bundle; 31import android.os.Handler; 32import android.os.Looper; 33import android.os.Message; 34import android.os.WorkSource; 35 36public class FusedLocationProvider extends LocationProviderBase implements FusionEngine.Callback { 37 private static final String TAG = "FusedLocationProvider"; 38 39 private static ProviderPropertiesUnbundled PROPERTIES = ProviderPropertiesUnbundled.create( 40 false, false, false, false, true, true, true, Criteria.POWER_LOW, 41 Criteria.ACCURACY_FINE); 42 43 private static final int MSG_ENABLE = 1; 44 private static final int MSG_DISABLE = 2; 45 private static final int MSG_SET_REQUEST = 3; 46 47 private final Context mContext; 48 private final FusionEngine mEngine; 49 50 private static class RequestWrapper { 51 public ProviderRequestUnbundled request; 52 public WorkSource source; 53 public RequestWrapper(ProviderRequestUnbundled request, WorkSource source) { 54 this.request = request; 55 this.source = source; 56 } 57 } 58 59 public FusedLocationProvider(Context context) { 60 super(TAG, PROPERTIES); 61 mContext = context; 62 mEngine = new FusionEngine(context, Looper.myLooper()); 63 } 64 65 /** 66 * For serializing requests to mEngine. 67 */ 68 private Handler mHandler = new Handler() { 69 @Override 70 public void handleMessage(Message msg) { 71 switch (msg.what) { 72 case MSG_ENABLE: 73 mEngine.init(FusedLocationProvider.this); 74 break; 75 case MSG_DISABLE: 76 mEngine.deinit(); 77 break; 78 case MSG_SET_REQUEST: 79 { 80 RequestWrapper wrapper = (RequestWrapper) msg.obj; 81 mEngine.setRequest(wrapper.request, wrapper.source); 82 break; 83 } 84 } 85 } 86 }; 87 88 @Override 89 public void onEnable() { 90 mHandler.sendEmptyMessage(MSG_ENABLE); 91 } 92 93 @Override 94 public void onDisable() { 95 mHandler.sendEmptyMessage(MSG_DISABLE); 96 } 97 98 @Override 99 public void onSetRequest(ProviderRequestUnbundled request, WorkSource source) { 100 mHandler.obtainMessage(MSG_SET_REQUEST, new RequestWrapper(request, source)).sendToTarget(); 101 } 102 103 @Override 104 public void onDump(FileDescriptor fd, PrintWriter pw, String[] args) { 105 // perform synchronously 106 mEngine.dump(fd, pw, args); 107 } 108 109 @Override 110 public int onGetStatus(Bundle extras) { 111 return LocationProvider.AVAILABLE; 112 } 113 114 @Override 115 public long onGetStatusUpdateTime() { 116 return 0; 117 } 118} 119