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);
}
}
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);
}
}
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;
}
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;
}
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");
}
}
}
Aggregations