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