Java Code Examples for android.app.Application#getSharedPreferences()

The following examples show how to use android.app.Application#getSharedPreferences() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BackupViewModel.java    From SAI with GNU General Public License v3.0 6 votes vote down vote up
public BackupViewModel(@NonNull Application application) {
    super(application);

    mFilterPrefs = application.getSharedPreferences("backup_filter", Context.MODE_PRIVATE);

    BackupPackagesFilterConfig filterConfig = new BackupPackagesFilterConfig(mFilterPrefs);
    mBackupFilterConfig.setValue(filterConfig);
    mComplexFilterConfig = filterConfig.toComplexFilterConfig(application);

    mPackagesLiveData.setValue(new ArrayList<>());

    mBackupManager = DefaultBackupManager.getInstance(getApplication());
    mBackupRepoPackagesObserver = (packages) -> search(mCurrentSearchQuery);
    mBackupManager.getApps().observeForever(mBackupRepoPackagesObserver);

    mLiveFilterApplier.asLiveData().observeForever(mLiveFilterObserver);
    mBackupFilterConfig.setValue(new BackupPackagesFilterConfig(mComplexFilterConfig));
}
 
Example 2
Source File: Tracker.java    From Tracker with MIT License 6 votes vote down vote up
public void init(Application context, TrackerConfiguration config) {
	if (config == null) {
		throw new IllegalArgumentException("config can't be null");
	}

	isInit = true;
	this.context = context;
	setTrackerConfig(config);
	preferences = context.getSharedPreferences(context.getPackageName(), MODE_PRIVATE);
	if (preferences.getBoolean(KEY_IS_NEW_DEVICE, true) && !TextUtils.isEmpty(config.getNewDeviceUrl())) {
		submitDeviceInfo();
	}
	context.registerActivityLifecycleCallbacks(new ActivityLifecycleListener());
	if (config.getUploadCategory() == UPLOAD_CATEGORY.REAL_TIME) {
		UploadEventService.enter(context, config.getHostName(), config.getHostPort(), null);
	} else {
		if (!requestConfig) {
			startRequestConfig();
		}
	}
}
 
Example 3
Source File: SharedPrefsTest.java    From Android-NoSql with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    final Application application = RuntimeEnvironment.application;
    sharedPreferences = application.getSharedPreferences("test", Context.MODE_PRIVATE);
    sharedPreferences.edit().clear().apply();

    //dataSaver = new SharedPreferencesDataSaver(sharedPreferences);
    dataSaver = new PaperDataSaver(application);
    dataSaver = spy(dataSaver);

    AndroidNoSql.initWith(
            dataSaver
    );
    noSql = NoSql.getInstance();
    noSql.reset();
}
 
Example 4
Source File: SmoothieApplicationModuleTest.java    From toothpick with Apache License 2.0 6 votes vote down vote up
@Test
public void testModule_shouldReturnNamedSharedPreferences() throws Exception {
  // GIVEN
  Application application = ApplicationProvider.getApplicationContext();

  String sharedPreferencesName = "test";
  String itemKey = "isValid";
  SharedPreferences sharedPreferences =
      application.getSharedPreferences(sharedPreferencesName, Context.MODE_PRIVATE);
  sharedPreferences.edit().putBoolean(itemKey, true).commit();

  Scope appScope = Toothpick.openScope(application);
  appScope.installModules(new SmoothieApplicationModule(application, sharedPreferencesName));

  // WHEN
  SharedPreferences sharedPreferencesFromScope = appScope.getInstance(SharedPreferences.class);

  // THEN
  assertThat(sharedPreferencesFromScope.getBoolean(itemKey, false), is(true));
}
 
Example 5
Source File: SharedPreferencesUtils.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method for getting a boolean value from the apps stored preferences.
 *
 * @param preference the preference key.
 * @param defaultValue the default value to return if the key is not found.
 * @return the value stored or the default if the stored value is not found.
 */
