package ir.windroid.erfannj;
import android.annotation.*;
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import com.util.*;
import com.util.IabHelper.*;
import java.util.*;
@SuppressLint("NewApi") public class BuyPremiumActivity extends Activity
{
/////***************
protected static final int BUY_REQUEST_CODE = 12345;
private IabHelper buyHelper;
private Button butBuy;
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener;
IabHelper.QueryInventoryFinishedListener mGotInventoryListener;
////***************
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.buy_premium_activity);
///////////******************
// 1
//*** amadesazi
butBuy = (Button) findViewById(R.id.button_buy);
butBuy.setEnabled(false);
butBuy.setText("لطفا صبر کنید");
buyHelper = new IabHelper(this, Billing.PUBLIC_KEY);
buyHelper.startSetup(new OnIabSetupFinishedListener() {
@Override
public void onIabSetupFinished(IabResult result)
{
if (result.isSuccess())
{
final ArrayList<String> moreSkus = new ArrayList<String>();
moreSkus.add(Billing.SKU_NAME_PREMIUM);
//buyHelper.queryInventoryAsync(true, moreSkus, mGotInventoryListener);
final TextView tvPrice = (TextView) findViewById(R.id.price);
tvPrice.setText("در حال انتظار جهت دریافت قیمت");
// Start the query for the details for the SKUs. This runs asynchronously, so
// it may be that the price appears a bit later after the rest of the Activity is shown.
buyHelper.queryInventoryAsync(true, moreSkus, new QueryInventoryFinishedListener() {
@Override
public void onQueryInventoryFinished(IabResult result, Inventory inv) {
if(result.isSuccess()) {
// If we successfully got the price, show it in the text field
SkuDetails details = inv.getSkuDetails(Billing.SKU_NAME_PREMIUM);
String price = details.getPrice();
tvPrice.setText(price);
// On successful init and price getting, enable the "buy me" button
butBuy.setEnabled(true);
} else {
// Error getting the price... show a sorry text in the price field now
tvPrice.setText("قیمت دریافت نشد");
//
butBuy.setEnabled(true);
//
}
}
});
butBuy.setEnabled(false);
butBuy.setText("ارتقا به نسخه کامل");
}
}
});
// 2
butBuy.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if (Billing.isPackageInstalled(getBaseContext(), "com.farsitel.bazaar"))
{
if (Billing.isConnected(getBaseContext()))
{
buyHelper.launchPurchaseFlow(BuyPremiumActivity.this, Billing.SKU_NAME_PREMIUM, Billing.BUY_REQUEST_CODE, mPurchaseFinishedListener, "payload-string");
}
else
{
Billing.toast(getBaseContext(), "لطفا ابتدا به اینترنت متصل شوید");
}
}
else
{
Billing.toast(getBaseContext(), "لطفا برنامه بازار را بر روی دستگاه خود نصب نمایید!");
}
}
});
//////////////**********************
// kodhaye digar
// end of contenView۰
}
//////////////////////********************
// 3
public BuyPremiumActivity()
{
// 3-1
this.mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener(){
@Override
public void onIabPurchaseFinished(IabResult result, Purchase info)
{
if ((result.isSuccess()))
{
SharedPreferences shared = getSharedPreferences("Prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit();
editor.putString(Billing.p_token, info.getToken());
editor.apply();
String msg="در حال بررسی وضعیت خرید...";
Billing.toast(getBaseContext(), msg);
butBuy.setEnabled(false);
butBuy.setText("بررسی خرید");
new verify_buying().execute(Billing.urls);
}
else
{
Billing.toast(getBaseContext(), "خرید ناموفق");
}
}
};
// 3-2
mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener(){
@Override
public void onQueryInventoryFinished(IabResult iabResult, Inventory inventory)
{
if (iabResult.isSuccess())
{
butBuy.setEnabled(true);
butBuy.setText("ارتقا به نسخه کامل");
boolean premium = inventory.hasPurchase(Billing.SKU_NAME_PREMIUM);
if (premium)
{
new verify_buying().execute(Billing.urls);
}
}
else
{
butBuy.setEnabled(true);
butBuy.setText("ارتقا به نسخه کامل");
}
}
};
}
// 4
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
buyHelper.handleActivityResult(requestCode, resultCode, data);
}
@Override
protected void onDestroy()
{
super.onDestroy();
buyHelper.dispose();
}
// 5
private class verify_buying extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... urls)
{
if (Billing.isConnected(getBaseContext()))
{
SharedPreferences shared = getSharedPreferences("Prefs", MODE_PRIVATE);
int no=shared.getInt("no", 0);
return Billing.GET_Server_Response(no, getBaseContext());
}
return null;
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result)
{
//---1
if (result.contains("purchase: ok"))
{
Billing.saveData(getBaseContext());
}
//---2
else if (result.isEmpty())
{
butBuy.setEnabled(true);
butBuy.setText("ارتقا به نسخه کامل");
Billing.toast(getBaseContext(), "لطفا در صورت نصب لاکی پچر آن را حذف کنید.");
}
//---3
else if (result.contains("HTML"))
{
SharedPreferences shared = getSharedPreferences("Prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit();
editor.putInt("no", 1);
editor.apply();
butBuy.setEnabled(true);
butBuy.setText("ارتقا به نسخه کامل");
Billing.toast(getBaseContext(), "خطا در ارتباط با سرور!!\nلطفا دوباره تلاش کنید..");
}
}
/////////************************************
/// kodhaye digar
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(this);
dialog.getWindow();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.exit_menu_buy);
Button notexit = (Button) dialog.findViewById(R.id.no);
// if button is clicked, close the custom dialog
notexit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
Button yesexit = (Button) dialog.findViewById(R.id.yes);
// if button is clicked, close the custom dialog
yesexit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
dialog.show();
}}