Search in sources :

Example 1 with ProfileItem

use of ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProfileItem in project Saiy-PS by brandall76.

the class FetchIDProfile method getProfile.

/**
 * Method to get an enrollment id.
 *
 * @return an {@link Pair} of which the first parameter will denote success and the second an
 * {@link ProfileItem} object. If the request was unsuccessful,
 * the second parameter may be null.
 */
public Pair<Boolean, ProfileItem> getProfile() {
    if (DEBUG) {
        MyLog.i(CLS_NAME, "getProfile");
    }
    final long then = System.nanoTime();
    String url = null;
    try {
        url = FETCH_URL + URLEncoder.encode(profileId, ENCODING);
    } catch (final UnsupportedEncodingException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "getProfile: UnsupportedEncodingException");
            e.printStackTrace();
        }
    }
    final RequestFuture<JSONObject> future = RequestFuture.newFuture();
    final RequestQueue queue = Volley.newRequestQueue(mContext);
    queue.start();
    final JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, url, null, future, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(final VolleyError error) {
            if (DEBUG) {
                MyLog.w(CLS_NAME, "onErrorResponse: " + error.toString());
                FetchIDProfile.this.verboseError(error);
            }
            queue.stop();
        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            final Map<String, String> params = new HashMap<>();
            params.put(CHARSET, ENCODING);
            params.put(JSON_HEADER_ACCEPT, JSON_HEADER_VALUE_ACCEPT);
            params.put(OCP_SUBSCRIPTION_KEY_HEADER, apiKey);
            return params;
        }
    };
    jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    queue.add(jsonObjReq);
    JSONObject response = null;
    try {
        response = future.get(THREAD_TIMEOUT, TimeUnit.SECONDS);
    } catch (final InterruptedException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "getProfile: InterruptedException");
            e.printStackTrace();
        }
    } catch (final ExecutionException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "getProfile: ExecutionException");
            e.printStackTrace();
        }
    } catch (final TimeoutException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "getProfile: TimeoutException");
            e.printStackTrace();
        }
    } finally {
        queue.stop();
    }
    if (DEBUG) {
        MyLog.getElapsed(CLS_NAME, then);
    }
    if (response != null) {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "response: " + response.toString());
        }
        final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        final ProfileItem profileItem = gson.fromJson(response.toString(), ProfileItem.class);
        if (DEBUG) {
            MyLog.i(CLS_NAME, "response: getId: " + profileItem.getId());
        }
        return new Pair<>(true, profileItem);
    } else {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "response: failed");
        }
        return new Pair<>(false, null);
    }
}
Also used : VolleyError(com.android.volley.VolleyError) ProfileItem(ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProfileItem) HashMap(java.util.HashMap) GsonBuilder(com.google.gson.GsonBuilder) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DefaultRetryPolicy(com.android.volley.DefaultRetryPolicy) Gson(com.google.gson.Gson) Response(com.android.volley.Response) NetworkResponse(com.android.volley.NetworkResponse) JSONObject(org.json.JSONObject) RequestQueue(com.android.volley.RequestQueue) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) Pair(android.util.Pair)

Example 2 with ProfileItem

use of ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProfileItem in project Saiy-PS by brandall76.

the class ListIDProfiles method getProfiles.

/**
 * Method to get all enrollment profile information.
 *
 * @return an {@link Pair} of which the first parameter will denote success and the second an
 * {@link ProfileList} object, containing the list of profiles. If the request was unsuccessful,
 * the second parameter may be null.
 */
