package com.astutesol.product.livecricketalpha;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.android.vending.billing.IInAppBillingService;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.astutesol.product.livecricketalpha.Adapters.ScheduleDataAdapter;
import com.astutesol.product.livecricketalpha.DataModel.Schedule_Data_Model;
import com.astutesol.product.livecricketalpha.stats.MyDataSource;
import com.astutesol.product.livecricketalpha.stats.MySharedPreferences;
import com.astutesol.product.livecricketalpha.util.IabHelper;
import com.astutesol.product.livecricketalpha.util.IabResult;
import com.astutesol.product.livecricketalpha.util.Inventory;
import com.astutesol.product.livecricketalpha.util.Purchase;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class PslSchedule extends BaseActivity implements AdapterView.OnItemClickListener {
Handler mHandler = new Handler();
public static List<Schedule_Data_Model> scchedule_list;
AlertDialog.Builder builder;
IabHelper mHelper;
private static final String TAG
= "LiveCricket";
static final String ITEM_SKU
= "android.test.purchased";
IInAppBillingService mService;
public boolean REGISTRATION_CHECK_FLAGE;
//static final String ITEM_SKU = "monthly_subcription";
// static final String ITEM_SKU = "monthly_subcription";
//IInAppBillingService mService;
/* boolean blnBind;
private IInAppBillingService mService;
private ServiceConnection mServiceConn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mService = IInAppBillingService.Stub.asInterface(service);
}
};
*/
int subcribeFlag;
int adapterPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater
= (LayoutInflater
) this.
getSystemService(Context.
LAYOUT_INFLATER_SERVICE);
View contentView
= inflater.
inflate(R.
layout.
activity_psl_schedule,
null,
false);
mDrawerLayout.addView(contentView, 0);
title_TV.setText("SCHEDULE");
builder = new AlertDialog.Builder(this);
// consumeItem();
// String base64EncodedPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAr/eEzQdxiJnicLmCzHiANddeb+6B2JUiOyWQ7kssy81THGhW7JTOu9BaVPRhcw7il/dyUQtjHuJYuGxdvG2Pm7M854Kb3lJTLYs8rrSiXyP0jT2eWhKt1ydN3tc2kOnGRnobra2vB+ZTXfo93of402ZeTnuKNka5GcPLQ374Dk1vNaIpGPp5Tg6PZamLOayZre87JXw8ot+W0LIs5IObaUZyvb1Ehtlw/UssykWnwsI/BK60Tsy8xfd3jwAhBfCHTzi1W7rek1j+UUbvO55SSbbsQJ/wNorQjsTAhW8w35g4KjJZpnhZ1TArd6nzAqlEzzYYw/Y+jwcd/77cN3ChNwIDAQAB";
/* sohailString */
String base64EncodedPublicKey
= "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnq7tmXGWEaMgkljjWDfXheskJD7ZyKaU4F+c9Jt7gfqN3Dh5PeaWUhQCsKI0F2qYjbhpNu5GMaeAwIRudAI97HA1VMQ08ocrFoBvjr2mSe6jRJwXplEykQsumn8sS/n4mCUZvxpq4M7gg1bxmgoAWfOnVWkukyVIICYviPAEQtFcanqefXrqPwopyJ43HJiP0lzyoB5mj/2gdnEgxbbJkUxIAuEP76heVlOONkATXE983Od/d84v3rJb+v84zPq7To782GX6hvCD7OlXmgcMMrS/lnOhH07iHG+zK36tAcRtGnnBg+k3WA7+1R7kuttFxBZ3PdtPBmuuYk2N3q1FBwIDAQAB";
//requestNewInterstitial();
listSchedule
= (ListView) findViewById
(R.
id.
schedule);
mydata = new MyDataSource(this);
// LoadData("http://blogswizards.com/restaurant/admin/fetch_classes.php?app_id=100");
scchedule_list = mydata.GetSchedule();
String[] colors_list
= getResources
().
getStringArray(R.
array.
colors_grid_list);
int colorIdx = 0;
for (int i = 0; i < scchedule_list.size(); i++) {
if (colorIdx == colors_list.length) {
colorIdx = 0;
}
scchedule_list.get(i).gridColor = colors_list[colorIdx];
colorIdx++;
}
final ScheduleDataAdapter adapter = new ScheduleDataAdapter(this, R.layout.predict_list_item, scchedule_list);
adapter.notifyDataSetChanged();
listSchedule.setAdapter(adapter);
listSchedule.setOnItemClickListener((AdapterView.OnItemClickListener) context);
//Add running code
/* Runnable runnable = new Runnable() {
public void run() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
mHandler.postDelayed(this, 10000); // to load ad at every 10 sec
}
};
runnable.run();
mHandler.postDelayed(runnable, 2500);
*/
// mHelper = new IabHelper(this, base64EncodedPublicKey);
/**comment start code 11-jan-2017*/
/* mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
@Override
public void onIabSetupFinished(IabResult result) {
if (!result.isSuccess()) {
Log.d("asda", "hhhh");
Log.d(TAG, "setUpFail" + result);
} else {
Log.d(TAG, "setupok");
}
}
});*/
/*comment end code 11-jan-2017*/
bindService
(new Intent
("com.android.vending.billing.InAppBillingService.BIND"), mServiceConn,
Context.
BIND_AUTO_CREATE);
/* blnBind = bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"), mServiceConn, Context.BIND_AUTO_CREATE);
Toast.makeText(context, "bindService - return " + String.valueOf(blnBind), Toast.LENGTH_SHORT).show();
Log.i(TAG, "bindService - return " + String.valueOf(blnBind));*/
}
@Override
public void onItemClick
(AdapterView
<?> adapter,
View v,
final int position,
long arg3
) {
Log.d("SChedule", "postion " + position);
// http://astutesolhosting.com/cricketApp/service.php?name=subcriber_status&user_id=21
// bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"), mServiceConn, Context.BIND_AUTO_CREATE);
checkRegistration(position);
// checkitem();
String urlsubcription
= BaseURL
+ "name=subcriber_status" + "&user_id=" + mpref.
GetUserID();
Log.d(TAG, "mpref.GetUserID()" + mpref.GetUserID());
checkSubcription(urlsubcription);
checkownedItems();
adapterPosition = position;
}
@Override
public void onBackPressed() {
mHandler.removeCallbacks(null);
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
Log.d("usm_onBackPressed", "ad is loaded already");
} else {
Log.d("usm_onBackPressed", "ad is not loaded");
}
super.onBackPressed();
}
public void launchPurches() {
mHelper.launchPurchaseFlow(PslSchedule.this, ITEM_SKU, 10001, mPurchaseFinishedListener, "mypurchasetoken");
}
public void checkRegistration(int position) {
String strMatch_id
= scchedule_list.
get(position
).
match_id;
mpref = new MySharedPreferences(context);
String user_id
= mpref.
GetUserID();
Log.d("user id", user_id);
if (user_id.isEmpty() || user_id.equals("0")) {
builder.setMessage("Not a Registered User").setPositiveButton("Want to Register", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(context, RegistrationActivity.class);
// intent.putExtra("match_id", strMatch_id);
context.startActivity(intent);
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
} else {
}
}
/*in app purches methods*/
//=======inappPurches Methods==================
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (mHelper == null) return;
if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
// Log.i("purchase.getSku()", purchase.getSku());
// Log.i("ITEM_SKU", ITEM_SKU);
if (result.isFailure()) {
// Handle error
return;
} else if (purchase.getSku().equals(ITEM_SKU)) {
Log.d(TAG, "purchase.getSku() ");
/*09jan17*/
int sb = 1;
String setSubcribeStatus
= BaseURL
+ "name=update_subcriber_status" + "&user_id=" + mpref.
GetUserID() + "&subcribed=" + sb
;
String strMatch_id
= scchedule_list.
get(adapterPosition
).
match_id;
mpref = new MySharedPreferences(context);
String user_id
= mpref.
GetUserID();
Log.d("user id", user_id);
if (user_id.isEmpty() || user_id.equals("0")) {
Intent intent = new Intent(context, RegistrationActivity.class);
intent.putExtra("match_id", strMatch_id);
context.startActivity(intent);
} else {
Log.d("Match id is:::: ", strMatch_id);
setSubcription(setSubcribeStatus);
Intent intent = new Intent(context, PredictActivity.class);
intent.putExtra("match_id", strMatch_id);
intent.putExtra("user_id", user_id);
context.startActivity(intent);
}
}
}
};
public void consumeItem() {
mHelper.queryInventoryAsync(mReceivedInventoryListener);
}
IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
if (result.isFailure()) {
// Handle failure
} else {
mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU), mConsumeFinishedListener);
}
}
};
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
public void onConsumeFinished(Purchase purchase, IabResult result) {
if (result.isSuccess()) {
int sb = 1;
String setSubcribeStatus
= BaseURL
+ "name=update_subcriber_status" + "&user_id=" + mpref.
GetUserID() + "&subcribed=" + sb
;
String strMatch_id
= scchedule_list.
get(adapterPosition
).
match_id;
mpref = new MySharedPreferences(context);
String user_id
= mpref.
GetUserID();
Log.d("user id", user_id);
if (user_id.isEmpty() || user_id.equals("0")) {
Intent intent = new Intent(context, RegistrationActivity.class);
intent.putExtra("match_id", strMatch_id);
context.startActivity(intent);
} else {
setSubcription(setSubcribeStatus);
Log.d("Match id is:::: ", strMatch_id);
Intent intent = new Intent(context, PredictActivity.class);
intent.putExtra("match_id", strMatch_id);
intent.putExtra("user_id", user_id);
context.startActivity(intent); // LoadData("http://blogswizards.com/psl/service.php?name=load_prediction&match_id="+match_id1+"&user_id="+user_id);
}
} else {
// handle error
}
}
};
public void moveToPridictAndWin() {
String strMatch_id
= scchedule_list.
get(adapterPosition
).
match_id;
mpref = new MySharedPreferences(context);
String user_id
= mpref.
GetUserID();
Intent intent = new Intent(context, PredictActivity.class);
intent.putExtra("match_id", strMatch_id);
intent.putExtra("user_id", user_id);
context.startActivity(intent);
}
public void checkSubcription
(String url
) {
// password_et.setFocusable(true);
Log.e("url : ", url);
JsonObjectRequest jsonObjectRequest
= new JsonObjectRequest
(Request.
Method.
GET, url,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response1) {
Log.d("service_response", response1.toString());
try {
String message
= response1.
getString("subscribed");
String serverSubcribeValue
= "0";
if (serverSubcribeValue.equals(message)) {
Toast.makeText(context, "You are not subcribe", Toast.LENGTH_SHORT).show();
builder.setMessage("Please Subscribe First")
.setPositiveButton("Want to subscribe", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"), mServiceConn, Context.BIND_AUTO_CREATE);
// checkownedItems();
launchPurches();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
} else {
String strMatch_id
= scchedule_list.
get(adapterPosition
).
match_id;
mpref = new MySharedPreferences(context);
Log.d("Match id is:::: ", strMatch_id);
String user_id
= mpref.
GetUserID();
Log.d("user id", user_id);
Intent intent = new Intent(context, PredictActivity.class);
intent.putExtra("match_id", strMatch_id);
intent.putExtra("user_id", user_id);
context.startActivity(intent);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Error handling
System.
out.
println("Something went wrong!");
error.printStackTrace();
}
});
jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
10000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Add the request to the queue
Volley.newRequestQueue(this).add(jsonObjectRequest);
}
public void setSubcription
(String url
) {
// password_et.se
// tFocusable(true);
Log.e("url : ", url);
JsonObjectRequest jsonObjectRequest
= new JsonObjectRequest
(Request.
Method.
GET, url,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response1) {
Log.d("service_response", response1.toString());
try {
String message
= response1.
getString("subscribed");
String tempSubMessage
= "Member Subscribed";
if (tempSubMessage.equals(message)) {
String strMatch_id
= scchedule_list.
get(adapterPosition
).
match_id;
mpref = new MySharedPreferences(context);
Log.d("Match id is:::: ", strMatch_id);
String user_id
= mpref.
GetUserID();
Log.d("user id", user_id);
Intent intent = new Intent(context, PredictActivity.class);
intent.putExtra("match_id", strMatch_id);
intent.putExtra("user_id", user_id);
context.startActivity(intent);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Error handling
// Toast.makeText(LoginActivity.this, "Error", Toast.LENGTH_LONG).show();
System.
out.
println("Something went wrong!");
error.printStackTrace();
}
});
jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
10000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Add the request to the queue
Volley.newRequestQueue(this).add(jsonObjectRequest);
}
/*check owned item*/
ServiceConnection mServiceConn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mService
= IInAppBillingService.
Stub.
asInterface(service
);
Log.d("TEST", "mService ready to go!");
checkownedItems();
}
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
}
};
private void checkownedItems() {
try {
Bundle ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null);
if (ownedItems.getInt("RESPONSE_CODE") == 0) {
ArrayList<String> ownedSkus = ownedItems.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
ArrayList<String> purchaseDataList = ownedItems.getStringArrayList("INAPP_PURCHASE_DATA_LIST");
ArrayList<String> signatureList = ownedItems.getStringArrayList("INAPP_DATA_SIGNATURE");
String continuationToken
= ownedItems.
getString("INAPP_CONTINUATION_TOKEN");
if (purchaseDataList.size() > 0) {
moveToPridictAndWin();
Toast.makeText(context, "item already owned", Toast.LENGTH_SHORT).show(); //Item(s) owned
for(int i=0; i<purchaseDataList.size(); ++i)
{
String purchaseData
= purchaseDataList.
get(i
);
//String signature = signatureList.get(i); //Note signatures do not appear to work with android.test.purchased (silly google)
// String sku = ownedSkus.get(i);
}
} else {
//Item(s) not owned
String base64EncodedPublicKey
= "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnq7tmXGWEaMgkljjWDfXheskJD7ZyKaU4F+c9Jt7gfqN3Dh5PeaWUhQCsKI0F2qYjbhpNu5GMaeAwIRudAI97HA1VMQ08ocrFoBvjr2mSe6jRJwXplEykQsumn8sS/n4mCUZvxpq4M7gg1bxmgoAWfOnVWkukyVIICYviPAEQtFcanqefXrqPwopyJ43HJiP0lzyoB5mj/2gdnEgxbbJkUxIAuEP76heVlOONkATXE983Od/d84v3rJb+v84zPq7To782GX6hvCD7OlXmgcMMrS/lnOhH07iHG+zK36tAcRtGnnBg+k3WA7+1R7kuttFxBZ3PdtPBmuuYk2N3q1FBwIDAQAB";
mHelper = new IabHelper(this, base64EncodedPublicKey);
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
@Override
public void onIabSetupFinished(IabResult result) {
if (!result.isSuccess()) {
Log.d("TEST", "In-app Billing setup failed: " + result);
} else {
Log.d("TEST", "In-app Billing is set up OK");
}
}
});
}
} else {
//Error checking owned items
}
e.printStackTrace();
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (mServiceConn != null) {
unbindService(mServiceConn);
}
if (mHelper != null) mHelper.dispose();
mHelper = null;
}
/*private void checkownedItems() {
try {
Bundle ownedItems = mService.getPurchases(3, getPackageName(), "subs", null);
if (ownedItems.getInt("RESPONSE_CODE") == 0) {
Log.d(TAG, "checkownedItems: " + ownedItems.getInt("RESPONSE_CODE"));
ArrayList<String> ownedSkus = ownedItems.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
ArrayList<String> purchaseDataList = ownedItems.getStringArrayList("INAPP_PURCHASE_DATA_LIST");
ArrayList<String> signatureList = ownedItems.getStringArrayList("INAPP_DATA_SIGNATURE");
String continuationToken = ownedItems.getString("INAPP_CONTINUATION_TOKEN");
if (purchaseDataList.size() > 0) {
//Item(s) owned
moveToPridictAndWin();
for(int i=0; i<purchaseDataList.size(); ++i)
{
String purchaseData = purchaseDataList.get(i);
String signature = signatureList.get(i); //Note signatures do not appear to work with android.test.purchased (silly google)
String sku = ownedSkus.get(i);
}
} else {
//Item(s) not owned
String base64EncodedPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnq7tmXGWEaMgkljjWDfXheskJD7ZyKaU4F+c9Jt7gfqN3Dh5PeaWUhQCsKI0F2qYjbhpNu5GMaeAwIRudAI97HA1VMQ08ocrFoBvjr2mSe6jRJwXplEykQsumn8sS/n4mCUZvxpq4M7gg1bxmgoAWfOnVWkukyVIICYviPAEQtFcanqefXrqPwopyJ43HJiP0lzyoB5mj/2gdnEgxbbJkUxIAuEP76heVlOONkATXE983Od/d84v3rJb+v84zPq7To782GX6hvCD7OlXmgcMMrS/lnOhH07iHG+zK36tAcRtGnnBg+k3WA7+1R7kuttFxBZ3PdtPBmuuYk2N3q1FBwIDAQAB";
mHelper = new IabHelper(this, base64EncodedPublicKey);
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
@Override
public void onIabSetupFinished(IabResult result) {
if (!result.isSuccess()) {
Log.d("TEST", "In-app Billing setup failed: " + result);
} else {
Log.d("TEST", "In-app Billing is set up OK");
*//* String urlsubcription = BaseURL + "name=subcriber_status" + "&user_id=" + mpref.GetUserID();
Log.d(TAG, "mpref.GetUserID()" + mpref.GetUserID());
checkSubcription(urlsubcription);
checkRegistration(adapterPosition);*//*
}
}
});
}
} else {
//Error checking owned items
}
} catch (RemoteException e) {
e.printStackTrace();
}
}*/
/*check owned item*/
public void checkitem() {
// if (!blnBind) return;
if (mService == null) return;
Bundle ownedItems;
try {
ownedItems = mService.getPurchases(3, getPackageName(), "subs", null);
Toast.makeText(context, "getPurchases() - success return Bundle", Toast.LENGTH_SHORT).show();
Log.i(TAG, "getPurchases() - success return Bundle");
e.printStackTrace();
Toast.makeText(context, "getPurchases - fail!", Toast.LENGTH_SHORT).show();
Log.w(TAG, "getPurchases() - fail!");
return;
}
int response = ownedItems.getInt("RESPONSE_CODE");
Toast.
makeText(context,
"getPurchases() - \"RESPONSE_CODE\" return " + String.
valueOf(response
), Toast.
LENGTH_SHORT).
show();
Log.
i(TAG,
"getPurchases() - \"RESPONSE_CODE\" return " + String.
valueOf(response
));
if (response != 0) return;
ArrayList<String> ownedSkus = ownedItems.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
ArrayList<String> purchaseDataList = ownedItems.getStringArrayList("INAPP_PURCHASE_DATA_LIST");
ArrayList<String> signatureList = ownedItems.getStringArrayList("INAPP_DATA_SIGNATURE");
String continuationToken
= ownedItems.
getString("INAPP_CONTINUATION_TOKEN");
Log.i(TAG, "getPurchases() - \"INAPP_PURCHASE_ITEM_LIST\" return " + ownedSkus.toString());
Log.i(TAG, "getPurchases() - \"INAPP_PURCHASE_DATA_LIST\" return " + purchaseDataList.toString());
Log.i(TAG, "getPurchases() - \"INAPP_DATA_SIGNATURE\" return " + (signatureList != null ? signatureList.toString() : "null"));
Log.i(TAG, "getPurchases() - \"INAPP_CONTINUATION_TOKEN\" return " + (continuationToken != null ? continuationToken : "null"));
}
}