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 #define LOG_TAG "bt_shim_activity_attribution"
18 #include "activity_attribution.h"
19 
20 #include "btif/include/btif_common.h"
21 #include "gd/btaa/activity_attribution.h"
22 #include "helpers.h"
23 #include "main/shim/entry.h"
24 
25 class ActivityAttributionInterfaceImpl
26     : public ActivityAttributionInterface,
27       public bluetooth::activity_attribution::ActivityAttributionCallback {
28  public:
29   ~ActivityAttributionInterfaceImpl() override = default;
30 
GetInstance()31   static ActivityAttributionInterfaceImpl* GetInstance() {
32     static ActivityAttributionInterfaceImpl* instance =
33         new ActivityAttributionInterfaceImpl();
34     return instance;
35   }
36 
Init()37   void Init() override {
38     bluetooth::shim::GetActivityAttribution()
39         ->RegisterActivityAttributionCallback(this);
40   }
41 
RegisterCallbacks(ActivityAttributionCallbacks * callbacks)42   void RegisterCallbacks(ActivityAttributionCallbacks* callbacks) override {
43     this->callbacks = callbacks;
44   }
45 
Cleanup(void)46   void Cleanup(void) override{};
47 
OnWakeup(const Activity activity,const bluetooth::hci::Address & address)48   void OnWakeup(const Activity activity,
49                 const bluetooth::hci::Address& address) override {
50     do_in_jni_thread(
51         FROM_HERE, base::Bind(&ActivityAttributionCallbacks::OnWakeup,
52                               base::Unretained(callbacks),
53                               (ActivityAttributionCallbacks::Activity)activity,
54                               bluetooth::ToRawAddress(address)));
55   }
56 
OnActivityLogsReady(const std::vector<BtaaAggregationEntry> logs)57   void OnActivityLogsReady(
58       const std::vector<BtaaAggregationEntry> logs) override {
59     std::vector<ActivityAttributionCallbacks::BtaaAggregationEntry>
60         callback_logs;
61     for (auto& it : logs) {
62       ActivityAttributionCallbacks::BtaaAggregationEntry entry{
63           bluetooth::ToRawAddress(it.address),
64           (ActivityAttributionCallbacks::Activity)it.activity, it.wakeup_count,
65           it.byte_count, it.wakelock_duration_ms};
66       callback_logs.push_back(entry);
67     }
68     do_in_jni_thread(
69         FROM_HERE,
70         base::Bind(&ActivityAttributionCallbacks::OnActivityLogsReady,
71                    base::Unretained(callbacks), callback_logs));
72   }
73 
74  private:
75   // Private constructor to prevent construction.
ActivityAttributionInterfaceImpl()76   ActivityAttributionInterfaceImpl() {}
77 
78   ActivityAttributionCallbacks* callbacks;
79 };
80 
81 ActivityAttributionInterface*
get_activity_attribution_instance()82 bluetooth::shim::get_activity_attribution_instance() {
83   return ActivityAttributionInterfaceImpl::GetInstance();
84 }
85 
init_activity_attribution()86 void bluetooth::shim::init_activity_attribution() {
87   bluetooth::shim::get_activity_attribution_instance()->Init();
88 }
89