public Pair<Boolean, ProfileList> getProfiles() {
    if (DEBUG) {
        MyLog.i(CLS_NAME, "getProfiles");
    }
    final long then = System.nanoTime();
    final RequestFuture<String> future = RequestFuture.newFuture();
    final RequestQueue queue = Volley.newRequestQueue(mContext);
    queue.start();
    final StringRequest request = new StringRequest(Request.Method.GET, LIST_URL, future, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(final VolleyError error) {
            if (DEBUG) {
                MyLog.w(CLS_NAME, "onErrorResponse: " + error.toString());
                ListIDProfiles.this.verboseError(error);
            }
            queue.stop();
        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            final Map<String, String> params = new HashMap<>();
            params.put(CHARSET, ENCODING);
            params.put(OCP_SUBSCRIPTION_KEY_HEADER, apiKey);
            return params;
        }
    };
    request.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * OTT_MULTIPLIER, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    queue.add(request);
    String response = null;
    try {
        response = future.get(THREAD_TIMEOUT, TimeUnit.SECONDS);
    } catch (final InterruptedException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "getProfiles: InterruptedException");
            e.printStackTrace();
        }
    } catch (final ExecutionException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "getProfiles: ExecutionException");
            e.printStackTrace();
        }
    } catch (final TimeoutException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "getProfiles: TimeoutException");
            e.printStackTrace();
        }
    } finally {
        queue.stop();
    }
    if (DEBUG) {
        MyLog.getElapsed(CLS_NAME, then);
    }
    if (response != null) {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "response: " + response);
        }
        final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        final Type type = new TypeToken<List<ProfileItem>>() {
        }.getType();
        final ProfileList profileList = new ProfileList(gson.<List<ProfileItem>>fromJson(response, type));
        if (DEBUG) {
            MyLog.i(CLS_NAME, "onResponse: profileList size: " + profileList.getItems().size());
        }
        return new Pair<>(true, profileList);
    } else {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "response: failed");
        }
        return new Pair<>(false, null);
    }
}
Also used : VolleyError(com.android.volley.VolleyError) ProfileItem(ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProfileItem) HashMap(java.util.HashMap) GsonBuilder(com.google.gson.GsonBuilder) StringRequest(com.android.volley.toolbox.StringRequest) DefaultRetryPolicy(com.android.volley.DefaultRetryPolicy) Gson(com.google.gson.Gson) Response(com.android.volley.Response) NetworkResponse(com.android.volley.NetworkResponse) Type(java.lang.reflect.Type) RequestQueue(com.android.volley.RequestQueue) ProfileList(ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProfileList) List(java.util.List) ProfileList(ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProfileList) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) Pair(android.util.Pair)

Example 3 with ProfileItem

use of ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProfileItem in project Saiy-PS by brandall76.

the class CommandVocalRecognition method getResponse.

/**
 * Resolve the required command returning an {@link Outcome} object
 *
 * @param ctx the application context
 * @param sl  the {@link SupportedLanguage} we are using to analyse the voice data.
 * @return {@link Outcome} containing everything we need to respond to the command.
 */
