Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

4PX strategy and other fixes #23

Merged
merged 8 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ android {
minSdkVersion 23
targetSdkVersion 29

versionCode 121
versionName "2.1.5"
versionCode 122
versionName "2.1.6"
multiDexEnabled true // Enabling multi dex support.
vectorDrawables.useSupportLibrary = true
}
Expand Down Expand Up @@ -61,6 +61,7 @@ dependencies {
// For the notifications
implementation 'com.google.firebase:firebase-messaging:20.1.3'
implementation 'joda-time:joda-time:2.10.5'
implementation 'com.google.code.gson:gson:2.8.6'
}

apply plugin: 'com.google.gms.google-services'
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
/*
* Copyright (c) 2020
* Paketin Seuranta
*
* @author developerfromjokela
* @author norkator
*/

package com.nitramite.adapters;

import android.annotation.SuppressLint;
import android.content.Context;
import androidx.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nitramite.utils.CarrierUtils;
import androidx.annotation.NonNull;

import com.nitramite.paketinseuranta.ParcelItem;
import com.nitramite.paketinseuranta.PhaseNumber;
import com.nitramite.paketinseuranta.R;
import com.nitramite.utils.CarrierUtils;

import java.util.ArrayList;

Expand Down Expand Up @@ -83,6 +93,7 @@ public View getView(int position, View view, @NonNull ViewGroup parent) {
R.mipmap.muu_logo,
R.drawable.returned,
R.drawable.customs,
R.drawable.ic_waiting4pickup
};

