1 /* 2 * Copyright (C) 2015 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.settings.vpn2; 18 19 import static com.android.internal.net.LegacyVpnInfo.STATE_CONNECTED; 20 21 import android.content.Context; 22 import android.text.TextUtils; 23 import android.view.View; 24 25 import androidx.preference.Preference; 26 27 import com.android.internal.net.VpnProfile; 28 import com.android.settings.R; 29 30 /** 31 * {@link androidx.preference.Preference} tracks the underlying legacy vpn profile and 32 * its connection state. 33 */ 34 public class LegacyVpnPreference extends ManageablePreference { 35 private VpnProfile mProfile; 36 LegacyVpnPreference(Context context)37 LegacyVpnPreference(Context context) { 38 super(context, null /* attrs */); 39 setIcon(R.drawable.ic_vpn_key); 40 setIconSize(ICON_SIZE_SMALL); 41 } 42 getProfile()43 public VpnProfile getProfile() { 44 return mProfile; 45 } 46 setProfile(VpnProfile profile)47 public void setProfile(VpnProfile profile) { 48 final String oldLabel = (mProfile != null ? mProfile.name : null); 49 final String newLabel = (profile != null ? profile.name : null); 50 if (!TextUtils.equals(oldLabel, newLabel)) { 51 setTitle(newLabel); 52 notifyHierarchyChanged(); 53 } 54 mProfile = profile; 55 } 56 57 @Override compareTo(Preference preference)58 public int compareTo(Preference preference) { 59 if (preference instanceof LegacyVpnPreference) { 60 LegacyVpnPreference another = (LegacyVpnPreference) preference; 61 int result; 62 if ((result = another.mState - mState) == 0 && 63 (result = mProfile.name.compareToIgnoreCase(another.mProfile.name)) == 0 && 64 (result = mProfile.type - another.mProfile.type) == 0) { 65 result = mProfile.key.compareTo(another.mProfile.key); 66 } 67 return result; 68 } else if (preference instanceof AppPreference) { 69 // Try to sort connected VPNs first 70 AppPreference another = (AppPreference) preference; 71 if (mState != STATE_CONNECTED && another.getState() == AppPreference.STATE_CONNECTED) { 72 return 1; 73 } 74 // Show configured VPNs before app VPNs 75 return -1; 76 } else { 77 return super.compareTo(preference); 78 } 79 } 80 81 @Override onClick(View v)82 public void onClick(View v) { 83 if (v.getId() == R.id.settings_button && isDisabledByAdmin()) { 84 performClick(); 85 return; 86 } 87 super.onClick(v); 88 } 89 } 90