2011-10-28 04:31:45 +00:00
|
|
|
/* Copyright (c) 2009, Nathan Freitas, Orbot / The Guardian Project - http://openideals.com/guardian */
|
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
package org.torproject.android.settings;
|
|
|
|
|
2012-06-20 00:45:25 +00:00
|
|
|
import java.util.ArrayList;
|
2011-10-28 04:31:45 +00:00
|
|
|
import java.util.Arrays;
|
2012-06-29 15:41:36 +00:00
|
|
|
import java.util.Collections;
|
2011-10-28 04:31:45 +00:00
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.StringTokenizer;
|
|
|
|
|
|
|
|
import org.torproject.android.R;
|
|
|
|
import org.torproject.android.TorConstants;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.content.SharedPreferences.Editor;
|
|
|
|
import android.content.pm.ApplicationInfo;
|
2012-06-20 00:45:25 +00:00
|
|
|
import android.content.pm.PackageInfo;
|
2011-10-28 04:31:45 +00:00
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.View.OnClickListener;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ArrayAdapter;
|
2011-10-28 17:39:14 +00:00
|
|
|
import android.widget.Button;
|
2011-10-28 04:31:45 +00:00
|
|
|
import android.widget.CheckBox;
|
|
|
|
import android.widget.CompoundButton;
|
|
|
|
import android.widget.CompoundButton.OnCheckedChangeListener;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.ListAdapter;
|
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
|
|
|
|
public class AppManager extends Activity implements OnCheckedChangeListener, OnClickListener, TorConstants {
|
|
|
|
|
2012-06-20 00:45:25 +00:00
|
|
|
private static ArrayList<TorifiedApp> apps = null;
|
2011-10-28 04:31:45 +00:00
|
|
|
|
|
|
|
private ListView listApps;
|
|
|
|
|
|
|
|
private AppManager mAppManager;
|
|
|
|
|
|
|
|
|
|
|
|
private boolean appsLoaded = false;
|
|
|
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
this.setContentView(R.layout.layout_apps);
|
|
|
|
|
|
|
|
mAppManager = this;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
listApps = (ListView)findViewById(R.id.applistview);
|
|
|
|
|
2011-10-28 17:39:14 +00:00
|
|
|
Button btnSave = (Button)findViewById(R.id.btnsave);
|
|
|
|
btnSave.setOnClickListener(new OnClickListener()
|
|
|
|
{
|
|
|
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-10-28 04:31:45 +00:00
|
|
|
if (!appsLoaded)
|
|
|
|
loadApps();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void loadApps ()
|
|
|
|
{
|
|
|
|
resetApps(this);
|
2012-06-20 00:45:25 +00:00
|
|
|
final ArrayList<TorifiedApp> apps = getApps(this);
|
2011-10-28 04:31:45 +00:00
|
|
|
|
2012-06-20 00:45:25 +00:00
|
|
|
/*
|
2011-10-28 04:31:45 +00:00
|
|
|
Arrays.sort(apps, new Comparator<TorifiedApp>() {
|
|
|
|
public int compare(TorifiedApp o1, TorifiedApp o2) {
|
|
|
|
if (o1.isTorified() == o2.isTorified()) return o1.getName().compareTo(o2.getName());
|
|
|
|
if (o1.isTorified()) return -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
});
|
2012-06-20 00:45:25 +00:00
|
|
|
*/
|
2011-10-28 04:31:45 +00:00
|
|
|
|
|
|
|
final LayoutInflater inflater = getLayoutInflater();
|
|
|
|
|
|
|
|
final ListAdapter adapter = new ArrayAdapter<TorifiedApp>(this,R.layout.layout_apps_item,R.id.itemtext,apps) {
|
|
|
|
public View getView(int position, View convertView, ViewGroup parent) {
|
|
|
|
ListEntry entry;
|
|
|
|
if (convertView == null) {
|
|
|
|
// Inflate a new view
|
|
|
|
convertView = inflater.inflate(R.layout.layout_apps_item, parent, false);
|
|
|
|
entry = new ListEntry();
|
|
|
|
entry.icon = (ImageView) convertView.findViewById(R.id.itemicon);
|
|
|
|
entry.box = (CheckBox) convertView.findViewById(R.id.itemcheck);
|
|
|
|
entry.text = (TextView) convertView.findViewById(R.id.itemtext);
|
|
|
|
|
|
|
|
entry.text.setOnClickListener(mAppManager);
|
|
|
|
entry.text.setOnClickListener(mAppManager);
|
|
|
|
|
|
|
|
convertView.setTag(entry);
|
|
|
|
|
|
|
|
entry.box.setOnCheckedChangeListener(mAppManager);
|
|
|
|
} else {
|
|
|
|
// Convert an existing view
|
|
|
|
entry = (ListEntry) convertView.getTag();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-20 00:45:25 +00:00
|
|
|
final TorifiedApp app = apps.get(position);
|
2011-10-28 04:31:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
entry.icon.setImageDrawable(app.getIcon());
|
|
|
|
entry.text.setText(app.getName());
|
|
|
|
|
|
|
|
final CheckBox box = entry.box;
|
|
|
|
box.setTag(app);
|
|
|
|
box.setChecked(app.isTorified());
|
|
|
|
|
|
|
|
entry.text.setTag(box);
|
|
|
|
entry.icon.setTag(box);
|
|
|
|
|
|
|
|
return convertView;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
listApps.setAdapter(adapter);
|
|
|
|
|
|
|
|
appsLoaded = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class ListEntry {
|
|
|
|
private CheckBox box;
|
|
|
|
private TextView text;
|
|
|
|
private ImageView icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (non-Javadoc)
|
|
|
|
* @see android.app.Activity#onStop()
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
protected void onStop() {
|
|
|
|
super.onStop();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-06-20 00:45:25 +00:00
|
|
|
public static ArrayList<TorifiedApp> getApps (Context context)
|
2011-10-28 04:31:45 +00:00
|
|
|
{
|
|
|
|
if (apps == null)
|
|
|
|
resetApps(context);
|
|
|
|
|
|
|
|
return apps;
|
|
|
|
}
|
|
|
|
|
2012-06-20 00:45:25 +00:00
|
|
|
public static ArrayList<TorifiedApp> resetApps (Context context)
|
2011-10-28 04:31:45 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
//else load the apps up
|
|
|
|
PackageManager pMgr = context.getPackageManager();
|
|
|
|
|
|
|
|
List<ApplicationInfo> lAppInfo = pMgr.getInstalledApplications(0);
|
|
|
|
|
|
|
|
Iterator<ApplicationInfo> itAppInfo = lAppInfo.iterator();
|
|
|
|
|
2012-06-20 00:45:25 +00:00
|
|
|
apps = new ArrayList<TorifiedApp>();
|
2011-10-28 04:31:45 +00:00
|
|
|
|
|
|
|
ApplicationInfo aInfo = null;
|
|
|
|
|
|
|
|
int appIdx = 0;
|
2012-06-20 00:45:25 +00:00
|
|
|
TorifiedApp app = null;
|
2011-10-28 04:31:45 +00:00
|
|
|
|
|
|
|
while (itAppInfo.hasNext())
|
|
|
|
{
|
|
|
|
aInfo = itAppInfo.next();
|
|
|
|
|
2012-06-20 00:45:25 +00:00
|
|
|
app = new TorifiedApp();
|
|
|
|
|
|
|
|
try {
|
|
|
|
PackageInfo pInfo = pMgr.getPackageInfo(aInfo.packageName, PackageManager.GET_PERMISSIONS);
|
|
|
|
|
2012-10-01 07:05:48 +00:00
|
|
|
if (pInfo != null && pInfo.requestedPermissions != null)
|
2012-06-20 00:45:25 +00:00
|
|
|
{
|
|
|
|
for (String permInfo:pInfo.requestedPermissions)
|
|
|
|
{
|
|
|
|
if (permInfo.equals("android.permission.INTERNET"))
|
|
|
|
{
|
|
|
|
app.setUsesInternet(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((aInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1)
|
|
|
|
{
|
|
|
|
//System app
|
|
|
|
app.setUsesInternet(true);
|
|
|
|
}
|
2011-10-28 04:31:45 +00:00
|
|
|
|
2012-04-09 03:22:54 +00:00
|
|
|
|
2012-06-28 21:00:41 +00:00
|
|
|
/*
|
|
|
|
* TODO n8fr8 June 2012: adding all apps for now b/c the Internet permission concept is not really working
|
2012-06-20 00:45:25 +00:00
|
|
|
if (!app.usesInternet())
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
apps.add(app);
|
2012-06-28 21:00:41 +00:00
|
|
|
}*/
|
|
|
|
apps.add(app);
|
2012-06-20 00:45:25 +00:00
|
|
|
|
|
|
|
app.setEnabled(aInfo.enabled);
|
|
|
|
app.setUid(aInfo.uid);
|
|
|
|
app.setUsername(pMgr.getNameForUid(app.getUid()));
|
|
|
|
app.setProcname(aInfo.processName);
|
|
|
|
app.setName(pMgr.getApplicationLabel(aInfo).toString());
|
|
|
|
app.setIcon(pMgr.getApplicationIcon(aInfo));
|
2011-10-28 04:31:45 +00:00
|
|
|
|
|
|
|
// check if this application is allowed
|
2012-06-20 00:45:25 +00:00
|
|
|
if (Arrays.binarySearch(tordApps, app.getUsername()) >= 0) {
|
|
|
|
app.setTorified(true);
|
2011-10-28 04:31:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-06-20 00:45:25 +00:00
|
|
|
app.setTorified(false);
|
2011-10-28 04:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
appIdx++;
|
|
|
|
}
|
|
|
|
|
2012-06-29 15:41:36 +00:00
|
|
|
Collections.sort(apps);
|
2011-10-28 04:31:45 +00:00
|
|
|
|
|
|
|
return apps;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void saveAppSettings (Context context)
|
|
|
|
{
|
|
|
|
if (apps == null)
|
|
|
|
return;
|
|
|
|
|
2013-07-22 19:18:02 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2011-10-28 04:31:45 +00:00
|
|
|
|
|
|
|
// final SharedPreferences prefs = context.getSharedPreferences(PREFS_KEY, 0);
|
|
|
|
|
|
|
|
StringBuilder tordApps = new StringBuilder();
|
|
|
|
|
2012-06-20 00:45:25 +00:00
|
|
|
for (TorifiedApp tApp:apps)
|
2011-10-28 04:31:45 +00:00
|
|
|
{
|
2012-06-20 00:45:25 +00:00
|
|
|
if (tApp.isTorified())
|
2011-10-28 04:31:45 +00:00
|
|
|
{
|
2012-06-20 00:45:25 +00:00
|
|
|
tordApps.append(tApp.getUsername());
|
2011-10-28 04:31:45 +00:00
|
|
|
tordApps.append("|");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Editor edit = prefs.edit();
|
|
|
|
edit.putString(PREFS_KEY_TORIFIED, tordApps.toString());
|
|
|
|
edit.commit();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called an application is check/unchecked
|
|
|
|
*/
|
|
|
|
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
|
|
|
final TorifiedApp app = (TorifiedApp) buttonView.getTag();
|
|
|
|
if (app != null) {
|
|
|
|
app.setTorified(isChecked);
|
|
|
|
}
|
|
|
|
|
|
|
|
saveAppSettings(this);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
|
|
|
CheckBox cbox = (CheckBox)v.getTag();
|
|
|
|
|
|
|
|
final TorifiedApp app = (TorifiedApp)cbox.getTag();
|
|
|
|
if (app != null) {
|
|
|
|
app.setTorified(!app.isTorified());
|
|
|
|
cbox.setChecked(app.isTorified());
|
|
|
|
}
|
|
|
|
|
|
|
|
saveAppSettings(this);
|
|
|
|
|
|
|
|
}
|
2012-04-09 03:22:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-28 04:31:45 +00:00
|
|
|
|
|
|
|
}
|