Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make geo classes less linked and implement build without them #130

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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: 5 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ jobs:
with:
name: obf.zip
path: ${{ github.workspace }}/mm_v1_obf.jar
- name: Upload no_geo jar
uses: actions/upload-artifact@v2
with:
name: base_no_geo.zip
path: ${{ github.workspace }}/mm_v1_no_geo.jar
- name: Upload obf mappings
uses: actions/upload-artifact@v2
with:
Expand Down
10 changes: 10 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ fi

echo "Build done!" ./${APP}.jar

echo "Removing location classes..."
rm ./classes/mahomaps/map/LocationAPI*

echo "Jaring preverified class files..."
${JAR} cmf ${MANIFEST} ${APP}_no_geo.jar -C ./classes .

if [ -d ${RES} ] ; then
${JAR} uf ${APP}_no_geo.jar -C ${RES} .
fi

echo Optimizing ${APP}
chmod +x ${PROGUARD}
touch cf.cfg
Expand Down
1 change: 1 addition & 0 deletions proguard.basecfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@
}

-keep public class * extends javax.microedition.midlet.MIDlet
-keep public class * extends mahomaps.map.ILocationAPI
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

С этим не согласен

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Доправь тогда как надо. По идее, надо только имя запретить трогать.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Перечитал все, понял почему так надо


143 changes: 3 additions & 140 deletions src/mahomaps/map/GeoUpdateThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

import java.util.Vector;

import javax.microedition.location.Coordinates;
import javax.microedition.location.Location;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;

import mahomaps.MahoMapsApp;
import mahomaps.screens.MapCanvas;

