This commit is contained in:
eskimo
2025-04-13 18:12:59 -04:00
parent 1cfde91b76
commit 9e726d0626
7 changed files with 3834 additions and 40 deletions

View File

@@ -1,11 +1,81 @@
package software.eskimo.capacitor.promocode;
package eskimo.software.capacitor.promocode;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;
public class PromoCode {
import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
public String echo(String value) {
Log.i("Echo", value);
return value;
@CapacitorPlugin(name = "PromoCode")
public class PromoCode extends Plugin {
private static final String TAG = "PromoCode";
private static final int PROMO_CODE_REQUEST = 5001;
@PluginMethod
public void isPromoCodeSupported(PluginCall call) {
JSObject ret = new JSObject();
ret.put("supported", true);
call.resolve(ret);
}
}
@PluginMethod
public void launchPromoCodeRedemptionFlow(PluginCall call) {
try {
saveCall(call);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://play.google.com/redeem?code="));
intent.setPackage("com.android.vending");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (intent.resolveActivity(getContext().getPackageManager()) != null) {
getActivity().runOnUiThread(() -> {
Toast.makeText(getContext(),
"Redeem your code in Google Play. Press back to return.",
Toast.LENGTH_SHORT).show();
});
startActivityForResult(call, intent, PROMO_CODE_REQUEST);
} else {
Intent webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/redeem"));
startActivityForResult(call, webIntent, PROMO_CODE_REQUEST);
}
} catch (Exception e) {
Log.e(TAG, "Error launching promo code flow", e);
JSObject ret = new JSObject();
ret.put("success", false);
ret.put("message", "Failed to launch promo code flow: " + e.getMessage());
call.resolve(ret);
}
}
@Override
protected void handleOnActivityResult(int requestCode, int resultCode, Intent data) {
super.handleOnActivityResult(requestCode, resultCode, data);
if (requestCode == PROMO_CODE_REQUEST) {
PluginCall savedCall = getSavedCall();
if (savedCall == null) {
return;
}
JSObject ret = new JSObject();
if (resultCode == Activity.RESULT_OK) {
ret.put("success", true);
} else {
ret.put("success", false);
ret.put("message", "User canceled or redemption failed");
}
savedCall.resolve(ret);
}
}
}

View File

@@ -1,22 +0,0 @@
package software.eskimo.capacitor.promocode;
import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
@CapacitorPlugin(name = "PromoCode")
public class PromoCodePlugin extends Plugin {
private PromoCode implementation = new PromoCode();
@PluginMethod
public void echo(PluginCall call) {
String value = call.getString("value");
JSObject ret = new JSObject();
ret.put("value", implementation.echo(value));
call.resolve(ret);
}
}