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

206 lines
8.1 KiB
Java
Raw Normal View History

2015-12-30 08:58:21 +00:00
package eu.faircode.netguard;
/*
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/>.
2016-01-01 13:52:09 +00:00
Copyright 2015-2016 by Marcel Bokhorst (M66B)
2015-12-30 08:58:21 +00:00
*/
import android.content.Intent;
import android.content.SharedPreferences;
2015-12-31 09:03:32 +00:00
import android.os.Build;
2015-12-30 08:58:21 +00:00
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
2015-12-31 09:03:32 +00:00
import android.text.Editable;
import android.text.TextWatcher;
2015-12-30 08:58:21 +00:00
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
2015-12-31 09:03:32 +00:00
import android.view.WindowManager;
2015-12-30 08:58:21 +00:00
import android.widget.Button;
2015-12-31 09:03:32 +00:00
import android.widget.EditText;
import android.widget.LinearLayout;
2015-12-30 08:58:21 +00:00
import android.widget.TextView;
public class ActivityPro extends AppCompatActivity {
private static final String TAG = "NetGuard.Pro";
private IAB iab;
// adb shell pm clear com.android.vending
2015-12-31 09:03:32 +00:00
//public static final String SKU_NOTIFY = "notify";
2016-01-01 13:52:09 +00:00
public static final String SKU_NOTIFY = "android.test.purchased";
2015-12-30 08:58:21 +00:00
public static final String SKU_THEME = "theme";
public static final String SKU_SPEED = "speed";
public static final String SKU_DONATION = "donation";
@Override
protected void onCreate(Bundle savedInstanceState) {
2015-12-31 09:18:00 +00:00
Log.i(TAG, "Create");
2015-12-30 08:58:21 +00:00
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
setTheme(prefs.getBoolean("dark_theme", false) ? R.style.AppThemeDark : R.style.AppTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.pro);
getSupportActionBar().setTitle(R.string.title_pro);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
2015-12-31 09:03:32 +00:00
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
2015-12-31 15:55:39 +00:00
// Initial state
2015-12-30 08:58:21 +00:00
updateState();
2015-12-31 15:55:39 +00:00
// Challenge
2015-12-31 09:03:32 +00:00
TextView tvChallenge = (TextView) findViewById(R.id.tvChallenge);
tvChallenge.setText(Build.SERIAL);
2015-12-31 15:55:39 +00:00
// Response
try {
final String response = Util.md5(Build.SERIAL, "NetGuard");
EditText etResponse = (EditText) findViewById(R.id.etResponse);
etResponse.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Do nothing
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Do nothing
}
@Override
public void afterTextChanged(Editable editable) {
if (response.equals(editable.toString().toUpperCase())) {
2015-12-31 09:03:32 +00:00
IAB.setBought(SKU_DONATION, ActivityPro.this);
updateState();
}
}
2015-12-31 15:55:39 +00:00
});
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
2015-12-31 09:03:32 +00:00
2015-12-30 08:58:21 +00:00
try {
iab = new IAB(new IAB.Delegate() {
@Override
public void onReady(final IAB iab) {
Log.i(TAG, "IAB ready");
try {
2015-12-31 15:55:39 +00:00
iab.updatePurchases();
updateState();
2015-12-30 08:58:21 +00:00
2015-12-31 09:03:32 +00:00
final Button btnNotify = (Button) findViewById(R.id.btnNotify);
2015-12-30 08:58:21 +00:00
final Button btnTheme = (Button) findViewById(R.id.btnTheme);
final Button btnSpeed = (Button) findViewById(R.id.btnSpeed);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
if (view == btnNotify)
2015-12-31 09:03:32 +00:00
startIntentSenderForResult(iab.getBuyIntent(SKU_NOTIFY).getIntentSender(), view.getId(), new Intent(), 0, 0, 0);
2015-12-30 08:58:21 +00:00
else if (view == btnTheme)
startIntentSenderForResult(iab.getBuyIntent(SKU_THEME).getIntentSender(), view.getId(), new Intent(), 0, 0, 0);
else if (view == btnSpeed)
startIntentSenderForResult(iab.getBuyIntent(SKU_SPEED).getIntentSender(), view.getId(), new Intent(), 0, 0, 0);
} catch (Throwable ex) {
Log.i(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
Util.sendCrashReport(ex, ActivityPro.this);
}
}
};
2015-12-31 09:03:32 +00:00
btnNotify.setOnClickListener(listener);
2015-12-30 08:58:21 +00:00
btnTheme.setOnClickListener(listener);
btnSpeed.setOnClickListener(listener);
2015-12-31 09:03:32 +00:00
btnNotify.setEnabled(true);
2015-12-30 08:58:21 +00:00
btnTheme.setEnabled(true);
btnSpeed.setEnabled(true);
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
}
}, this);
iab.bind();
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
Util.sendCrashReport(ex, ActivityPro.this);
}
}
@Override
protected void onDestroy() {
2015-12-31 09:18:00 +00:00
Log.i(TAG, "Destroy");
2015-12-30 08:58:21 +00:00
iab.unbind();
super.onDestroy();
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
2015-12-31 09:03:32 +00:00
case R.id.btnNotify:
IAB.setBought(SKU_NOTIFY, this);
2015-12-30 08:58:21 +00:00
updateState();
break;
case R.id.btnTheme:
IAB.setBought(SKU_THEME, this);
updateState();
break;
case R.id.btnSpeed:
IAB.setBought(SKU_SPEED, this);
updateState();
break;
}
}
}
private void updateState() {
2015-12-31 09:03:32 +00:00
Button btnNotify = (Button) findViewById(R.id.btnNotify);
2015-12-30 08:58:21 +00:00
Button btnTheme = (Button) findViewById(R.id.btnTheme);
Button btnSpeed = (Button) findViewById(R.id.btnSpeed);
2015-12-31 09:03:32 +00:00
TextView tvNotify = (TextView) findViewById(R.id.tvNotify);
2015-12-30 08:58:21 +00:00
TextView tvTheme = (TextView) findViewById(R.id.tvTheme);
TextView tvSpeed = (TextView) findViewById(R.id.tvSpeed);
2015-12-31 09:03:32 +00:00
LinearLayout llChallenge = (LinearLayout) findViewById(R.id.llChallenge);
2015-12-30 08:58:21 +00:00
2016-01-01 18:03:41 +00:00
btnNotify.setVisibility(IAB.isPurchased(SKU_NOTIFY, this) ? View.GONE : View.VISIBLE);
btnTheme.setVisibility(IAB.isPurchased(SKU_THEME, this) ? View.GONE : View.VISIBLE);
btnSpeed.setVisibility(IAB.isPurchased(SKU_SPEED, this) ? View.GONE : View.VISIBLE);
2016-01-01 18:03:41 +00:00
tvNotify.setVisibility(IAB.isPurchased(SKU_NOTIFY, this) ? View.VISIBLE : View.GONE);
tvTheme.setVisibility(IAB.isPurchased(SKU_THEME, this) ? View.VISIBLE : View.GONE);
tvSpeed.setVisibility(IAB.isPurchased(SKU_SPEED, this) ? View.VISIBLE : View.GONE);
2015-12-31 09:03:32 +00:00
2016-01-01 18:03:41 +00:00
llChallenge.setVisibility(IAB.isPurchased(SKU_DONATION, this) ? View.GONE : View.VISIBLE);
2015-12-30 08:58:21 +00:00
}
}