public boolean getAndSetBooleanPreference(String preference, boolean defaultValue) {
  Application application = (Application) firebaseApp.getApplicationContext();
  SharedPreferences preferences =
      application.getSharedPreferences(PREFERENCES_PACKAGE_NAME, Context.MODE_PRIVATE);

  // Value set at runtime overrides anything else, but default to defaultValue.
  if (preferences.contains(preference)) {
    boolean result = preferences.getBoolean(preference, defaultValue);
    return result;
  }
  // No preferences set yet - use and set defaultValue.
  setBooleanPreference(preference, defaultValue);
  return defaultValue;
}
 
Example 6
Source File: SharedPreferencesUtils.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method for getting a boolean value from the apps stored preferences.
 *
 * @param preference the preference key.
 * @param defaultValue the default value to return if the key is not found.
 * @return the value stored or the default if the stored value is not found.
 */
public boolean getBooleanPreference(String preference, boolean defaultValue) {
  Application application = (Application) firebaseApp.getApplicationContext();
  SharedPreferences preferences =
      application.getSharedPreferences(PREFERENCES_PACKAGE_NAME, Context.MODE_PRIVATE);

  // Value set at runtime overrides anything else, but default to defaultValue.
  if (preferences.contains(preference)) {
    boolean result = preferences.getBoolean(preference, defaultValue);
    return result;
  }
  // No preferences set yet - use  defaultValue.
  return defaultValue;
}
 
Example 7
Source File: SharedPreferencesUtils.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method for getting a boolean value from the apps stored preferences.
 *
 * @param preference the preference key.
 * @return whether the preference has been set or not
 */
public boolean isPreferenceSet(String preference) {
  Application application = (Application) firebaseApp.getApplicationContext();
  SharedPreferences preferences =
      application.getSharedPreferences(PREFERENCES_PACKAGE_NAME, Context.MODE_PRIVATE);

  return preferences.contains(preference);
}
 
Example 8
Source File: SqrlApplication.java    From secure-quick-reliable-login with MIT License 5 votes vote down vote up
/**
 * Saves the provided identity id as the currently active id in the app preferences.
 *
 * @param application The caller's application object.
 */
