summaryrefslogtreecommitdiff
path: root/main
diff options
context:
space:
mode:
Diffstat (limited to 'main')
-rw-r--r--main/src/main/java/de/blinkt/openvpn/fragments/Settings_Allowed_Apps.java355
-rw-r--r--main/src/main/java/de/blinkt/openvpn/fragments/Settings_Allowed_Apps.kt317
2 files changed, 317 insertions, 355 deletions
diff --git a/main/src/main/java/de/blinkt/openvpn/fragments/Settings_Allowed_Apps.java b/main/src/main/java/de/blinkt/openvpn/fragments/Settings_Allowed_Apps.java
deleted file mode 100644
index aac96f10..00000000
--- a/main/src/main/java/de/blinkt/openvpn/fragments/Settings_Allowed_Apps.java
+++ /dev/null
@@ -1,355 +0,0 @@
-/*
- * Copyright (c) 2012-2016 Arne Schwabe
- * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt
- */
-
-package de.blinkt.openvpn.fragments;
-
-import android.Manifest;
-import android.app.Activity;
-import android.app.Fragment;
-import android.content.Context;
-import android.content.pm.ApplicationInfo;
-import android.content.pm.PackageManager;
-import android.os.Bundle;
-import android.text.TextUtils;
-import android.util.Log;
-import android.view.LayoutInflater;
-import android.view.Menu;
-import android.view.MenuInflater;
-import android.view.View;
-import android.view.ViewGroup;
-import android.widget.AdapterView;
-import android.widget.BaseAdapter;
-import android.widget.CompoundButton;
-import android.widget.Filter;
-import android.widget.Filterable;
-import android.widget.ImageView;
-import android.widget.ListView;
-import android.widget.SearchView;
-import android.widget.Switch;
-import android.widget.TextView;
-
-import java.util.Collections;
-import java.util.List;
-import java.util.Locale;
-import java.util.Vector;
-
-import de.blinkt.openvpn.R;
-import de.blinkt.openvpn.VpnProfile;
-import de.blinkt.openvpn.core.ProfileManager;
-
-/**
- * Created by arne on 16.11.14.
- */
-public class Settings_Allowed_Apps extends Fragment implements AdapterView.OnItemClickListener, CompoundButton.OnCheckedChangeListener, View.OnClickListener {
- private ListView mListView;
- private VpnProfile mProfile;
- private TextView mDefaultAllowTextView;
- private TextView mAllowBypassTextView;
- private PackageAdapter mListAdapter;
-
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- AppViewHolder avh = (AppViewHolder) view.getTag();
- avh.checkBox.toggle();
- }
-
- @Override
- public void onClick(View v) {
-
- }
-
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- String packageName = (String) buttonView.getTag();
- if (isChecked) {
- Log.d("openvpn", "adding to allowed apps" + packageName);
- mProfile.mAllowedAppsVpn.add(packageName);
- } else {
- Log.d("openvpn", "removing from allowed apps" + packageName);
- mProfile.mAllowedAppsVpn.remove(packageName);
- }
-
- }
-
- @Override
- public void onResume() {
- super.onResume();
- changeDisallowText(mProfile.mAllowedAppsVpnAreDisallowed);
- }
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- String profileUuid = getArguments().getString(getActivity().getPackageName() + ".profileUUID");
- mProfile = ProfileManager.get(getActivity(), profileUuid);
- getActivity().setTitle(getString(R.string.edit_profile_title, mProfile.getName()));
- setHasOptionsMenu(true);
- }
-
- @Override
- public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
- inflater.inflate(R.menu.allowed_apps, menu);
-
- SearchView searchView = (SearchView) menu.findItem(R.id.app_search_widget).getActionView();
- searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
- @Override
- public boolean onQueryTextSubmit(String query) {
- mListView.setFilterText(query);
- mListView.setTextFilterEnabled(true);
- return true;
- }
-
- @Override
- public boolean onQueryTextChange(String newText) {
- mListView.setFilterText(newText);
- if (TextUtils.isEmpty(newText))
- mListView.setTextFilterEnabled(false);
- else
- mListView.setTextFilterEnabled(true);
-
- return true;
- }
- });
- searchView.setOnCloseListener(new SearchView.OnCloseListener() {
- @Override
- public boolean onClose() {
- mListView.clearTextFilter();
- mListAdapter.getFilter().filter("");
- return false;
- }
- });
-
- super.onCreateOptionsMenu(menu, inflater);
- }
-
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View v = inflater.inflate(R.layout.allowed_vpn_apps, container, false);
-
- mDefaultAllowTextView = (TextView) v.findViewById(R.id.default_allow_text);
-
- Switch vpnOnDefaultSwitch = (Switch) v.findViewById(R.id.default_allow);
-
- vpnOnDefaultSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
-
- changeDisallowText(isChecked);
- mProfile.mAllowedAppsVpnAreDisallowed = isChecked;
- }
- });
-
- vpnOnDefaultSwitch.setChecked(mProfile.mAllowedAppsVpnAreDisallowed);
-
- Switch vpnAllowBypassSwitch = (Switch) v.findViewById(R.id.allow_bypass);
-
- vpnAllowBypassSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- mProfile.mAllowAppVpnBypass = isChecked;
- }
- });
-
- vpnAllowBypassSwitch.setChecked(mProfile.mAllowAppVpnBypass);
-
- mListView = (ListView) v.findViewById(android.R.id.list);
-
- mListAdapter = new PackageAdapter(getActivity(), mProfile);
- mListView.setAdapter(mListAdapter);
- mListView.setOnItemClickListener(this);
-
- mListView.setEmptyView(v.findViewById(R.id.loading_container));
-
- new Thread(new Runnable() {
- @Override
- public void run() {
- mListAdapter.populateList(getActivity());
- }
- }).start();
-
- return v;
- }
-
- private void changeDisallowText(boolean selectedAreDisallowed) {
- if (selectedAreDisallowed)
- mDefaultAllowTextView.setText(R.string.vpn_disallow_radio);
- else
- mDefaultAllowTextView.setText(R.string.vpn_allow_radio);
- }
-
- static class AppViewHolder {
- public ApplicationInfo mInfo;
- public View rootView;
- public TextView appName;
- public ImageView appIcon;
- //public TextView appSize;
- //public TextView disabled;
- public CompoundButton checkBox;
-
- static public AppViewHolder createOrRecycle(LayoutInflater inflater, View convertView, ViewGroup parent) {
- if (convertView == null) {
- convertView = inflater.inflate(R.layout.allowed_application_layout, parent, false);
-
- // Creates a ViewHolder and store references to the two children views
- // we want to bind data to.
- AppViewHolder holder = new AppViewHolder();
- holder.rootView = convertView;
- holder.appName = (TextView) convertView.findViewById(R.id.app_name);
- holder.appIcon = (ImageView) convertView.findViewById(R.id.app_icon);
- //holder.appSize = (TextView) convertView.findViewById(R.id.app_size);
- //holder.disabled = (TextView) convertView.findViewById(R.id.app_disabled);
- holder.checkBox = (CompoundButton) convertView.findViewById(R.id.app_selected);
- convertView.setTag(holder);
-
-
- return holder;
- } else {
- // Get the ViewHolder back to get fast access to the TextView
- // and the ImageView.
- return (AppViewHolder) convertView.getTag();
- }
- }
-
- }
-
- class PackageAdapter extends BaseAdapter implements Filterable {
- private final LayoutInflater mInflater;
- private final PackageManager mPm;
- private Vector<ApplicationInfo> mPackages;
- private ItemFilter mFilter = new ItemFilter();
- private Vector<ApplicationInfo> mFilteredData;
-
-
- PackageAdapter(Context c, VpnProfile vp) {
- mPm = c.getPackageManager();
- mProfile = vp;
- mInflater = LayoutInflater.from(c);
-
- mPackages = new Vector<>();
- mFilteredData = mPackages;
- }
-
- private void populateList(Activity c) {
- List<ApplicationInfo> installedPackages = mPm.getInstalledApplications(PackageManager.GET_META_DATA);
-
- // Remove apps not using Internet
-
- int androidSystemUid = 0;
- ApplicationInfo system = null;
- Vector<ApplicationInfo> apps = new Vector<ApplicationInfo>();
-
- try {
- system = mPm.getApplicationInfo("android", PackageManager.GET_META_DATA);
- androidSystemUid = system.uid;
- apps.add(system);
- } catch (PackageManager.NameNotFoundException e) {
- }
-
-
- for (ApplicationInfo app : installedPackages) {
-
- if (mPm.checkPermission(Manifest.permission.INTERNET, app.packageName) == PackageManager.PERMISSION_GRANTED &&
- app.uid != androidSystemUid) {
-
- apps.add(app);
- }
- }
-
- Collections.sort(apps, new ApplicationInfo.DisplayNameComparator(mPm));
- mPackages = apps;
- mFilteredData = apps;
- c.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- notifyDataSetChanged();
- }
- });
- }
-
- @Override
- public int getCount() {
- return mFilteredData.size();
- }
-
- @Override
- public Object getItem(int position) {
- return mFilteredData.get(position);
- }
-
- @Override
- public long getItemId(int position) {
- return mFilteredData.get(position).packageName.hashCode();
- }
-
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- AppViewHolder viewHolder = AppViewHolder.createOrRecycle(mInflater, convertView, parent);
- convertView = viewHolder.rootView;
- viewHolder.mInfo = mFilteredData.get(position);
- final ApplicationInfo mInfo = mFilteredData.get(position);
-
-
- CharSequence appName = mInfo.loadLabel(mPm);
-
- if (TextUtils.isEmpty(appName))
- appName = mInfo.packageName;
- viewHolder.appName.setText(appName);
- viewHolder.appIcon.setImageDrawable(mInfo.loadIcon(mPm));
- viewHolder.checkBox.setTag(mInfo.packageName);
- viewHolder.checkBox.setOnCheckedChangeListener(Settings_Allowed_Apps.this);
-
-
- viewHolder.checkBox.setChecked(mProfile.mAllowedAppsVpn.contains(mInfo.packageName));
- return viewHolder.rootView;
- }
-
- @Override
- public Filter getFilter() {
- return mFilter;
- }
-
- private class ItemFilter extends Filter {
- @Override
- protected FilterResults performFiltering(CharSequence constraint) {
-
- String filterString = constraint.toString().toLowerCase(Locale.getDefault());
-
- FilterResults results = new FilterResults();
-
-
- int count = mPackages.size();
- final Vector<ApplicationInfo> nlist = new Vector<>(count);
-
- for (int i = 0; i < count; i++) {
- ApplicationInfo pInfo = mPackages.get(i);
- CharSequence appName = pInfo.loadLabel(mPm);
-
- if (TextUtils.isEmpty(appName))
- appName = pInfo.packageName;
-
- if (appName instanceof String) {
- if (((String) appName).toLowerCase(Locale.getDefault()).contains(filterString))
- nlist.add(pInfo);
- } else {
- if (appName.toString().toLowerCase(Locale.getDefault()).contains(filterString))
- nlist.add(pInfo);
- }
- }
- results.values = nlist;
- results.count = nlist.size();
-
- return results;
- }
-
- @Override
- protected void publishResults(CharSequence constraint, FilterResults results) {
- mFilteredData = (Vector<ApplicationInfo>) results.values;
- notifyDataSetChanged();
- }
-
- }
- }
-}
diff --git a/main/src/main/java/de/blinkt/openvpn/fragments/Settings_Allowed_Apps.kt b/main/src/main/java/de/blinkt/openvpn/fragments/Settings_Allowed_Apps.kt
new file mode 100644
index 00000000..64790cc3
--- /dev/null
+++ b/main/src/main/java/de/blinkt/openvpn/fragments/Settings_Allowed_Apps.kt
@@ -0,0 +1,317 @@
+/*
+ * Copyright (c) 2012-2016 Arne Schwabe
+ * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt
+ */
+
+package de.blinkt.openvpn.fragments
+
+import android.Manifest
+import android.app.Activity
+import android.app.Fragment
+import android.content.Context
+import android.content.pm.ApplicationInfo
+import android.content.pm.PackageManager
+import android.os.Bundle
+import android.text.TextUtils
+import android.util.Log
+import android.view.LayoutInflater
+import android.view.Menu
+import android.view.MenuInflater
+import android.view.View
+import android.view.ViewGroup
+import android.widget.AdapterView
+import android.widget.BaseAdapter
+import android.widget.CompoundButton
+import android.widget.Filter
+import android.widget.Filterable
+import android.widget.ImageView
+import android.widget.ListView
+import android.widget.SearchView
+import android.widget.Switch
+import android.widget.TextView
+
+import java.util.Collections
+import java.util.Locale
+import java.util.Vector
+
+import de.blinkt.openvpn.R
+import de.blinkt.openvpn.VpnProfile
+import de.blinkt.openvpn.core.ProfileManager
+
+/**
+ * Created by arne on 16.11.14.
+ */
+class Settings_Allowed_Apps : Fragment(), AdapterView.OnItemClickListener, CompoundButton.OnCheckedChangeListener, View.OnClickListener {
+ private var mListView: ListView? = null
+ private var mProfile: VpnProfile? = null
+ private var mDefaultAllowTextView: TextView? = null
+ private val mAllowBypassTextView: TextView? = null
+ private var mListAdapter: PackageAdapter? = null
+
+ override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) {
+ val avh = view.tag as AppViewHolder
+ avh.checkBox.toggle()
+ }
+
+ override fun onClick(v: View) {
+
+ }
+
+ override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {
+ val packageName = buttonView.tag as String
+ if (isChecked) {
+ mProfile!!.mAllowedAppsVpn.add(packageName)
+ } else {
+ mProfile!!.mAllowedAppsVpn.remove(packageName)
+ }
+ }
+
+ override fun onResume() {
+ super.onResume()
+ changeDisallowText(mProfile!!.mAllowedAppsVpnAreDisallowed)
+ }
+
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+
+ val profileUuid = arguments.getString(activity.packageName + ".profileUUID")
+ mProfile = ProfileManager.get(activity, profileUuid)
+ activity.title = getString(R.string.edit_profile_title, mProfile!!.name)
+ setHasOptionsMenu(true)
+ }
+
+ override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
+ inflater.inflate(R.menu.allowed_apps, menu)
+
+ val searchView = menu.findItem(R.id.app_search_widget).actionView as SearchView
+ searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
+ override fun onQueryTextSubmit(query: String): Boolean {
+ mListView!!.setFilterText(query)
+ mListView!!.isTextFilterEnabled = true
+ return true
+ }
+
+ override fun onQueryTextChange(newText: String): Boolean {
+ mListView!!.setFilterText(newText)
+ if (TextUtils.isEmpty(newText))
+ mListView!!.isTextFilterEnabled = false
+ else
+ mListView!!.isTextFilterEnabled = true
+
+ return true
+ }
+ })
+ searchView.setOnCloseListener {
+ mListView!!.clearTextFilter()
+ mListAdapter!!.filter.filter("")
+ false
+ }
+
+ super.onCreateOptionsMenu(menu, inflater)
+ }
+
+ override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle): View? {
+ val v = inflater.inflate(R.layout.allowed_vpn_apps, container, false)
+
+ mDefaultAllowTextView = v.findViewById<View>(R.id.default_allow_text) as TextView
+
+ val vpnOnDefaultSwitch = v.findViewById<View>(R.id.default_allow) as Switch
+
+ vpnOnDefaultSwitch.setOnCheckedChangeListener { buttonView, isChecked ->
+ changeDisallowText(isChecked)
+ mProfile!!.mAllowedAppsVpnAreDisallowed = isChecked
+ }
+
+ vpnOnDefaultSwitch.isChecked = mProfile!!.mAllowedAppsVpnAreDisallowed
+
+ val vpnAllowBypassSwitch = v.findViewById<View>(R.id.allow_bypass) as Switch
+
+ vpnAllowBypassSwitch.setOnCheckedChangeListener { buttonView, isChecked -> mProfile!!.mAllowAppVpnBypass = isChecked }
+
+ vpnAllowBypassSwitch.isChecked = mProfile!!.mAllowAppVpnBypass
+
+ mListView = v.findViewById<View>(android.R.id.list) as ListView
+
+ mListAdapter = PackageAdapter(activity, mProfile)
+ mListView!!.adapter = mListAdapter
+ mListView!!.onItemClickListener = this
+
+ mListView!!.emptyView = v.findViewById(R.id.loading_container)
+
+ Thread(Runnable { mListAdapter!!.populateList(activity) }).start()
+
+ return v
+ }
+
+ private fun changeDisallowText(selectedAreDisallowed: Boolean) {
+ if (selectedAreDisallowed)
+ mDefaultAllowTextView!!.setText(R.string.vpn_disallow_radio)
+ else
+ mDefaultAllowTextView!!.setText(R.string.vpn_allow_radio)
+ }
+
+ internal class AppViewHolder {
+ var mInfo: ApplicationInfo? = null
+ var rootView: View? = null
+ var appName: TextView
+ var appIcon: ImageView
+ //public TextView appSize;
+ //public TextView disabled;
+ var checkBox: CompoundButton
+
+ companion object {
+
+ fun createOrRecycle(inflater: LayoutInflater, convertView: View?, parent: ViewGroup): AppViewHolder {
+ var convertView = convertView
+ if (convertView == null) {
+ convertView = inflater.inflate(R.layout.allowed_application_layout, parent, false)
+
+ // Creates a ViewHolder and store references to the two children views
+ // we want to bind data to.
+ val holder = AppViewHolder()
+ holder.rootView = convertView
+ holder.appName = convertView!!.findViewById<View>(R.id.app_name) as TextView
+ holder.appIcon = convertView.findViewById<View>(R.id.app_icon) as ImageView
+ //holder.appSize = (TextView) convertView.findViewById(R.id.app_size);
+ //holder.disabled = (TextView) convertView.findViewById(R.id.app_disabled);
+ holder.checkBox = convertView.findViewById<View>(R.id.app_selected) as CompoundButton
+ convertView.tag = holder
+
+
+ return holder
+ } else {
+ // Get the ViewHolder back to get fast access to the TextView
+ // and the ImageView.
+ return convertView.tag as AppViewHolder
+ }
+ }
+ }
+
+ }
+
+ internal inner class PackageAdapter(c: Context, vp: VpnProfile) : BaseAdapter(), Filterable {
+ private val mInflater: LayoutInflater
+ private val mPm: PackageManager
+ private var mPackages: Vector<ApplicationInfo>? = null
+ private val mFilter = ItemFilter()
+ private var mFilteredData: Vector<ApplicationInfo>? = null
+
+
+ init {
+ mPm = c.packageManager
+ mProfile = vp
+ mInflater = LayoutInflater.from(c)
+
+ mPackages = Vector()
+ mFilteredData = mPackages
+ }
+
+ private fun populateList(c: Activity) {
+ val installedPackages = mPm.getInstalledApplications(PackageManager.GET_META_DATA)
+
+ // Remove apps not using Internet
+
+ var androidSystemUid = 0
+ var system: ApplicationInfo? = null
+ val apps = Vector<ApplicationInfo>()
+
+ try {
+ system = mPm.getApplicationInfo("android", PackageManager.GET_META_DATA)
+ androidSystemUid = system!!.uid
+ apps.add(system)
+ } catch (e: PackageManager.NameNotFoundException) {
+ }
+
+
+ for (app in installedPackages) {
+
+ if (mPm.checkPermission(Manifest.permission.INTERNET, app.packageName) == PackageManager.PERMISSION_GRANTED && app.uid != androidSystemUid) {
+
+ apps.add(app)
+ }
+ }
+
+ Collections.sort(apps, ApplicationInfo.DisplayNameComparator(mPm))
+ mPackages = apps
+ mFilteredData = apps
+ c.runOnUiThread { notifyDataSetChanged() }
+ }
+
+ override fun getCount(): Int {
+ return mFilteredData!!.size
+ }
+
+ override fun getItem(position: Int): Any {
+ return mFilteredData!![position]
+ }
+
+ override fun getItemId(position: Int): Long {
+ return mFilteredData!![position].packageName.hashCode().toLong()
+ }
+
+ override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? {
+ var convertView = convertView
+ val viewHolder = AppViewHolder.createOrRecycle(mInflater, convertView, parent)
+ convertView = viewHolder.rootView
+ viewHolder.mInfo = mFilteredData!![position]
+ val mInfo = mFilteredData!![position]
+
+
+ var appName = mInfo.loadLabel(mPm)
+
+ if (TextUtils.isEmpty(appName))
+ appName = mInfo.packageName
+ viewHolder.appName.text = appName
+ viewHolder.appIcon.setImageDrawable(mInfo.loadIcon(mPm))
+ viewHolder.checkBox.tag = mInfo.packageName
+ viewHolder.checkBox.setOnCheckedChangeListener(this@Settings_Allowed_Apps)
+
+
+ viewHolder.checkBox.isChecked = mProfile!!.mAllowedAppsVpn.contains(mInfo.packageName)
+ return viewHolder.rootView
+ }
+
+ override fun getFilter(): Filter {
+ return mFilter
+ }
+
+ private inner class ItemFilter : Filter() {
+ override fun performFiltering(constraint: CharSequence): Filter.FilterResults {
+
+ val filterString = constraint.toString().toLowerCase(Locale.getDefault())
+
+ val results = Filter.FilterResults()
+
+
+ val count = mPackages!!.size
+ val nlist = Vector<ApplicationInfo>(count)
+
+ for (i in 0 until count) {
+ val pInfo = mPackages!![i]
+ var appName = pInfo.loadLabel(mPm)
+
+ if (TextUtils.isEmpty(appName))
+ appName = pInfo.packageName
+
+ if (appName is String) {
+ if (appName.toLowerCase(Locale.getDefault()).contains(filterString))
+ nlist.add(pInfo)
+ } else {
+ if (appName.toString().toLowerCase(Locale.getDefault()).contains(filterString))
+ nlist.add(pInfo)
+ }
+ }
+ results.values = nlist
+ results.count = nlist.size
+
+ return results
+ }
+
+ override fun publishResults(constraint: CharSequence, results: Filter.FilterResults) {
+ mFilteredData = results.values as Vector<ApplicationInfo>
+ notifyDataSetChanged()
+ }
+
+ }
+ }
+}