Search in sources :

Example 6 with SupportedLanguage

use of ai.saiy.android.localisation.SupportedLanguage in project Saiy-PS by brandall76.

the class ActivityHome method speak.

/**
 * Utility method to vocalise any UI content or descriptions
 *
 * @param resId  to the String to speak
 * @param action one of {@link LocalRequest#ACTION_SPEAK_ONLY} {@link LocalRequest#ACTION_SPEAK_LISTEN}
 */
public void speak(final int resId, final int action) {
    new Thread(new Runnable() {

        @Override
        public void run() {
            final SupportedLanguage sl = SupportedLanguage.getSupportedLanguage(SPH.getVRLocale(ActivityHome.this.getApplicationContext()));
            ActivityHome.this.speak(SaiyResourcesHelper.getStringResource(getApplicationContext(), sl, resId), action);
        }
    }).start();
}
Also used : SupportedLanguage(ai.saiy.android.localisation.SupportedLanguage)

Example 7 with SupportedLanguage

use of ai.saiy.android.localisation.SupportedLanguage in project Saiy-PS by brandall76.

the class LocalRequest method prepareIntro.

/**
 * Utility method to cut down on boiler plate requests
 */
public void prepareIntro() {
    if (DEBUG) {
        MyLog.i(CLS_NAME, "prepareIntro");
    }
    final Locale vrLocale = SPH.getVRLocale(weakContext.get());
    final SupportedLanguage sl = SupportedLanguage.getSupportedLanguage(vrLocale);
    bundle.putInt(LocalRequest.EXTRA_ACTION, LocalRequest.ACTION_SPEAK_LISTEN);
    bundle.putString(LocalRequest.EXTRA_UTTERANCE, PersonalityHelper.getIntro(weakContext.get(), sl));
    bundle.putString(LocalRequest.EXTRA_RECOGNITION_LANGUAGE, vrLocale.toString());
    bundle.putString(LocalRequest.EXTRA_TTS_LANGUAGE, SPH.getTTSLocale(weakContext.get()).toString());
    bundle.putSerializable(LocalRequest.EXTRA_SUPPORTED_LANGUAGE, sl);
}
Also used : UtilsLocale(ai.saiy.android.utils.UtilsLocale) Locale(java.util.Locale) SupportedLanguage(ai.saiy.android.localisation.SupportedLanguage)

Example 8 with SupportedLanguage

use of ai.saiy.android.localisation.SupportedLanguage in project Saiy-PS by brandall76.

the class LocalRequest method getSupportedLanguage.

/**
 * Get the {@link SupportedLanguage}
 *
 * @return the {@link SupportedLanguage}
 */
public SupportedLanguage getSupportedLanguage() {
    if (bundle.containsKey(EXTRA_SUPPORTED_LANGUAGE)) {
        return (SupportedLanguage) bundle.getSerializable(EXTRA_SUPPORTED_LANGUAGE);
    }
    final SupportedLanguage sl = SupportedLanguage.getSupportedLanguage(getVRLocale());
    bundle.putSerializable(EXTRA_SUPPORTED_LANGUAGE, sl);
    return sl;
}
Also used : SupportedLanguage(ai.saiy.android.localisation.SupportedLanguage)

Example 9 with SupportedLanguage

use of ai.saiy.android.localisation.SupportedLanguage in project Saiy-PS by brandall76.

the class NotificationService method onHandleIntent.