public class GeoUpdateThread extends Thread {
Expand All @@ -20,7 +14,7 @@ public class GeoUpdateThread extends Thread {
* Состояние получения геопозиции. Одно из state-значений.
*/
public int state;
private LocationAPI locationAPI;
private ILocationAPI locationAPI;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А вот это гениально

public boolean loop = true;
public long lastUpdateTime = System.currentTimeMillis();
public String method = null;
Expand All @@ -39,7 +33,8 @@ public void run() {
try {
Class.forName("javax.microedition.location.LocationProvider");
try {
locationAPI = new LocationAPI();
locationAPI = (ILocationAPI) Class.forName("mahomaps.map.LocationAPI").newInstance();
locationAPI.setThread(this);
} catch (Exception e) {
state = e.toString().indexOf("LocationException") != -1 ? STATE_UNAVAILABLE : STATE_UNSUPPORTED;
e.printStackTrace();
Expand Down Expand Up @@ -151,136 +146,4 @@ public boolean DrawPoint() {
public final static int STATE_OK = 2;

public final static int[] states = new int[] { 93, 93, 94, 88, 95, 96 };

// для безопасных вызовов
class LocationAPI {
public LocationProvider locationProvider;

public LocationAPI() throws Exception {
locationProvider = LocationProvider.getInstance(null);
}

public double[] getLastKnownCoordinates() {
Location location = LocationProvider.getLastKnownLocation();
if (location == null || !location.isValid())
return null;
Coordinates coordinates = location.getQualifiedCoordinates();
if (coordinates == null)
return null;
return new double[] { coordinates.getLatitude(), coordinates.getLongitude() };
}

public void setupListener() {
locationProvider.setLocationListener(new LocationAPIListener(), 5, 5, 5);
}

public void resetProvider() throws Exception {
System.out.println("resetProvider");
Thread.sleep(5000);
LocationProvider old = locationProvider;
try {
locationProvider = LocationProvider.getInstance(null);
old.setLocationListener(null, 0, 0, 0);
setupListener();
} catch (Exception e) {
e.printStackTrace();
}
}

class LocationAPIListener implements LocationListener {

public void locationUpdated(LocationProvider provider, Location location) {
// определение кол-ва спутников
String nmea = location.getExtraInfo("application/X-jsr179-location-nmea");
if (nmea != null) {
String[] sequence = split(nmea, '$');
int s1 = -1;
int s2 = -1;
for (int i = sequence.length - 1; i >= 0; i--) {
String[] sentence = split(sequence[i], ',');
if (sentence[0].endsWith("GGA")) {
try {
s1 = Integer.parseInt(sentence[7]);
} catch (Exception e) {
s1 = -1;
}
s2 = Math.max(s2, s1);
} else if (sentence[0].endsWith("GSV")) {
try {
s2 = Math.max(s2, Integer.parseInt(sentence[3]));
} catch (Exception e) {
}
}
}
sattelites = s1;
totalSattelitesInView = s2;
} else {
totalSattelitesInView = sattelites = -1;
}
String s = "";
int t = location.getLocationMethod();
if ((t & Location.MTE_SATELLITE) == Location.MTE_SATELLITE) {
s = "GPS";
}
if ((t & Location.MTE_TIMEDIFFERENCE) == Location.MTE_TIMEDIFFERENCE) {
s += "TD";
}
if ((t & Location.MTE_TIMEOFARRIVAL) == Location.MTE_TIMEOFARRIVAL) {
s += "TOA";
}
if ((t & Location.MTE_CELLID) == Location.MTE_CELLID) {
s += "CID";
}
if ((t & Location.MTE_SHORTRANGE) == Location.MTE_SHORTRANGE) {
s += "SR";
}
if ((t & Location.MTE_ANGLEOFARRIVAL) == Location.MTE_ANGLEOFARRIVAL) {
s += "AOA";
}
if ((t & Location.MTA_ASSISTED) == Location.MTA_ASSISTED) {
s = "A" + s;
} else if ((t & Location.MTA_UNASSISTED) == Location.MTA_UNASSISTED) {
s = "U" + s;
}
if ((t & Location.MTY_TERMINALBASED) == Location.MTY_TERMINALBASED) {
s = "TB " + s;
}
if ((t & Location.MTY_NETWORKBASED) == Location.MTY_NETWORKBASED) {
s = "NB " + s;
}
method = s.length() == 0 ? null : s;
if (location.isValid()) {
Coordinates coordinates = location.getQualifiedCoordinates();
if (coordinates.getLatitude() != 0 && coordinates.getLongitude() != 0) {
if (!vibrated) {
try {
MahoMapsApp.display.vibrate(100);
} catch (Exception e) {
}
vibrated = true;
}
positionPoint.lat = coordinates.getLatitude();
positionPoint.lon = coordinates.getLongitude();
positionPoint.color = Geopoint.COLOR_RED;
state = STATE_OK;
lastUpdateTime = System.currentTimeMillis();
MahoMapsApp.GetCanvas().requestRepaint();
} else {
state = STATE_UNAVAILABLE;
}
}
updateCount++;
}

public void providerStateChanged(LocationProvider provider, int newState) {
if (newState != LocationProvider.AVAILABLE) {
state = STATE_OK_PENDING;
}
// на случай если изменился провайдер
if (newState == LocationProvider.OUT_OF_SERVICE) {
restart();
}
}
}
}
}
12 changes: 12 additions & 0 deletions src/mahomaps/map/ILocationAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package mahomaps.map;

interface ILocationAPI {

void setThread(GeoUpdateThread geoUpdateThread);

void setupListener();

double[] getLastKnownCoordinates();

void resetProvider() throws Exception;
}
149 changes: 149 additions & 0 deletions src/mahomaps/map/LocationAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package mahomaps.map;

import javax.microedition.location.Coordinates;
import javax.microedition.location.Location;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;

import mahomaps.MahoMapsApp;

// для безопасных вызовов
class LocationAPI implements ILocationAPI {

/**
* Geo thread, where this api will work.
*/
private GeoUpdateThread geoUpdateThread;
public LocationProvider locationProvider;

public LocationAPI() throws Exception {
locationProvider = LocationProvider.getInstance(null);
}

public void setThread(GeoUpdateThread geoUpdateThread) {
this.geoUpdateThread = geoUpdateThread;
}

public double[] getLastKnownCoordinates() {
Location location = LocationProvider.getLastKnownLocation();
if (location == null || !location.isValid())
return null;
Coordinates coordinates = location.getQualifiedCoordinates();
if (coordinates == null)
return null;
return new double[] { coordinates.getLatitude(), coordinates.getLongitude() };
}

public void setupListener() {
locationProvider.setLocationListener(new LocationAPIListener(), 5, 5, 5);
}

public void resetProvider() throws Exception {
System.out.println("resetProvider");
Thread.sleep(5000);
LocationProvider old = locationProvider;
try {
locationProvider = LocationProvider.getInstance(null);
old.setLocationListener(null, 0, 0, 0);
setupListener();
} catch (Exception e) {
e.printStackTrace();
}
}

class LocationAPIListener implements LocationListener {

public void locationUpdated(LocationProvider provider, Location location) {
// определение кол-ва спутников
String nmea = location.getExtraInfo("application/X-jsr179-location-nmea");
if (nmea != null) {
String[] sequence = GeoUpdateThread.split(nmea, '$');
int s1 = -1;
int s2 = -1;
for (int i = sequence.length - 1; i >= 0; i--) {
String[] sentence = GeoUpdateThread.split(sequence[i], ',');
if (sentence[0].endsWith("GGA")) {
try {
s1 = Integer.parseInt(sentence[7]);
} catch (Exception e) {
s1 = -1;
}
s2 = Math.max(s2, s1);
} else if (sentence[0].endsWith("GSV")) {
try {
s2 = Math.max(s2, Integer.parseInt(sentence[3]));
} catch (Exception e) {
}
}
}
geoUpdateThread.sattelites = s1;
geoUpdateThread.totalSattelitesInView = s2;
} else {
geoUpdateThread.totalSattelitesInView = geoUpdateThread.sattelites = -1;
}
String s = "";
int t = location.getLocationMethod();
if ((t & Location.MTE_SATELLITE) == Location.MTE_SATELLITE) {
s = "GPS";
}
if ((t & Location.MTE_TIMEDIFFERENCE) == Location.MTE_TIMEDIFFERENCE) {
s += "TD";
}
if ((t & Location.MTE_TIMEOFARRIVAL) == Location.MTE_TIMEOFARRIVAL) {
s += "TOA";
}
if ((t & Location.MTE_CELLID) == Location.MTE_CELLID) {
s += "CID";
}
if ((t & Location.MTE_SHORTRANGE) == Location.MTE_SHORTRANGE) {
s += "SR";
}
if ((t & Location.MTE_ANGLEOFARRIVAL) == Location.MTE_ANGLEOFARRIVAL) {
s += "AOA";
}
if ((t & Location.MTA_ASSISTED) == Location.MTA_ASSISTED) {
s = "A" + s;
} else if ((t & Location.MTA_UNASSISTED) == Location.MTA_UNASSISTED) {
s = "U" + s;
}
if ((t & Location.MTY_TERMINALBASED) == Location.MTY_TERMINALBASED) {
s = "TB " + s;
}
if ((t & Location.MTY_NETWORKBASED) == Location.MTY_NETWORKBASED) {
s = "NB " + s;
}
geoUpdateThread.method = s.length() == 0 ? null : s;
if (location.isValid()) {
Coordinates coordinates = location.getQualifiedCoordinates();
if (coordinates.getLatitude() != 0 && coordinates.getLongitude() != 0) {
if (!geoUpdateThread.vibrated) {
try {
MahoMapsApp.display.vibrate(100);
} catch (Exception e) {
}
geoUpdateThread.vibrated = true;
}
geoUpdateThread.positionPoint.lat = coordinates.getLatitude();
geoUpdateThread.positionPoint.lon = coordinates.getLongitude();
geoUpdateThread.positionPoint.color = Geopoint.COLOR_RED;
geoUpdateThread.state = GeoUpdateThread.STATE_OK;
geoUpdateThread.lastUpdateTime = System.currentTimeMillis();
MahoMapsApp.GetCanvas().requestRepaint();
} else {
geoUpdateThread.state = GeoUpdateThread.STATE_UNAVAILABLE;
}
}
geoUpdateThread.updateCount++;
}

public void providerStateChanged(LocationProvider provider, int newState) {
if (newState != LocationProvider.AVAILABLE) {
geoUpdateThread.state = GeoUpdateThread.STATE_OK_PENDING;
}
// на случай если изменился провайдер
if (newState == LocationProvider.OUT_OF_SERVICE) {
geoUpdateThread.restart();
}
}
}
}