Skip to content

Commit

Permalink
Code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp0002 committed Jul 29, 2024
1 parent a003c5b commit 24c333a
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 55 deletions.
17 changes: 17 additions & 0 deletions app/src/main/java/de/raffaelhahn/xadgps_client/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package de.raffaelhahn.xadgps_client;

public class Constants {

public static final String API_URL = "https://appapi.xadgps.com/openapiv4.asmx";
public static final String APP_ID = "25";
public static final String SP_NAME = "de.raffaelhahn.xadgps_client";

public static final String NOTIFY_DEVICES_PREF_KEY = "notifyDevices";

public static final String SP_KEY_OPERATING_MODE = "operating_mode";
public static final String SP_KEY_USERNAME = "username";
public static final String SP_KEY_LOGIN_NAME = "loginName";
public static final String SP_KEY_USER_ID = "userId";
public static final String SP_KEY_DEVICE_ID = "deviceId";

}
30 changes: 16 additions & 14 deletions app/src/main/java/de/raffaelhahn/xadgps_client/LoginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.Locale;

import de.raffaelhahn.xadgps_client.async.AsyncCallback;
import de.raffaelhahn.xadgps_client.async.Constants;
import de.raffaelhahn.xadgps_client.async.LoginAsync;

public class LoginActivity extends AppCompatActivity {
Expand All @@ -42,7 +41,7 @@ private void performLogin() {
LoginAsync loginAsync = new LoginAsync();
loginAsync.paramName = username;
loginAsync.paramPass = password;
loginAsync.paramLoginType = "3";
loginAsync.paramLoginType = OperatingMode.UNKNOWN.numericString;
loginAsync.paramAppID = Constants.APP_ID;
loginAsync.paramLanguage = Locale.getDefault().getLanguage() + "-" + Locale.getDefault().getCountry();

Expand All @@ -51,12 +50,15 @@ private void performLogin() {
@Override
public void received(JSONObject data) throws Exception {
if("0".equals(data.getString("state"))){
if("0".equals(data.getString("loginType"))) {
JSONObject userInfo = data.getJSONObject("userInfo");
saveUserData(userInfo.getString("userName"), userInfo.getString("loginName"), userInfo.getString("userID"));
} else {
JSONObject deviceInfo = data.getJSONObject("deviceInfo");
saveDeviceData(deviceInfo.getString("deviceID"), deviceInfo.getString("deviceName"));
switch(OperatingMode.fromNumericString(data.getString("loginType"))) {
case USER:
JSONObject userInfo = data.getJSONObject("userInfo");
saveUserData(userInfo.getString("userName"), userInfo.getString("loginName"), userInfo.getString("userID"));
break;
case DEVICE:
JSONObject deviceInfo = data.getJSONObject("deviceInfo");
saveDeviceData(deviceInfo.getString("deviceID"), deviceInfo.getString("deviceName"));
break;
}
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
Expand Down Expand Up @@ -85,18 +87,18 @@ public void finished() {
public void saveUserData(String username, String loginName, String userId) {
SharedPreferences preferences = getSharedPreferences(Constants.SP_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("operating_mode", "USER");
editor.putString("username", username);
editor.putString("loginName", loginName);
editor.putString("userId", userId);
editor.putString(Constants.SP_KEY_OPERATING_MODE, OperatingMode.USER.name());
editor.putString(Constants.SP_KEY_USERNAME, username);
editor.putString(Constants.SP_KEY_LOGIN_NAME, loginName);
editor.putString(Constants.SP_KEY_USER_ID, userId);
editor.apply();
}

public void saveDeviceData(String deviceId, String deviceName) {
SharedPreferences preferences = getSharedPreferences(Constants.SP_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("operating_mode", "DEVICE");
editor.putString("deviceId", deviceId);
editor.putString(Constants.SP_KEY_OPERATING_MODE, OperatingMode.DEVICE.name());
editor.putString(Constants.SP_KEY_DEVICE_ID, deviceId);
editor.apply();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import org.osmdroid.config.Configuration;

import de.raffaelhahn.xadgps_client.async.Constants;
import de.raffaelhahn.xadgps_client.services.DeviceListService;

public class MainActivity extends AppCompatActivity {
Expand All @@ -31,7 +30,7 @@ protected void onCreate(Bundle savedInstanceState) {
Configuration.getInstance().setUserAgentValue(getPackageName());

SharedPreferences sharedPreferences = getSharedPreferences(Constants.SP_NAME, MODE_PRIVATE);
if(!sharedPreferences.contains("operating_mode")){
if(!sharedPreferences.contains(Constants.SP_KEY_OPERATING_MODE)){
startActivity(new Intent(this, LoginActivity.class));
finish();
return;
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/de/raffaelhahn/xadgps_client/OperatingMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package de.raffaelhahn.xadgps_client;

public enum OperatingMode {

USER("0"),
DEVICE("1"),
UNKNOWN("3");

public final String numericString;
OperatingMode(String numericString) {
this.numericString = numericString;
}

public static OperatingMode fromNumericString(String value) {
for (OperatingMode mode : OperatingMode.values()) {
if (mode.numericString.equals(value)) {
return mode;
}
}
return OperatingMode.UNKNOWN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

import java.util.ArrayList;

import de.raffaelhahn.xadgps_client.async.Constants;
import de.raffaelhahn.xadgps_client.services.DeviceListService;

public class TrackingFragment extends Fragment implements DeviceListService.DeviceListUpdateListener {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import de.raffaelhahn.xadgps_client.Constants;


public class GetDeviceListAsync extends AsyncTask<Void, Void, Void> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import de.raffaelhahn.xadgps_client.Constants;


public class GetDeviceTrackingAsync extends AsyncTask<Void, Void, Void> {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
package de.raffaelhahn.xadgps_client.async;

import android.os.AsyncTask;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.nio.charset.Charset;

import de.raffaelhahn.xadgps_client.Constants;


public class LoginAsync extends AsyncTask<Void, Void, Void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
import java.util.stream.Collectors;

import de.raffaelhahn.xadgps_client.NotifyDevice;
import de.raffaelhahn.xadgps_client.OperatingMode;
import de.raffaelhahn.xadgps_client.R;
import de.raffaelhahn.xadgps_client.async.Constants;
import de.raffaelhahn.xadgps_client.Constants;
import de.raffaelhahn.xadgps_client.async.GetDeviceListAsync;
import de.raffaelhahn.xadgps_client.services.MovementMonitorService;

Expand All @@ -39,14 +40,12 @@ public Result doWork() {
SharedPreferences preferences = getApplicationContext().getSharedPreferences(Constants.SP_NAME, Context.MODE_PRIVATE);

GetDeviceListAsync getDeviceListAsync = new GetDeviceListAsync();
if(preferences.getString("operating_mode", "").equals("USER")) {
getDeviceListAsync.paramUserId = preferences.getString("userId", "");
getDeviceListAsync.paramTypeId = "0";
} else {
getDeviceListAsync.paramUserId = preferences.getString("deviceId", "");
getDeviceListAsync.paramTypeId = "1";
}

OperatingMode operatingMode = OperatingMode.valueOf(preferences.getString(Constants.SP_KEY_OPERATING_MODE, ""));
getDeviceListAsync.paramUserId = preferences.getString(
operatingMode == OperatingMode.USER ? Constants.SP_KEY_USER_ID : Constants.SP_KEY_DEVICE_ID,
""
);
getDeviceListAsync.paramTypeId = operatingMode.numericString;
getDeviceListAsync.paramMapType = "Google";
getDeviceListAsync.paramLanguage = Locale.getDefault().getLanguage() + "-" + Locale.getDefault().getCountry();
getDeviceListAsync.runFetch();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package de.raffaelhahn.xadgps_client.services;

import android.content.Context;
import android.util.Log;
import android.content.SharedPreferences;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import java.util.Timer;
import java.util.TimerTask;

import de.raffaelhahn.xadgps_client.Device;
import de.raffaelhahn.xadgps_client.OperatingMode;
import de.raffaelhahn.xadgps_client.async.AsyncCallback;
import de.raffaelhahn.xadgps_client.async.Constants;
import de.raffaelhahn.xadgps_client.Constants;
import de.raffaelhahn.xadgps_client.async.GetDeviceListAsync;
import de.raffaelhahn.xadgps_client.async.GetDeviceTrackingAsync;

Expand Down Expand Up @@ -65,13 +65,14 @@ public void stopRequestDeviceListUpdate() {

public void updateDeviceList() {
GetDeviceListAsync getDeviceListAsync = new GetDeviceListAsync();
if(context.getSharedPreferences(Constants.SP_NAME, Context.MODE_PRIVATE).getString("operating_mode", "").equals("USER")) {
getDeviceListAsync.paramUserId = context.getSharedPreferences(Constants.SP_NAME, Context.MODE_PRIVATE).getString("userId", "");
getDeviceListAsync.paramTypeId = "0";
} else {
getDeviceListAsync.paramUserId = context.getSharedPreferences(Constants.SP_NAME, Context.MODE_PRIVATE).getString("deviceId", "");
getDeviceListAsync.paramTypeId = "1";
}
SharedPreferences preferences = context.getSharedPreferences(Constants.SP_NAME, Context.MODE_PRIVATE);

OperatingMode operatingMode = OperatingMode.valueOf(preferences.getString(Constants.SP_KEY_OPERATING_MODE, ""));
getDeviceListAsync.paramUserId = preferences.getString(
operatingMode == OperatingMode.USER ? Constants.SP_KEY_USER_ID : Constants.SP_KEY_DEVICE_ID,
""
);
getDeviceListAsync.paramTypeId = operatingMode.numericString;
getDeviceListAsync.paramMapType = "Google";
getDeviceListAsync.paramLanguage = Locale.getDefault().getLanguage() + "-" + Locale.getDefault().getCountry();
getDeviceListAsync.callback = new AsyncCallback<JSONObject>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import de.raffaelhahn.xadgps_client.Device;
import de.raffaelhahn.xadgps_client.NotifyDevice;
import de.raffaelhahn.xadgps_client.R;
import de.raffaelhahn.xadgps_client.async.Constants;
import de.raffaelhahn.xadgps_client.Constants;
import de.raffaelhahn.xadgps_client.background.BackgroundWorkService;

import android.Manifest;
Expand Down

0 comments on commit 24c333a

Please sign in to comment.