Java Code Examples for android.app.Activity#getPreferences()

The following examples show how to use android.app.Activity#getPreferences() . These examples are extracted from open source projects. 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
/**
 * Loads from the specified Activity's preferences.
 *
 * @param activity The Activity
 */
public static synchronized void load(Activity activity) {
    SharedPreferences pref = activity.getPreferences(Context.MODE_PRIVATE);
    String defaultJson = m_oGson.toJson(new ArrayList<RobotInfo>());

    String robotInfoJson = pref.getString(ROBOT_INFOS_KEY, defaultJson);
    Type listOfRobotInfoType = new TypeToken<List<RobotInfo>>() {
    }.getType();

    g_cRobotInfos = m_oGson.fromJson(robotInfoJson, listOfRobotInfoType);

    RobotInfo.resolveRobotCount(g_cRobotInfos);

    g_cPrefKeyMap.put(RobotInfo.JOYSTICK_TOPIC_KEY, activity.getString(R.string.prefs_joystick_topic_edittext_key));
    g_cPrefKeyMap.put(RobotInfo.CAMERA_TOPIC_KEY, activity.getString(R.string.prefs_camera_topic_edittext_key));
    g_cPrefKeyMap.put(RobotInfo.LASER_SCAN_TOPIC_KEY, activity.getString(R.string.prefs_laserscan_topic_edittext_key));
    g_cPrefKeyMap.put(RobotInfo.NAVSAT_TOPIC_KEY, activity.getString(R.string.prefs_navsat_topic_edittext_key));
    g_cPrefKeyMap.put(RobotInfo.ODOMETRY_TOPIC_KEY, activity.getString(R.string.prefs_odometry_topic_edittext_key));
    g_cPrefKeyMap.put(RobotInfo.POSE_TOPIC_KEY, activity.getString(R.string.prefs_pose_topic_edittext_key));
    g_cPrefKeyMap.put(RobotInfo.REVERSE_LASER_SCAN_KEY, activity.getString(R.string.prefs_reverse_angle_reading_key));
    g_cPrefKeyMap.put(RobotInfo.INVERT_X_KEY, activity.getString(R.string.prefs_invert_x_axis_key));
    g_cPrefKeyMap.put(RobotInfo.INVERT_Y_KEY, activity.getString(R.string.prefs_invert_y_axis_key));
    g_cPrefKeyMap.put(RobotInfo.INVERT_ANGULAR_VELOCITY_KEY, activity.getString(R.string.prefs_invert_angular_velocity_key));
}
 
Example 2
Source Project: mirror   File: Network.java    License: MIT License 6 votes vote down vote up
/**
 * Loads an access token from shared preferences.
 */
private static AccessToken loadAccessToken(Activity activity, OAuthDataProvider data) {
  // Check if all keys are present.
  SharedPreferences preferences = activity.getPreferences(Context.MODE_PRIVATE);
  String accessTokenKey = getScopedKey(data, KEY_ACCESS_TOKEN);
  String refreshTokenKey = getScopedKey(data, KEY_REFRESH_TOKEN);
  String expiresInKey = getScopedKey(data, KEY_EXPIRES_IN);
  String refreshTimeKey = getScopedKey(data, KEY_REFRESH_TIME);
  if (!preferences.contains(accessTokenKey) || !preferences.contains(refreshTokenKey)
      || !preferences.contains(expiresInKey) || !preferences.contains(refreshTimeKey)) {
    return null;
  }

  // Load the access token data from shared preferences.
  String accessToken = preferences.getString(accessTokenKey, null);
  String refreshToken = preferences.getString(refreshTokenKey, null);
  int expiresIn = preferences.getInt(expiresInKey, 0);
  long refreshTime = preferences.getLong(refreshTimeKey, 0);

  // Create the access token from the data.
  return new AccessToken(accessToken, expiresIn, refreshToken, refreshTime);
}
 
Example 3
public static void save(Activity activity, Mat cameraMatrix, Mat distortionCoefficients) {
    SharedPreferences sharedPref = activity.getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();

    double[] cameraMatrixArray = new double[CAMERA_MATRIX_ROWS * CAMERA_MATRIX_COLS];
    cameraMatrix.get(0,  0, cameraMatrixArray);
    for (int i = 0; i < CAMERA_MATRIX_ROWS; i++) {
        for (int j = 0; j < CAMERA_MATRIX_COLS; j++) {
            Integer id = i * CAMERA_MATRIX_ROWS + j;
            editor.putFloat(id.toString(), (float)cameraMatrixArray[id]);
        }
    }

    double[] distortionCoefficientsArray = new double[DISTORTION_COEFFICIENTS_SIZE];
    distortionCoefficients.get(0, 0, distortionCoefficientsArray);
    int shift = CAMERA_MATRIX_ROWS * CAMERA_MATRIX_COLS;
    for (Integer i = shift; i < DISTORTION_COEFFICIENTS_SIZE + shift; i++) {
        editor.putFloat(i.toString(), (float)distortionCoefficientsArray[i-shift]);
    }

    editor.commit();
    Log.i(TAG, "Saved camera matrix: " + cameraMatrix.dump());
    Log.i(TAG, "Saved distortion coefficients: " + distortionCoefficients.dump());
}
 
