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