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

612 lines
23 KiB
Java
Raw Normal View History

2015-10-24 18:01:55 +00:00
package eu.faircode.netguard;
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-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-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-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-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.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;
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-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;
private MenuItem menuNetwork = null;
2015-10-30 15:51:24 +00:00
private IInAppBillingService billingService = null;
2015-10-24 18:01:55 +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
private static final String SKU_DONATE = "donation"; // "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
// 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-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");
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-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);
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
swipeRefresh.post(new Runnable() {
@Override
public void run() {
swipeRefresh.setRefreshing(true);
}
});
// 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
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-31 13:15:26 +00:00
updateApplicationList();
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-31 13:15:26 +00:00
private void updateApplicationList() {
2015-10-24 18:01:55 +00:00
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) {
2015-10-31 17:07:21 +00:00
return Rule.getRules(false, ActivityMain.this);
2015-10-24 18:01:55 +00:00
}
@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-31 13:15:26 +00:00
if (adapter != null) {
adapter.clear();
adapter.addAll(result);
}
if (swipeRefresh != null)
swipeRefresh.setRefreshing(false);
2015-10-24 18:01:55 +00:00
}
}
}.execute();
}
@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));
if ("enabled".equals(name)) {
2015-10-25 15:28:41 +00:00
// Get enabled
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);
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) {
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;
}
});
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-31 17:07:21 +00:00
MenuItem menuSystem = menu.findItem(R.id.menu_system);
menuSystem.setChecked(prefs.getBoolean("manage_system", false));
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);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
2015-10-25 22:04:10 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
// 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-25 22:04:10 +00:00
case R.id.menu_whitelist_wifi:
2015-10-30 07:57:36 +00:00
menu_whitelist_wifi(prefs);
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);
return true;
2015-10-31 17:07:21 +00:00
case R.id.menu_system:
menu_system(prefs);
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;
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();
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_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();
2015-10-31 13:15:26 +00:00
updateApplicationList();
2015-10-30 07:57:36 +00:00
SinkholeService.reload("wifi", this);
}
private void menu_whitelist_other(SharedPreferences prefs) {
prefs.edit().putBoolean("whitelist_other", !prefs.getBoolean("whitelist_other", true)).apply();
2015-10-31 13:15:26 +00:00
updateApplicationList();
2015-10-30 07:57:36 +00:00
SinkholeService.reload("other", this);
}
2015-10-31 17:07:21 +00:00
private void menu_system(SharedPreferences prefs) {
prefs.edit().putBoolean("manage_system", !prefs.getBoolean("manage_system", true)).apply();
updateApplicationList();
SinkholeService.reload(null, this);
}
2015-10-30 07:57:36 +00:00
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-31 08:11:23 +00:00
final boolean valid = Util.hasValidFingerprint(TAG, this);
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);
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-31 08:11:23 +00:00
if (valid)
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
// Handle donate
2015-10-31 08:11:23 +00:00
final Intent donate = new Intent(Intent.ACTION_VIEW);
donate.setData(Uri.parse("http://www.netguard.me/"));
2015-10-30 15:51:24 +00:00
btnDonate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (valid && billingService != null)
2015-10-31 08:11:23 +00:00
IABinitiate();
else
startActivity(donate);
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-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) {
2015-10-31 08:11:23 +00:00
LocalBroadcastManager.getInstance(ActivityMain.this).unregisterReceiver(onIABsuccess);
2015-10-30 15:51:24 +00:00
}
})
.create();
2015-10-30 07:57:36 +00:00
dialog.show();
2015-10-30 15:51:24 +00:00
2015-10-31 08:11:23 +00:00
// Validate IAB
if (valid && billingService != null)
new AsyncTask<Object, Object, Boolean>() {
2015-10-30 15:51:24 +00:00
@Override
2015-10-31 08:11:23 +00:00
protected Boolean doInBackground(Object... objects) {
return IABvalidate();
2015-10-30 15:51:24 +00:00
}
@Override
2015-10-31 08:11:23 +00:00
protected void onPostExecute(Boolean ok) {
btnDonate.setVisibility(ok ? View.GONE : View.VISIBLE);
tvThanks.setVisibility(ok ? View.VISIBLE : View.GONE);
2015-10-30 15:51:24 +00:00
}
}.execute();
2015-10-31 08:11:23 +00:00
else
btnDonate.setVisibility(donate.resolveActivity(getPackageManager()) == null ? View.GONE : View.VISIBLE);
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
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
2015-10-31 08:11:23 +00:00
Intent intent = new Intent(ACTION_IAB);
2015-10-30 15:51:24 +00:00
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
2015-10-30 18:57:18 +00:00
} else if (data != null) {
int response = data.getIntExtra("RESPONSE_CODE", -1);
2015-10-31 08:11:23 +00:00
Log.i(TAG, "Billing response=" + getIABResult(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
2015-10-31 08:11:23 +00:00
private boolean IABvalidate() {
try {
// Get available SKUs
ArrayList<String> skuList = new ArrayList<>();
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);
int details_response = details.getInt("RESPONSE_CODE");
Log.i(TAG, "Billing response=" + getIABResult(details_response));
if (details_response != 0)
return false;
// 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 false;
// Get purchases
Bundle purchases = billingService.getPurchases(3, getPackageName(), "inapp", null);
Log.i(TAG, "Billing.getPurchases");
Util.logBundle(TAG, purchases);
int purchases_response = purchases.getInt("RESPONSE_CODE");
Log.i(TAG, "Billing response=" + getIABResult(purchases_response));
if (purchases_response != 0)
return false;
// Check purchases
ArrayList<String> skus = purchases.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
return skus.contains(SKU_DONATE);
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
return false;
}
}
private void IABinitiate() {
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");
Log.i(TAG, "Billing response=" + getIABResult(response));
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();
}
}
private static String getIABResult(int responseCode) {
2015-10-30 18:57:18 +00:00
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
}