• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 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.bluetooth.btservice.activityattribution;
18 
19 import android.util.Log;
20 
21 import java.util.Objects;
22 
23 /**
24  * Service used for attributes wakeup, wakelock and Bluetooth traffic into per-app and per-device
25  * based activities.
26  */
27 public class ActivityAttributionService {
28     private boolean mCleaningUp;
29     private static ActivityAttributionService sActivityAttributionService;
30     private static final boolean DBG = false;
31     private static final String TAG = "ActivityAttributionService";
32 
33     ActivityAttributionNativeInterface mActivityAttributionNativeInterface;
34 
35     /** Start and initialize the Activity Attribution service. */
start()36     public void start() {
37         debugLog("start()");
38 
39         if (sActivityAttributionService != null) {
40             Log.e(TAG, "start() called twice");
41             return;
42         }
43 
44         mActivityAttributionNativeInterface =
45                 Objects.requireNonNull(
46                         ActivityAttributionNativeInterface.getInstance(),
47                         "ActivityAttributionNativeInterface "
48                                 + "cannot be null when ActivityAttributionService starts");
49 
50         // Mark service as started
51         setActivityAttributionService(this);
52     }
53 
54     /** Cleans up the Activity Attribution service. */
cleanup()55     public void cleanup() {
56         debugLog("cleanup");
57         if (mCleaningUp) {
58             debugLog("already doing cleanup");
59             return;
60         }
61 
62         mCleaningUp = true;
63 
64         if (sActivityAttributionService == null) {
65             debugLog("cleanup() called before start()");
66             return;
67         }
68 
69         // Mark service as stopped
70         setActivityAttributionService(null);
71 
72         // Cleanup native interface
73         mActivityAttributionNativeInterface.cleanup();
74         mActivityAttributionNativeInterface = null;
75     }
76 
77     /** Get the ActivityAttributionService instance */
getActivityAttributionService()78     public static synchronized ActivityAttributionService getActivityAttributionService() {
79         if (sActivityAttributionService == null) {
80             Log.w(TAG, "getActivityAttributionService(): service is NULL");
81             return null;
82         }
83 
84         if (!sActivityAttributionService.isAvailable()) {
85             Log.w(TAG, "getActivityAttributionService(): service is not available");
86             return null;
87         }
88         return sActivityAttributionService;
89     }
90 
91     /** Init JNI */
initJni()92     public void initJni() {
93         debugLog("initJni()");
94         // Initialize native interface
95         mActivityAttributionNativeInterface.init();
96     }
97 
isAvailable()98     private boolean isAvailable() {
99         return !mCleaningUp;
100     }
101 
setActivityAttributionService( ActivityAttributionService instance)102     private static synchronized void setActivityAttributionService(
103             ActivityAttributionService instance) {
104         debugLog("setActivityAttributionService(): set to: " + instance);
105         sActivityAttributionService = instance;
106     }
107 
debugLog(String msg)108     private static void debugLog(String msg) {
109         if (DBG) {
110             Log.d(TAG, msg);
111         }
112     }
113 }
114