Search in sources :

Example 1 with VpnProfile

use of android.net.vpn.VpnProfile in project android_packages_apps_Settings by LineageOS.

the class VpnSettings method replaceProfile.

// index: index to mVpnProfileList
private void replaceProfile(int index, VpnProfile p) throws IOException {
    Map<String, VpnPreference> map = mVpnPreferenceMap;
    VpnProfile oldProfile = mVpnProfileList.set(index, p);
    VpnPreference pref = map.remove(oldProfile.getName());
    if (pref.mProfile != oldProfile) {
        throw new RuntimeException("inconsistent state!");
    }
    p.setId(oldProfile.getId());
    processSecrets(p);
    // directories.
    if (Util.copyFiles(getProfileDir(oldProfile), getProfileDir(p))) {
        removeProfileFromStorage(oldProfile);
    }
    saveProfileToStorage(p);
    pref.setProfile(p);
    map.put(p.getName(), pref);
}
Also used : VpnProfile(android.net.vpn.VpnProfile)

Example 2 with VpnProfile

use of android.net.vpn.VpnProfile in project android_packages_apps_Settings by LineageOS.

the class VpnSettings method onActivityResult.

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    if ((resultCode == RESULT_CANCELED) || (data == null)) {
        Log.d(TAG, "no result returned by editor");
        return;
    }
    if (requestCode == REQUEST_SELECT_VPN_TYPE) {
        String typeName = data.getStringExtra(KEY_VPN_TYPE);
        startVpnEditor(createVpnProfile(typeName));
    } else if (requestCode == REQUEST_ADD_OR_EDIT_PROFILE) {
        VpnProfile p = data.getParcelableExtra(KEY_VPN_PROFILE);
        if (p == null) {
            Log.e(TAG, "null object returned by editor");
            return;
        }
        int index = getProfileIndexFromId(p.getId());
        if (checkDuplicateName(p, index)) {
            final VpnProfile profile = p;
            Util.showErrorMessage(this, String.format(getString(R.string.vpn_error_duplicate_name), p.getName()), new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int w) {
                    startVpnEditor(profile);
                }
            });
            return;
        }
        if (needKeyStoreToSave(p)) {
            Runnable action = new Runnable() {

                public void run() {
                    onActivityResult(requestCode, resultCode, data);
                }
            };
            if (!unlockKeyStore(p, action))
                return;
        }
        try {
            if (index < 0) {
                addProfile(p);
                Util.showShortToastMessage(this, String.format(getString(R.string.vpn_profile_added), p.getName()));
            } else {
                replaceProfile(index, p);
                Util.showShortToastMessage(this, String.format(getString(R.string.vpn_profile_replaced), p.getName()));
            }
        } catch (IOException e) {
            final VpnProfile profile = p;
            Util.showErrorMessage(this, e + ": " + e.getMessage(), new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int w) {
                    startVpnEditor(profile);
                }
            });
        }
    } else {
        throw new RuntimeException("unknown request code: " + requestCode);
    }
}
Also used : VpnProfile(android.net.vpn.VpnProfile) DialogInterface(android.content.DialogInterface) IOException(java.io.IOException)

Example 3 with VpnProfile

use of android.net.vpn.VpnProfile in project android_packages_apps_Settings by LineageOS.

the class VpnSettings method retrieveVpnListFromStorage.

private void retrieveVpnListFromStorage() {
    mVpnPreferenceMap = new LinkedHashMap<String, VpnPreference>();
    mVpnProfileList = Collections.synchronizedList(new ArrayList<VpnProfile>());
    mVpnListContainer.removeAll();
    File root = new File(PROFILES_ROOT);
    String[] dirs = root.list();
    if (dirs == null)
        return;
    for (String dir : dirs) {
        File f = new File(new File(root, dir), PROFILE_OBJ_FILE);
        if (!f.exists())
            continue;
        try {
            VpnProfile p = deserialize(f);
            if (p == null)
                continue;
            if (!checkIdConsistency(dir, p))
                continue;
            mVpnProfileList.add(p);
        } catch (IOException e) {
            Log.e(TAG, "retrieveVpnListFromStorage()", e);
        }
    }
    Collections.sort(mVpnProfileList, new Comparator<VpnProfile>() {

        public int compare(VpnProfile p1, VpnProfile p2) {
            return p1.getName().compareTo(p2.getName());
        }

        public boolean equals(VpnProfile p) {
            // not used
            return false;
        }
    });
    for (VpnProfile p : mVpnProfileList) {
        Preference pref = addPreferenceFor(p, false);
    }
    disableProfilePreferencesIfOneActive();
}
Also used : VpnProfile(android.net.vpn.VpnProfile) Preference(android.preference.Preference) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File)

Example 4 with VpnProfile

use of android.net.vpn.VpnProfile in project android_packages_apps_Settings by LineageOS.

the class VpnSettings method onCreateContextMenu.

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    VpnProfile p = getProfile(getProfilePositionFrom((AdapterContextMenuInfo) menuInfo));
    if (p != null) {
        VpnState state = p.getState();
        menu.setHeaderTitle(p.getName());
        boolean isIdle = (state == VpnState.IDLE);
        boolean isNotConnect = (isIdle || (state == VpnState.DISCONNECTING) || (state == VpnState.CANCELLED));
        menu.add(0, CONTEXT_MENU_CONNECT_ID, 0, R.string.vpn_menu_connect).setEnabled(isIdle && (mActiveProfile == null));
        menu.add(0, CONTEXT_MENU_DISCONNECT_ID, 0, R.string.vpn_menu_disconnect).setEnabled(state == VpnState.CONNECTED);
        menu.add(0, CONTEXT_MENU_EDIT_ID, 0, R.string.vpn_menu_edit).setEnabled(isNotConnect);
        menu.add(0, CONTEXT_MENU_DELETE_ID, 0, R.string.vpn_menu_delete).setEnabled(isNotConnect);
    }
}
Also used : VpnProfile(android.net.vpn.VpnProfile) VpnState(android.net.vpn.VpnState) AdapterContextMenuInfo(android.widget.AdapterView.AdapterContextMenuInfo)

Example 5 with VpnProfile

use of android.net.vpn.VpnProfile in project android_packages_apps_Settings by LineageOS.

the class VpnSettings method onContextItemSelected.

@Override
public boolean onContextItemSelected(MenuItem item) {
    int position = getProfilePositionFrom((AdapterContextMenuInfo) item.getMenuInfo());
    VpnProfile p = getProfile(position);
    switch(item.getItemId()) {
        case CONTEXT_MENU_CONNECT_ID:
        case CONTEXT_MENU_DISCONNECT_ID:
            connectOrDisconnect(p);
            return true;
        case CONTEXT_MENU_EDIT_ID:
            startVpnEditor(p);
            return true;
        case CONTEXT_MENU_DELETE_ID:
            deleteProfile(position);
            return true;
    }
    return super.onContextItemSelected(item);
}
Also used : VpnProfile(android.net.vpn.VpnProfile)

Aggregations

VpnProfile (android.net.vpn.VpnProfile)9 DialogInterface (android.content.DialogInterface)2 IOException (java.io.IOException)2 AlertDialog (android.app.AlertDialog)1 VpnState (android.net.vpn.VpnState)1 Parcel (android.os.Parcel)1 Preference (android.preference.Preference)1 AdapterContextMenuInfo (android.widget.AdapterView.AdapterContextMenuInfo)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ArrayList (java.util.ArrayList)1