1 /*
2  * Copyright (C) 2023 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.internal.telephony.metrics;
18 
19 import static com.android.internal.telephony.metrics.MetricsCollector.getPhonesIfAny;
20 import static com.android.internal.telephony.metrics.PerSimStatus.isVonrEnabled;
21 
22 import android.annotation.NonNull;
23 import android.os.Handler;
24 import android.os.HandlerThread;
25 
26 import com.android.internal.annotations.VisibleForTesting;
27 import com.android.internal.telephony.Phone;
28 import com.android.internal.telephony.flags.FeatureFlags;
29 
30 import java.util.Map;
31 import java.util.concurrent.ConcurrentHashMap;
32 
33 /**
34  * Vonr state handler.
35  *
36  * <p>This class is instantiated in {@link MetricsCollector}.
37 */
38 public class VonrHelper {
39     private final @NonNull FeatureFlags mFlags;
40 
41     private Handler mHandler;
42     private Map<Integer, Boolean> mPhoneVonrState = new ConcurrentHashMap<>();
43 
VonrHelper(@onNull FeatureFlags featureFlags)44     public VonrHelper(@NonNull FeatureFlags featureFlags) {
45         this.mFlags = featureFlags;
46         if (mFlags.vonrEnabledMetric()) {
47             HandlerThread mHandlerThread = new HandlerThread("VonrHelperThread");
48             mHandlerThread.start();
49             mHandler = new Handler(mHandlerThread.getLooper());
50         }
51     }
52 
53     /** Update vonr_enabled state */
updateVonrEnabledState()54     public void updateVonrEnabledState() {
55         if (mFlags.vonrEnabledMetric()) {
56             mHandler.post(mVonrRunnable);
57         }
58     }
59 
60     @VisibleForTesting
61     protected Runnable mVonrRunnable =
62             new Runnable() {
63                 @Override
64                 public void run() {
65                     mPhoneVonrState.clear();
66                     for (Phone phone : getPhonesIfAny()) {
67                         mPhoneVonrState.put(phone.getSubId(), isVonrEnabled(phone));
68                     }
69                 }
70             };
71 
72     /** Get vonr_enabled per subId */
getVonrEnabled(int subId)73     public boolean getVonrEnabled(int subId) {
74         if (mFlags.vonrEnabledMetric()) {
75             return mPhoneVonrState.getOrDefault(subId, false);
76         } else {
77             return false;
78         }
79     }
80 }
81