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-30 15:51:24 +00:00
|
|
|
import android.app.PendingIntent;
|
2015-10-25 14:22:55 +00:00
|
|
|
import android.content.BroadcastReceiver;
|
2015-10-30 15:51:24 +00:00
|
|
|
import android.content.ComponentName;
|
2015-10-25 14:22:55 +00:00
|
|
|
import android.content.Context;
|
2015-10-30 15:51:24 +00:00
|
|
|
import android.content.DialogInterface;
|
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-30 15:51:24 +00:00
|
|
|
import android.content.ServiceConnection;
|
2015-10-24 18:01:55 +00:00
|
|
|
import android.content.SharedPreferences;
|
2015-10-26 12:19:52 +00:00
|
|
|
import android.net.ConnectivityManager;
|
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;
|
2015-10-30 15:51:24 +00:00
|
|
|
import android.os.IBinder;
|
2015-10-24 18:01:55 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2015-10-26 12:19:52 +00:00
|
|
|
import android.provider.Settings;
|
2015-10-30 15:51:24 +00:00
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
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-26 16:28:47 +00:00
|
|
|
import android.support.v7.widget.SwitchCompat;
|
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;
|
2015-10-30 15:51:24 +00:00
|
|
|
import android.widget.Button;
|
2015-10-24 18:01:55 +00:00
|
|
|
import android.widget.CompoundButton;
|
2015-10-30 18:21:10 +00:00
|
|
|
import android.widget.ImageView;
|
2015-10-25 09:29:49 +00:00
|
|
|
import android.widget.TextView;
|
2015-10-25 18:02:33 +00:00
|
|
|
import android.widget.Toast;
|
2015-10-24 18:01:55 +00:00
|
|
|
|
2015-10-30 15:51:24 +00:00
|
|
|
import com.android.vending.billing.IInAppBillingService;
|
|
|
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
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-29 08:56:22 +00:00
|
|
|
private MenuItem menuSearch = null;
|
|
|
|
private MenuItem menuNetwork = null;
|
2015-10-30 15:51:24 +00:00
|
|
|
private IInAppBillingService billingService = null;
|
2015-10-24 18:01:55 +00:00
|
|
|
|
2015-10-25 09:29:49 +00:00
|
|
|
private static final int REQUEST_VPN = 1;
|
2015-10-30 15:51:24 +00:00
|
|
|
private static final int REQUEST_DONATION = 2;
|
|
|
|
|
2015-10-30 17:44:20 +00:00
|
|
|
// adb shell pm clear com.android.vending
|
2015-10-30 18:21:10 +00:00
|
|
|
private static final String SKU_DONATE = "donation"; // "android.test.purchased"
|
2015-10-30 15:51:24 +00:00
|
|
|
private static final String ACTION_DONATE = "eu.faircode.netguard.DONATE";
|
2015-10-25 09:29:49 +00:00
|
|
|
|
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-26 17:47:31 +00:00
|
|
|
|
|
|
|
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
setTheme(prefs.getBoolean("dark_theme", false) ? R.style.AppThemeDark : R.style.AppTheme);
|
|
|
|
|
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-29 16:33:06 +00:00
|
|
|
boolean enabled = prefs.getBoolean("enabled", false);
|
2015-10-24 18:01:55 +00:00
|
|
|
|
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);
|
|
|
|
|
2015-10-29 16:33:06 +00:00
|
|
|
// Disabled warning
|
|
|
|
TextView tvDisabled = (TextView) findViewById(R.id.tvDisabled);
|
|
|
|
tvDisabled.setVisibility(enabled ? View.GONE : View.VISIBLE);
|
|
|
|
|
2015-10-24 19:50:29 +00:00
|
|
|
// On/off switch
|
2015-10-26 16:28:47 +00:00
|
|
|
SwitchCompat swEnabled = (SwitchCompat) view.findViewById(R.id.swEnabled);
|
2015-10-29 16:33:06 +00:00
|
|
|
swEnabled.setChecked(enabled);
|
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-25 18:02:33 +00:00
|
|
|
Intent prepare = VpnService.prepare(ActivityMain.this);
|
|
|
|
if (prepare == null) {
|
2015-10-24 18:01:55 +00:00
|
|
|
Log.e(TAG, "Prepare done");
|
|
|
|
onActivityResult(REQUEST_VPN, RESULT_OK, null);
|
|
|
|
} else {
|
2015-10-25 18:02:33 +00:00
|
|
|
Log.i(TAG, "Start intent=" + prepare);
|
|
|
|
try {
|
|
|
|
startActivityForResult(prepare, REQUEST_VPN);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
|
|
|
onActivityResult(REQUEST_VPN, RESULT_CANCELED, null);
|
|
|
|
Toast.makeText(ActivityMain.this, ex.toString(), Toast.LENGTH_LONG).show();
|
|
|
|
}
|
2015-10-24 18:01:55 +00:00
|
|
|
}
|
|
|
|
} 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-29 06:47:12 +00:00
|
|
|
SinkholeService.stop(ActivityMain.this);
|
2015-10-24 18:01:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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
|
|
|
|
2015-10-26 16:32:03 +00:00
|
|
|
// Listen for connectivity updates
|
2015-10-26 12:19:52 +00:00
|
|
|
IntentFilter ifConnectivity = new IntentFilter();
|
|
|
|
ifConnectivity.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
|
|
|
|
registerReceiver(connectivityChangedReceiver, ifConnectivity);
|
|
|
|
|
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);
|
2015-10-30 15:51:24 +00:00
|
|
|
|
|
|
|
// Connect to billing
|
|
|
|
Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
|
|
|
|
serviceIntent.setPackage("com.android.vending");
|
|
|
|
bindService(serviceIntent, billingConnection, Context.BIND_AUTO_CREATE);
|
2015-10-25 14:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
Log.i(TAG, "Destroy");
|
|
|
|
running = false;
|
2015-10-30 15:51:24 +00:00
|
|
|
|
2015-10-25 14:22:55 +00:00
|
|
|
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
|
2015-10-26 12:19:52 +00:00
|
|
|
unregisterReceiver(connectivityChangedReceiver);
|
2015-10-25 14:22:55 +00:00
|
|
|
unregisterReceiver(packageChangedReceiver);
|
2015-10-30 15:51:24 +00:00
|
|
|
|
|
|
|
if (billingConnection != null)
|
|
|
|
unbindService(billingConnection);
|
|
|
|
|
2015-10-25 14:22:55 +00:00
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
2015-10-26 12:19:52 +00:00
|
|
|
private BroadcastReceiver connectivityChangedReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
Log.i(TAG, "Received " + intent);
|
|
|
|
Util.logExtras(TAG, intent);
|
2015-10-29 08:56:22 +00:00
|
|
|
if (menuNetwork != null)
|
|
|
|
menuNetwork.setIcon(Util.isWifiActive(ActivityMain.this) ? R.drawable.ic_network_wifi_white_24dp : R.drawable.ic_network_cell_white_24dp);
|
2015-10-26 12:19:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-25 14:22:55 +00:00
|
|
|
private BroadcastReceiver packageChangedReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
Log.i(TAG, "Received " + intent);
|
2015-10-26 12:19:52 +00:00
|
|
|
Util.logExtras(TAG, intent);
|
2015-10-25 15:28:41 +00:00
|
|
|
fillApplicationList();
|
2015-10-25 14:22:55 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-30 15:51:24 +00:00
|
|
|
private ServiceConnection billingConnection = new ServiceConnection() {
|
|
|
|
@Override
|
|
|
|
public void onServiceDisconnected(ComponentName name) {
|
|
|
|
Log.i(TAG, "Billing disconnected");
|
|
|
|
billingService = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onServiceConnected(ComponentName name, IBinder service) {
|
|
|
|
Log.i(TAG, "Billing connected");
|
|
|
|
billingService = IInAppBillingService.Stub.asInterface(service);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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-29 08:56:22 +00:00
|
|
|
if (menuSearch != null)
|
|
|
|
MenuItemCompat.collapseActionView(menuSearch);
|
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) {
|
2015-10-25 17:37:09 +00:00
|
|
|
Log.i(TAG, "Preference " + name + "=" + prefs.getAll().get(name));
|
2015-10-25 09:29:49 +00:00
|
|
|
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
|
|
|
|
2015-10-29 16:33:06 +00:00
|
|
|
// Display disabled warning
|
|
|
|
TextView tvDisabled = (TextView) findViewById(R.id.tvDisabled);
|
|
|
|
tvDisabled.setVisibility(enabled ? View.GONE : View.VISIBLE);
|
|
|
|
|
2015-10-25 15:28:41 +00:00
|
|
|
// Check switch state
|
2015-10-26 16:28:47 +00:00
|
|
|
SwitchCompat swEnabled = (SwitchCompat) getSupportActionBar().getCustomView().findViewById(R.id.swEnabled);
|
2015-10-25 09:29:49 +00:00
|
|
|
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-29 08:56:22 +00:00
|
|
|
menuSearch = menu.findItem(R.id.menu_search);
|
|
|
|
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuSearch);
|
2015-10-25 10:11:46 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-25 22:04:10 +00:00
|
|
|
@Override
|
|
|
|
public boolean onPrepareOptionsMenu(Menu menu) {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
|
2015-10-29 08:56:22 +00:00
|
|
|
menuNetwork = menu.findItem(R.id.menu_network);
|
|
|
|
menuNetwork.setIcon(Util.isWifiActive(this) ? R.drawable.ic_network_wifi_white_24dp : R.drawable.ic_network_cell_white_24dp);
|
2015-10-26 12:19:52 +00:00
|
|
|
|
2015-10-30 15:51:24 +00:00
|
|
|
MenuItem menuWifi = menu.findItem(R.id.menu_whitelist_wifi);
|
|
|
|
menuWifi.setChecked(prefs.getBoolean("whitelist_wifi", true));
|
2015-10-25 22:04:10 +00:00
|
|
|
|
2015-10-30 07:57:36 +00:00
|
|
|
MenuItem menuOther = menu.findItem(R.id.menu_whitelist_other);
|
|
|
|
menuOther.setChecked(prefs.getBoolean("whitelist_other", true));
|
2015-10-25 22:04:10 +00:00
|
|
|
|
2015-10-30 07:57:36 +00:00
|
|
|
MenuItem menuTheme = menu.findItem(R.id.menu_theme);
|
|
|
|
menuTheme.setChecked(prefs.getBoolean("dark_theme", false));
|
2015-10-26 17:47:31 +00:00
|
|
|
|
2015-10-25 22:04:10 +00:00
|
|
|
return super.onPrepareOptionsMenu(menu);
|
|
|
|
}
|
|
|
|
|
2015-10-25 09:29:49 +00:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
2015-10-25 22:04:10 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
|
2015-10-25 09:29:49 +00:00
|
|
|
// Handle item selection
|
|
|
|
switch (item.getItemId()) {
|
2015-10-26 12:19:52 +00:00
|
|
|
case R.id.menu_network:
|
2015-10-30 07:57:36 +00:00
|
|
|
menu_network();
|
2015-10-26 12:19:52 +00:00
|
|
|
return true;
|
|
|
|
|
2015-10-27 07:04:49 +00:00
|
|
|
case R.id.menu_refresh:
|
|
|
|
fillApplicationList();
|
|
|
|
return true;
|
|
|
|
|
2015-10-25 22:04:10 +00:00
|
|
|
case R.id.menu_whitelist_wifi:
|
2015-10-30 07:57:36 +00:00
|
|
|
menu_whitelist_wifi(prefs);
|
2015-10-25 11:52:54 +00:00
|
|
|
return true;
|
|
|
|
|
2015-10-25 22:04:10 +00:00
|
|
|
case R.id.menu_whitelist_other:
|
2015-10-30 07:57:36 +00:00
|
|
|
menu_whitelist_other(prefs);
|
2015-10-25 11:52:54 +00:00
|
|
|
return true;
|
|
|
|
|
2015-10-30 07:57:36 +00:00
|
|
|
case R.id.menu_theme:
|
|
|
|
menu_theme(prefs);
|
2015-10-26 17:47:31 +00:00
|
|
|
return true;
|
|
|
|
|
2015-10-25 09:29:49 +00:00
|
|
|
case R.id.menu_vpn_settings:
|
2015-10-30 07:57:36 +00:00
|
|
|
menu_vpn_settings();
|
2015-10-25 15:48:41 +00:00
|
|
|
return true;
|
|
|
|
|
2015-10-30 07:05:47 +00:00
|
|
|
case R.id.menu_support:
|
2015-10-30 07:57:36 +00:00
|
|
|
menu_support();
|
2015-10-29 14:01:41 +00:00
|
|
|
return true;
|
|
|
|
|
2015-10-25 09:29:49 +00:00
|
|
|
case R.id.menu_about:
|
2015-10-30 07:57:36 +00:00
|
|
|
menu_about();
|
2015-10-25 09:29:49 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
2015-10-24 18:01:55 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 07:57:36 +00:00
|
|
|
private void menu_network() {
|
|
|
|
Intent settings = new Intent(Util.isWifiActive(this)
|
|
|
|
? Settings.ACTION_WIFI_SETTINGS : Settings.ACTION_WIRELESS_SETTINGS);
|
|
|
|
if (settings.resolveActivity(getPackageManager()) != null)
|
|
|
|
startActivity(settings);
|
|
|
|
else
|
|
|
|
Log.w(TAG, settings + " not available");
|
|
|
|
}
|
|
|
|
|
|
|
|
private void menu_whitelist_wifi(SharedPreferences prefs) {
|
|
|
|
prefs.edit().putBoolean("whitelist_wifi", !prefs.getBoolean("whitelist_wifi", true)).apply();
|
|
|
|
fillApplicationList();
|
|
|
|
SinkholeService.reload("wifi", this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void menu_whitelist_other(SharedPreferences prefs) {
|
|
|
|
prefs.edit().putBoolean("whitelist_other", !prefs.getBoolean("whitelist_other", true)).apply();
|
|
|
|
fillApplicationList();
|
|
|
|
SinkholeService.reload("other", this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void menu_theme(SharedPreferences prefs) {
|
|
|
|
prefs.edit().putBoolean("dark_theme", !prefs.getBoolean("dark_theme", false)).apply();
|
|
|
|
recreate();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void menu_vpn_settings() {
|
|
|
|
Intent vpn = new Intent("android.net.vpn.SETTINGS");
|
|
|
|
if (vpn.resolveActivity(getPackageManager()) != null)
|
|
|
|
startActivity(vpn);
|
|
|
|
else
|
|
|
|
Log.w(TAG, vpn + " not available");
|
|
|
|
}
|
|
|
|
|
|
|
|
private void 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");
|
|
|
|
}
|
|
|
|
|
|
|
|
private void menu_about() {
|
2015-10-30 15:51:24 +00:00
|
|
|
// Create view
|
2015-10-30 07:57:36 +00:00
|
|
|
LayoutInflater inflater = LayoutInflater.from(this);
|
|
|
|
View view = inflater.inflate(R.layout.about, null);
|
|
|
|
TextView tvVersion = (TextView) view.findViewById(R.id.tvVersion);
|
2015-10-30 15:51:24 +00:00
|
|
|
final Button btnDonate = (Button) view.findViewById(R.id.btnDonate);
|
2015-10-30 18:21:10 +00:00
|
|
|
final ImageView ivBitcoin = (ImageView) view.findViewById(R.id.ivBitcoin);
|
2015-10-30 15:51:24 +00:00
|
|
|
final TextView tvThanks = (TextView) view.findViewById(R.id.tvThanks);
|
|
|
|
|
|
|
|
// Show version
|
2015-10-30 07:57:36 +00:00
|
|
|
tvVersion.setText(Util.getSelfVersionName(this));
|
2015-10-30 15:51:24 +00:00
|
|
|
|
|
|
|
// Handle logcat
|
2015-10-30 07:57:36 +00:00
|
|
|
view.setOnClickListener(new View.OnClickListener() {
|
|
|
|
private short tap = 0;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
if (++tap == 7) {
|
|
|
|
tap = 0;
|
|
|
|
Util.sendLogcat(TAG, ActivityMain.this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2015-10-30 15:51:24 +00:00
|
|
|
|
2015-10-30 18:21:10 +00:00
|
|
|
// Handle IAB
|
2015-10-30 15:51:24 +00:00
|
|
|
btnDonate.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
try {
|
|
|
|
Bundle bundle = billingService.getBuyIntent(3, getPackageName(), SKU_DONATE, "inapp", "");
|
|
|
|
Log.i(TAG, "Billing.getBuyIntent");
|
|
|
|
Util.logBundle(TAG, bundle);
|
|
|
|
int response = bundle.getInt("RESPONSE_CODE");
|
2015-10-30 18:57:18 +00:00
|
|
|
Log.i(TAG, "Billing response=" + getBillingResult(response));
|
2015-10-30 15:51:24 +00:00
|
|
|
if (response == 0) {
|
|
|
|
PendingIntent pi = bundle.getParcelable("BUY_INTENT");
|
|
|
|
startIntentSenderForResult(
|
|
|
|
pi.getIntentSender(),
|
|
|
|
REQUEST_DONATION,
|
|
|
|
new Intent(),
|
|
|
|
Integer.valueOf(0),
|
|
|
|
Integer.valueOf(0),
|
|
|
|
Integer.valueOf(0));
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
|
|
|
Toast.makeText(ActivityMain.this, ex.toString(), Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-10-30 18:21:10 +00:00
|
|
|
// Handle Bitcoin
|
|
|
|
final Intent bitcoin = new Intent(Intent.ACTION_VIEW);
|
|
|
|
bitcoin.setData(Uri.parse("bitcoin:1NTdeAhWXTiQRTMVvusscHXguACok5KQpw?label=M66B"));
|
|
|
|
final boolean hasBitCoin = (bitcoin.resolveActivity(getPackageManager()) != null);
|
|
|
|
|
|
|
|
ivBitcoin.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
startActivity(bitcoin);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-10-30 15:51:24 +00:00
|
|
|
// Handle donated
|
|
|
|
final BroadcastReceiver onDonated = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
btnDonate.setVisibility(View.GONE);
|
|
|
|
tvThanks.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
IntentFilter iff = new IntentFilter(ACTION_DONATE);
|
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(onDonated, iff);
|
|
|
|
|
|
|
|
// Show dialog
|
2015-10-30 07:57:36 +00:00
|
|
|
AlertDialog dialog = new AlertDialog.Builder(this)
|
|
|
|
.setView(view)
|
2015-10-30 15:51:24 +00:00
|
|
|
.setCancelable(true)
|
|
|
|
.setOnDismissListener(new DialogInterface.OnDismissListener() {
|
|
|
|
@Override
|
|
|
|
public void onDismiss(DialogInterface dialogInterface) {
|
|
|
|
LocalBroadcastManager.getInstance(ActivityMain.this).unregisterReceiver(onDonated);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.create();
|
2015-10-30 07:57:36 +00:00
|
|
|
dialog.show();
|
2015-10-30 15:51:24 +00:00
|
|
|
|
|
|
|
// Handle SKUs
|
|
|
|
if (billingService != null)
|
|
|
|
new AsyncTask<Object, Object, Object>() {
|
|
|
|
@Override
|
|
|
|
protected Object doInBackground(Object... objects) {
|
|
|
|
try {
|
|
|
|
// Get available SKUs
|
|
|
|
ArrayList<String> skuList = new ArrayList<String>();
|
|
|
|
skuList.add(SKU_DONATE);
|
|
|
|
Bundle query = new Bundle();
|
|
|
|
query.putStringArrayList("ITEM_ID_LIST", skuList);
|
|
|
|
Bundle details = billingService.getSkuDetails(3, getPackageName(), "inapp", query);
|
|
|
|
Log.i(TAG, "Billing.getSkuDetails");
|
|
|
|
Util.logBundle(TAG, details);
|
2015-10-30 18:57:18 +00:00
|
|
|
int details_response = details.getInt("RESPONSE_CODE");
|
|
|
|
Log.i(TAG, "Billing response=" + getBillingResult(details_response));
|
|
|
|
if (details_response != 0)
|
2015-10-30 15:51:24 +00:00
|
|
|
return null;
|
|
|
|
|
|
|
|
// Check available SKUs
|
|
|
|
boolean found = false;
|
|
|
|
for (String item : details.getStringArrayList("DETAILS_LIST")) {
|
|
|
|
JSONObject object = new JSONObject(item);
|
|
|
|
if (SKU_DONATE.equals(object.getString("productId"))) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Log.i(TAG, SKU_DONATE + "=" + found);
|
|
|
|
if (!found)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
// Get purchases
|
|
|
|
Bundle purchases = billingService.getPurchases(3, getPackageName(), "inapp", null);
|
|
|
|
Log.i(TAG, "Billing.getPurchases");
|
|
|
|
Util.logBundle(TAG, purchases);
|
2015-10-30 18:57:18 +00:00
|
|
|
int purchases_response = purchases.getInt("RESPONSE_CODE");
|
|
|
|
Log.i(TAG, "Billing response=" + getBillingResult(purchases_response));
|
|
|
|
return (purchases_response == 0 ? purchases : null);
|
2015-10-30 15:51:24 +00:00
|
|
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
return ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Object result) {
|
|
|
|
if (result instanceof Throwable) {
|
|
|
|
// Handle exception
|
|
|
|
Throwable ex = (Throwable) result;
|
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
|
|
|
Toast.makeText(ActivityMain.this, ex.toString(), Toast.LENGTH_LONG).show();
|
|
|
|
|
2015-10-30 18:21:10 +00:00
|
|
|
} else if (result == null)
|
|
|
|
ivBitcoin.setVisibility(hasBitCoin ? View.VISIBLE : View.GONE);
|
|
|
|
|
|
|
|
else {
|
2015-10-30 15:51:24 +00:00
|
|
|
// Show donate/donated
|
|
|
|
Bundle bundle = (Bundle) result;
|
|
|
|
ArrayList<String> skus = bundle.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
|
|
|
|
btnDonate.setVisibility(skus.contains(SKU_DONATE) ? View.GONE : View.VISIBLE);
|
2015-10-30 18:21:10 +00:00
|
|
|
ivBitcoin.setVisibility(hasBitCoin && !skus.contains(SKU_DONATE) ? View.VISIBLE : View.GONE);
|
2015-10-30 15:51:24 +00:00
|
|
|
tvThanks.setVisibility(skus.contains(SKU_DONATE) ? View.VISIBLE : View.GONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.execute();
|
2015-10-30 07:57:36 +00:00
|
|
|
}
|
|
|
|
|
2015-10-24 18:01:55 +00:00
|
|
|
@Override
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
2015-10-30 18:57:18 +00:00
|
|
|
Log.i(TAG, "onActivityResult request=" + requestCode + " result=" + requestCode + " ok=" + (resultCode == RESULT_OK));
|
2015-10-30 17:44:20 +00:00
|
|
|
if (data != null)
|
|
|
|
Util.logExtras(TAG, data);
|
2015-10-30 15:51:24 +00:00
|
|
|
|
2015-10-24 18:01:55 +00:00
|
|
|
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-26 16:23:41 +00:00
|
|
|
if (resultCode == RESULT_OK)
|
2015-10-29 06:47:12 +00:00
|
|
|
SinkholeService.start(this);
|
2015-10-30 15:51:24 +00:00
|
|
|
|
|
|
|
} else if (requestCode == REQUEST_DONATION) {
|
|
|
|
if (resultCode == RESULT_OK) {
|
|
|
|
// Handle donation
|
|
|
|
Intent intent = new Intent(ACTION_DONATE);
|
|
|
|
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
|
2015-10-30 18:57:18 +00:00
|
|
|
} else if (data != null) {
|
|
|
|
int response = data.getIntExtra("RESPONSE_CODE", -1);
|
|
|
|
Log.i(TAG, "Billing response=" + getBillingResult(response));
|
2015-10-30 15:51:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
Log.w(TAG, "Unknown activity result request=" + requestCode);
|
2015-10-24 18:01:55 +00:00
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
2015-10-30 15:51:24 +00:00
|
|
|
}
|
2015-10-24 18:01:55 +00:00
|
|
|
}
|
2015-10-30 18:57:18 +00:00
|
|
|
|
|
|
|
private static String getBillingResult(int responseCode) {
|
|
|
|
switch (responseCode) {
|
|
|
|
case 0:
|
|
|
|
return "BILLING_RESPONSE_RESULT_OK";
|
|
|
|
case 1:
|
|
|
|
return "BILLING_RESPONSE_RESULT_USER_CANCELED";
|
|
|
|
case 2:
|
|
|
|
return "BILLING_RESPONSE_RESULT_SERVICE_UNAVAILABLE";
|
|
|
|
case 3:
|
|
|
|
return "BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE";
|
|
|
|
case 4:
|
|
|
|
return "BILLING_RESPONSE_RESULT_ITEM_UNAVAILABLE";
|
|
|
|
case 5:
|
|
|
|
return "BILLING_RESPONSE_RESULT_DEVELOPER_ERROR";
|
|
|
|
case 6:
|
|
|
|
return "BILLING_RESPONSE_RESULT_ERROR";
|
|
|
|
case 7:
|
|
|
|
return "BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED";
|
|
|
|
case 8:
|
|
|
|
return "BILLING_RESPONSE_RESULT_ITEM_NOT_OWNED";
|
|
|
|
default:
|
|
|
|
return Integer.toString(responseCode);
|
|
|
|
}
|
|
|
|
}
|
2015-10-24 18:01:55 +00:00
|
|
|
}
|