Skip to content

Instantly share code, notes, and snippets.

@RamitPahwa
Last active July 27, 2021 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RamitPahwa/8592a307e6a69f028e3556ecf71afc8b to your computer and use it in GitHub Desktop.
Save RamitPahwa/8592a307e6a69f028e3556ecf71afc8b to your computer and use it in GitHub Desktop.
public static final String AFW_RESTRICION_PREFERENCES = "com.adobe.reader.restrictions";
public static final String ENABLE_HELLO_PREFS_KEY = "enablePrintingKey";
private static final String KEY_CAN_SAY_HELLO = "can_say_hello";
private boolean getPreferenceValue(Context context, Bundle restrictions, String keyValue, @BoolRes int id)
{
boolean enableKey = context.getResources().getBoolean(id);
if (restrictions != null && restrictions.containsKey(keyValue))
{
enableKey = restrictions.getBoolean(keyValue);
}
return enableKey;
}
private boolean isEnabledInAFW(Context context, String preferenceKey, @BoolRes int id)
{
boolean isEnabled = false;
if(context != null)
{
SharedPreferences preferences = context.getSharedPreferences(AFW_RESTRICION_PREFERENCES, Context.MODE_PRIVATE);
isEnabled = preferences.getBoolean(preferenceKey, context.getResources().getBoolean(id));
}
return isEnabled;
}
private void resolveRestrictions()
{
RestrictionsManager manager = (RestrictionsManager) getActivity().getSystemService(Context.RESTRICTIONS_SERVICE);
Bundle restrictions = manager.getApplicationRestrictions();
List<RestrictionEntry> entries = manager.getManifestRestrictions(getActivity().getApplicationContext().getPackageName());
SharedPreferences preferences = getContext().getSharedPreferences(AFW_RESTRICION_PREFERENCES, Context.MODE_PRIVATE);
for (RestrictionEntry entry : entries)
{
String key = entry.getKey();
String preferencesKey = null;
boolean preferencesValue = false;
Log.d(TAG, "key: " + key);
if (key.equals(KEY_CAN_SAY_HELLO))
{
preferencesKey = ENABLE_HELLO_PREFS_KEY;
preferencesValue = getPreferenceValue(getContext(), restrictions, KEY_CAN_SAY_HELLO, R.bool.default_can_say_hello);
}
if (preferencesKey != null)
{
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(preferencesKey, preferencesValue);
editor.apply();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment