Use constants for preference keys

This commit is contained in:
Jannis Mattheis
2018-05-20 15:13:20 +02:00
parent a9ed6a2439
commit 9c734563fd

View File

@@ -28,7 +28,9 @@ import okhttp3.WebSocketListener;
public class PushService extends Service { public class PushService extends Service {
private final OkHttpClient client = new OkHttpClient.Builder().readTimeout(0, TimeUnit.MILLISECONDS).build(); private final OkHttpClient client = new OkHttpClient.Builder().readTimeout(0, TimeUnit.MILLISECONDS).build();
public Handler handler = null; private static final String TOKEN = "@global:token";
private static final String URL = "@global:url";
private Handler handler = null;
private WebSocket socket = null; private WebSocket socket = null;
private Gson gson = null; private Gson gson = null;
@@ -62,9 +64,7 @@ public class PushService extends Service {
handler = new Handler(); handler = new Handler();
gson = new Gson(); gson = new Gson();
start(); start();
// https://github.com/sriraman/react-native-shared-preferences/issues/12 for why wit_player_shared_preferences appPreferences().registerOnSharedPreferenceChangeListener(listener);
final SharedPreferences preferences = this.getSharedPreferences("wit_player_shared_preferences", Context.MODE_PRIVATE);
preferences.registerOnSharedPreferenceChangeListener(listener);
} }
private void foregroundNotification(String message) { private void foregroundNotification(String message) {
@@ -84,10 +84,8 @@ public class PushService extends Service {
} }
private void start() { private void start() {
final SharedPreferences preferences = this.getSharedPreferences("wit_player_shared_preferences", Context.MODE_PRIVATE); String url = appPreferences().getString(URL, null);
String url = preferences.getString("@global:url", null); String token = appPreferences().getString(TOKEN, null);
String token = preferences.getString("@global:token", null);
if (url == null || token == null) { if (url == null || token == null) {
foregroundNotification("login required"); foregroundNotification("login required");
@@ -127,8 +125,8 @@ public class PushService extends Service {
foregroundNotification("Error: " + t.getMessage()); foregroundNotification("Error: " + t.getMessage());
Log.e("WebSocket failure", t); Log.e("WebSocket failure", t);
if (response != null && response.code() >= 400 && response.code() <= 499) { if (response != null && response.code() >= 400 && response.code() <= 499) {
preferences.edit().remove("@global:token").apply();
showNotification(-2, "WebSocket Bad-Request", "Could not connect: " + response.message()); showNotification(-2, "WebSocket Bad-Request", "Could not connect: " + response.message());
appPreferences().edit().remove(TOKEN).apply();
return; return;
} }
@@ -164,6 +162,11 @@ public class PushService extends Service {
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(id, b.build()); notificationManager.notify(id, b.build());
} }
private SharedPreferences appPreferences() {
// https://github.com/sriraman/react-native-shared-preferences/issues/12 for why wit_player_shared_preferences
return this.getSharedPreferences("wit_player_shared_preferences", Context.MODE_PRIVATE);
}
@Override @Override
public void onDestroy() { public void onDestroy() {