public Outcome getResponse(@NonNull final Context ctx, @NonNull final SupportedLanguage sl) {
    final long then = System.nanoTime();
    final Outcome outcome = new Outcome();
    outcome.setQubit(new Qubit());
    final SaiyAccountList saiyAccountList = SaiyAccountHelper.getAccounts(ctx);
    if (saiyAccountList != null && saiyAccountList.size() > 0) {
        if (DEBUG) {
            MyLog.v(CLS_NAME, "saiyAccountList.size: " + saiyAccountList.size());
        }
        switch(saiyAccountList.size()) {
            case 1:
            default:
                final SaiyAccount saiyAccount = saiyAccountList.getSaiyAccountList().get(0);
                if (saiyAccount != null) {
                    final ProfileItem profileItem = saiyAccount.getProfileItem();
                    if (profileItem != null) {
                        final String profileId = profileItem.getId();
                        if (UtilsString.notNaked(profileId)) {
                            if (DEBUG) {
                                MyLog.d(CLS_NAME, "profileId: " + profileId);
                            }
                            final Speaker.Status status = Speaker.Status.getStatus(profileItem.getStatus());
                            if (DEBUG) {
                                MyLog.d(CLS_NAME, "status: " + status.name());
                            }
                            switch(status) {
                                case SUCCEEDED:
                                    outcome.setUtterance(SaiyResourcesHelper.getStringResource(ctx, sl, R.string.speech_enroll_instructions_15));
                                    outcome.setAction(LocalRequest.ACTION_SPEAK_LISTEN);
                                    outcome.setOutcome(Outcome.SUCCESS);
                                    outcome.setExtra(profileId);
                                    break;
                                default:
                                    if (DEBUG) {
                                        MyLog.w(CLS_NAME, "enrollment status");
                                    }
                                    outcome.setOutcome(Outcome.FAILURE);
                                    outcome.setUtterance(SaiyResourcesHelper.getStringResource(ctx, sl, R.string.error_vi_status));
                                    break;
                            }
                        } else {
                            if (DEBUG) {
                                MyLog.w(CLS_NAME, "profile id naked");
                            }
                            outcome.setOutcome(Outcome.FAILURE);
                            outcome.setUtterance(SaiyResourcesHelper.getStringResource(ctx, sl, R.string.error_vi_status));
                        }
                    } else {
                        if (DEBUG) {
                            MyLog.w(CLS_NAME, "profile item null");
                        }
                        outcome.setOutcome(Outcome.FAILURE);
                        outcome.setUtterance(SaiyResourcesHelper.getStringResource(ctx, sl, R.string.error_vi_status));
                    }
                } else {
                    if (DEBUG) {
                        MyLog.w(CLS_NAME, "account null");
                    }
                    outcome.setOutcome(Outcome.FAILURE);
                    outcome.setUtterance(SaiyResourcesHelper.getStringResource(ctx, sl, R.string.error_vi_no_account));
                }
                break;
        }
    } else {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "no accounts");
        }
        outcome.setOutcome(Outcome.FAILURE);
        outcome.setUtterance(SaiyResourcesHelper.getStringResource(ctx, sl, R.string.error_vi_no_account));
    }
    if (DEBUG) {
        MyLog.getElapsed(CLS_NAME, then);
    }
    return outcome;
}
Also used : ProfileItem(ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProfileItem) Qubit(ai.saiy.android.processing.Qubit) SaiyAccountList(ai.saiy.android.user.SaiyAccountList) Outcome(ai.saiy.android.processing.Outcome) SaiyAccount(ai.saiy.android.user.SaiyAccount) UtilsString(ai.saiy.android.utils.UtilsString) Speaker(ai.saiy.android.cognitive.identity.provider.microsoft.Speaker)

Example 4 with ProfileItem

use of ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProfileItem in project Saiy-PS by brandall76.

the class SaiyAccountHelper method deleteAccount.

/**
 * Delete a current {@link SaiyAccount}
 *
 * @param ctx         the application context
 * @param accountName the account name
 * @param accountId   the account id
 */
