Search in sources :

Example 1 with BlackListHelper

use of ai.saiy.android.api.helper.BlackListHelper in project Saiy-PS by brandall76.

the class FragmentSuperuserHelper method showBlackListSelector.

/**
 * Show the Blacklist selector
 *
 * @return true if the applications installed to populate the selector, false otherwise
 */
public boolean showBlackListSelector() {
    final BlackListHelper blackListHelper = new BlackListHelper();
    final ArrayList<String> blackListArray = blackListHelper.fetch(getApplicationContext());
    if (DEBUG) {
        MyLog.i(CLS_NAME, "showBlackListSelector: blackListArray: " + blackListArray.size());
    }
    final ArrayList<Pair<String, String>> installedPackages = Installed.declaresSaiyPermission(getApplicationContext());
    if (DEBUG) {
        MyLog.i(CLS_NAME, "showBlackListSelector: installedPackages: " + installedPackages.size());
    }
    if (!UtilsList.notNaked(installedPackages)) {
        return false;
    }
    final ArrayList<String> appNames = new ArrayList<>(installedPackages.size());
    final ArrayList<Integer> selectedList = new ArrayList<>();
    for (final Pair appPair : installedPackages) {
        appNames.add((String) appPair.first);
        // noinspection SuspiciousMethodCalls
        if (blackListArray.contains(appPair.second)) {
            selectedList.add((appNames.size() - 1));
        }
    }
    getParentActivity().runOnUiThread(new Runnable() {

        @Override
        public void run() {
            final MaterialDialog materialDialog = new MaterialDialog.Builder(getParentActivity()).autoDismiss(false).title(R.string.menu_blacklist).content(R.string.blacklist_intro_text).iconRes(R.drawable.ic_traffic_light).positiveText(R.string.save).neutralText(R.string.clear).negativeText(android.R.string.cancel).items(appNames).itemsCallbackMultiChoice(selectedList.toArray(new Integer[0]), new MaterialDialog.ListCallbackMultiChoice() {

                @Override
                public boolean onSelection(final MaterialDialog dialog, final Integer[] which, final CharSequence[] text) {
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "showBlackListSelector: onSelection: " + which.length);
                    }
                    return true;
                }
            }).onNeutral(new MaterialDialog.SingleButtonCallback() {

                @Override
                public void onClick(@NonNull final MaterialDialog dialog, @NonNull final DialogAction which) {
                    dialog.clearSelectedIndices();
                }
            }).onPositive(new MaterialDialog.SingleButtonCallback() {

                @Override
                public void onClick(@NonNull final MaterialDialog dialog, @NonNull final DialogAction which) {
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "showBlackListSelector: onPositive");
                    }
                    final Integer[] selected = dialog.getSelectedIndices();
                    assert selected != null;
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "showBlackListSelector: onPositive: " + selected.length);
                    }
                    final ArrayList<String> userBlackListed = new ArrayList<>(selected.length);
                    for (final Integer aSelected : selected) {
                        if (DEBUG) {
                            MyLog.i(CLS_NAME, "showBlackListSelector: onPositive: " + installedPackages.get(aSelected).second);
                        }
                        userBlackListed.add(installedPackages.get(aSelected).second);
                    }
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "showBlackListSelector: onPositive: would save: " + userBlackListed.toString());
                    }
                    blackListHelper.save(FragmentSuperuserHelper.this.getApplicationContext(), userBlackListed);
                    dialog.dismiss();
                }
            }).onNegative(new MaterialDialog.SingleButtonCallback() {

                @Override
                public void onClick(@NonNull final MaterialDialog dialog, @NonNull final DialogAction which) {
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "showBlackListSelector: onNegative");
                    }
                    dialog.dismiss();
                }
            }).cancelListener(new DialogInterface.OnCancelListener() {

                @Override
                public void onCancel(final DialogInterface dialog) {
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "showBlackListSelector: onCancel");
                    }
                    dialog.dismiss();
                }
            }).build();
            materialDialog.getWindow().getAttributes().windowAnimations = R.style.dialog_animation_left;
            materialDialog.show();
        }
    });
    return true;
}
Also used : BlackListHelper(ai.saiy.android.api.helper.BlackListHelper) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) DialogInterface(android.content.DialogInterface) ArrayList(java.util.ArrayList) UtilsString(ai.saiy.android.utils.UtilsString) DialogAction(com.afollestad.materialdialogs.DialogAction) NonNull(android.support.annotation.NonNull) Pair(android.util.Pair)

Example 2 with BlackListHelper

use of ai.saiy.android.api.helper.BlackListHelper 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)2 UtilsString (ai.saiy.android.utils.UtilsString)2 Pair (android.util.Pair)2 ArrayList (java.util.ArrayList)2 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 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 NonNull (android.support.annotation.NonNull)1 DialogAction (com.afollestad.materialdialogs.DialogAction)1 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)1 Locale (java.util.Locale)1