NetGuard/app/src/main/java/eu/faircode/netguard/ActivityMain.java

770 lines
31 KiB
Java
Raw Normal View History

2015-10-24 18:01:55 +00:00
package eu.faircode.netguard;
2015-11-03 17:57:29 +00:00
/*
This file is part of NetGuard.
NetGuard is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NetGuard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2015 by Marcel Bokhorst (M66B)
*/
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-11-02 18:16:30 +00:00
import android.content.IntentSender;
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-31 13:15:26 +00:00
import android.graphics.Color;
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-11-02 18:16:30 +00:00
import android.os.RemoteException;
2015-10-24 18:01:55 +00:00
import android.preference.PreferenceManager;
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-31 13:15:26 +00:00
import android.support.v4.widget.SwipeRefreshLayout;
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-11-01 06:31:36 +00:00
import android.text.method.LinkMovementMethod;
2015-10-24 18:01:55 +00:00
import android.util.Log;
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;
import android.widget.ImageView;
import android.widget.LinearLayout;
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;
2015-11-02 18:16:30 +00:00
import org.json.JSONException;
2015-10-30 15:51:24 +00:00
import org.json.JSONObject;
2015-10-30 15:51:24 +00:00
import java.util.ArrayList;
2015-10-24 18:01:55 +00:00
import java.util.List;
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;
2015-11-04 08:57:38 +00:00
private View actionView;
private LinearLayout llIndicators;
private ImageView ivInteractive;
2015-11-04 12:14:44 +00:00
private ImageView ivNetwork;
2015-11-04 17:35:24 +00:00
private ImageView ivMetered;
2015-10-31 13:15:26 +00:00
private SwipeRefreshLayout swipeRefresh;
2015-10-24 18:01:55 +00:00
private RuleAdapter adapter = null;
2015-10-29 08:56:22 +00:00
private MenuItem menuSearch = null;
2015-11-02 12:37:33 +00:00
private IInAppBillingService IABService = null;
2015-11-02 18:53:56 +00:00
private AlertDialog dialogFirst = null;
2015-11-03 11:21:34 +00:00
private AlertDialog dialogVpn = null;
2015-11-02 18:53:56 +00:00
private AlertDialog dialogAbout = null;
2015-10-24 18:01:55 +00:00
private static final int REQUEST_VPN = 1;
2015-11-02 18:16:30 +00:00
private static final int REQUEST_IAB = 2;
2015-11-07 11:43:30 +00:00
private static final int REQUEST_INVITE = 3;
2015-10-30 15:51:24 +00:00
2015-10-30 17:44:20 +00:00
// adb shell pm clear com.android.vending
2015-11-02 13:36:14 +00:00
private static final String SKU_DONATE = "donation";
// private static final String SKU_DONATE = "android.test.purchased";
2015-10-31 08:11:23 +00:00
private static final String ACTION_IAB = "eu.faircode.netguard.IAB";
2015-10-24 18:01:55 +00:00
@Override
protected void onCreate(Bundle savedInstanceState) {
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-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-11-04 19:48:27 +00:00
if (enabled)
SinkholeService.start(this);
else
SinkholeService.stop(this);
// Action bar
2015-11-04 08:57:38 +00:00
actionView = getLayoutInflater().inflate(R.layout.action, null);
SwitchCompat swEnabled = (SwitchCompat) actionView.findViewById(R.id.swEnabled);
llIndicators = (LinearLayout) actionView.findViewById(R.id.llIndicators);
ivInteractive = (ImageView) actionView.findViewById(R.id.ivInteractive);
2015-11-04 12:14:44 +00:00
ivNetwork = (ImageView) actionView.findViewById(R.id.ivNetwork);
2015-11-04 17:35:24 +00:00
ivMetered = (ImageView) actionView.findViewById(R.id.ivMetered);
2015-10-24 18:01:55 +00:00
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(actionView);
2015-10-24 18:01:55 +00:00
2015-10-24 19:50:29 +00:00
// On/off switch
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) {
prefs.edit().putBoolean("enabled", isChecked).apply();
2015-10-24 18:01:55 +00:00
if (isChecked) {
2015-10-25 10:55:49 +00:00
Log.i(TAG, "Switch on");
2015-11-03 11:21:34 +00:00
final Intent prepare = VpnService.prepare(ActivityMain.this);
2015-10-25 18:02:33 +00:00
if (prepare == null) {
2015-10-24 18:01:55 +00:00
Log.e(TAG, "Prepare done");
onActivityResult(REQUEST_VPN, RESULT_OK, null);
} else {
2015-11-03 11:21:34 +00:00
// Show dialog
LayoutInflater inflater = LayoutInflater.from(ActivityMain.this);
View view = inflater.inflate(R.layout.vpn, null);
dialogVpn = new AlertDialog.Builder(ActivityMain.this)
.setView(view)
.setCancelable(false)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
2015-11-06 22:49:03 +00:00
public void onClick(DialogInterface dialog, int which) {
2015-11-03 11:21:34 +00:00
if (running) {
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();
}
}
}
})
.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
dialogVpn = null;
}
})
.create();
dialogVpn.show();
2015-10-24 18:01:55 +00:00
}
} else {
2015-10-25 10:55:49 +00:00
Log.i(TAG, "Switch off");
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
}
}
});
// Indicators
llIndicators.setVisibility(Util.isDebuggable(this) ? View.VISIBLE : View.GONE);
2015-10-31 13:15:26 +00:00
// Disabled warning
TextView tvDisabled = (TextView) findViewById(R.id.tvDisabled);
tvDisabled.setVisibility(enabled ? View.GONE : View.VISIBLE);
// Application list
RecyclerView rvApplication = (RecyclerView) findViewById(R.id.rvApplication);
rvApplication.setHasFixedSize(true);
rvApplication.setLayoutManager(new LinearLayoutManager(this));
adapter = new RuleAdapter(ActivityMain.this);
2015-10-31 13:15:26 +00:00
rvApplication.setAdapter(adapter);
2015-10-31 13:23:18 +00:00
// Swipe to refresh
2015-10-31 13:15:26 +00:00
swipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swipeRefresh);
swipeRefresh.setColorSchemeColors(Color.WHITE, Color.WHITE, Color.WHITE);
swipeRefresh.setProgressBackgroundColorSchemeResource(R.color.colorPrimary);
swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
updateApplicationList();
}
});
2015-10-31 13:23:18 +00:00
// Fill application list
updateApplicationList();
2015-10-31 13:15:26 +00:00
// Listen for preference changes
prefs.registerOnSharedPreferenceChangeListener(this);
2015-10-25 14:22:55 +00:00
// Listen for interactive state changes
IntentFilter ifInteractive = new IntentFilter();
ifInteractive.addAction(Intent.ACTION_SCREEN_ON);
ifInteractive.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(interactiveStateReceiver, ifInteractive);
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
2015-11-02 13:48:08 +00:00
if (Util.hasValidFingerprint(TAG, this)) {
Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage("com.android.vending");
bindService(serviceIntent, IABConnection, Context.BIND_AUTO_CREATE);
}
// First use
if (!prefs.getBoolean("initialized", false)) {
// Create view
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.first, null);
TextView tvFirst = (TextView) view.findViewById(R.id.tvFirst);
tvFirst.setMovementMethod(LinkMovementMethod.getInstance());
// Show dialog
2015-11-02 18:53:56 +00:00
dialogFirst = new AlertDialog.Builder(this)
.setView(view)
.setCancelable(false)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
2015-11-06 22:49:03 +00:00
public void onClick(DialogInterface dialog, int which) {
2015-11-02 18:53:56 +00:00
if (running)
prefs.edit().putBoolean("initialized", true).apply();
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
2015-11-06 22:49:03 +00:00
public void onClick(DialogInterface dialog, int which) {
2015-11-02 21:18:56 +00:00
finish();
}
})
2015-11-02 18:53:56 +00:00
.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
dialogFirst = null;
}
})
.create();
2015-11-02 18:53:56 +00:00
dialogFirst.show();
}
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);
unregisterReceiver(interactiveStateReceiver);
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
2015-11-03 11:21:34 +00:00
if (IABService != null) {
2015-11-02 12:37:33 +00:00
unbindService(IABConnection);
2015-11-03 11:21:34 +00:00
IABService = null;
}
2015-10-30 15:51:24 +00:00
2015-11-03 11:21:34 +00:00
if (dialogFirst != null) {
2015-11-02 18:53:56 +00:00
dialogFirst.dismiss();
2015-11-03 11:21:34 +00:00
dialogFirst = null;
}
if (dialogVpn != null) {
dialogVpn.dismiss();
dialogVpn = null;
}
if (dialogAbout != null) {
2015-11-02 18:53:56 +00:00
dialogAbout.dismiss();
2015-11-03 11:21:34 +00:00
dialogAbout = null;
}
2015-11-02 18:53:56 +00:00
2015-10-25 14:22:55 +00:00
super.onDestroy();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
Log.i(TAG, "onActivityResult request=" + requestCode + " result=" + requestCode + " ok=" + (resultCode == RESULT_OK));
Util.logExtras(TAG, data);
if (requestCode == REQUEST_VPN) {
// Handle VPN approval
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putBoolean("enabled", resultCode == RESULT_OK).apply();
if (resultCode == RESULT_OK)
SinkholeService.start(this);
} else if (requestCode == REQUEST_IAB) {
if (resultCode == RESULT_OK) {
// Handle donation
Intent intent = new Intent(ACTION_IAB);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
} else {
int response = (data == null ? -1 : data.getIntExtra("RESPONSE_CODE", -1));
Log.i(TAG, "IAB response=" + getIABResult(response));
2015-11-07 12:21:11 +00:00
// Fail-safe
Intent donate = new Intent(Intent.ACTION_VIEW);
donate.setData(Uri.parse("http://www.netguard.me/"));
if (donate.resolveActivity(getPackageManager()) != null)
startActivity(donate);
}
2015-11-07 17:18:37 +00:00
} else if (requestCode == REQUEST_INVITE) {
2015-11-08 09:14:56 +00:00
// Do nothing
2015-11-07 17:18:37 +00:00
} else {
Log.w(TAG, "Unknown activity result request=" + requestCode);
super.onActivityResult(requestCode, resultCode, data);
}
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String name) {
Log.i(TAG, "Preference " + name + "=" + prefs.getAll().get(name));
if ("enabled".equals(name)) {
// Get enabled
boolean enabled = prefs.getBoolean(name, false);
// Display disabled warning
TextView tvDisabled = (TextView) findViewById(R.id.tvDisabled);
tvDisabled.setVisibility(enabled ? View.GONE : View.VISIBLE);
// Check switch state
SwitchCompat swEnabled = (SwitchCompat) getSupportActionBar().getCustomView().findViewById(R.id.swEnabled);
if (swEnabled.isChecked() != enabled)
swEnabled.setChecked(enabled);
2015-11-04 22:44:17 +00:00
} else if ("whitelist_wifi".equals(name) ||
"whitelist_other".equals(name) ||
"whitelist_roaming".equals(name) ||
2015-11-08 12:05:55 +00:00
"manage_system".equals(name) ||
"imported".equals(name))
2015-11-04 22:44:17 +00:00
updateApplicationList();
else if ("dark_theme".equals(name))
recreate();
else if ("indicators".equals(name))
2015-11-07 12:25:17 +00:00
llIndicators.setVisibility(prefs.getBoolean(name, false) ? View.VISIBLE : View.GONE);
}
private BroadcastReceiver interactiveStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Received " + intent);
Util.logExtras(TAG, intent);
ivInteractive.setVisibility(Intent.ACTION_SCREEN_ON.equals(intent.getAction()) ? View.VISIBLE : View.INVISIBLE);
2015-11-04 08:57:38 +00:00
actionView.postInvalidate();
}
};
2015-10-26 12:19:52 +00:00
private BroadcastReceiver connectivityChangedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
2015-11-08 08:40:17 +00:00
int networkType = intent.getIntExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, ConnectivityManager.TYPE_DUMMY);
if (networkType == ConnectivityManager.TYPE_VPN)
return;
2015-10-26 12:19:52 +00:00
Log.i(TAG, "Received " + intent);
Util.logExtras(TAG, intent);
2015-11-04 12:14:44 +00:00
ivNetwork.setVisibility(View.VISIBLE);
2015-11-04 17:35:24 +00:00
ivMetered.setVisibility(View.VISIBLE);
2015-11-13 07:07:15 +00:00
if (Util.isLocalNetwork(context))
2015-11-04 17:35:24 +00:00
ivNetwork.setImageLevel(1);
2015-11-08 10:29:31 +00:00
else {
if (Util.isRoaming(context))
ivNetwork.setImageLevel(3);
else
ivNetwork.setImageLevel(2);
}
2015-11-13 07:07:15 +00:00
ivMetered.setImageLevel(Util.isMeteredNetwork(context) ? 1 : 0);
2015-11-04 08:57:38 +00:00
actionView.postInvalidate();
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-31 13:15:26 +00:00
updateApplicationList();
2015-10-25 14:22:55 +00:00
}
};
2015-11-02 12:37:33 +00:00
private ServiceConnection IABConnection = new ServiceConnection() {
2015-10-30 15:51:24 +00:00
@Override
public void onServiceDisconnected(ComponentName name) {
2015-11-02 12:37:33 +00:00
Log.i(TAG, "IAB disconnected");
IABService = null;
2015-10-30 15:51:24 +00:00
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
2015-11-02 12:37:33 +00:00
Log.i(TAG, "IAB connected");
IABService = IInAppBillingService.Stub.asInterface(service);
2015-10-30 15:51:24 +00:00
}
};
2015-10-31 13:15:26 +00:00
private void updateApplicationList() {
2015-10-24 18:01:55 +00:00
new AsyncTask<Object, Object, List<Rule>>() {
2015-10-31 19:52:24 +00:00
private boolean refreshing = true;
2015-10-31 17:12:57 +00:00
@Override
protected void onPreExecute() {
swipeRefresh.post(new Runnable() {
@Override
public void run() {
2015-10-31 19:52:24 +00:00
if (refreshing)
swipeRefresh.setRefreshing(true);
2015-10-31 17:12:57 +00:00
}
});
}
2015-10-24 18:01:55 +00:00
@Override
protected List<Rule> doInBackground(Object... arg) {
2015-11-01 08:38:51 +00:00
return Rule.getRules(false, TAG, ActivityMain.this);
2015-10-24 18:01:55 +00:00
}
@Override
protected void onPostExecute(List<Rule> result) {
if (running) {
2015-10-31 17:12:57 +00:00
if (adapter != null)
adapter.set(result);
if (menuSearch != null)
MenuItemCompat.collapseActionView(menuSearch);
2015-10-31 19:52:24 +00:00
if (swipeRefresh != null) {
refreshing = false;
2015-10-31 13:15:26 +00:00
swipeRefresh.setRefreshing(false);
2015-10-31 19:52:24 +00:00
}
2015-10-24 18:01:55 +00:00
}
}
}.execute();
}
@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) {
if (adapter != null)
adapter.getFilter().filter(query);
2015-10-25 10:11:46 +00:00
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
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() {
if (adapter != null)
adapter.getFilter().filter(null);
2015-10-25 10:11:46 +00:00
return true;
}
});
2015-11-08 09:14:56 +00:00
if (!Util.hasValidFingerprint(TAG, this) || getIntentInvite(this).resolveActivity(getPackageManager()) == null)
2015-11-07 11:46:55 +00:00
menu.removeItem(R.id.menu_invite);
if (getIntentSupport().resolveActivity(getPackageManager()) == null)
menu.removeItem(R.id.menu_support);
2015-10-31 20:48:09 +00:00
2015-11-04 22:44:17 +00:00
return true;
2015-10-25 22:04:10 +00:00
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
2015-11-04 22:44:17 +00:00
case R.id.menu_settings:
startActivity(new Intent(this, ActivitySettings.class));
2015-10-26 17:47:31 +00:00
return true;
2015-11-07 11:43:30 +00:00
case R.id.menu_invite:
startActivityForResult(getIntentInvite(this), REQUEST_INVITE);
return true;
2015-10-30 07:05:47 +00:00
case R.id.menu_support:
2015-10-31 20:48:09 +00:00
startActivity(getIntentSupport());
return true;
case R.id.menu_about:
2015-10-30 07:57:36 +00:00
menu_about();
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_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-11-07 16:51:13 +00:00
Button btnRate = (Button) view.findViewById(R.id.btnRate);
2015-10-30 15:51:24 +00:00
final Button btnDonate = (Button) view.findViewById(R.id.btnDonate);
final TextView tvThanks = (TextView) view.findViewById(R.id.tvThanks);
2015-11-01 06:31:36 +00:00
TextView tvLicense = (TextView) view.findViewById(R.id.tvLicense);
2015-10-30 15:51:24 +00:00
// Show version
2015-10-30 07:57:36 +00:00
tvVersion.setText(Util.getSelfVersionName(this));
2015-10-30 15:51:24 +00:00
2015-11-01 06:31:36 +00:00
// Handle license
tvLicense.setMovementMethod(LinkMovementMethod.getInstance());
2015-10-30 15:51:24 +00:00
// Handle logcat
2015-11-02 13:48:08 +00:00
if (Util.hasValidFingerprint(TAG, this))
2015-10-31 08:11:23 +00:00
view.setOnClickListener(new View.OnClickListener() {
private short tap = 0;
2015-10-30 07:57:36 +00:00
2015-10-31 08:11:23 +00:00
@Override
public void onClick(View view) {
if (++tap == 7) {
tap = 0;
Util.sendLogcat(TAG, ActivityMain.this);
}
2015-10-30 07:57:36 +00:00
}
2015-10-31 08:11:23 +00:00
});
2015-10-30 15:51:24 +00:00
2015-11-07 16:51:13 +00:00
// Handle rate
btnRate.setVisibility(getIntentRate(this).resolveActivity(getPackageManager()) == null ? View.GONE : View.VISIBLE);
btnRate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(getIntentRate(ActivityMain.this));
}
});
// Handle donate
2015-10-30 15:51:24 +00:00
btnDonate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
2015-11-02 18:16:30 +00:00
new AsyncTask<Object, Object, Object>() {
2015-11-02 20:57:05 +00:00
@Override
protected void onPreExecute() {
btnDonate.setEnabled(false);
}
2015-11-02 18:16:30 +00:00
@Override
protected Object doInBackground(Object... objects) {
try {
if (IABService == null || !IABisAvailable(SKU_DONATE, IABService, ActivityMain.this))
return false;
if (IABService == null || IABisPurchased(SKU_DONATE, IABService, ActivityMain.this))
return false;
return true;
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
return ex;
}
}
@Override
protected void onPostExecute(Object result) {
2015-11-09 17:21:29 +00:00
if (running)
try {
if (result instanceof Boolean && (Boolean) result && IABService != null) {
IntentSender sender = IABgetIntent(SKU_DONATE, IABService, ActivityMain.this);
startIntentSenderForResult(sender, REQUEST_IAB, new Intent(), 0, 0, 0);
} else {
Intent donate = new Intent(Intent.ACTION_VIEW);
donate.setData(Uri.parse("http://www.netguard.me/"));
startActivity(donate);
}
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
Toast.makeText(ActivityMain.this, result.toString(), Toast.LENGTH_LONG).show();
} finally {
btnDonate.setEnabled(true);
2015-11-02 18:16:30 +00:00
}
}
}.execute();
2015-10-30 15:51:24 +00:00
}
});
// Handle donated
2015-10-31 08:11:23 +00:00
final BroadcastReceiver onIABsuccess = new BroadcastReceiver() {
2015-10-30 15:51:24 +00:00
@Override
public void onReceive(Context context, Intent intent) {
btnDonate.setVisibility(View.GONE);
tvThanks.setVisibility(View.VISIBLE);
}
};
2015-10-31 08:11:23 +00:00
IntentFilter iff = new IntentFilter(ACTION_IAB);
LocalBroadcastManager.getInstance(this).registerReceiver(onIABsuccess, iff);
2015-10-30 15:51:24 +00:00
// Show dialog
2015-11-02 18:53:56 +00:00
dialogAbout = new AlertDialog.Builder(this)
2015-10-30 07:57:36 +00:00
.setView(view)
2015-10-30 15:51:24 +00:00
.setCancelable(true)
.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
2015-11-02 18:53:56 +00:00
if (running)
LocalBroadcastManager.getInstance(ActivityMain.this).unregisterReceiver(onIABsuccess);
dialogAbout = null;
2015-10-30 15:51:24 +00:00
}
})
.create();
2015-11-02 18:53:56 +00:00
dialogAbout.show();
2015-10-30 15:51:24 +00:00
2015-11-02 18:47:09 +00:00
// Check if IAB purchased
2015-11-02 18:16:30 +00:00
new AsyncTask<Object, Object, Object>() {
@Override
protected Object doInBackground(Object... objects) {
try {
return (IABService != null && IABisPurchased(SKU_DONATE, IABService, ActivityMain.this));
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
return ex;
2015-10-30 15:51:24 +00:00
}
2015-11-02 18:16:30 +00:00
}
2015-10-30 15:51:24 +00:00
2015-11-02 18:16:30 +00:00
@Override
protected void onPostExecute(Object result) {
2015-11-09 17:21:29 +00:00
if (running) {
boolean purchased = (result instanceof Boolean && (Boolean) result);
btnDonate.setVisibility(purchased ? View.GONE : View.VISIBLE);
tvThanks.setVisibility(purchased ? View.VISIBLE : View.GONE);
}
2015-11-02 18:16:30 +00:00
}
}.execute();
2015-10-30 07:57:36 +00:00
}
2015-11-07 11:43:30 +00:00
private static Intent getIntentInvite(Context context) {
2015-11-08 09:14:56 +00:00
Intent intent = new Intent("com.google.android.gms.appinvite.ACTION_APP_INVITE");
intent.setPackage("com.google.android.gms");
intent.putExtra("com.google.android.gms.appinvite.TITLE", context.getString(R.string.menu_invite));
intent.putExtra("com.google.android.gms.appinvite.MESSAGE", context.getString(R.string.msg_try));
intent.putExtra("com.google.android.gms.appinvite.BUTTON_TEXT", context.getString(R.string.msg_try));
// com.google.android.gms.appinvite.DEEP_LINK_URL
return intent;
2015-11-07 11:43:30 +00:00
}
2015-11-07 16:51:13 +00:00
private static Intent getIntentRate(Context context) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + context.getPackageName()));
if (intent.resolveActivity(context.getPackageManager()) == null)
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + context.getPackageName()));
return intent;
}
2015-11-01 06:38:52 +00:00
private static Intent getIntentSupport() {
2015-10-31 20:48:09 +00:00
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://forum.xda-developers.com/showthread.php?t=3233012"));
return intent;
}
2015-11-02 18:16:30 +00:00
private static boolean IABisAvailable(String sku, IInAppBillingService service, Context context) throws RemoteException, JSONException {
ArrayList<String> skuList = new ArrayList<>();
skuList.add(sku);
Bundle query = new Bundle();
query.putStringArrayList("ITEM_ID_LIST", skuList);
Bundle bundle = service.getSkuDetails(3, context.getPackageName(), "inapp", query);
Log.i(TAG, "IAB.getSkuDetails");
Util.logBundle(TAG, bundle);
int response = (bundle == null ? -1 : bundle.getInt("RESPONSE_CODE", -1));
Log.i(TAG, "IAB response=" + getIABResult(response));
if (response != 0)
return false;
// Check available SKUs
boolean found = false;
ArrayList<String> details = bundle.getStringArrayList("DETAILS_LIST");
if (details != null)
for (String item : details) {
JSONObject object = new JSONObject(item);
if (sku.equals(object.getString("productId"))) {
found = true;
break;
2015-10-31 08:11:23 +00:00
}
2015-11-02 18:16:30 +00:00
}
Log.i(TAG, sku + "=" + found);
return found;
}
private static boolean IABisPurchased(String sku, IInAppBillingService service, Context context) throws RemoteException {
// Get purchases
Bundle bundle = service.getPurchases(3, context.getPackageName(), "inapp", null);
Log.i(TAG, "IAB.getPurchases");
Util.logBundle(TAG, bundle);
int response = (bundle == null ? -1 : bundle.getInt("RESPONSE_CODE", -1));
Log.i(TAG, "IAB response=" + getIABResult(response));
if (response != 0)
2015-10-31 08:11:23 +00:00
return false;
2015-11-02 18:16:30 +00:00
// Check purchases
ArrayList<String> skus = bundle.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
return (skus != null && skus.contains(sku));
2015-10-31 08:11:23 +00:00
}
2015-11-02 18:16:30 +00:00
private static IntentSender IABgetIntent(String sku, IInAppBillingService service, Context context) throws RemoteException {
Bundle bundle = service.getBuyIntent(3, context.getPackageName(), sku, "inapp", "");
Log.i(TAG, "IAB.getBuyIntent");
Util.logBundle(TAG, bundle);
int response = (bundle == null ? -1 : bundle.getInt("RESPONSE_CODE", -1));
Log.i(TAG, "IAB response=" + getIABResult(response));
if (response != 0 || !bundle.containsKey("BUY_INTENT"))
return null;
PendingIntent pi = bundle.getParcelable("BUY_INTENT");
return (pi == null ? null : pi.getIntentSender());
2015-10-31 08:11:23 +00:00
}
private static String getIABResult(int responseCode) {
2015-10-30 18:57:18 +00:00
switch (responseCode) {
case 0:
2015-11-02 12:37:33 +00:00
return "OK";
2015-10-30 18:57:18 +00:00
case 1:
2015-11-02 12:37:33 +00:00
return "USER_CANCELED";
2015-10-30 18:57:18 +00:00
case 2:
2015-11-02 12:37:33 +00:00
return "SERVICE_UNAVAILABLE";
2015-10-30 18:57:18 +00:00
case 3:
2015-11-02 12:37:33 +00:00
return "BILLING_UNAVAILABLE";
2015-10-30 18:57:18 +00:00
case 4:
2015-11-02 12:37:33 +00:00
return "ITEM_UNAVAILABLE";
2015-10-30 18:57:18 +00:00
case 5:
2015-11-02 12:37:33 +00:00
return "DEVELOPER_ERROR";
2015-10-30 18:57:18 +00:00
case 6:
2015-11-02 12:37:33 +00:00
return "ERROR";
2015-10-30 18:57:18 +00:00
case 7:
2015-11-02 12:37:33 +00:00
return "ITEM_ALREADY_OWNED";
2015-10-30 18:57:18 +00:00
case 8:
2015-11-02 12:37:33 +00:00
return "ITEM_NOT_OWNED";
2015-10-30 18:57:18 +00:00
default:
return Integer.toString(responseCode);
}
}
2015-10-31 19:02:22 +00:00
}