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.location.fused;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.location.Criteria;
24 import android.os.Handler;
25 import android.os.Looper;
26 import android.os.UserHandle;
27 import android.os.WorkSource;
28 
29 import com.android.location.provider.LocationProviderBase;
30 import com.android.location.provider.ProviderPropertiesUnbundled;
31 import com.android.location.provider.ProviderRequestUnbundled;
32 
33 import java.io.FileDescriptor;
34 import java.io.PrintWriter;
35 
36 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 final Context mContext;
44     private final Handler mHandler;
45     private final FusionEngine mEngine;
46 
47     private final BroadcastReceiver mUserSwitchReceiver = new BroadcastReceiver() {
48         @Override
49         public void onReceive(Context context, Intent intent) {
50             String action = intent.getAction();
51             if (Intent.ACTION_USER_SWITCHED.equals(action)) {
52                 mEngine.switchUser();
53             }
54         }
55     };
56 
FusedLocationProvider(Context context)57     FusedLocationProvider(Context context) {
58         super(TAG, PROPERTIES);
59 
60         mContext = context;
61         mHandler = new Handler(Looper.myLooper());
62         mEngine = new FusionEngine(context, Looper.myLooper(), this);
63     }
64 
init()65     void init() {
66         // listen for user change
67         mContext.registerReceiverAsUser(mUserSwitchReceiver, UserHandle.ALL,
68                 new IntentFilter(Intent.ACTION_USER_SWITCHED), null, mHandler);
69     }
70 
destroy()71     void destroy() {
72         mContext.unregisterReceiver(mUserSwitchReceiver);
73         mHandler.post(() -> mEngine.setRequest(null));
74     }
75 
76     @Override
onSetRequest(ProviderRequestUnbundled request, WorkSource source)77     public void onSetRequest(ProviderRequestUnbundled request, WorkSource source) {
78         mHandler.post(() -> mEngine.setRequest(request));
79     }
80 
81     @Override
onDump(FileDescriptor fd, PrintWriter pw, String[] args)82     public void onDump(FileDescriptor fd, PrintWriter pw, String[] args) {
83         mEngine.dump(fd, pw, args);
84     }
85 }
86