1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
/*
* Copyright (c) 2012-2014 Arne Schwabe
* Distributed under the GNU GPL v2. For full terms see the file doc/LICENSE.txt
*/
package de.blinkt.openvpn.fragments;
import android.Manifest;
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.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;
import java.util.Collections;
import java.util.List;
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 {
private ListView mListView;
private VpnProfile mProfile;
private TextView mDefaultAllowTextView;
private PackageAdapter mListAdapter;
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AppViewHolder avh = (AppViewHolder) view.getTag();
avh.checkBox.toggle();
}
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) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.allowed_application_layout, null);
// 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();
}
}
}
@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);
}
}
class PackageAdapter extends BaseAdapter {
private final List<ApplicationInfo> mPackages;
private final LayoutInflater mInflater;
private final PackageManager mPm;
PackageAdapter(Context c, VpnProfile vp) {
mPm = c.getPackageManager();
mProfile = vp;
List<ApplicationInfo> installedPackages = mPm.getInstalledApplications(PackageManager.GET_META_DATA);
mInflater = LayoutInflater.from(c);
// 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;
}
@Override
public int getCount() {
return mPackages.size();
}
@Override
public Object getItem(int position) {
return mPackages.get(position);
}
@Override
public long getItemId(int position) {
return mPackages.get(position).packageName.hashCode();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
AppViewHolder viewHolder = AppViewHolder.createOrRecycle(mInflater, convertView);
convertView = viewHolder.rootView;
viewHolder.mInfo = mPackages.get(position);
final ApplicationInfo mInfo = mPackages.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 void onResume() {
super.onResume();
}
@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()));
}
@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);
mListView = (ListView) v.findViewById(android.R.id.list);
mListAdapter = new PackageAdapter(getActivity(), mProfile);
mListView.setAdapter(mListAdapter);
mListView.setOnItemClickListener(this);
return v;
}
private void changeDisallowText(boolean selectedAreDisallowed) {
if (selectedAreDisallowed)
mDefaultAllowTextView.setText(R.string.vpn_disallow_radio);
else
mDefaultAllowTextView.setText(R.string.vpn_allow_radio);
}
}
|