Search in sources :

Example 1 with TaskerHelper

use of ai.saiy.android.thirdparty.tasker.TaskerHelper in project Saiy-PS by brandall76.

the class FragmentSettingsHelper method showUnknownCommandSelector.

/**
 * Show the unknown command action selector
 */
@SuppressWarnings("ConstantConditions")
public void showUnknownCommandSelector() {
    AsyncTask.execute(new Runnable() {

        @Override
        public void run() {
            final String[] actions = FragmentSettingsHelper.this.getParent().getResources().getStringArray(R.array.array_unknown_action);
            for (int i = 0; i < actions.length; i++) {
                switch(i) {
                    case Unknown.UNKNOWN_STATE:
                        break;
                    case Unknown.UNKNOWN_REPEAT:
                        break;
                    case Unknown.UNKNOWN_GOOGLE_SEARCH:
                        actions[i] = getParent().getString(R.string.menu_send_to) + " " + actions[i];
                        break;
                    case Unknown.UNKNOWN_WOLFRAM_ALPHA:
                        actions[i] = getParent().getString(R.string.menu_send_to) + " " + actions[i];
                        break;
                    case Unknown.UNKNOWN_TASKER:
                        actions[i] = getParent().getString(R.string.menu_send_to) + " " + actions[i];
                        break;
                }
            }
            final ArrayList<Integer> disabledIndicesList = new ArrayList<>();
            if (!Installed.isPackageInstalled(FragmentSettingsHelper.this.getApplicationContext(), Installed.PACKAGE_WOLFRAM_ALPHA)) {
                disabledIndicesList.add(Unknown.UNKNOWN_WOLFRAM_ALPHA);
            }
            if (!new TaskerHelper().isTaskerInstalled(FragmentSettingsHelper.this.getApplicationContext()).first) {
                disabledIndicesList.add(Unknown.UNKNOWN_TASKER);
            }
            FragmentSettingsHelper.this.getParentActivity().runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    final MaterialDialog materialDialog = new MaterialDialog.Builder(getParentActivity()).autoDismiss(false).alwaysCallSingleChoiceCallback().title(R.string.menu_unknown_commands).items((CharSequence[]) actions).itemsDisabledIndices(disabledIndicesList.toArray(new Integer[0])).content(R.string.content_unknown_command).positiveText(R.string.menu_select).negativeText(android.R.string.cancel).iconRes(R.drawable.ic_not_equal).backgroundColorRes(R.color.colorTint).itemsCallbackSingleChoice(SPH.getCommandUnknownAction(FragmentSettingsHelper.this.getApplicationContext()), new MaterialDialog.ListCallbackSingleChoice() {

                        @Override
                        public boolean onSelection(final MaterialDialog dialog, final View view, final int which, final CharSequence text) {
                            if (DEBUG) {
                                MyLog.i(CLS_NAME, "showUnknownCommandSelector: onSelection: " + which + ": " + text);
                            }
                            return true;
                        }
                    }).onPositive(new MaterialDialog.SingleButtonCallback() {

                        @Override
                        public void onClick(@NonNull final MaterialDialog dialog, @NonNull final DialogAction which) {
                            if (DEBUG) {
                                MyLog.i(CLS_NAME, "showUnknownCommandSelector: onPositive: " + dialog.getSelectedIndex());
                            }
                            SPH.setCommandUnknownAction(FragmentSettingsHelper.this.getApplicationContext(), dialog.getSelectedIndex());
                            dialog.dismiss();
                        }
                    }).onNegative(new MaterialDialog.SingleButtonCallback() {

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

                        @Override
                        public void onCancel(final DialogInterface dialog) {
                            if (DEBUG) {
                                MyLog.i(CLS_NAME, "showUnknownCommandSelector: onCancel");
                            }
                            dialog.dismiss();
                        }
                    }).build();
                    materialDialog.getWindow().getAttributes().windowAnimations = R.style.dialog_animation_left;
                    materialDialog.show();
                }
            });
        }
    });
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) DialogInterface(android.content.DialogInterface) TaskerHelper(ai.saiy.android.thirdparty.tasker.TaskerHelper) ArrayList(java.util.ArrayList) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) DialogAction(com.afollestad.materialdialogs.DialogAction) NonNull(android.support.annotation.NonNull)

Example 2 with TaskerHelper

use of ai.saiy.android.thirdparty.tasker.TaskerHelper in project Saiy-PS by brandall76.

the class CommandTasker method getResponse.

/**
 * Resolve the required command returning an {@link Outcome} object
 *
 * @param ctx       the application context
 * @param voiceData ArrayList<String> containing the voice data
 * @param sl        the {@link SupportedLanguage} we are using to analyse the voice data.
 * @param cr        the {@link CommandRequest}
 * @return {@link Outcome} containing everything we need to respond to the command.
 */