String phase = parcelItems.get(position).getParcelPhase();
Expand All @@ -97,32 +108,36 @@ public View getView(int position, View view, @NonNull ViewGroup parent) {
//}

// Check for item in transit
if (phase.length() == 11 || phase.equals("TRANSIT")) {
if (phase.equals(PhaseNumber.PHASE_WAITING_FOR_PICKUP)) {
// TODO made new icon. if it's good, let's use that in upcoming ones, as it's SVG not png
pos = 9;
phaseTextFix = context.getString(R.string.status_waiting_for_pickup);
} else if (phase.length() == 11 || phase.equals("TRANSIT")) {
pos = 3;
phaseTextFix = "Kuljetuksessa!";
phaseTextFix = context.getString(R.string.status_in_transit);
} else if (phase.length() == 12) {
pos = 3;
phaseTextFix = "Kuljetuksessa!";
phaseTextFix = context.getString(R.string.status_in_transit);
} else if (phase.length() == 16 || latestEventDescription.contains("ilmoitus tekstiviestillä") || latestEventDescription.contains("toimitettu noutopisteeseen")) {
pos = 4;
phaseTextFix = "Valmiina noudettavaksi!";
phaseTextFix = context.getString(R.string.status_ready);
} else if (phase.length() == 9) {
pos = 5;
phaseTextFix = "Toimitettu!";
phaseTextFix = context.getString(R.string.status_delivered);
} else if (phase.length() == 8 || phase.equals("RETURNED_TO_SENDER")) {
pos = 7;
phaseTextFix = "Palautettu!";
phaseTextFix = context.getString(R.string.status_returned);
} else if (phase.length() == 7 || phase.equals("CUSTOMS")) {
pos = 8;
phaseTextFix = "Tullaus vaaditaan!";
phaseTextFix = context.getString(R.string.status_customs);
}
// Not inside Finland
else if (phase.length() == 24 || latestEventDescription.equals("Lähetys ei ole vielä saapunut Postille, odotathan") ||
latestEventDescription.equals("Lähetys on saapunut varastolle") || latestEventDescription.equals("Lähetys on lähtenyt varastolta")
|| latestEventDescription.equals("Lähetys on rekisteröity.") || latestEventDescription.equals("Lähetys on matkalla kohdemaahan")
) {
pos = 2;
phaseTextFix = "Kuljetuksessa!";
phaseTextFix = context.getString(R.string.status_in_transit);
}
else if (parcelItems.get(position).getParcelCarrier().equals("99") && phase.equals("")) {
pos = 6;
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/com/nitramite/courier/BringStrategy.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* Copyright (c) 2020
* Paketin Seuranta
*
* @author developerfromjokela
* @author norkator
*/

package com.nitramite.courier;

import android.annotation.SuppressLint;
Expand Down
12 changes: 9 additions & 3 deletions app/src/main/java/com/nitramite/courier/CainiaoStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.util.Log;

import com.nitramite.paketinseuranta.EventObject;
import com.nitramite.paketinseuranta.PhaseNumber;
import com.nitramite.utils.Utils;

import org.json.JSONArray;
Expand Down Expand Up @@ -125,7 +126,10 @@ private void proceedParsing(ParcelObject parcelObject, JSONObject detailsObject)
}
setBasicDetails(parcelObject, detailsObject);
// Add to stack
parcelObject.setEventObjects(eventObjects);
if (!eventObjects.isEmpty())
parcelObject.setEventObjects(eventObjects);
else
parcelObject.setEventObjects(null);
}

private EventObject parseEventObject(JSONObject event) throws JSONException, ParseException {
Expand All @@ -152,11 +156,13 @@ private void setBasicDetails(ParcelObject parcelObject, JSONObject jsonObject) {
String dest = jsonObject.optString("destCountry", "null");
String status = jsonObject.optString("status", "");
parcelObject.setDestinationCountry(dest);

Log.i(TAG, status);
if (status.equals("LTL_SIGNIN") || status.equals("SIGNIN") || status.equals("OWS_SIGNIN") || status.contains("WAIT4SIGNIN")) {
parcelObject.setPhase("DELIVERED");
} else if (status.equals("CWS_WAIT4SIGNIN") || status.equals("LTL_WAIT4SIGNIN") || status.equals("WAIT4SIGNIN") || status.contains("WAIT4PICKUP")) {
} else if (status.equals("CWS_WAIT4SIGNIN") || status.equals("LTL_WAIT4SIGNIN") || status.equals("WAIT4SIGNIN")) {
parcelObject.setPhase("READY_FOR_PICKUP");
} else if (status.contains("WAIT4PICKUP")) {
parcelObject.setPhase(PhaseNumber.PHASE_WAITING_FOR_PICKUP);
} else if (status.contains("RETURN")) {
parcelObject.setPhase("RETURNED");
}
Expand Down
168 changes: 168 additions & 0 deletions app/src/main/java/com/nitramite/courier/FourPXStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
* Copyright (c) 2020
* Paketin Seuranta
*
* @author developerfromjokela
* @author norkator
*/

package com.nitramite.courier;

import android.util.Log;

import com.google.gson.Gson;
import com.nitramite.courier.fourpx.FourPXEvent;
import com.nitramite.courier.fourpx.FourPXParcel;
import com.nitramite.courier.fourpx.ListTrackResponse;
import com.nitramite.paketinseuranta.EventObject;
import com.nitramite.utils.OkHttpUtils;

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

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class FourPXStrategy implements CourierStrategy {

// Logging
private static final String TAG = "FourPXStrategy";
private static final String URL = "http://track.4px.com/track/v2/front/listTrack";


private String getRequest(String url) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.addHeader("User-Agent", Constants.UserAgent)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}

private String generatePostBody(String trackingCode) {
try {
JSONObject object = new JSONObject();
JSONArray array = new JSONArray();
array.put(trackingCode);
object.put("serveCodes", array);
object.put("language", "en-us");
return object.toString();
} catch (Exception ignored) {
}
return "";
}

private String postJSONRequest(String url, String json) throws IOException {
RequestBody body = RequestBody.create(json, OkHttpUtils.JSON); // new
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.addHeader("User-Agent", Constants.UserAgent)
.post(body)
.build();
Response response = client.newCall(request).execute();
ResponseBody respBody = response.body();
return respBody != null ? respBody.string() : null;
}

@Override
public ParcelObject execute(String parcelCode) {
ParcelObject parcelObject = new ParcelObject(parcelCode);

try {

String postResult = postJSONRequest(URL, generatePostBody(parcelCode));

if (postResult != null) {
// Validating JSON
if (!isValidJSONObject(postResult)) {
Log.i(TAG, "FPX strategy fetching failed to parse JSON");
parcelObject.setIsFound(false); // Parcel not found
return parcelObject;
}

// If parcel is not found due to an server error, interrupting
ListTrackResponse trackResponse = new Gson().fromJson(postResult, ListTrackResponse.class);
if (!trackResponse.isParcelFound()) {
Log.i(TAG, "FPX strategy fetching failed finding any parcel details");
parcelObject.setIsFound(false); // Parcel not found
return parcelObject;
}

// If the parcel isn't found in 4PX system, interrupting
FourPXParcel pxParcel = trackResponse.getParcel();
if (pxParcel.getServerCode() == null || pxParcel.getServerCode().equals("")) {
Log.i(TAG, "FPX strategy parcel not found");
parcelObject.setIsFound(false); // Parcel not found
return parcelObject;
}

if (pxParcel.getDestinationName() != null)
parcelObject.setDestinationCountry(pxParcel.getDestinationName());

proceedParsing(pxParcel, parcelObject);
} else {
Log.i(TAG, "FPX strategy fetching failed to read response");
parcelObject.setIsFound(false); // Parcel not found
}
} catch (Exception e) {
e.printStackTrace();
}
return parcelObject;
}


private boolean isValidJSONObject(String json) {
try {
new JSONObject(json);
return true;
} catch (Exception ignored) {
}
return false;
}

private void proceedParsing(FourPXParcel fourPXParcel, ParcelObject parcelObject) throws JSONException, ParseException {
ArrayList<EventObject> eventObjects = new ArrayList<>();
parcelObject.setIsFound(true);
parcelObject.setPhase("IN_TRANSPORT");

// Parse events
for (FourPXEvent event : fourPXParcel.getEvents()) {
eventObjects.add(event.toEventObject());
}

setBasicDetails(parcelObject, fourPXParcel);
// Add to stack
parcelObject.setEventObjects(eventObjects);
}


private void setBasicDetails(ParcelObject parcelObject, FourPXParcel parcel) {
boolean hasArrivedAtDestCountry = false;
for (FourPXEvent event : parcel.getEvents()) {
if (event.getTkCode() != null)
if (event.getTkCode().equals("FPX_M_ATA")) {
hasArrivedAtDestCountry = true;
break;
}
}
// This part is tricky. I didn't find any list of what different TkCodes mean. I am waiting my parcel to be delivered, and possibly extracting info from that
if (!hasArrivedAtDestCountry && parcel.getDestinationCode().equals("FI")) {
parcelObject.setPhase("INTRANSPORT_NOTINFINLAND");
}
if (parcel.getStatus() == 2) {
parcelObject.setPhase("DELIVERED");
}
}


} // End of class
Loading