Search in sources :

Example 1 with Regex

use of ai.saiy.android.api.request.Regex in project Saiy-PS by brandall76.

the class BRRemote method onReceive.

@Override
public void onReceive(final Context context, final Intent intent) {
    if (DEBUG) {
        MyLog.i(CLS_NAME, "onReceive");
    }
    if (intent == null) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, " onHandleIntent: Intent null");
        }
        return;
    }
    final String action = intent.getAction();
    if (DEBUG) {
        examineIntent(intent);
    }
    if (!UtilsString.notNaked(action)) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, " onHandleIntent: action null");
        }
        return;
    }
    if (!intent.getAction().equals(SaiyKeyphrase.SAIY_REQUEST_RECEIVER)) {
        Log.e("Saiy Remote Request", "Incorrect ACTION: rejecting");
        return;
    }
    switch(intent.getIntExtra(SaiyKeyphrase.REQUEST_TYPE, 0)) {
        case SaiyKeyphrase.REQUEST_KEYPHRASE:
            if (DEBUG) {
                MyLog.i(CLS_NAME, "onHandleIntent: REQUEST_KEYPHRASE");
            }
            final String keyphrase = intent.getStringExtra(SaiyKeyphrase.SAIY_KEYPHRASE);
            if (UtilsString.notNaked(keyphrase)) {
                final String packageName = intent.getStringExtra(SaiyKeyphrase.REQUESTING_PACKAGE);
                if (UtilsString.notNaked(packageName)) {
                    final BlackListHelper blackListHelper = new BlackListHelper();
                    if (!blackListHelper.isBlacklisted(context, packageName)) {
                        final Pair<Boolean, String> appPair = UtilsApplication.getAppNameFromPackage(context.getApplicationContext(), packageName);
                        if (appPair.first && UtilsString.notNaked(appPair.second)) {
                            final Locale vrLocale = SPH.getVRLocale(context.getApplicationContext());
                            final SupportedLanguage sl = SupportedLanguage.getSupportedLanguage(vrLocale);
                            final ArrayList<String> voiceData = new ArrayList<>(1);
                            voiceData.add(keyphrase);
                            final float[] confidence = new float[1];
                            confidence[0] = 1f;
                            final ArrayList<Pair<CC, Float>> resolvePair = new Resolve(context.getApplicationContext(), voiceData, confidence, sl).resolve();
                            if (!UtilsList.notNaked(resolvePair)) {
                                final CustomCommand customCommand = new CustomCommand(CCC.CUSTOM_INTENT_SERVICE, CC.COMMAND_USER_CUSTOM, keyphrase, SaiyRequestParams.SILENCE, SaiyRequestParams.SILENCE, SPH.getTTSLocale(context.getApplicationContext()).toString(), vrLocale.toString(), LocalRequest.ACTION_SPEAK_ONLY);
                                final Regex regex = (Regex) intent.getSerializableExtra(SaiyKeyphrase.KEYPHRASE_REGEX);
                                switch(regex) {
                                    case MATCHES:
                                    case STARTS_WITH:
                                    case ENDS_WITH:
                                    case CONTAINS:
                                        customCommand.setRegex(regex);
                                        break;
                                    case CUSTOM:
                                        customCommand.setRegex(regex);
                                        customCommand.setRegularExpression(intent.getStringExtra(SaiyKeyphrase.REGEX_CONTENT));
                                        break;
                                }
                                final Intent remoteIntent = new Intent(SAIY_INTENT_RECEIVER);
                                remoteIntent.setPackage(packageName);
                                final Bundle bundle = intent.getExtras();
                                if (UtilsBundle.notNaked(bundle)) {
                                    if (!UtilsBundle.isSuspicious(bundle)) {
                                        if (DEBUG) {
                                            examineIntent(intent);
                                        }
                                        remoteIntent.putExtras(bundle);
                                        customCommand.setIntent(remoteIntent.toUri(0));
                                        final Pair<Boolean, Long> successPair = CustomCommandHelper.setCommand(context.getApplicationContext(), customCommand, -1);
                                        if (DEBUG) {
                                            MyLog.i(CLS_NAME, "Custom command created: " + successPair.first);
                                        }
                                        final Bundle responseBundle = new Bundle();
                                        final int responseCode = bundle.getInt(SaiyKeyphrase.SAIY_KEYPHRASE_ID, 0);
                                        if (DEBUG) {
                                            MyLog.i(CLS_NAME, "Custom command responseCode: " + responseCode);
                                        }
                                        responseBundle.putInt(SaiyKeyphrase.SAIY_KEYPHRASE_ID, responseCode);
                                        final LocalRequest request = new LocalRequest(context.getApplicationContext());
                                        request.setVRLocale(vrLocale);
                                        request.setTTSLocale(SPH.getTTSLocale(context.getApplicationContext()));
                                        request.setSupportedLanguage(sl);
                                        request.setQueueType(SaiyTextToSpeech.QUEUE_ADD);
                                        request.setAction(LocalRequest.ACTION_SPEAK_ONLY);
                                        if (successPair.first) {
                                            request.setUtterance(PersonalityResponse.getRemoteCommandRegisterSuccess(context.getApplicationContext(), sl, appPair.second, keyphrase));
                                            request.execute();
                                            setResult(Activity.RESULT_OK, SaiyKeyphrase.class.getSimpleName(), responseBundle);
                                        } else {
                                            request.setUtterance(PersonalityResponse.getErrorRemoteCommandRegister(context.getApplicationContext(), sl, appPair.second));
                                            request.execute();
                                            setResult(Activity.RESULT_CANCELED, SaiyKeyphrase.class.getSimpleName(), responseBundle);
                                        }
                                    } else {
                                        Log.e("Saiy Remote Request", "Bundle rejected due to contents");
                                    }
                                } else {
                                    Log.e("Saiy Remote Request", "Request bundle missing contents: rejected");
                                }
                            } else {
                                Log.e("Saiy Remote Request", "Conflict with inbuilt command: rejected");
                            }
                        } else {
                            Log.e("Saiy Remote Request", "Application name undetectable: rejected");
                        }
                    } else {
                        Log.e("Saiy Remote Request", "Application blacklisted: rejected");
                    }
                } else {
                    Log.e("Saiy Remote Request", "Package name missing: rejected");
                }
            } else {
                Log.e("Saiy Remote Request", "Keyphrase missing: rejected");
            }
            break;
        default:
            Log.e("Saiy Remote Request", "Internal type error: rejected");
            break;
    }
}
Also used : Locale(java.util.Locale) BlackListHelper(ai.saiy.android.api.helper.BlackListHelper) LocalRequest(ai.saiy.android.service.helper.LocalRequest) Bundle(android.os.Bundle) UtilsBundle(ai.saiy.android.utils.UtilsBundle) ArrayList(java.util.ArrayList) Intent(android.content.Intent) UtilsString(ai.saiy.android.utils.UtilsString) SaiyKeyphrase(ai.saiy.android.api.request.SaiyKeyphrase) Resolve(ai.saiy.android.nlu.local.Resolve) CustomCommand(ai.saiy.android.custom.CustomCommand) SupportedLanguage(ai.saiy.android.localisation.SupportedLanguage) Regex(ai.saiy.android.api.request.Regex) Pair(android.util.Pair)

Aggregations

BlackListHelper (ai.saiy.android.api.helper.BlackListHelper)1 Regex (ai.saiy.android.api.request.Regex)1 SaiyKeyphrase (ai.saiy.android.api.request.SaiyKeyphrase)1 CustomCommand (ai.saiy.android.custom.CustomCommand)1 SupportedLanguage (ai.saiy.android.localisation.SupportedLanguage)1 Resolve (ai.saiy.android.nlu.local.Resolve)1 LocalRequest (ai.saiy.android.service.helper.LocalRequest)1 UtilsBundle (ai.saiy.android.utils.UtilsBundle)1 UtilsString (ai.saiy.android.utils.UtilsString)1 Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 Pair (android.util.Pair)1 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1