From 86ca0a38494f6c72b0da69b627fd10720a309fa9 Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Fri, 27 Jul 2018 16:50:07 +0300 Subject: [PATCH] Add the ability to add navigation drawer item for opening some app from AIDL API --- .../net/osmand/aidl/IOsmAndAidlInterface.aidl | 4 ++ .../AddOpenAppNavDrawerItemParams.aidl | 3 + .../AddOpenAppNavDrawerItemParams.java | 63 +++++++++++++++++++ .../osmand/telegram/TelegramApplication.kt | 9 +++ .../telegram/helpers/OsmandAidlHelper.kt | 12 ++++ .../net/osmand/aidl/IOsmAndAidlInterface.aidl | 4 ++ OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java | 42 +++++++++++++ .../net/osmand/aidl/OsmandAidlService.java | 11 ++++ .../AddOpenAppNavDrawerItemParams.aidl | 3 + .../AddOpenAppNavDrawerItemParams.java | 63 +++++++++++++++++++ .../src/net/osmand/plus/OsmandSettings.java | 4 ++ .../plus/activities/MapActivityActions.java | 2 + 12 files changed, 220 insertions(+) create mode 100644 OsmAnd-telegram/src/net/osmand/aidl/navdrawer/AddOpenAppNavDrawerItemParams.aidl create mode 100644 OsmAnd-telegram/src/net/osmand/aidl/navdrawer/AddOpenAppNavDrawerItemParams.java create mode 100644 OsmAnd/src/net/osmand/aidl/navdrawer/AddOpenAppNavDrawerItemParams.aidl create mode 100644 OsmAnd/src/net/osmand/aidl/navdrawer/AddOpenAppNavDrawerItemParams.java diff --git a/OsmAnd-telegram/src/net/osmand/aidl/IOsmAndAidlInterface.aidl b/OsmAnd-telegram/src/net/osmand/aidl/IOsmAndAidlInterface.aidl index 9b0606c0ac..dace493dd0 100644 --- a/OsmAnd-telegram/src/net/osmand/aidl/IOsmAndAidlInterface.aidl +++ b/OsmAnd-telegram/src/net/osmand/aidl/IOsmAndAidlInterface.aidl @@ -53,6 +53,8 @@ import net.osmand.aidl.gpx.RemoveGpxParams; import net.osmand.aidl.maplayer.point.ShowMapPointParams; +import net.osmand.aidl.navdrawer.AddOpenAppNavDrawerItemParams; + // NOTE: Add new methods at the end of file!!! interface IOsmAndAidlInterface { @@ -105,4 +107,6 @@ interface IOsmAndAidlInterface { boolean removeGpx(in RemoveGpxParams params); boolean showMapPoint(in ShowMapPointParams params); + + boolean addOpenAppNavDrawerItem(in AddOpenAppNavDrawerItemParams params); } \ No newline at end of file diff --git a/OsmAnd-telegram/src/net/osmand/aidl/navdrawer/AddOpenAppNavDrawerItemParams.aidl b/OsmAnd-telegram/src/net/osmand/aidl/navdrawer/AddOpenAppNavDrawerItemParams.aidl new file mode 100644 index 0000000000..212c3ddcb0 --- /dev/null +++ b/OsmAnd-telegram/src/net/osmand/aidl/navdrawer/AddOpenAppNavDrawerItemParams.aidl @@ -0,0 +1,3 @@ +package net.osmand.aidl.navdrawer; + +parcelable AddOpenAppNavDrawerItemParams; \ No newline at end of file diff --git a/OsmAnd-telegram/src/net/osmand/aidl/navdrawer/AddOpenAppNavDrawerItemParams.java b/OsmAnd-telegram/src/net/osmand/aidl/navdrawer/AddOpenAppNavDrawerItemParams.java new file mode 100644 index 0000000000..55fcacfc5f --- /dev/null +++ b/OsmAnd-telegram/src/net/osmand/aidl/navdrawer/AddOpenAppNavDrawerItemParams.java @@ -0,0 +1,63 @@ +package net.osmand.aidl.navdrawer; + +import android.os.Parcel; +import android.os.Parcelable; + +public class AddOpenAppNavDrawerItemParams implements Parcelable { + + private String itemName; + private String appPackage; + private int flags; + + public AddOpenAppNavDrawerItemParams(String itemName, String appPackage) { + this(itemName, appPackage, -1); + } + + public AddOpenAppNavDrawerItemParams(String itemName, String appPackage, int flags) { + this.itemName = itemName; + this.appPackage = appPackage; + this.flags = flags; + } + + protected AddOpenAppNavDrawerItemParams(Parcel in) { + itemName = in.readString(); + appPackage = in.readString(); + flags = in.readInt(); + } + + public String getItemName() { + return itemName; + } + + public String getAppPackage() { + return appPackage; + } + + public int getFlags() { + return flags; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeString(itemName); + dest.writeString(appPackage); + dest.writeInt(flags); + } + + @Override + public int describeContents() { + return 0; + } + + public static final Creator CREATOR = new Creator() { + @Override + public AddOpenAppNavDrawerItemParams createFromParcel(Parcel in) { + return new AddOpenAppNavDrawerItemParams(in); + } + + @Override + public AddOpenAppNavDrawerItemParams[] newArray(int size) { + return new AddOpenAppNavDrawerItemParams[size]; + } + }; +} diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt index c37a837a2a..7c1279513c 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt @@ -41,6 +41,15 @@ class TelegramApplication : Application(), OsmandHelperListener { settings = TelegramSettings(this) uiUtils = UiUtils(this) osmandAidlHelper = OsmandAidlHelper(this) + osmandAidlHelper.listener = object : OsmandAidlHelper.OsmandHelperListener { + override fun onOsmandConnectionStateChanged(connected: Boolean) { + if (connected) { + val basePackage = "net.osmand.telegram" + val appPackage = if (BuildConfig.DEBUG) "$basePackage.debug" else basePackage + osmandAidlHelper.addOpenAppNavDrawerItem(getString(R.string.app_name), appPackage, -1) + } + } + } shareLocationHelper = ShareLocationHelper(this) showLocationHelper = ShowLocationHelper(this) notificationHelper = NotificationHelper(this) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt index e3d8fec7ef..e9c8713f63 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt @@ -33,6 +33,7 @@ import net.osmand.aidl.mapwidget.AMapWidget import net.osmand.aidl.mapwidget.AddMapWidgetParams import net.osmand.aidl.mapwidget.RemoveMapWidgetParams import net.osmand.aidl.mapwidget.UpdateMapWidgetParams +import net.osmand.aidl.navdrawer.AddOpenAppNavDrawerItemParams import net.osmand.aidl.navigation.NavigateGpxParams import net.osmand.aidl.navigation.NavigateParams import net.osmand.aidl.note.StartAudioRecordingParams @@ -866,4 +867,15 @@ class OsmandAidlHelper(private val app: Application) { } return false } + + fun addOpenAppNavDrawerItem(itemName: String, appPackage: String, flags: Int): Boolean { + if (mIOsmAndAidlInterface != null) { + try { + return mIOsmAndAidlInterface!!.addOpenAppNavDrawerItem(AddOpenAppNavDrawerItemParams(itemName, appPackage, flags)) + } catch (e: RemoteException) { + e.printStackTrace() + } + } + return false + } } diff --git a/OsmAnd/src/net/osmand/aidl/IOsmAndAidlInterface.aidl b/OsmAnd/src/net/osmand/aidl/IOsmAndAidlInterface.aidl index 9b0606c0ac..dace493dd0 100644 --- a/OsmAnd/src/net/osmand/aidl/IOsmAndAidlInterface.aidl +++ b/OsmAnd/src/net/osmand/aidl/IOsmAndAidlInterface.aidl @@ -53,6 +53,8 @@ import net.osmand.aidl.gpx.RemoveGpxParams; import net.osmand.aidl.maplayer.point.ShowMapPointParams; +import net.osmand.aidl.navdrawer.AddOpenAppNavDrawerItemParams; + // NOTE: Add new methods at the end of file!!! interface IOsmAndAidlInterface { @@ -105,4 +107,6 @@ interface IOsmAndAidlInterface { boolean removeGpx(in RemoveGpxParams params); boolean showMapPoint(in ShowMapPointParams params); + + boolean addOpenAppNavDrawerItem(in AddOpenAppNavDrawerItemParams params); } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java index 6e5c45166c..3667da2d0d 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java @@ -1,5 +1,6 @@ package net.osmand.aidl; +import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; @@ -12,6 +13,7 @@ import android.os.ParcelFileDescriptor; import android.support.v7.app.AlertDialog; import android.text.TextUtils; import android.view.View; +import android.widget.ArrayAdapter; import net.osmand.IndexConstants; import net.osmand.PlatformUtil; @@ -28,6 +30,8 @@ import net.osmand.data.FavouritePoint; import net.osmand.data.LatLon; import net.osmand.data.PointDescription; import net.osmand.plus.ApplicationMode; +import net.osmand.plus.ContextMenuAdapter; +import net.osmand.plus.ContextMenuItem; import net.osmand.plus.FavouritesDbHelper; import net.osmand.plus.GPXDatabase.GpxDataItem; import net.osmand.plus.GPXUtilities; @@ -38,6 +42,7 @@ import net.osmand.plus.MapMarkersHelper; import net.osmand.plus.MapMarkersHelper.MapMarker; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; +import net.osmand.plus.OsmandSettings; import net.osmand.plus.TargetPointsHelper; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.audionotes.AudioVideoNotesPlugin; @@ -1186,4 +1191,41 @@ public class OsmandAidlApi { app.sendBroadcast(intent); return true; } + + boolean addOpenAppNavDrawerItem(String itemName, String appPackage, int flags) { + if (!TextUtils.isEmpty(itemName) && !TextUtils.isEmpty(appPackage)) { + OsmandSettings settings = app.getSettings(); + settings.API_NAV_DRAWER_ITEM_NAME.set(itemName); + settings.API_NAV_DRAWER_ITEM_APP_PACKAGE.set(appPackage); + settings.API_NAV_DRAWER_ITEM_FLAGS.set(flags); + return true; + } + return false; + } + + public void registerNavDrawerItem(final Activity activity, ContextMenuAdapter adapter) { + final OsmandSettings settings = app.getSettings(); + final String itemName = settings.API_NAV_DRAWER_ITEM_NAME.get(); + final String appPackage = settings.API_NAV_DRAWER_ITEM_APP_PACKAGE.get(); + if (!TextUtils.isEmpty(itemName) && !TextUtils.isEmpty(appPackage)) { + final Intent intent = activity.getPackageManager().getLaunchIntentForPackage(appPackage); + if (intent != null) { + int flags = settings.API_NAV_DRAWER_ITEM_FLAGS.get(); + if (flags != -1) { + intent.addFlags(flags); + } + + adapter.addItem(new ContextMenuItem.ItemBuilder() + .setTitle(itemName) + .setListener(new ContextMenuAdapter.ItemClickListener() { + @Override + public boolean onContextMenuClick(ArrayAdapter adapter, int itemId, int position, boolean isChecked, int[] viewCoordinates) { + activity.startActivity(intent); + return true; + } + }) + .createItem()); + } + } + } } diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java index 8da6e91a2a..6bc3759877 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java @@ -34,6 +34,7 @@ import net.osmand.aidl.mapmarker.UpdateMapMarkerParams; import net.osmand.aidl.mapwidget.AddMapWidgetParams; import net.osmand.aidl.mapwidget.RemoveMapWidgetParams; import net.osmand.aidl.mapwidget.UpdateMapWidgetParams; +import net.osmand.aidl.navdrawer.AddOpenAppNavDrawerItemParams; import net.osmand.aidl.navigation.NavigateGpxParams; import net.osmand.aidl.navigation.NavigateParams; import net.osmand.aidl.note.StartAudioRecordingParams; @@ -457,5 +458,15 @@ public class OsmandAidlService extends Service { return false; } } + + @Override + public boolean addOpenAppNavDrawerItem(AddOpenAppNavDrawerItemParams params) throws RemoteException { + try { + return params != null && getApi("addOpenAppNavDrawerItem").addOpenAppNavDrawerItem(params.getItemName(), params.getAppPackage(), params.getFlags()); + } catch (Exception e) { + handleException(e); + return false; + } + } }; } diff --git a/OsmAnd/src/net/osmand/aidl/navdrawer/AddOpenAppNavDrawerItemParams.aidl b/OsmAnd/src/net/osmand/aidl/navdrawer/AddOpenAppNavDrawerItemParams.aidl new file mode 100644 index 0000000000..212c3ddcb0 --- /dev/null +++ b/OsmAnd/src/net/osmand/aidl/navdrawer/AddOpenAppNavDrawerItemParams.aidl @@ -0,0 +1,3 @@ +package net.osmand.aidl.navdrawer; + +parcelable AddOpenAppNavDrawerItemParams; \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/aidl/navdrawer/AddOpenAppNavDrawerItemParams.java b/OsmAnd/src/net/osmand/aidl/navdrawer/AddOpenAppNavDrawerItemParams.java new file mode 100644 index 0000000000..55fcacfc5f --- /dev/null +++ b/OsmAnd/src/net/osmand/aidl/navdrawer/AddOpenAppNavDrawerItemParams.java @@ -0,0 +1,63 @@ +package net.osmand.aidl.navdrawer; + +import android.os.Parcel; +import android.os.Parcelable; + +public class AddOpenAppNavDrawerItemParams implements Parcelable { + + private String itemName; + private String appPackage; + private int flags; + + public AddOpenAppNavDrawerItemParams(String itemName, String appPackage) { + this(itemName, appPackage, -1); + } + + public AddOpenAppNavDrawerItemParams(String itemName, String appPackage, int flags) { + this.itemName = itemName; + this.appPackage = appPackage; + this.flags = flags; + } + + protected AddOpenAppNavDrawerItemParams(Parcel in) { + itemName = in.readString(); + appPackage = in.readString(); + flags = in.readInt(); + } + + public String getItemName() { + return itemName; + } + + public String getAppPackage() { + return appPackage; + } + + public int getFlags() { + return flags; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeString(itemName); + dest.writeString(appPackage); + dest.writeInt(flags); + } + + @Override + public int describeContents() { + return 0; + } + + public static final Creator CREATOR = new Creator() { + @Override + public AddOpenAppNavDrawerItemParams createFromParcel(Parcel in) { + return new AddOpenAppNavDrawerItemParams(in); + } + + @Override + public AddOpenAppNavDrawerItemParams[] newArray(int size) { + return new AddOpenAppNavDrawerItemParams[size]; + } + }; +} diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index f4c373cb3f..07f83583b6 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -742,6 +742,10 @@ public class OsmandSettings { public final CommonPreference SHOW_DASHBOARD_ON_START = new BooleanPreference("should_show_dashboard_on_start", false).makeGlobal(); public final CommonPreference SHOW_DASHBOARD_ON_MAP_SCREEN = new BooleanPreference("show_dashboard_on_map_screen", false).makeGlobal(); + public final CommonPreference API_NAV_DRAWER_ITEM_NAME = new StringPreference("api_nav_drawer_item_name", "").makeGlobal(); + public final CommonPreference API_NAV_DRAWER_ITEM_APP_PACKAGE = new StringPreference("api_nav_drawer_item_app_package", "").makeGlobal(); + public final CommonPreference API_NAV_DRAWER_ITEM_FLAGS = new IntPreference("api_nav_drawer_item_flags", -1).makeGlobal(); + public final CommonPreference NUMBER_OF_STARTS_FIRST_XMAS_SHOWN = new IntPreference("number_of_starts_first_xmas_shown", 0).makeGlobal(); // this value string is synchronized with settings_pref.xml preference name diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java index 77abd91a0d..218c1a36c1 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java @@ -795,6 +795,8 @@ public class MapActivityActions implements DialogProvider { } }).createItem()); + app.getAidlApi().registerNavDrawerItem(mapActivity, optionsMenuHelper); + optionsMenuHelper.addItem(new ItemBuilder().setTitleId(R.string.layer_map_appearance, mapActivity) .setIcon(R.drawable.ic_configure_screen_dark) .setListener(new ContextMenuAdapter.ItemClickListener() {