• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.server.connectivity;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.net.NetworkCapabilities;
22 import android.os.UserHandle;
23 
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 
28 /**
29  * A data class containing all the per-profile network preferences.
30  *
31  * A given profile can only have one preference.
32  */
33 public class ProfileNetworkPreferences {
34     /**
35      * A single preference, as it applies to a given user profile.
36      */
37     public static class Preference {
38         @NonNull public final UserHandle user;
39         // Capabilities are only null when sending an object to remove the setting for a user
40         @Nullable public final NetworkCapabilities capabilities;
41 
Preference(@onNull final UserHandle user, @Nullable final NetworkCapabilities capabilities)42         public Preference(@NonNull final UserHandle user,
43                 @Nullable final NetworkCapabilities capabilities) {
44             this.user = user;
45             this.capabilities = null == capabilities ? null : new NetworkCapabilities(capabilities);
46         }
47 
48         /** toString */
toString()49         public String toString() {
50             return "[ProfileNetworkPreference user=" + user + " caps=" + capabilities + "]";
51         }
52     }
53 
54     @NonNull public final List<Preference> preferences;
55 
ProfileNetworkPreferences()56     public ProfileNetworkPreferences() {
57         preferences = Collections.EMPTY_LIST;
58     }
59 
ProfileNetworkPreferences(@onNull final List<Preference> list)60     private ProfileNetworkPreferences(@NonNull final List<Preference> list) {
61         preferences = Collections.unmodifiableList(list);
62     }
63 
64     /**
65      * Returns a new object consisting of this object plus the passed preference.
66      *
67      * If a preference already exists for the same user, it will be replaced by the passed
68      * preference. Passing a Preference object containing a null capabilities object is equivalent
69      * to (and indeed, implemented as) removing the preference for this user.
70      */
plus(@onNull final Preference pref)71     public ProfileNetworkPreferences plus(@NonNull final Preference pref) {
72         final ArrayList<Preference> newPrefs = new ArrayList<>();
73         for (final Preference existingPref : preferences) {
74             if (!existingPref.user.equals(pref.user)) {
75                 newPrefs.add(existingPref);
76             }
77         }
78         if (null != pref.capabilities) {
79             newPrefs.add(pref);
80         }
81         return new ProfileNetworkPreferences(newPrefs);
82     }
83 
isEmpty()84     public boolean isEmpty() {
85         return preferences.isEmpty();
86     }
87 }
88