• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 package com.android.google.gce.gceservice;
17 
18 import android.bluetooth.BluetoothAdapter;
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.util.Log;
22 
23 /*
24  * A job that checks for Bluetooth being enabled before reporting VIRTUAL_DEVICE_BOOT_COMPLETED. Our
25  * devices should always boot with bt enabled, it can be configured in
26  * gce_x86/overlay_<device>/frameworks/base/packages/SettingsProvider/res/values/defaults.xml
27  */
28 public class BluetoothChecker extends JobBase {
29     private static final String LOG_TAG = "GceBluetoothChecker";
30     private final GceFuture<Boolean> mEnabled = new GceFuture<Boolean>("Bluetooth");
31 
32 
BluetoothChecker(Context context)33     public BluetoothChecker(Context context) {
34         super(LOG_TAG);
35         Resources res = context.getResources();
36         if (!res.getBoolean(R.bool.config_bt_required)) {
37             Log.i(LOG_TAG, "Bluetooth not required, assuming enabled.");
38             mEnabled.set(true);
39         }
40     }
41 
42 
43     @Override
execute()44     public int execute() {
45         if (mEnabled.isDone()) {
46             return 0;
47         }
48         BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
49         if (bluetoothAdapter == null) {
50             Log.e(LOG_TAG, "No bluetooth adapter found");
51             mEnabled.set(new Exception("No bluetooth adapter found"));
52         } else {
53             if (bluetoothAdapter.isEnabled()) {
54                 Log.i(LOG_TAG, "Bluetooth enabled with name: " + bluetoothAdapter.getName());
55                 mEnabled.set(true);
56             } else {
57                 Log.i(LOG_TAG, "Bluetooth disabled with name: " + bluetoothAdapter.getName());
58             }
59         }
60         return 0;
61     }
62 
63 
64     @Override
onDependencyFailed(Exception e)65     public void onDependencyFailed(Exception e) {
66         mEnabled.set(e);
67     }
68 
69 
getEnabled()70     public GceFuture<Boolean> getEnabled() {
71         return mEnabled;
72     }
73 }
74