Add getId for callback and add check for changing connected package

This commit is contained in:
Chumva 2018-08-30 12:37:18 +03:00
parent 1ac5fd6ce9
commit 8cbf7862a6
8 changed files with 62 additions and 10 deletions

View file

@ -6,4 +6,6 @@ interface IOsmAndAidlCallback {
void onSearchComplete(in List<SearchResult> resultSet);
void update();
int getId();
}

View file

@ -130,4 +130,5 @@ interface IOsmAndAidlInterface {
boolean search(in SearchParams params, IOsmAndAidlCallback callback);
boolean registerCallback(in IOsmAndAidlCallback callback);
boolean unRegisterCallback(in IOsmAndAidlCallback callback);
}

View file

@ -51,6 +51,8 @@ class OsmandAidlHelper(private val app: TelegramApplication) {
companion object {
const val OSMAND_FREE_PACKAGE_NAME = "net.osmand"
const val OSMAND_PLUS_PACKAGE_NAME = "net.osmand.plus"
const val OSMAND_CALLBACK_ID = 17
}
private var mIOsmAndAidlInterface: IOsmAndAidlInterface? = null
@ -73,6 +75,9 @@ class OsmandAidlHelper(private val app: TelegramApplication) {
}
private val mIOsmAndAidlCallback = object : IOsmAndAidlCallback.Stub() {
override fun getId(): Int = OSMAND_CALLBACK_ID
@Throws(RemoteException::class)
override fun onSearchComplete(resultSet: List<SearchResult>) {
if (mSearchCompleteListener != null) {
@ -139,6 +144,8 @@ class OsmandAidlHelper(private val app: TelegramApplication) {
fun isOsmandConnected(): Boolean {
return mIOsmAndAidlInterface != null
}
fun getOsmandBoundPackage(): String = boundPackage
/**
* Get list of active GPX files.
@ -196,6 +203,7 @@ class OsmandAidlHelper(private val app: TelegramApplication) {
fun cleanupResources() {
try {
if (mIOsmAndAidlInterface != null) {
unRegisterCallback()
mIOsmAndAidlInterface = null
app.unbindService(mConnection)
}
@ -1034,4 +1042,15 @@ class OsmandAidlHelper(private val app: TelegramApplication) {
}
return false
}
fun unRegisterCallback(): Boolean {
if (mIOsmAndAidlInterface != null) {
try {
return mIOsmAndAidlInterface!!.unRegisterCallback(mIOsmAndAidlCallback)
} catch (e: RemoteException) {
e.printStackTrace()
}
}
return false
}
}

View file

@ -148,24 +148,31 @@ class ShowLocationHelper(private val app: TelegramApplication) {
fun startShowingLocation() {
if (!showingLocation) {
val packageOsmAndInfo = app.packageManager.getPackageInfo(app.settings.appToConnectPackage, 0)
showingLocation =
if (packageOsmAndInfo.versionCode >= MIN_OSMAND_CALLBACK_VERSION_CODE) {
osmandAidlHelper.registerCallback()
} else {
app.startUserLocationService()
true
}
showingLocation = if (isOsmandUseCallback()) {
osmandAidlHelper.registerCallback()
} else {
app.startUserLocationService()
true
}
}
}
fun stopShowingLocation() {
if (showingLocation) {
showingLocation = false
app.stopUserLocationService()
if (isOsmandUseCallback()) {
osmandAidlHelper.unRegisterCallback()
} else {
app.stopUserLocationService()
}
}
}
fun isOsmandUseCallback(): Boolean {
val packageOsmAndInfo = app.packageManager.getPackageInfo(app.settings.appToConnectPackage, 0)
return packageOsmAndInfo.versionCode >= MIN_OSMAND_CALLBACK_VERSION_CODE
}
fun startShowMessagesTask(chatId: Long, vararg messages: TdApi.Message) {
if (app.settings.isShowingChatOnMap(chatId)) {
ShowMessagesTask(app).executeOnExecutor(executor, *messages)

View file

@ -70,7 +70,11 @@ class SettingsDialogFragment : BaseDialogFragment() {
isChecked = pack == settings.appToConnectPackage
}
setOnClickListener {
settings.appToConnectPackage = appConn.appPackage
val newAppPackage = appConn.appPackage
if (app.osmandAidlHelper.getOsmandBoundPackage() != newAppPackage) {
app.showLocationHelper.stopShowingLocation()
}
settings.appToConnectPackage = newAppPackage
app.osmandAidlHelper.reconnectOsmand()
updateSelectedAppConn()
}

View file

@ -6,4 +6,6 @@ interface IOsmAndAidlCallback {
void onSearchComplete(in List<SearchResult> resultSet);
void update();
int getId();
}

View file

@ -130,4 +130,5 @@ interface IOsmAndAidlInterface {
boolean search(in SearchParams params, IOsmAndAidlCallback callback);
boolean registerCallback(in IOsmAndAidlCallback callback);
boolean unRegisterCallback(in IOsmAndAidlCallback callback);
}

View file

@ -63,6 +63,7 @@ import org.apache.commons.logging.Log;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
@ -592,6 +593,21 @@ public class OsmandAidlService extends Service {
}
return false;
}
@Override
public boolean unRegisterCallback(IOsmAndAidlCallback callback) throws RemoteException {
if (callback != null) {
Iterator<IOsmAndAidlCallback> i = callbacks.iterator();
while (i.hasNext()) {
IOsmAndAidlCallback aidlCallback = i.next();
if (aidlCallback.getId() == callback.getId()) {
i.remove();
}
}
return true;
}
return false;
}
};
void startRemoteUpdates() {