2015-10-24 18:01:55 +00:00
|
|
|
package eu.faircode.netguard;
|
|
|
|
|
2015-10-25 09:29:49 +00:00
|
|
|
import android.app.AlertDialog;
|
2015-10-25 14:22:55 +00:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
2015-10-24 18:01:55 +00:00
|
|
|
import android.content.Intent;
|
2015-10-25 14:22:55 +00:00
|
|
|
import android.content.IntentFilter;
|
2015-10-24 18:01:55 +00:00
|
|
|
import android.content.SharedPreferences;
|
2015-10-25 15:48:41 +00:00
|
|
|
import android.net.Uri;
|
2015-10-24 18:01:55 +00:00
|
|
|
import android.net.VpnService;
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.preference.PreferenceManager;
|
2015-10-25 10:11:46 +00:00
|
|
|
import android.support.v4.view.MenuItemCompat;
|
2015-10-24 18:01:55 +00:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
2015-10-25 10:11:46 +00:00
|
|
|
import android.support.v7.widget.SearchView;
|
2015-10-24 18:01:55 +00:00
|
|
|
import android.util.Log;
|
2015-10-25 09:29:49 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
2015-10-24 18:01:55 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.CompoundButton;
|
|
|
|
import android.widget.Switch;
|
2015-10-25 09:29:49 +00:00
|
|
|
import android.widget.TextView;
|
2015-10-24 18:01:55 +00:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
2015-10-25 09:29:49 +00:00
|
|
|
public class ActivityMain extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
|
2015-10-24 18:01:55 +00:00
|
|
|
private static final String TAG = "NetGuard.Main";
|
|
|
|
|
|
|
|
private boolean running = false;
|
|
|
|
private RuleAdapter adapter = null;
|
|
|
|
|
2015-10-25 09:29:49 +00:00
|
|
|
private static final int REQUEST_VPN = 1;
|
|
|
|
|
2015-10-24 18:01:55 +00:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
2015-10-25 09:29:49 +00:00
|
|
|
Log.i(TAG, "Create");
|
2015-10-24 18:01:55 +00:00
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.main);
|
2015-10-25 09:29:49 +00:00
|
|
|
|
2015-10-24 18:01:55 +00:00
|
|
|
running = true;
|
|
|
|
|
2015-10-25 09:29:49 +00:00
|
|
|
// Action bar
|
2015-10-24 18:01:55 +00:00
|
|
|
View view = getLayoutInflater().inflate(R.layout.actionbar, null);
|
|
|
|
getSupportActionBar().setDisplayShowCustomEnabled(true);
|
|
|
|
getSupportActionBar().setCustomView(view);
|
|
|
|
|
|
|
|
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
|
2015-10-24 19:50:29 +00:00
|
|
|
// On/off switch
|
2015-10-24 18:01:55 +00:00
|
|
|
Switch swEnabled = (Switch) view.findViewById(R.id.swEnabled);
|
2015-10-25 17:30:44 +00:00
|
|
|
swEnabled.setChecked(prefs.getBoolean("enabled", false));
|
2015-10-24 18:01:55 +00:00
|
|
|
swEnabled.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
|
|
|
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
|
|
|
if (isChecked) {
|
2015-10-25 10:55:49 +00:00
|
|
|
Log.i(TAG, "Switch on");
|
2015-10-24 18:01:55 +00:00
|
|
|
Intent intent = VpnService.prepare(ActivityMain.this);
|
|
|
|
if (intent == null) {
|
|
|
|
Log.e(TAG, "Prepare done");
|
|
|
|
onActivityResult(REQUEST_VPN, RESULT_OK, null);
|
|
|
|
} else {
|
|
|
|
Log.i(TAG, "Start intent=" + intent);
|
|
|
|
startActivityForResult(intent, REQUEST_VPN);
|
|
|
|
}
|
|
|
|
} else {
|
2015-10-25 10:55:49 +00:00
|
|
|
Log.i(TAG, "Switch off");
|
2015-10-25 09:29:49 +00:00
|
|
|
prefs.edit().putBoolean("enabled", false).apply();
|
2015-10-24 18:01:55 +00:00
|
|
|
Intent intent = new Intent(ActivityMain.this, BlackHoleService.class);
|
2015-10-25 09:29:49 +00:00
|
|
|
intent.putExtra(BlackHoleService.EXTRA_COMMAND, BlackHoleService.Command.stop);
|
2015-10-24 18:01:55 +00:00
|
|
|
startService(intent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-10-25 15:28:41 +00:00
|
|
|
// Listen for preference changes
|
2015-10-25 09:29:49 +00:00
|
|
|
prefs.registerOnSharedPreferenceChangeListener(this);
|
|
|
|
|
2015-10-25 14:22:55 +00:00
|
|
|
// Fill application list
|
2015-10-25 15:28:41 +00:00
|
|
|
fillApplicationList();
|
2015-10-25 14:22:55 +00:00
|
|
|
|
|
|
|
// Listen for added/removed applications
|
|
|
|
IntentFilter intentFilter = new IntentFilter();
|
|
|
|
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
|
|
|
|
intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
|
|
|
|
intentFilter.addDataScheme("package");
|
|
|
|
registerReceiver(packageChangedReceiver, intentFilter);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
Log.i(TAG, "Destroy");
|
|
|
|
running = false;
|
|
|
|
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
|
|
|
|
unregisterReceiver(packageChangedReceiver);
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
private BroadcastReceiver packageChangedReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
Log.i(TAG, "Received " + intent);
|
2015-10-25 15:28:41 +00:00
|
|
|
fillApplicationList();
|
2015-10-25 14:22:55 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-25 15:28:41 +00:00
|
|
|
private void fillApplicationList() {
|
|
|
|
// Get recycler view
|
2015-10-24 18:01:55 +00:00
|
|
|
final RecyclerView rvApplication = (RecyclerView) findViewById(R.id.rvApplication);
|
|
|
|
rvApplication.setHasFixedSize(true);
|
|
|
|
rvApplication.setLayoutManager(new LinearLayoutManager(this));
|
|
|
|
|
2015-10-25 15:28:41 +00:00
|
|
|
// Get/set application list
|
2015-10-24 18:01:55 +00:00
|
|
|
new AsyncTask<Object, Object, List<Rule>>() {
|
|
|
|
@Override
|
|
|
|
protected List<Rule> doInBackground(Object... arg) {
|
|
|
|
return Rule.getRules(ActivityMain.this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(List<Rule> result) {
|
|
|
|
if (running) {
|
2015-10-25 16:52:33 +00:00
|
|
|
adapter = new RuleAdapter(result, ActivityMain.this);
|
2015-10-24 18:01:55 +00:00
|
|
|
rvApplication.setAdapter(adapter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.execute();
|
|
|
|
}
|
|
|
|
|
2015-10-25 09:29:49 +00:00
|
|
|
@Override
|
|
|
|
public void onSharedPreferenceChanged(SharedPreferences prefs, String name) {
|
|
|
|
Log.i(TAG, "Changed pref=" + name);
|
|
|
|
if ("enabled".equals(name)) {
|
2015-10-25 15:28:41 +00:00
|
|
|
// Get enabled
|
2015-10-25 09:29:49 +00:00
|
|
|
boolean enabled = prefs.getBoolean(name, false);
|
2015-10-25 15:28:41 +00:00
|
|
|
|
|
|
|
// Check switch state
|
2015-10-25 09:29:49 +00:00
|
|
|
Switch swEnabled = (Switch) getSupportActionBar().getCustomView().findViewById(R.id.swEnabled);
|
|
|
|
if (swEnabled.isChecked() != enabled)
|
|
|
|
swEnabled.setChecked(enabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
MenuInflater inflater = getMenuInflater();
|
|
|
|
inflater.inflate(R.menu.main, menu);
|
2015-10-25 10:11:46 +00:00
|
|
|
|
2015-10-25 15:28:41 +00:00
|
|
|
// Search
|
2015-10-25 10:11:46 +00:00
|
|
|
MenuItem searchItem = menu.findItem(R.id.menu_search);
|
|
|
|
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
|
|
|
|
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onQueryTextSubmit(String query) {
|
2015-10-25 11:52:54 +00:00
|
|
|
if (adapter != null)
|
|
|
|
adapter.getFilter().filter(query);
|
2015-10-25 10:11:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onQueryTextChange(String newText) {
|
2015-10-25 11:52:54 +00:00
|
|
|
if (adapter != null)
|
|
|
|
adapter.getFilter().filter(newText);
|
2015-10-25 10:11:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onClose() {
|
2015-10-25 11:52:54 +00:00
|
|
|
if (adapter != null)
|
|
|
|
adapter.getFilter().filter(null);
|
2015-10-25 10:11:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-10-25 09:29:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
// Handle item selection
|
|
|
|
switch (item.getItemId()) {
|
2015-10-25 11:52:54 +00:00
|
|
|
case R.id.menu_wifi:
|
2015-10-25 15:28:41 +00:00
|
|
|
// Toggle Wi-Fi
|
2015-10-25 11:52:54 +00:00
|
|
|
if (adapter != null)
|
2015-10-25 12:19:39 +00:00
|
|
|
adapter.toggle("wifi", this);
|
2015-10-25 11:52:54 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case R.id.menu_other:
|
2015-10-25 15:28:41 +00:00
|
|
|
// Toggle other
|
2015-10-25 11:52:54 +00:00
|
|
|
if (adapter != null)
|
2015-10-25 12:19:39 +00:00
|
|
|
adapter.toggle("other", this);
|
2015-10-25 11:52:54 +00:00
|
|
|
return true;
|
|
|
|
|
2015-10-25 09:29:49 +00:00
|
|
|
case R.id.menu_vpn_settings:
|
2015-10-25 15:28:41 +00:00
|
|
|
// Open VPN settings
|
2015-10-25 15:48:41 +00:00
|
|
|
Intent vpn = new Intent("android.net.vpn.SETTINGS");
|
|
|
|
if (vpn.resolveActivity(getPackageManager()) != null)
|
|
|
|
startActivity(vpn);
|
2015-10-25 09:29:49 +00:00
|
|
|
else
|
2015-10-25 15:48:41 +00:00
|
|
|
Log.w(TAG, vpn + " not available");
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case R.id.menu_support:
|
|
|
|
Intent xda = new Intent(Intent.ACTION_VIEW);
|
|
|
|
xda.setData(Uri.parse("http://forum.xda-developers.com/showthread.php?t=3233012"));
|
|
|
|
if (xda.resolveActivity(getPackageManager()) != null)
|
|
|
|
startActivity(xda);
|
|
|
|
else
|
|
|
|
Log.w(TAG, xda + " not available");
|
2015-10-25 09:29:49 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case R.id.menu_about:
|
2015-10-25 15:28:41 +00:00
|
|
|
// Show about
|
2015-10-25 09:29:49 +00:00
|
|
|
LayoutInflater inflater = LayoutInflater.from(this);
|
|
|
|
View view = inflater.inflate(R.layout.about, null);
|
|
|
|
TextView tvVersion = (TextView) view.findViewById(R.id.tvVersion);
|
|
|
|
tvVersion.setText(Util.getSelfVersionName(this));
|
|
|
|
AlertDialog dialog = new AlertDialog.Builder(this)
|
|
|
|
.setView(view)
|
|
|
|
.setCancelable(true).create();
|
|
|
|
dialog.show();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
2015-10-24 18:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
|
if (requestCode == REQUEST_VPN) {
|
2015-10-25 15:28:41 +00:00
|
|
|
// Update enabled state
|
2015-10-25 09:29:49 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
prefs.edit().putBoolean("enabled", resultCode == RESULT_OK).apply();
|
|
|
|
|
2015-10-25 15:28:41 +00:00
|
|
|
// Start service
|
2015-10-24 18:01:55 +00:00
|
|
|
if (resultCode == RESULT_OK) {
|
2015-10-25 09:29:49 +00:00
|
|
|
Intent intent = new Intent(ActivityMain.this, BlackHoleService.class);
|
|
|
|
intent.putExtra(BlackHoleService.EXTRA_COMMAND, BlackHoleService.Command.start);
|
2015-10-24 18:01:55 +00:00
|
|
|
startService(intent);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
}
|
|
|
|
}
|