public static void saveCurrentId(Application application, long newIdentityId) {
    SharedPreferences sharedPref = application.getSharedPreferences(APPS_PREFERENCES, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putLong(CURRENT_ID, newIdentityId);
    editor.apply();
}
 
Example 9
Source File: Utils.java    From ACDD with MIT License 5 votes vote down vote up
/**
 * save OpenAtlas runtime info to sharedPreference
 **/
public static void saveAtlasInfoBySharedPreferences(Application application) {
    Map<String, String> concurrentHashMap = new ConcurrentHashMap<String, String>();
    concurrentHashMap.put(getPackageInfo(application).versionName, "dexopt");
    SharedPreferences sharedPreferences = application.getSharedPreferences(OpenAtlasInternalConstant.OPENATLAS_CONFIGURE, Context.MODE_PRIVATE);
    if (sharedPreferences == null) {
        sharedPreferences = application.getSharedPreferences(OpenAtlasInternalConstant.OPENATLAS_CONFIGURE, Context.MODE_PRIVATE);
    }
    Editor edit = sharedPreferences.edit();
    for (String key : concurrentHashMap.keySet()) {
        edit.putString(key, concurrentHashMap.get(key));
    }
    edit.commit();
}
 
Example 10
Source File: DataModule.java    From u2020-mvp with Apache License 2.0 4 votes vote down vote up
@Provides
@ApplicationScope
SharedPreferences provideSharedPreferences(Application app) {
    return app.getSharedPreferences("u2020", MODE_PRIVATE);
}
 
Example 11
Source File: RNPushNotificationHelper.java    From react-native-push-notification-CE with MIT License 4 votes vote down vote up
public RNPushNotificationHelper(Application context) {
    this.context = context;
    this.scheduledNotificationsPersistence = context.getSharedPreferences(RNPushNotificationHelper.PREFERENCES_KEY, Context.MODE_PRIVATE);
}
 
Example 12
Source File: DataModule.java    From dagger2-example with MIT License 4 votes vote down vote up
@Provides
@Singleton
SharedPreferences provideSharedPreferences(Application app) {
    return app.getSharedPreferences("daggerdemo", Context.MODE_PRIVATE);
}
 
Example 13
Source File: ApplicationModule.java    From DataInspector with Apache License 2.0 4 votes vote down vote up
@Provides @Singleton SharedPreferences provideSharedPreferences(Application app) {
  return app.getSharedPreferences("data-inspector", MODE_PRIVATE);
}
 
Example 14
Source File: ApplicationModule.java    From ViewInspector with Apache License 2.0 4 votes vote down vote up
@Provides @Singleton SharedPreferences provideSharedPreferences(Application app) {
  return app.getSharedPreferences("view-inspector", MODE_PRIVATE);
}
 
Example 15
Source File: RNPushNotificationHelper.java    From react-native-push-notification with MIT License 4 votes vote down vote up
public RNPushNotificationHelper(Application context) {
    this.context = context;
    this.config = new RNPushNotificationConfig(context);
    this.scheduledNotificationsPersistence = context.getSharedPreferences(RNPushNotificationHelper.PREFERENCES_KEY, Context.MODE_PRIVATE);
}
 
Example 16
Source File: PreferencesManager.java    From AndroidArchitecture with Apache License 2.0 4 votes vote down vote up
public PreferencesManager(Application application){
     sharedPreferences = application.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
}
 
Example 17
Source File: ForceUpdate.java    From force-update with Apache License 2.0 4 votes vote down vote up
public ForceUpdate(Application gApplication,
                   boolean gDebug,
                   VersionProvider gForcedVersionProvider, int gForcedVersionInterval,
                   VersionProvider gRecommendedVersionProvider, int gRecommendedVersionInterval,
                   VersionProvider gExcludedVersionListProvider, int gExcludedVersionListInterval,
                   VersionProvider gCurrentVersionProvider,
                   UpdateView gForcedVersionView,
                   UpdateView gRecommendedVersionView,
                   List<Class<?>> gForceUpdateActivities) {

    if(alreadyInstantiated) {
        throw new RuntimeException("ForceUpdate library is already initialized.");
    }

    String permission = "android.permission.INTERNET";
    int res = gApplication.checkCallingOrSelfPermission(permission);
    if(res != PackageManager.PERMISSION_GRANTED) {
        throw new RuntimeException("Internet permission is necessary for version checks.");
    }

    if(!gDebug && (gForcedVersionInterval < 60 || gRecommendedVersionInterval < 60 || gExcludedVersionListInterval < 60)) {
        throw new RuntimeException("Minimal fetch interval is 60s");
    }

    application = gApplication;

    minAllowedVersionProvider = gForcedVersionProvider;

    minAllowedVersionInterval = gForcedVersionInterval;

    recommendedVersionProvider = gRecommendedVersionProvider;

    recommendedVersionInterval = gRecommendedVersionInterval;

    excludedVersionProvider  = gExcludedVersionListProvider;

    excludedVersionInterval = gExcludedVersionListInterval;

    currentVersionProvider = gCurrentVersionProvider;

    forcedUpdateView = gForcedVersionView;

    recommendedUpdateView = gRecommendedVersionView;

    forceUpdateActivities = gForceUpdateActivities;

    sharedPreferences = gApplication.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);

    alreadyInstantiated = true;
}
 
Example 18
Source File: MessageStateStorage.java    From eternity with Apache License 2.0 4 votes vote down vote up
@Inject
MessageStateStorage(Application application) {
  this.preferences = application.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
}
 
Example 19
Source File: AppModule.java    From DaggerAutoInject with Apache License 2.0 4 votes vote down vote up
@Provides
@Singleton
public SharedPreferences providesSharedPreferences(Application application){
    return application.getSharedPreferences("user", Context.MODE_PRIVATE);
}
 
Example 20
Source File: ListRemoteViewsFactory.java    From OmniList with GNU Affero General Public License v3.0 4 votes vote down vote up
ListRemoteViewsFactory(RemoteViewsService remoteViewsService, Application app, Intent intent) {
    this.app = (PalmApp) app;
    appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
    sharedPreferences = app.getSharedPreferences(Constants.PREFS_NAME, Context.MODE_MULTI_PROCESS);
    sharedPreferences.registerOnSharedPreferenceChangeListener(this);
}