-
-
Notifications
You must be signed in to change notification settings - Fork 326
/
AppManagerActivity.java
317 lines (240 loc) · 8.82 KB
/
AppManagerActivity.java
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* Copyright (c) 2009, Nathan Freitas, Orbot / The Guardian Project - http://openideals.com/guardian */
/* See LICENSE for licensing information */
package org.torproject.android.mini.ui;
import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;
import org.torproject.android.mini.R;
import org.torproject.android.service.OrbotConstants;
import org.torproject.android.service.util.Prefs;
import org.torproject.android.service.vpn.TorifiedApp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import static org.torproject.android.service.vpn.VpnPrefs.PREFS_KEY_TORIFIED;
public class AppManagerActivity extends AppCompatActivity implements OnClickListener, OrbotConstants {
private GridView listApps;
private ListAdapter adapterApps;
private ProgressBar progressBar;
PackageManager pMgr = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pMgr = getPackageManager();
this.setContentView(R.layout.layout_apps);
setTitle("");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
listApps = findViewById(R.id.applistview);
progressBar = findViewById(R.id.progressBar);
}
@Override
protected void onResume() {
super.onResume();
mPrefs = Prefs.getSharedPrefs(getApplicationContext());
reloadApps();
}
/*
* Create the UI Options Menu (non-Javadoc)
* @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.app_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_refresh_apps)
{
mApps = null;
reloadApps();
}
else if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
private void reloadApps () {
new AsyncTask<Void, Void, Void>() {
protected void onPreExecute() {
// Pre Code
progressBar.setVisibility(View.VISIBLE);
}
protected Void doInBackground(Void... unused) {
loadApps(mPrefs);
return null;
}
protected void onPostExecute(Void unused) {
listApps.setAdapter(adapterApps);
progressBar.setVisibility(View.GONE);
}
}.execute();
}
SharedPreferences mPrefs = null;
static ArrayList<TorifiedApp> mApps = null;
private void loadApps (SharedPreferences prefs) {
if (mApps == null)
mApps = getApps(prefs);
final LayoutInflater inflater = getLayoutInflater();
adapterApps = new ArrayAdapter<TorifiedApp>(this, R.layout.layout_apps_item, R.id.itemtext,mApps) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ListEntry entry = null;
if (convertView == null)
convertView = inflater.inflate(R.layout.layout_apps_item, parent, false);
else
entry = (ListEntry) convertView.getTag();
if (entry == null) {
// Inflate a new view
entry = new ListEntry();
entry.icon = convertView.findViewById(R.id.itemicon);
entry.text = convertView.findViewById(R.id.itemtext);
convertView.setTag(entry);
}
final TorifiedApp app = mApps.get(position);
if (entry.icon != null) {
try {
entry.icon.setImageDrawable(pMgr.getApplicationIcon(app.getPackageName()));
entry.icon.setOnClickListener(AppManagerActivity.this);
entry.icon.setTag(app);
}
catch (Exception e)
{
e.printStackTrace();
}
}
if (entry.text != null) {
entry.text.setText(app.getName());
}
return convertView;
}
};
}
private static class ListEntry {
private TextView text;
private ImageView icon;
}
public ArrayList<TorifiedApp> getApps(SharedPreferences prefs) {
String tordAppString = prefs.getString(PREFS_KEY_TORIFIED, "");
String[] tordApps;
StringTokenizer st = new StringTokenizer(tordAppString,"|");
tordApps = new String[st.countTokens()];
int tordIdx = 0;
while (st.hasMoreTokens())
{
tordApps[tordIdx++] = st.nextToken();
}
Arrays.sort(tordApps);
List<ApplicationInfo> lAppInfo = pMgr.getInstalledApplications(0);
Iterator<ApplicationInfo> itAppInfo = lAppInfo.iterator();
ArrayList<TorifiedApp> apps = new ArrayList<>();
ApplicationInfo aInfo;
TorifiedApp app;
while (itAppInfo.hasNext())
{
aInfo = itAppInfo.next();
// don't include apps user has disabled, often these ship with the device
if (!aInfo.enabled) continue;
app = new TorifiedApp();
try {
PackageInfo pInfo = pMgr.getPackageInfo(aInfo.packageName, PackageManager.GET_PERMISSIONS);
if (pInfo != null && pInfo.requestedPermissions != null)
{
for (String permInfo:pInfo.requestedPermissions)
{
if (permInfo.equals(Manifest.permission.INTERNET))
{
app.setUsesInternet(true);
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
app.setName(pMgr.getApplicationLabel(aInfo).toString());
}
catch (Exception e)
{
// no name
continue; //we only show apps with names
}
if (!app.usesInternet())
continue;
else
{
apps.add(app);
}
app.setEnabled(aInfo.enabled);
app.setUid(aInfo.uid);
app.setUsername(pMgr.getNameForUid(app.getUid()));
app.setProcname(aInfo.processName);
app.setPackageName(aInfo.packageName);
// check if this application is allowed
if (Arrays.binarySearch(tordApps, app.getUsername()) >= 0) {
app.setTorified(true);
}
else
{
app.setTorified(false);
}
}
Collections.sort(apps);
return apps;
}
public void saveAppSettings() {
StringBuilder tordApps = new StringBuilder();
Intent response = new Intent();
for (TorifiedApp tApp:mApps)
{
if (tApp.isTorified())
{
tordApps.append(tApp.getUsername());
tordApps.append("|");
response.putExtra(tApp.getUsername(),true);
}
}
Editor edit = mPrefs.edit();
edit.putString(PREFS_KEY_TORIFIED, tordApps.toString());
edit.commit();
setResult(RESULT_OK,response);
}
public void onClick(View v) {
final TorifiedApp app = (TorifiedApp) v.getTag();
if (app != null) {
app.setTorified(!app.isTorified());
Intent data = new Intent();
data.putExtra(Intent.EXTRA_PACKAGE_NAME,app.getPackageName());
setResult(RESULT_OK,data);
saveAppSettings();
finish();
}
}
}