RocketChatCache.java 1.03 KB
Newer Older
1 2 3 4 5
package chat.rocket.android;

import android.content.Context;
import android.content.SharedPreferences;

6 7
import java.util.UUID;

8 9 10 11 12
/**
 * sharedpreference-based cache.
 */
public class RocketChatCache {
  public static final String KEY_SELECTED_SERVER_CONFIG_ID = "selectedServerConfigId";
13
  public static final String KEY_SELECTED_ROOM_ID = "selectedRoomId";
Yusuke Iwaki's avatar
Yusuke Iwaki committed
14

Yusuke Iwaki's avatar
Yusuke Iwaki committed
15
  private static final String KEY_PUSH_ID = "pushId";
16

Yusuke Iwaki's avatar
Yusuke Iwaki committed
17 18 19
  /**
   * get SharedPreference instance for RocketChat application cache.
   */
20 21 22
  public static SharedPreferences get(Context context) {
    return context.getSharedPreferences("cache", Context.MODE_PRIVATE);
  }
23

Yusuke Iwaki's avatar
Yusuke Iwaki committed
24
  public static String getOrCreatePushId(Context context) {
25
    SharedPreferences preferences = get(context);
Yusuke Iwaki's avatar
Yusuke Iwaki committed
26
    if (!preferences.contains(KEY_PUSH_ID)) {
27
      // generates one and save
Yusuke Iwaki's avatar
Yusuke Iwaki committed
28 29 30 31 32
      String newId = UUID.randomUUID().toString().replace("-", "");
      preferences.edit()
          .putString(KEY_PUSH_ID, newId)
          .apply();
      return newId;
33
    }
Yusuke Iwaki's avatar
Yusuke Iwaki committed
34
    return preferences.getString(KEY_PUSH_ID, null);
35
  }
36
}