/** * @param context The context for the current application * @param obfuscator An obfuscator to be used with preferences. */ public ServerManagedPolicy(Context context, Obfuscator obfuscator) { // Import old values SharedPreferences sp = context.getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE); mPreferences = new PreferenceObfuscator(sp, obfuscator); mLastResponse = Integer.parseInt(mPreferences.getString(PREF_LAST_RESPONSE, Integer.toString(Policy.RETRY))); mValidityTimestamp = Long.parseLong(mPreferences.getString(PREF_VALIDITY_TIMESTAMP, DEFAULT_VALIDITY_TIMESTAMP)); mRetryUntil = Long.parseLong(mPreferences.getString(PREF_RETRY_UNTIL, DEFAULT_RETRY_UNTIL)); mMaxRetries = Long.parseLong(mPreferences.getString(PREF_MAX_RETRIES, DEFAULT_MAX_RETRIES)); mRetryCount = Long.parseLong(mPreferences.getString(PREF_RETRY_COUNT, DEFAULT_RETRY_COUNT)); }
/** * Set the licensing URL that displays a Play Store UI for the user to regain app access. * * @param url the LU string received */ private void setLicensingUrl(String url) { mLicensingUrl = url; mPreferences.putString(PREF_LICENSING_URL, url); }
@Before public void initFixture() { final byte[] SALT = new byte[] { 104, -12, 112, 82, -85, -10, -11, 61, 15, 54, 44, -66, -117, -89, -64, 110, -53, 123, 33 }; // Prepare PreferenceObfuscator instance Context ctx = InstrumentationRegistry.getTargetContext(); sp = ctx.getSharedPreferences(filename, Context.MODE_PRIVATE); String deviceId = Settings.Secure.getString( ctx.getContentResolver(), Settings.Secure.ANDROID_ID); Obfuscator o = new AESObfuscator(SALT, ctx.getPackageName(), deviceId); op = new PreferenceObfuscator(sp, o); // Populate with test data op.putString("testString", "Hello world"); op.commit(); }
/** * We call this to guarantee that we fetch a fresh policy from the server. * This is to be used if the URL is invalid. */ public void resetPolicy() { mPreferences.putString(PREF_LAST_RESPONSE, Integer.toString(Policy.RETRY)); setRetryUntil(DEFAULT_RETRY_UNTIL); setMaxRetries(DEFAULT_MAX_RETRIES); setRetryCount(Long.parseLong(DEFAULT_RETRY_COUNT)); setValidityTimestamp(DEFAULT_VALIDITY_TIMESTAMP); mPreferences.commit(); }
mPreferences.commit();
@Test public void corruptDataRetunsDefaultString() { // Insert non-obfuscated string SharedPreferences.Editor spe = sp.edit(); spe.putString("corruptData", "foo"); spe.commit(); // Read back contents assertEquals("Android rocks", op.getString("corruptdata", "Android rocks")); }
/** * We call this to guarantee that we fetch a fresh policy from the server. * This is to be used if the URL is invalid. */ public void resetPolicy() { mPreferences.putString(PREF_LAST_RESPONSE, Integer.toString(Policy.RETRY)); setRetryUntil(DEFAULT_RETRY_UNTIL); setMaxRetries(DEFAULT_MAX_RETRIES); setRetryCount(Long.parseLong(DEFAULT_RETRY_COUNT)); setValidityTimestamp(DEFAULT_VALIDITY_TIMESTAMP); mPreferences.commit(); }
mPreferences.commit();
@Test public void getString() { assertEquals("Hello world", op.getString("testString", "fail")); }
/** * Set the license URL value (LU) as received from the server and add to preferences. You must * manually call PreferenceObfuscator.commit() to commit these changes to disk. * * @param url the LU string received */ private void setLicensingUrl(String url) { mLicensingUrl = url; mPreferences.putString(PREF_LICENSING_URL, url); }
/** * @param context The context for the current application * @param obfuscator An obfuscator to be used with preferences. */ public APKExpansionPolicy(Context context, Obfuscator obfuscator) { // Import old values SharedPreferences sp = context.getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE); mPreferences = new PreferenceObfuscator(sp, obfuscator); mLastResponse = Integer.parseInt(mPreferences.getString(PREF_LAST_RESPONSE, Integer.toString(Policy.RETRY))); mValidityTimestamp = Long.parseLong(mPreferences.getString(PREF_VALIDITY_TIMESTAMP, DEFAULT_VALIDITY_TIMESTAMP)); mRetryUntil = Long.parseLong(mPreferences.getString(PREF_RETRY_UNTIL, DEFAULT_RETRY_UNTIL)); mMaxRetries = Long.parseLong(mPreferences.getString(PREF_MAX_RETRIES, DEFAULT_MAX_RETRIES)); mRetryCount = Long.parseLong(mPreferences.getString(PREF_RETRY_COUNT, DEFAULT_RETRY_COUNT)); }
/** * We call this to guarantee that we fetch a fresh policy from the server. * This is to be used if the URL is invalid. */ public void resetPolicy() { mPreferences.putString(PREF_LAST_RESPONSE, Integer.toString(Policy.RETRY)); setRetryUntil(DEFAULT_RETRY_UNTIL); setMaxRetries(DEFAULT_MAX_RETRIES); setRetryCount(Long.parseLong(DEFAULT_RETRY_COUNT)); setValidityTimestamp(DEFAULT_VALIDITY_TIMESTAMP); mPreferences.commit(); }
mPreferences.commit();
@Test public void getDefaultString() { assertEquals("Android rocks", op.getString("noExist", "Android rocks")); }
/** * Set the current retry count and add to preferences. You must manually * call PreferenceObfuscator.commit() to commit these changes to disk. * * @param c the new retry count */ private void setRetryCount(long c) { mRetryCount = c; mPreferences.putString(PREF_RETRY_COUNT, Long.toString(c)); }
/** * @param context The context for the current application * @param obfuscator An obfuscator to be used with preferences. */ public APKExpansionPolicy(Context context, Obfuscator obfuscator) { // Import old values SharedPreferences sp = context.getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE); mPreferences = new PreferenceObfuscator(sp, obfuscator); mLastResponse = Integer.parseInt( mPreferences.getString(PREF_LAST_RESPONSE, Integer.toString(Policy.RETRY))); mValidityTimestamp = Long.parseLong(mPreferences.getString(PREF_VALIDITY_TIMESTAMP, DEFAULT_VALIDITY_TIMESTAMP)); mRetryUntil = Long.parseLong(mPreferences.getString(PREF_RETRY_UNTIL, DEFAULT_RETRY_UNTIL)); mMaxRetries = Long.parseLong(mPreferences.getString(PREF_MAX_RETRIES, DEFAULT_MAX_RETRIES)); mRetryCount = Long.parseLong(mPreferences.getString(PREF_RETRY_COUNT, DEFAULT_RETRY_COUNT)); }
/** * We call this to guarantee that we fetch a fresh policy from the server. * This is to be used if the URL is invalid. */ public void resetPolicy() { mPreferences.putString(PREF_LAST_RESPONSE, Integer.toString(Policy.RETRY)); setRetryUntil(DEFAULT_RETRY_UNTIL); setMaxRetries(DEFAULT_MAX_RETRIES); setRetryCount(Long.parseLong(DEFAULT_RETRY_COUNT)); setValidityTimestamp(DEFAULT_VALIDITY_TIMESTAMP); mPreferences.commit(); }
mPreferences.commit();
@Test public void getDefaultNullString() { assertEquals(null, op.getString("noExist", null)); }
/** * Set the current retry count and add to preferences. You must manually * call PreferenceObfuscator.commit() to commit these changes to disk. * * @param c the new retry count */ private void setRetryCount(long c) { mRetryCount = c; mPreferences.putString(PREF_RETRY_COUNT, Long.toString(c)); }