@Override
protected void onHandleIntent(final Intent intent) {
    if (DEBUG) {
        MyLog.i(CLS_NAME, "onHandleIntent");
    }
    if (intent != null) {
        final String action = intent.getAction();
        final Bundle bundle = intent.getExtras();
        if (bundle != null) {
            if (DEBUG) {
                examineIntent(intent);
            }
            if (UtilsString.notNaked(action)) {
                if (intent.getAction().equals(INTENT_CLICK)) {
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "onHandleIntent: INTENT_CLICK");
                    }
                    final SupportedLanguage sl = SupportedLanguage.getSupportedLanguage(SPH.getVRLocale(getApplicationContext()));
                    bundle.putSerializable(LocalRequest.EXTRA_SUPPORTED_LANGUAGE, sl);
                    switch(bundle.getInt(CLICK_ACTION, 0)) {
                        case NOTIFICATION_FOREGROUND:
                            if (DEBUG) {
                                MyLog.i(CLS_NAME, "onHandleIntent: NOTIFICATION_FOREGROUND");
                            }
                            bundle.putInt(LocalRequest.EXTRA_ACTION, LocalRequest.ACTION_SPEAK_LISTEN);
                            bundle.putString(LocalRequest.EXTRA_UTTERANCE, PersonalityHelper.getIntro(getApplicationContext(), sl));
                            bundle.putString(LocalRequest.EXTRA_RECOGNITION_LANGUAGE, SPH.getVRLocale(getApplicationContext()).toString());
                            bundle.putString(LocalRequest.EXTRA_TTS_LANGUAGE, SPH.getTTSLocale(getApplicationContext()).toString());
                            new LocalRequest(getApplicationContext(), bundle).execute();
                            break;
                        case NOTIFICATION_LISTENING:
                            if (DEBUG) {
                                MyLog.i(CLS_NAME, "onHandleIntent: NOTIFICATION_LISTENING");
                            }
                            new LocalRequest(getApplicationContext(), bundle).execute();
                            break;
                        case NOTIFICATION_SPEAKING:
                            if (DEBUG) {
                                MyLog.i(CLS_NAME, "onHandleIntent: NOTIFICATION_SPEAKING");
                            }
                            new LocalRequest(getApplicationContext(), bundle).execute();
                            break;
                        case NOTIFICATION_FETCHING:
                            if (DEBUG) {
                                MyLog.i(CLS_NAME, "onHandleIntent: NOTIFICATION_FETCHING");
                            }
                            new LocalRequest(getApplicationContext(), bundle).execute();
                            break;
                        case NOTIFICATION_INITIALISING:
                            if (DEBUG) {
                                MyLog.i(CLS_NAME, "onHandleIntent: NOTIFICATION_INITIALISING");
                            }
                            new LocalRequest(getApplicationContext(), bundle).execute();
                            break;
                        case NOTIFICATION_COMPUTING:
                            if (DEBUG) {
                                MyLog.i(CLS_NAME, "onHandleIntent: NOTIFICATION_COMPUTING");
                            }
                            new LocalRequest(getApplicationContext(), bundle).execute();
                            break;
                        case NOTIFICATION_PERMISSIONS:
                            if (DEBUG) {
                                MyLog.i(CLS_NAME, "onHandleIntent: NOTIFICATION_PERMISSIONS");
                            }
                            ExecuteIntent.openApplicationSpecificSettings(getApplicationContext(), getPackageName());
                            bundle.putInt(LocalRequest.EXTRA_ACTION, LocalRequest.ACTION_SPEAK_ONLY);
                            bundle.putString(LocalRequest.EXTRA_RECOGNITION_LANGUAGE, SPH.getVRLocale(getApplicationContext()).toString());
                            bundle.putString(LocalRequest.EXTRA_TTS_LANGUAGE, SPH.getTTSLocale(getApplicationContext()).toString());
                            String permissionContent;
                            switch(bundle.getInt(PermissionHelper.REQUESTED_PERMISSION, 0)) {
                                case PermissionHelper.REQUEST_AUDIO:
                                    if (DEBUG) {
                                        MyLog.i(CLS_NAME, "onHandleIntent: REQUEST_AUDIO");
                                    }
                                    permissionContent = getString(ai.saiy.android.R.string.permission_audio);
                                    break;
                                case PermissionHelper.REQUEST_GROUP_CONTACTS:
                                    if (DEBUG) {
                                        MyLog.i(CLS_NAME, "onHandleIntent: REQUEST_GROUP_CONTACTS");
                                    }
                                    permissionContent = getString(ai.saiy.android.R.string.permission_group_contacts);
                                    break;
                                case PermissionHelper.REQUEST_FILE:
                                    if (DEBUG) {
                                        MyLog.i(CLS_NAME, "onHandleIntent: REQUEST_FILE");
                                    }
                                    // TODO
                                    permissionContent = getString(ai.saiy.android.R.string.permission_unknown);
                                    break;
                                default:
                                    if (DEBUG) {
                                        MyLog.w(CLS_NAME, "onHandleIntent: PermissionHelper.UNKNOWN");
                                    }
                                    permissionContent = getString(ai.saiy.android.R.string.permission_unknown);
                                    break;
                            }
                            bundle.putString(LocalRequest.EXTRA_UTTERANCE, permissionContent);
                            new LocalRequest(getApplicationContext(), bundle).execute();
                            break;
                        case NOTIFICATION_EMOTION:
                            if (DEBUG) {
                                MyLog.i(CLS_NAME, "onHandleIntent: NOTIFICATION_EMOTION");
                            }
                            bundle.putInt(LocalRequest.EXTRA_ACTION, LocalRequest.ACTION_SPEAK_ONLY);
                            final String emotionAnalysis = AnalysisResultHelper.getEmotionDescription(getApplicationContext(), sl);
                            bundle.putString(LocalRequest.EXTRA_UTTERANCE, emotionAnalysis);
                            bundle.putString(LocalRequest.EXTRA_RECOGNITION_LANGUAGE, SPH.getVRLocale(getApplicationContext()).toString());
                            bundle.putString(LocalRequest.EXTRA_TTS_LANGUAGE, SPH.getTTSLocale(getApplicationContext()).toString());
                            bundle.putSerializable(LocalRequest.EXTRA_COMMAND, CC.COMMAND_EMOTION);
                            new LocalRequest(getApplicationContext(), bundle).execute();
                            break;
                        case NOTIFICATION_HOTWORD:
                            if (DEBUG) {
                                MyLog.i(CLS_NAME, "onHandleIntent: NOTIFICATION_HOTWORD");
                            }
                            bundle.putInt(LocalRequest.EXTRA_ACTION, LocalRequest.ACTION_STOP_HOTWORD);
                            final LocalRequest request = new LocalRequest(getApplicationContext(), bundle);
                            request.setShutdownHotword();
                            request.execute();
                            final Intent closeShadeIntent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
                            sendBroadcast(closeShadeIntent);
                            break;
                        case NOTIFICATION_IDENTIFICATION:
                            if (DEBUG) {
                                MyLog.i(CLS_NAME, "onHandleIntent: NOTIFICATION_IDENTIFICATION");
                            }
                            bundle.putInt(LocalRequest.EXTRA_ACTION, LocalRequest.ACTION_SPEAK_ONLY);
                            bundle.putString(LocalRequest.EXTRA_RECOGNITION_LANGUAGE, SPH.getVRLocale(getApplicationContext()).toString());
                            bundle.putString(LocalRequest.EXTRA_TTS_LANGUAGE, SPH.getTTSLocale(getApplicationContext()).toString());
                            switch(bundle.getInt(LocalRequest.EXTRA_CONDITION)) {
                                case Condition.CONDITION_IDENTIFY:
                                    if (DEBUG) {
                                        MyLog.i(CLS_NAME, "onHandleIntent: CONDITION_IDENTIFY");
                                    }
                                    final Speaker.Confidence confidence = (Speaker.Confidence) bundle.getSerializable(Speaker.EXTRA_IDENTIFY_OUTCOME);
                                    if (confidence != null) {
                                        switch(confidence) {
                                            case HIGH:
                                                if (DEBUG) {
                                                    MyLog.i(CLS_NAME, "checkResult: confidence: " + Speaker.Confidence.HIGH.name());
                                                }
                                                bundle.putString(LocalRequest.EXTRA_UTTERANCE, PersonalityResponse.getVocalIDHigh(getApplicationContext(), sl));
                                                break;
                                            case NORMAL:
                                                if (DEBUG) {
                                                    MyLog.i(CLS_NAME, "checkResult: confidence: " + Speaker.Confidence.NORMAL.name());
                                                }
                                                bundle.putString(LocalRequest.EXTRA_UTTERANCE, PersonalityResponse.getVocalIDMedium(getApplicationContext(), sl));
                                                break;
                                            case LOW:
                                                if (DEBUG) {
                                                    MyLog.i(CLS_NAME, "checkResult: confidence: " + Speaker.Confidence.LOW.name());
                                                }
                                                bundle.putString(LocalRequest.EXTRA_UTTERANCE, PersonalityResponse.getVocalIDLow(getApplicationContext(), sl));
                                                break;
                                            case UNDEFINED:
                                                if (DEBUG) {
                                                    MyLog.i(CLS_NAME, "checkResult: confidence: " + Speaker.Confidence.UNDEFINED.name());
                                                }
                                                bundle.putString(LocalRequest.EXTRA_UTTERANCE, PersonalityResponse.getVocalIDLow(getApplicationContext(), sl));
                                                break;
                                            case ERROR:
                                                if (DEBUG) {
                                                    MyLog.i(CLS_NAME, "checkResult: confidence: " + Speaker.Confidence.ERROR.name());
                                                }
                                                bundle.putString(LocalRequest.EXTRA_UTTERANCE, PersonalityResponse.getVocalIDError(getApplicationContext(), sl));
                                                break;
                                        }
                                    }
                                    break;
                                case Condition.CONDITION_IDENTITY:
                                    if (DEBUG) {
                                        MyLog.i(CLS_NAME, "onHandleIntent: CONDITION_IDENTITY");
                                    }
                                    if (bundle.getBoolean(Speaker.EXTRA_IDENTITY_OUTCOME, false)) {
                                        bundle.putString(LocalRequest.EXTRA_UTTERANCE, SaiyResourcesHelper.getStringResource(getApplicationContext(), sl, R.string.speech_enroll_success));
                                    } else {
                                        bundle.putString(LocalRequest.EXTRA_UTTERANCE, PersonalityResponse.getEnrollmentError(getApplicationContext(), sl));
                                    }
                                    break;
                            }
                            new LocalRequest(getApplicationContext(), bundle).execute();
                            break;
                        default:
                            if (DEBUG) {
                                MyLog.w(CLS_NAME, "onHandleIntent: default");
                            }
                            new LocalRequest(getApplicationContext(), bundle).execute();
                            break;
                    }
                } else {
                    if (DEBUG) {
                        MyLog.w(CLS_NAME, "onHandleIntent: ACTION_UNKNOWN");
                    }
                }
            } else {
                if (DEBUG) {
                    MyLog.w(CLS_NAME, "onHandleIntent: Bundle null");
                }
            }
        } else {
            if (DEBUG) {
                MyLog.w(CLS_NAME, "onHandleIntent: Action null");
            }
        }
    } else {
        if (DEBUG) {
            MyLog.w(CLS_NAME, " onHandleIntent: Intent null");
        }
    }
}
Also used : SupportedLanguage(ai.saiy.android.localisation.SupportedLanguage) LocalRequest(ai.saiy.android.service.helper.LocalRequest) Bundle(android.os.Bundle) Intent(android.content.Intent) ExecuteIntent(ai.saiy.android.intent.ExecuteIntent) UtilsString(ai.saiy.android.utils.UtilsString) Speaker(ai.saiy.android.cognitive.identity.provider.microsoft.Speaker)

Example 10 with SupportedLanguage

use of ai.saiy.android.localisation.SupportedLanguage 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

SupportedLanguage (ai.saiy.android.localisation.SupportedLanguage)12 Locale (java.util.Locale)5 UtilsLocale (ai.saiy.android.utils.UtilsLocale)4 UtilsString (ai.saiy.android.utils.UtilsString)3 Bundle (android.os.Bundle)3 LocalRequest (ai.saiy.android.service.helper.LocalRequest)2 Intent (android.content.Intent)2 Pair (android.util.Pair)2 ArrayList (java.util.ArrayList)2 BlackListHelper (ai.saiy.android.api.helper.BlackListHelper)1 Regex (ai.saiy.android.api.request.Regex)1 SaiyKeyphrase (ai.saiy.android.api.request.SaiyKeyphrase)1 Speaker (ai.saiy.android.cognitive.identity.provider.microsoft.Speaker)1 CC (ai.saiy.android.command.helper.CC)1 CustomCommand (ai.saiy.android.custom.CustomCommand)1 ExecuteIntent (ai.saiy.android.intent.ExecuteIntent)1 Resolve (ai.saiy.android.nlu.local.Resolve)1 UtilsBundle (ai.saiy.android.utils.UtilsBundle)1