public static boolean deleteAccount(@NonNull final Context ctx, @Nullable final String accountName, @Nullable final String accountId) {
    if (DEBUG) {
        MyLog.i(CLS_NAME, "deleteAccount");
    }
    if (haveAccounts(ctx)) {
        final SaiyAccountList saiyAccountList = getAccountList(ctx);
        final List<SaiyAccount> accountList = saiyAccountList.getSaiyAccountList();
        if (DEBUG) {
            MyLog.i(CLS_NAME, "deleteAccount have: " + accountList.size());
        }
        final ListIterator<SaiyAccount> itr = accountList.listIterator();
        SaiyAccount account;
        ProfileItem profileItem;
        String profileId;
        while (itr.hasNext()) {
            account = itr.next();
            if (accountName != null && accountId != null) {
                if (account.getAccountName().matches(Pattern.quote(accountName)) || (UtilsString.notNaked(account.getAccountId()) && account.getAccountId().matches(Pattern.quote(accountId)))) {
                    if (account.getProfileItem() != null) {
                        profileItem = account.getProfileItem();
                        profileId = profileItem.getId();
                        removeProfile(ctx, profileId);
                    }
                    itr.remove();
                }
            } else if (accountName != null) {
                if (account.getAccountName().matches(Pattern.quote(accountName))) {
                    if (account.getProfileItem() != null) {
                        profileItem = account.getProfileItem();
                        profileId = profileItem.getId();
                        removeProfile(ctx, profileId);
                    }
                    itr.remove();
                }
            } else {
                if (accountId != null && UtilsString.notNaked(account.getAccountId())) {
                    if (account.getAccountId().matches(Pattern.quote(accountId))) {
                        if (account.getProfileItem() != null) {
                            profileItem = account.getProfileItem();
                            profileId = profileItem.getId();
                            removeProfile(ctx, profileId);
                        }
                        itr.remove();
                    }
                }
            }
        }
        if (DEBUG) {
            MyLog.i(CLS_NAME, "deleteAccount post have: " + accountList.size());
        }
        return saveSaiyAccountList(ctx, new SaiyAccountList(accountList));
    }
    return true;
}
Also used : ProfileItem(ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProfileItem) UtilsString(ai.saiy.android.utils.UtilsString)

Example 5 with ProfileItem

use of ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProfileItem in project Saiy-PS by brandall76.

the class SaiyAccountHelper method debugAccountList.

/**
 * Verbosely debug all accounts
 *
 * @param ctx the application context
 */
private static void debugAccountList(@NonNull final Context ctx) {
    if (DEBUG) {
        MyLog.i(CLS_NAME, "debugAccountList");
        if (haveAccounts(ctx)) {
            final SaiyAccountList accountList = getAccountList(ctx);
            for (final SaiyAccount account : accountList.getSaiyAccountList()) {
                final ProfileItem item = account.getProfileItem();
                MyLog.i(CLS_NAME, "debugAccountList getAccountId: " + account.getAccountId());
                MyLog.i(CLS_NAME, "debugAccountList getAccountName: " + account.getAccountName());
                MyLog.i(CLS_NAME, "debugAccountList getPseudonym: " + account.getPseudonym());
                if (item != null) {
                    MyLog.i(CLS_NAME, "debugAccountList getId: " + item.getId());
                    MyLog.i(CLS_NAME, "debugAccountList getStatus: " + item.getStatus());
                    MyLog.i(CLS_NAME, "debugAccountList getSpeechTime: " + item.getSpeechTime());
                    MyLog.i(CLS_NAME, "debugAccountList getRemainingSpeechTime: " + item.getRemainingSpeechTime());
                    MyLog.i(CLS_NAME, "debugAccountList getLastAction: " + item.getLastAction());
                    MyLog.i(CLS_NAME, "debugAccountList getCreated: " + item.getCreated());
                } else {
                    MyLog.w(CLS_NAME, "debugAccountList: getProfileItem null");
                }
            }
        } else {
            MyLog.w(CLS_NAME, "debugAccountList: no account");
        }
    }
}
Also used : ProfileItem(ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProfileItem)

Aggregations

ProfileItem (ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProfileItem)6 UtilsString (ai.saiy.android.utils.UtilsString)3 Pair (android.util.Pair)2 DefaultRetryPolicy (com.android.volley.DefaultRetryPolicy)2 NetworkResponse (com.android.volley.NetworkResponse)2 RequestQueue (com.android.volley.RequestQueue)2 Response (com.android.volley.Response)2 VolleyError (com.android.volley.VolleyError)2 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 HashMap (java.util.HashMap)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 Speaker (ai.saiy.android.cognitive.identity.provider.microsoft.Speaker)1 ProfileList (ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProfileList)1 Outcome (ai.saiy.android.processing.Outcome)1 Qubit (ai.saiy.android.processing.Qubit)1 SaiyAccount (ai.saiy.android.user.SaiyAccount)1 SaiyAccountList (ai.saiy.android.user.SaiyAccountList)1 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)1