下面列出了com.facebook.react.bridge.ReactApplicationContext#getSharedPreferences ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
@Config(sdk = Build.VERSION_CODES.M)
public void testMigrateStorageFromOlder_api23() throws Exception {
// GIVEN:
final ReactApplicationContext context = getRNContext();
final CipherStorage aes = Mockito.mock(CipherStorage.class);
final CipherStorage rsa = Mockito.mock(CipherStorage.class);
when(rsa.getCipherStorageName()).thenReturn("dummy");
final CipherStorage.DecryptionResult decrypted = new CipherStorage.DecryptionResult("user", "password");
final CipherStorage.EncryptionResult encrypted = new CipherStorage.EncryptionResult("user".getBytes(), "password".getBytes(), rsa);
final KeychainModule module = new KeychainModule(context);
final SharedPreferences prefs = context.getSharedPreferences(PrefsStorage.KEYCHAIN_DATA, Context.MODE_PRIVATE);
when(
rsa.encrypt(eq("dummy"), eq("user"), eq("password"), any())
).thenReturn(encrypted);
// WHEN:
module.migrateCipherStorage("dummy", rsa, aes, decrypted);
final String username = prefs.getString(PrefsStorage.getKeyForUsername("dummy"), "");
final String password = prefs.getString(PrefsStorage.getKeyForPassword("dummy"), "");
final String cipherName = prefs.getString(PrefsStorage.getKeyForCipherStorage("dummy"), "");
// THEN:
// delete of key from old storage
// re-store of encrypted data in shared preferences
verify(rsa).encrypt("dummy", "user", "password", SecurityLevel.ANY);
verify(aes).removeKey("dummy");
// Base64.DEFAULT force '\n' char in the end of string
assertThat(username, is("dXNlcg==\n"));
assertThat(password, is("cGFzc3dvcmQ=\n"));
assertThat(cipherName, is("dummy"));
}
public PrefsStorage(ReactApplicationContext reactContext, String service) {
this.prefs = reactContext.getSharedPreferences(getSharedPreferenceName(service), Context.MODE_PRIVATE);
}
public void createMissedCallNotification(ReactApplicationContext context, CallInvite callInvite) {
SharedPreferences sharedPref = context.getSharedPreferences(PREFERENCE_KEY, Context.MODE_PRIVATE);
SharedPreferences.Editor sharedPrefEditor = sharedPref.edit();
/*
* Create a PendingIntent to specify the action when the notification is
* selected in the notification drawer
*/
Intent intent = new Intent(context, getMainActivityClass(context));
intent.setAction(ACTION_MISSED_CALL)
.putExtra(INCOMING_CALL_NOTIFICATION_ID, MISSED_CALLS_NOTIFICATION_ID)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent clearMissedCallsCountIntent = new Intent(ACTION_CLEAR_MISSED_CALLS_COUNT)
.putExtra(INCOMING_CALL_NOTIFICATION_ID, CLEAR_MISSED_CALLS_NOTIFICATION_ID);
PendingIntent clearMissedCallsCountPendingIntent = PendingIntent.getBroadcast(context, 0, clearMissedCallsCountIntent, 0);
/*
* Pass the notification id and call sid to use as an identifier to open the notification
*/
Bundle extras = new Bundle();
extras.putInt(INCOMING_CALL_NOTIFICATION_ID, MISSED_CALLS_NOTIFICATION_ID);
extras.putString(CALL_SID_KEY, callInvite.getCallSid());
extras.putString(NOTIFICATION_TYPE, ACTION_MISSED_CALL);
/*
* Create the notification shown in the notification drawer
*/
NotificationCompat.Builder notification =
new NotificationCompat.Builder(context, VOICE_CHANNEL)
.setGroup(MISSED_CALLS_GROUP)
.setGroupSummary(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setSmallIcon(R.drawable.ic_call_missed_white_24dp)
.setContentTitle("Missed call")
.setContentText(callInvite.getFrom() + " called")
.setAutoCancel(true)
.setShowWhen(true)
.setExtras(extras)
.setDeleteIntent(clearMissedCallsCountPendingIntent)
.setContentIntent(pendingIntent);
int missedCalls = sharedPref.getInt(MISSED_CALLS_GROUP, 0);
missedCalls++;
if (missedCalls == 1) {
inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle("Missed call");
} else {
inboxStyle.setBigContentTitle(String.valueOf(missedCalls) + " missed calls");
}
inboxStyle.addLine("from: " +callInvite.getFrom());
sharedPrefEditor.putInt(MISSED_CALLS_GROUP, missedCalls);
sharedPrefEditor.commit();
notification.setStyle(inboxStyle);
// build notification large icon
Resources res = context.getResources();
int largeIconResId = res.getIdentifier("ic_launcher", "mipmap", context.getPackageName());
Bitmap largeIconBitmap = BitmapFactory.decodeResource(res, largeIconResId);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && largeIconResId != 0) {
notification.setLargeIcon(largeIconBitmap);
}
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(MISSED_CALLS_NOTIFICATION_ID, notification.build());
}
public PrefsStorage(@NonNull final ReactApplicationContext reactContext) {
this.prefs = reactContext.getSharedPreferences(KEYCHAIN_DATA, Context.MODE_PRIVATE);
}