public Outcome getResponse(@NonNull final Context ctx, @NonNull final ArrayList<String> voiceData, @NonNull final SupportedLanguage sl, @NonNull final CommandRequest cr) {
    if (DEBUG) {
        MyLog.i(CLS_NAME, "voiceData: " + voiceData.size() + " : " + voiceData.toString());
    }
    then = System.nanoTime();
    final Outcome outcome = new Outcome();
    final TaskerHelper taskerHelper = new TaskerHelper();
    final Pair<Boolean, String> taskerPair = taskerHelper.isTaskerInstalled(ctx);
    String taskerPackage;
    if (taskerPair.first) {
        taskerPackage = taskerPair.second;
        if (DEBUG) {
            MyLog.i(CLS_NAME, "tasker installed: " + taskerPackage);
        }
        final Pair<Boolean, Boolean> taskerStatusPair = taskerHelper.canInteract(ctx);
        if (taskerStatusPair.first) {
            if (taskerStatusPair.second) {
                if (taskerHelper.receiverExists(ctx)) {
                    final ArrayList<TaskerTask> taskList = taskerHelper.getTasks(ctx);
                    if (UtilsList.notNaked(taskList)) {
                        if (DEBUG) {
                            MyLog.i(CLS_NAME, "task count: " + taskList.size());
                        }
                        final ArrayList<String> taskNames = new ArrayList<>();
                        String taskName;
                        if (cr.isResolved()) {
                            if (DEBUG) {
                                MyLog.i(CLS_NAME, "isResolved: true");
                            }
                            final CommandTaskerValues cwav = (CommandTaskerValues) cr.getVariableData();
                            taskName = cwav.getTaskName();
                            if (UtilsString.notNaked(taskName)) {
                                taskNames.add(taskName);
                            }
                        } else {
                            if (DEBUG) {
                                MyLog.i(CLS_NAME, "isResolved: false");
                            }
                            taskNames.addAll(new CommandTaskerLocal().getResponse(ctx, voiceData, sl));
                        }
                        if (UtilsList.notNaked(taskNames)) {
                            if (DEBUG) {
                                MyLog.v(CLS_NAME, "taskNames size: " + taskNames.size());
                            }
                            final ArrayList<String> taskNameList = new ArrayList<>(taskList.size());
                            for (final TaskerTask taskerTask : taskList) {
                                taskNameList.add(taskerTask.getTaskName());
                            }
                            final AlgorithmicResolver resolver = new AlgorithmicResolver(ctx, Algorithm.getAlgorithms(ctx, sl), sl.getLocale(), taskNames, taskNameList, AlgorithmicResolver.THREADS_TIMEOUT_500, false);
                            final AlgorithmicContainer container = resolver.resolve();
                            if (container != null) {
                                final boolean exactMatch = container.isExactMatch();
                                if (DEBUG) {
                                    MyLog.d(CLS_NAME, "container exactMatch: " + exactMatch);
                                    MyLog.d(CLS_NAME, "container getInput: " + container.getInput());
                                    MyLog.d(CLS_NAME, "container getGenericMatch: " + container.getGenericMatch());
                                    MyLog.d(CLS_NAME, "container getAlgorithm: " + container.getAlgorithm().name());
                                    MyLog.d(CLS_NAME, "container getScore: " + container.getScore());
                                    MyLog.d(CLS_NAME, "container getParentPosition: " + container.getParentPosition());
                                    MyLog.d(CLS_NAME, "container getVariableData: " + container.getVariableData());
                                }
                                TaskerTask taskerTask = null;
                                try {
                                    taskerTask = taskList.get(container.getParentPosition());
                                } catch (final IndexOutOfBoundsException e) {
                                    if (DEBUG) {
                                        MyLog.w(CLS_NAME, "taskList IndexOutOfBoundsException");
                                        e.printStackTrace();
                                    }
                                }
                                if (taskerTask != null) {
                                    if (DEBUG) {
                                        MyLog.d(CLS_NAME, "taskerTask getProjectName: " + taskerTask.getProjectName());
                                        MyLog.d(CLS_NAME, "taskerTask getTaskName: " + taskerTask.getTaskName());
                                    }
                                    if (taskerHelper.executeTask(ctx, taskerTask.getTaskName())) {
                                        if (SPH.getAnnounceTasker(ctx)) {
                                            outcome.setUtterance(PersonalityResponse.getTaskerTaskExecutedResponse(ctx, sl, taskerTask.getTaskName()));
                                        } else {
                                            if (DEBUG) {
                                                MyLog.w(CLS_NAME, "taskerTask don't announce");
                                            }
                                            outcome.setUtterance(SaiyRequestParams.SILENCE);
                                        }
                                        outcome.setOutcome(Outcome.SUCCESS);
                                    } else {
                                        if (DEBUG) {
                                            MyLog.w(CLS_NAME, "taskerTask failed to execute");
                                        }
                                        outcome.setUtterance(PersonalityResponse.getTaskerTaskFailedResponse(ctx, sl, taskerTask.getTaskName()));
                                        outcome.setOutcome(Outcome.FAILURE);
                                        return returnOutcome(outcome);
                                    }
                                } else {
                                    if (DEBUG) {
                                        MyLog.w(CLS_NAME, "index out of bounds");
                                    }
                                    outcome.setUtterance(PersonalityResponse.getTaskerTaskNotMatchedResponse(ctx, sl));
                                    outcome.setOutcome(Outcome.FAILURE);
                                    return returnOutcome(outcome);
                                }
                            } else {
                                if (DEBUG) {
                                    MyLog.w(CLS_NAME, "failed to find a match");
                                }
                                outcome.setUtterance(PersonalityResponse.getTaskerTaskNotMatchedResponse(ctx, sl));
                                outcome.setOutcome(Outcome.FAILURE);
                                return returnOutcome(outcome);
                            }
                        } else {
                            if (DEBUG) {
                                MyLog.w(CLS_NAME, "empty task name request");
                            }
                            outcome.setUtterance(PersonalityResponse.getTaskerTaskNotMatchedResponse(ctx, sl));
                            outcome.setOutcome(Outcome.FAILURE);
                            return returnOutcome(outcome);
                        }
                    } else {
                        if (DEBUG) {
                            MyLog.w(CLS_NAME, "no tasks");
                        }
                        outcome.setUtterance(PersonalityResponse.getTaskerNoTasksResponse(ctx, sl));
                        outcome.setOutcome(Outcome.FAILURE);
                        return returnOutcome(outcome);
                    }
                } else {
                    if (DEBUG) {
                        MyLog.w(CLS_NAME, "no receiver");
                    }
                    final Bundle bundle = new Bundle();
                    bundle.putInt(ActivityHome.FRAGMENT_INDEX, ActivityHome.INDEX_FRAGMENT_BUGS);
                    ExecuteIntent.saiyActivity(ctx, ActivityHome.class, bundle, true);
                    outcome.setUtterance(PersonalityResponse.getTaskerInstallOrderResponse(ctx, sl));
                    outcome.setOutcome(Outcome.FAILURE);
                    return returnOutcome(outcome);
                }
            } else {
                if (DEBUG) {
                    MyLog.w(CLS_NAME, "remote access error");
                }
                taskerHelper.showTaskerExternalAccess(ctx);
                outcome.setUtterance(PersonalityResponse.getTaskerExternalAccessResponse(ctx, sl));
                outcome.setOutcome(Outcome.FAILURE);
                return returnOutcome(outcome);
            }
        } else {
            if (DEBUG) {
                MyLog.w(CLS_NAME, "tasker disabled");
            }
            UtilsApplication.launchAppFromPackageName(ctx, taskerPackage);
            outcome.setUtterance(PersonalityResponse.getTaskerDisabledResponse(ctx, sl));
            outcome.setOutcome(Outcome.FAILURE);
            return returnOutcome(outcome);
        }
    } else {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "tasker not installed");
        }
        Install.showInstallLink(ctx, Installed.PACKAGE_TASKER_MARKET);
        outcome.setUtterance(PersonalityResponse.getTaskerInstallResponse(ctx, sl));
        outcome.setOutcome(Outcome.FAILURE);
        return returnOutcome(outcome);
    }
    return returnOutcome(outcome);
}
Also used : TaskerHelper(ai.saiy.android.thirdparty.tasker.TaskerHelper) Bundle(android.os.Bundle) TaskerTask(ai.saiy.android.thirdparty.tasker.TaskerTask) ArrayList(java.util.ArrayList) UtilsString(ai.saiy.android.utils.UtilsString) AlgorithmicContainer(ai.saiy.android.nlu.local.AlgorithmicContainer) Outcome(ai.saiy.android.processing.Outcome) AlgorithmicResolver(ai.saiy.android.nlu.local.AlgorithmicResolver)

Aggregations

TaskerHelper (ai.saiy.android.thirdparty.tasker.TaskerHelper)2 ArrayList (java.util.ArrayList)2 AlgorithmicContainer (ai.saiy.android.nlu.local.AlgorithmicContainer)1 AlgorithmicResolver (ai.saiy.android.nlu.local.AlgorithmicResolver)1 Outcome (ai.saiy.android.processing.Outcome)1 TaskerTask (ai.saiy.android.thirdparty.tasker.TaskerTask)1 UtilsString (ai.saiy.android.utils.UtilsString)1 DialogInterface (android.content.DialogInterface)1 Bundle (android.os.Bundle)1 NonNull (android.support.annotation.NonNull)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 TextView (android.widget.TextView)1 DialogAction (com.afollestad.materialdialogs.DialogAction)1 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)1