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.preference.Preference;
21import android.view.View.OnClickListener;
22
23import static com.android.internal.net.LegacyVpnInfo.STATE_CONNECTED;
24
25import com.android.internal.net.VpnProfile;
26import com.android.settings.R;
27
28/**
29 * {@link android.preference.Preference} referencing a VPN
30 * configuration. Tracks the underlying profile and its connection
31 * state.
32 */
33public class ConfigPreference extends ManageablePreference {
34    private VpnProfile mProfile;
35    private int mState = -1;
36
37    ConfigPreference(Context context, OnClickListener onManage, VpnProfile profile) {
38        super(context, null /* attrs */, onManage);
39        setProfile(profile);
40    }
41
42    public VpnProfile getProfile() {
43        return mProfile;
44    }
45
46    public void setProfile(VpnProfile profile) {
47        mProfile = profile;
48        update();
49    }
50
51    public void setState(int state) {
52        mState = state;
53        update();
54    }
55
56    private void update() {
57        if (mState < 0) {
58            setSummary("");
59        } else {
60            String[] states = getContext().getResources()
61                    .getStringArray(R.array.vpn_states);
62            setSummary(states[mState]);
63        }
64        setIcon(R.mipmap.ic_launcher_settings);
65        setTitle(mProfile.name);
66        notifyHierarchyChanged();
67    }
68
69    @Override
70    public int compareTo(Preference preference) {
71        if (preference instanceof ConfigPreference) {
72            ConfigPreference another = (ConfigPreference) preference;
73            int result;
74            if ((result = another.mState - mState) == 0 &&
75                    (result = mProfile.name.compareTo(another.mProfile.name)) == 0 &&
76                    (result = mProfile.type - another.mProfile.type) == 0) {
77                result = mProfile.key.compareTo(another.mProfile.key);
78            }
79            return result;
80        } else if (preference instanceof AppPreference) {
81            // Try to sort connected VPNs first
82            AppPreference another = (AppPreference) preference;
83            if (mState != STATE_CONNECTED && another.getState() == AppPreference.STATE_CONNECTED) {
84                return 1;
85            }
86            // Show configured VPNs before app VPNs
87            return -1;
88        } else {
89            return super.compareTo(preference);
90        }
91    }
92}
93
94