package com.vipera.apprating;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;
import com.vipera.de.apprating.DEAppRatingManager;
import com.vipera.de.apprating.DEAppRatingStatus;
import com.vipera.de.apprating.DEAppRatingType;
import com.vipera.de.apprating.DEAppRatingUI;
import com.vipera.de.apprating.impl.DERatingError;
import com.vipera.de.apprating.impl.DefaultAppRatingManager;
import com.vipera.de.apprating.impl.DefaultAppRatingUI;
import com.vipera.de.apprating.network.AppRatingCatalogConfig;
import com.vipera.de.apprating.translations.DEAppRatingTranslationsProvider;
import com.vipera.de.apprating.ui.DEAppRatingUIResult;
import com.vipera.de.motifconnector.IDEServerManager;
import com.vipera.de.motifconnector.config.DEConnectorProvider;
import com.vipera.de.motifconnector.nativekit.error.IDEError;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPreferences;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import java.util.HashMap;
import java.util.Map;
/**
* Rating plugin class
*/
public class DEAppRatingPlugin extends CordovaPlugin {
private static final String INITIALIZE_ACTION = "initialize";
private static final String START_RATING_ACTION="startRating";
private static final String SET_SESSION_INFO_ACTION = "setSessionInfo";
private static final String CAN_REQUIRE_FEEDBACK_ACTION ="canRequireFeedback";
private static final String SEND_FEEDBACK_WITH_MESSAGE_ACTION = "sendFeedbackWithMessage";
private static final String DECLINE_REQUEST_ACTION = "declineRequest";
private static final String RATE_IN_STORE_ACTION = "rateInStore";
private static final String STORE_REVIEW_SUPPORTED_ACTION = "isStoreReviewSupported";
private static final String UPDATE_FEEDBACK_STATUS_ACTION = "updateFeedbackStatus";
private static final String GET_FEEDBACK_STATUS_ACTION = "getFeedbackStatus";
private static final String GET_API_MODEL_ACTION = "getApiModel";
private static final String SET_TRANSLATIONS_ACTION = "setTranslations";
private static final String ASSET_VERSION_KEY = "assetVer";
private static final String APP_VERSION_KEY = "appVer";
private static final String ERROR_CODE_TAG = "errorCode";
private static final String ERROR_DATA_TAG = "errorData";
private static final String INVALID_PARAMETER_ERROR_CODE = "INVALID_PARAMETER_ERROR";
private static final String INVALID_CONNECTOR_ERROR_CODE = "INVALID_CONNECTOR_ERROR";
private static final String DOMAIN_PREF_KEY = "appRating.motif.domain";
private static final String APP_PREF_KEY = "appRating.motif.app";
private static final String SERVICE_PREF_KEY = "appRating.motif.service";
private static final String OP_PREF_KEY = "appRating.motif.operation";
private Map<String,Object> extraInfo = new HashMap<>();
private String userId;
private String sessionId;
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
}
private void storeExtraInfo(String key, String value) {
extraInfo.put(key,value);
}
private Map<String,Object> getExtraInfo(){
return extraInfo;
}
@Override
public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException {
switch (action){
case INITIALIZE_ACTION:
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
initRatingPlugin(args,callbackContext);
}
});
return true;
case SET_SESSION_INFO_ACTION:
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
setSessionInfo(args,callbackContext);
}
});
return true;
case START_RATING_ACTION:
startRatingWithUI(callbackContext);
return true;
case CAN_REQUIRE_FEEDBACK_ACTION:
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
canRequireFeedback(callbackContext);
}
});
return true;
case SEND_FEEDBACK_WITH_MESSAGE_ACTION:
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
sendFeedbackWithMessage(args,callbackContext);
}
});
return true;
case DECLINE_REQUEST_ACTION:
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
declineRequest(args,callbackContext);
}
});
return true;
case RATE_IN_STORE_ACTION:
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
rateInStore(callbackContext);
}
});
return true;
case STORE_REVIEW_SUPPORTED_ACTION:
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
isStoreReviewSupported(callbackContext);
}
});
return true;
case UPDATE_FEEDBACK_STATUS_ACTION:
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
updateFeedbackStatus(args,callbackContext);
}
});
return true;
case GET_FEEDBACK_STATUS_ACTION:
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
getFeedbackStatus(callbackContext);
}
});
return true;
case GET_API_MODEL_ACTION:
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
getApiModel(callbackContext);
}
});
return true;
case SET_TRANSLATIONS_ACTION:
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
setTranslations(args,callbackContext);
}
});
return true;
}
return false;
}
private void setTranslations(JSONArray args, CallbackContext callbackContext) {
try{
JSONArray array = args.optJSONArray(0);
if(array == null || array.length() == 0){
sendInvalidParameterError(callbackContext);
return;
}
for(int i = 0;i< array.length(); i++){
JSONObject entry = array.getJSONObject(i);
addTranslationEntry(entry);
}
callbackContext.success();
}catch (Exception ex){
sendInvalidParameterError(callbackContext);
}
}
private void addTranslationEntry(JSONObject entry) throws JSONException {
String key = entry.getString("key");
String value = entry.getString("value");
DEAppRatingTranslationsProvider.AppRatingTranslationKey trKey = DEAppRatingTranslationsProvider.AppRatingTranslationKey.valueOf(key);
DEAppRatingTranslationsProvider.getInstance(getAppContext()).set(trKey,value);
}
private Context getAppContext(){
return cordova.getActivity().getApplicationContext();
}
private void getApiModel(CallbackContext callbackContext) {
String apiModel = DefaultAppRatingManager.getInstance().getApiModel();
callbackContext.success(apiModel);
}
private void getFeedbackStatus(CallbackContext callbackContext) {
DEAppRatingStatus status = DefaultAppRatingManager.getInstance().getCurrentFeedbackStatus();
callbackContext.success(status.toMotifCode());
}
private void updateFeedbackStatus(JSONArray args, CallbackContext callbackContext) {
String statusStr = args.optString(0,null);
DEAppRatingStatus status = DEAppRatingStatus.fromMotifCode(statusStr);
if(status == null){
sendInvalidParameterError(callbackContext);
return;
}
DefaultAppRatingManager.getInstance().updateFeedbackStatus(status);
callbackContext.success();
}
private void isStoreReviewSupported(CallbackContext callbackContext) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK,false));
}
private void rateInStore(CallbackContext callbackContext) {
DefaultAppRatingManager.getInstance().openInStore(cordova.getActivity(), new DEAppRatingManager.DEAppRatingOpCallback() {
@Override
public void onOperationDone() {
}
@Override
public void onError(IDEError ideError) {
}
},getExtraInfo());
callbackContext.success();
}
private void sendFeedbackWithMessage(JSONArray args, final CallbackContext callbackContext) {
String message = args.optString(0,null);
int rating = args.optInt(1,-1);
if(message == null && rating < 0){
sendInvalidParameterError(callbackContext);
return;
}
DefaultAppRatingManager.getInstance().sendFeedbackWithMessage(message, rating, new DEAppRatingManager.DEAppRatingOpCallback() {
@Override
public void onOperationDone() {
Log.e("sendFeedback","send done");
}
@Override
public void onError(IDEError ideError) {
Log.e("sendFeedback",ideError.toString());
}
},getExtraInfo());
callbackContext.success();
}
private void declineRequest(JSONArray args, CallbackContext callbackContext) {
String typeAsStr = args.optString(0,null);
if(typeAsStr == null){
sendInvalidParameterError(callbackContext);
return;
}
try {
DEAppRatingType type = DEAppRatingType.fromStringValue(typeAsStr);
DefaultAppRatingManager.getInstance().declineRequest(type, new DEAppRatingManager.DEAppRatingOpCallback() {
@Override
public void onOperationDone() {
}
@Override
public void onError(IDEError ideError) {
}
},getExtraInfo());
callbackContext.success();
}catch (IllegalArgumentException ex){
sendInvalidParameterError(callbackContext);
}
}
private void setSessionInfo(JSONArray args, CallbackContext callbackContext) {
String userId = args.optString(0,null);
String sessionId = args.optString(1,null);
if(userId == null || userId.isEmpty()){
sendInvalidParameterError(callbackContext);
return;
}
if(sessionId == null || sessionId.isEmpty()) {
sendInvalidParameterError(callbackContext);
return;
}
this.userId = userId;
this.sessionId = sessionId;
DefaultAppRatingManager.getInstance().setUser(userId);
DefaultAppRatingManager.getInstance().setSessionId(sessionId);
callbackContext.success();
}
private void initRatingPlugin(JSONArray args, CallbackContext callbackContext) {
String domain = args.optString(0,null);
String app = args.optString(1,null);
String assetVersion = args.optString(2,null);
if(domain == null || domain.isEmpty()){
sendInvalidParameterError(callbackContext);
return;
}
if(app == null || app.isEmpty()) {
sendInvalidParameterError(callbackContext);
return;
}
IDEServerManager serverManager;
try {
serverManager = DEConnectorProvider.getSharedInstance();
}catch (IllegalStateException ex){
sendInvalidConnectorError(callbackContext);
return;
}
String ratingDomain = preferences.getString(DOMAIN_PREF_KEY,null);
String ratingApp = preferences.getString(APP_PREF_KEY,null);
String ratingSrv = preferences.getString(SERVICE_PREF_KEY,null);
String ratingOp = preferences.getString(OP_PREF_KEY,null);
Loading ...