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 
17 package android.net;
18 
19 import android.annotation.SystemService;
20 import android.content.Context;
21 import android.os.RemoteException;
22 import android.os.ServiceManager;
23 import android.util.Log;
24 
25 import com.android.internal.net.INetworkWatchlistManager;
26 import com.android.internal.util.Preconditions;
27 
28 /**
29  * Class that manage network watchlist in system.
30  * @hide
31  */
32 @SystemService(Context.NETWORK_WATCHLIST_SERVICE)
33 public class NetworkWatchlistManager {
34 
35     private static final String TAG = "NetworkWatchlistManager";
36     private static final String SHARED_MEMORY_TAG = "NETWORK_WATCHLIST_SHARED_MEMORY";
37 
38     private final Context mContext;
39     private final INetworkWatchlistManager mNetworkWatchlistManager;
40 
41     /**
42      * @hide
43      */
NetworkWatchlistManager(Context context, INetworkWatchlistManager manager)44     public NetworkWatchlistManager(Context context, INetworkWatchlistManager manager) {
45         mContext = context;
46         mNetworkWatchlistManager = manager;
47     }
48 
49     /**
50      * @hide
51      */
NetworkWatchlistManager(Context context)52     public NetworkWatchlistManager(Context context) {
53         mContext = Preconditions.checkNotNull(context, "missing context");
54         mNetworkWatchlistManager = (INetworkWatchlistManager)
55                 INetworkWatchlistManager.Stub.asInterface(
56                         ServiceManager.getService(Context.NETWORK_WATCHLIST_SERVICE));
57     }
58 
59     /**
60      * Report network watchlist records if necessary.
61      *
62      * Watchlist report process will summarize records into a single report, then the
63      * report will be processed by differential privacy framework and stored on disk.
64      *
65      * @hide
66      */
reportWatchlistIfNecessary()67     public void reportWatchlistIfNecessary() {
68         try {
69             mNetworkWatchlistManager.reportWatchlistIfNecessary();
70         } catch (RemoteException e) {
71             Log.e(TAG, "Cannot report records", e);
72             e.rethrowFromSystemServer();
73         }
74     }
75 
76     /**
77      * Reload network watchlist.
78      *
79      * @hide
80      */
reloadWatchlist()81     public void reloadWatchlist() {
82         try {
83             mNetworkWatchlistManager.reloadWatchlist();
84         } catch (RemoteException e) {
85             Log.e(TAG, "Unable to reload watchlist");
86             e.rethrowFromSystemServer();
87         }
88     }
89 
90     /**
91      * Get Network Watchlist config file hash.
92      */
getWatchlistConfigHash()93     public byte[] getWatchlistConfigHash() {
94         try {
95             return mNetworkWatchlistManager.getWatchlistConfigHash();
96         } catch (RemoteException e) {
97             Log.e(TAG, "Unable to get watchlist config hash");
98             throw e.rethrowFromSystemServer();
99         }
100     }
101 }
102