Example 4
public static void addToMap(Activity activity, MapView mapView) throws IOException, JSONException {
    /**
     * Deal with SharedPreferences. Use it if we haven't explicitly loaded. Set it if we have.
     */
    SharedPreferences preferences = activity.getPreferences(Context.MODE_PRIVATE);
    if (fpGeoJson == null) {
        String previousFpGeoJsonPath = preferences.getString(PREVIOUS_FP_FILE_PATH, null);
        if (previousFpGeoJsonPath == null) return;
        load(new File(previousFpGeoJsonPath));
    } else {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(PREVIOUS_FP_FILE_PATH, fpGeoJson.getAbsolutePath());
        editor.apply();
    }

    if (atlas == null) return;

    atlas.setActivity(activity);
    atlas.setupMapView(mapView);
}
 
Example 5
public static String checkVersionAndShowWhatsNewIfNeeded(Activity activity) {
	try {
		PackageInfo info = Utils.getPackageInfo(activity);
		SharedPreferences preferences = activity.getPreferences(0); 
		int newVersionCode = info.versionCode;
		int oldVersionCode = preferences.getInt("versionCode", -1);
		if (newVersionCode > oldVersionCode) {
			preferences.edit().putInt("versionCode", newVersionCode).commit();
			showWhatsNew(activity);
		}
		return "v. "+info.versionName;
	} catch(Exception ex) { 
		return "Free";
	}
}
 
Example 6
/**
 * Forget the account name and authToken.  With no account name the app will prompt 
 * the user to select a new account. This method is mostly used for testing purposes. 
 * 
 * @param activity
 */
public static void resetAccountCredential(Activity activity) {
  Log.i(TAG, "Reset credentials");
  SharedPreferences settings = activity.getPreferences(Activity.MODE_PRIVATE);
  SharedPreferences.Editor editor2 = settings.edit();
  editor2.remove(PREF_AUTH_TOKEN);
  editor2.remove(PREF_ACCOUNT_NAME);
  editor2.commit();
}
 
Example 7
/***
 * Checks that application runs first time and write flag at SharedPreferences
 * @return true if 1st time
 */
public static boolean isFirstTime(Activity activity) {
    //TODO Possible bug
    SharedPreferences preferences = activity.getPreferences(MODE_PRIVATE);
    boolean ranBefore = preferences.getBoolean(FIRST_TIME_KEY, false);
    if (!ranBefore) {
        // first time
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(FIRST_TIME_KEY, true);
        editor.apply();
    }
    return !ranBefore;
}
 
Example 8
public static boolean tryLoad(Activity activity, Mat cameraMatrix, Mat distortionCoefficients) {
    SharedPreferences sharedPref = activity.getPreferences(Context.MODE_PRIVATE);
    if (sharedPref.getFloat("0", -1) == -1) {
        Log.i(TAG, "No previous calibration results found");
        return false;
    }

    double[] cameraMatrixArray = new double[CAMERA_MATRIX_ROWS * CAMERA_MATRIX_COLS];
    for (int i = 0; i < CAMERA_MATRIX_ROWS; i++) {
        for (int j = 0; j < CAMERA_MATRIX_COLS; j++) {
            Integer id = i * CAMERA_MATRIX_ROWS + j;
            cameraMatrixArray[id] = sharedPref.getFloat(id.toString(), -1);
        }
    }
    cameraMatrix.put(0, 0, cameraMatrixArray);
    Log.i(TAG, "Loaded camera matrix: " + cameraMatrix.dump());

    double[] distortionCoefficientsArray = new double[DISTORTION_COEFFICIENTS_SIZE];
    int shift = CAMERA_MATRIX_ROWS * CAMERA_MATRIX_COLS;
    for (Integer i = shift; i < DISTORTION_COEFFICIENTS_SIZE + shift; i++) {
        distortionCoefficientsArray[i - shift] = sharedPref.getFloat(i.toString(), -1);
    }
    distortionCoefficients.put(0, 0, distortionCoefficientsArray);
    Log.i(TAG, "Loaded distortion coefficients: " + distortionCoefficients.dump());

    return true;
}
 
Example 9
public static String getSharedPreferences(PreferenceKey key, Activity activity){
	
	String result = null;		
	_settings = activity.getPreferences(Context.MODE_PRIVATE);		
	result = _settings.getString(key.toString(), null);
	
	return result;
}
 
Example 10
public Preferences(Activity activity) {
    prefs = activity.getPreferences(Activity.MODE_PRIVATE);
}
 
Example 11
public static boolean setSharedPreferences(PreferenceKey key, String value, Activity activity){
	
	boolean commit = false;
	
	_settings = activity.getPreferences(Context.MODE_PRIVATE);
	
	SharedPreferences.Editor editor = _settings.edit();
	editor.putString(key.toString(), value);
	commit = editor.commit();
	
	return commit;
	
}