• 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 ProfileNetworkPreferenceList {
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         public final boolean allowFallback;
42 
Preference(@onNull final UserHandle user, @Nullable final NetworkCapabilities capabilities, final boolean allowFallback)43         public Preference(@NonNull final UserHandle user,
44                 @Nullable final NetworkCapabilities capabilities,
45                 final boolean allowFallback) {
46             this.user = user;
47             this.capabilities = null == capabilities ? null : new NetworkCapabilities(capabilities);
48             this.allowFallback = allowFallback;
49         }
50 
51         /** toString */
toString()52         public String toString() {
53             return "[ProfileNetworkPreference user=" + user
54                     + " caps=" + capabilities
55                     + " allowFallback=" + allowFallback
56                     + "]";
57         }
58     }
59 
60     @NonNull public final List<Preference> preferences;
61 
ProfileNetworkPreferenceList()62     public ProfileNetworkPreferenceList() {
63         preferences = Collections.EMPTY_LIST;
64     }
65 
ProfileNetworkPreferenceList(@onNull final List<Preference> list)66     private ProfileNetworkPreferenceList(@NonNull final List<Preference> list) {
67         preferences = Collections.unmodifiableList(list);
68     }
69 
70     /**
71      * Returns a new object consisting of this object plus the passed preference.
72      *
73      * It is not expected that unwanted preference already exists for the same user.
74      * All preferences for the user that were previously configured should be cleared before
75      * adding a new preference.
76      * Passing a Preference object containing a null capabilities object is equivalent
77      * to removing the preference for this user.
78      */
plus(@onNull final Preference pref)79     public ProfileNetworkPreferenceList plus(@NonNull final Preference pref) {
80         final ArrayList<Preference> newPrefs = new ArrayList<>(preferences);
81         if (null != pref.capabilities) {
82             newPrefs.add(pref);
83         }
84         return new ProfileNetworkPreferenceList(newPrefs);
85     }
86 
87     /**
88      * Remove all preferences corresponding to a user.
89      */
withoutUser(UserHandle user)90     public ProfileNetworkPreferenceList withoutUser(UserHandle user) {
91         final ArrayList<Preference> newPrefs = new ArrayList<>();
92         for (final Preference existingPref : preferences) {
93             if (!existingPref.user.equals(user)) {
94                 newPrefs.add(existingPref);
95             }
96         }
97         return new ProfileNetworkPreferenceList(newPrefs);
98     }
99 
isEmpty()100     public boolean isEmpty() {
101         return preferences.isEmpty();
102     }
103 }
104