Add disable plugin dialog

This commit is contained in:
Vitaliy 2020-02-13 13:19:36 +02:00
parent 73eed6b5a8
commit e7aa7b5aac
12 changed files with 211 additions and 33 deletions

View file

@ -6,7 +6,7 @@
android:layout_width="match_parent"
android:layout_height="@dimen/bottom_sheet_title_height"
android:ellipsize="end"
android:gravity="bottom"
android:gravity="center_vertical"
android:letterSpacing="@dimen/text_button_letter_spacing"
android:maxLines="1"
android:minHeight="@dimen/bottom_sheet_title_height"

View file

@ -11,6 +11,9 @@
Thx - Hardy
-->
<string name="open_settings">Open settings</string>
<string name="plugin_disabled">Plugin disabled</string>
<string name="plugin_disabled_descr">This plugin is a separate application, you will need to remove it separately if you no longer plan to use it.\n\nThe plugin will remain on the device after removing OsmAnd.</string>
<string name="shared_string_menu">Menu</string>
<string name="ltr_or_rtl_triple_combine_via_dash">%1$s — %2$s — %3$s</string>
<string name="routing_profile_direct_to">Direct-to-point</string>

View file

@ -4,6 +4,7 @@ package net.osmand;
import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.content.res.Configuration;
import android.content.res.Resources;
@ -158,6 +159,10 @@ public class AndroidUtils {
}
}
public static boolean isIntentSafe(Context context, Intent intent) {
return intent.resolveActivity(context.getPackageManager()) != null;
}
public static Spannable replaceCharsWithIcon(String text, Drawable icon, String[] chars) {
Spannable spannable = new SpannableString(text);
for (String entry : chars) {

View file

@ -24,9 +24,9 @@ import net.osmand.plus.activities.TabActivity.TabItem;
import net.osmand.plus.audionotes.AudioVideoNotesPlugin;
import net.osmand.plus.dashboard.tools.DashFragmentData;
import net.osmand.plus.development.OsmandDevelopmentPlugin;
import net.osmand.plus.dialogs.PluginDisabledBottomSheet;
import net.osmand.plus.dialogs.PluginInstalledBottomSheetDialog;
import net.osmand.plus.download.IndexItem;
import net.osmand.plus.inapp.InAppPurchaseHelper;
import net.osmand.plus.mapcontextmenu.MenuBuilder;
import net.osmand.plus.mapcontextmenu.MenuController;
import net.osmand.plus.mapillary.MapillaryPlugin;
@ -166,6 +166,15 @@ public abstract class OsmandPlugin {
}
}
public void showDisableDialog(@Nullable Activity activity) {
if (activity instanceof FragmentActivity) {
FragmentManager fragmentManager = ((FragmentActivity) activity).getSupportFragmentManager();
if (fragmentManager != null) {
PluginDisabledBottomSheet.showInstance(fragmentManager, getId(), activity instanceof MapActivity);
}
}
}
public void disable(OsmandApplication app) {
}
@ -247,7 +256,7 @@ public abstract class OsmandPlugin {
private static boolean updateMarketPlugin(@NonNull OsmandApplication app, @NonNull Set<String> enabledPlugins, @NonNull OsmandPlugin plugin) {
boolean marketEnabled = Version.isMarketEnabled(app);
boolean pckg = checkPluginPackage(app, plugin);
boolean pckg = plugin.pluginAvailable(app);
boolean paid = plugin.isPaid();
if ((Version.isDeveloperVersion(app) || !Version.isProductionVersion(app)) && !paid) {
// for test reasons
@ -276,7 +285,7 @@ public abstract class OsmandPlugin {
public static void checkInstalledMarketPlugins(@NonNull OsmandApplication app, @Nullable Activity activity) {
Set<String> enabledPlugins = app.getSettings().getEnabledPlugins();
for (OsmandPlugin plugin : OsmandPlugin.getMarketPlugins()) {
if (plugin.getInstallURL() != null && checkPluginPackage(app, plugin)) {
if (plugin.getInstallURL() != null && plugin.pluginAvailable(app)) {
plugin.onInstall(app, activity);
initPlugin(app, plugin);
}
@ -284,13 +293,12 @@ public abstract class OsmandPlugin {
}
}
private static boolean checkPluginPackage(OsmandApplication app, OsmandPlugin plugin) {
return plugin.checkPluginPackage(app);
protected boolean pluginAvailable(OsmandApplication app) {
return checkPluginPackage(app, this);
}
protected boolean checkPluginPackage(OsmandApplication app) {
return isPackageInstalled(getComponentId1(), app) || isPackageInstalled(getComponentId2(), app);
public static boolean checkPluginPackage(@NonNull OsmandApplication app, @NonNull OsmandPlugin plugin) {
return isPackageInstalled(plugin.getComponentId1(), app) || isPackageInstalled(plugin.getComponentId2(), app);
}
public static boolean enablePlugin(@Nullable Activity activity, OsmandApplication app, OsmandPlugin plugin, boolean enable) {
@ -306,7 +314,8 @@ public abstract class OsmandPlugin {
plugin.setActive(false);
}
app.getSettings().enablePlugin(plugin.getId(), enable);
if (activity != null && activity instanceof MapActivity) {
if (activity != null) {
if (activity instanceof MapActivity) {
final MapActivity mapActivity = (MapActivity) activity;
plugin.updateLayers(mapActivity.getMapView(), mapActivity);
mapActivity.getDashboard().refreshDashboardFragments();
@ -320,6 +329,15 @@ public abstract class OsmandPlugin {
}
}
}
if (plugin.isMarketPlugin()) {
if (plugin.isActive()) {
plugin.showInstallDialog(activity);
} else if (OsmandPlugin.checkPluginPackage(app, plugin)) {
plugin.showDisableDialog(activity);
}
}
}
return true;
}

View file

@ -81,9 +81,6 @@ public class PluginsActivity extends OsmandListActivity implements DownloadIndex
listModified = true;
}
getListAdapter().notifyDataSetChanged();
if (plugin.isActive() && plugin.isMarketPlugin()) {
plugin.showInstallDialog(this);
}
}
}
@ -206,7 +203,7 @@ public class PluginsActivity extends OsmandListActivity implements DownloadIndex
if (active) {
pluginLogo.setBackgroundResource(isLightTheme ? R.drawable.bg_plugin_logo_enabled_light : R.drawable.bg_plugin_logo_enabled_dark);
} else {
TypedArray attributes = getTheme().obtainStyledAttributes(new int[]{R.attr.bg_plugin_logo_disabled});
TypedArray attributes = getTheme().obtainStyledAttributes(new int[] {R.attr.bg_plugin_logo_disabled});
pluginLogo.setBackgroundDrawable(attributes.getDrawable(0));
attributes.recycle();
}

View file

@ -32,7 +32,7 @@ public class BottomSheetItemTitleWithDescrAndButton extends BottomSheetItemWithD
int position,
Drawable icon,
Drawable background,
String title,
CharSequence title,
@ColorRes int titleColorId,
CharSequence description,
@ColorRes int descriptionColorId,

View file

@ -37,7 +37,7 @@ public class BottomSheetItemWithCompoundButton extends BottomSheetItemWithDescri
int position,
Drawable icon,
Drawable background,
String title,
CharSequence title,
@ColorRes int titleColorId,
CharSequence description,
@ColorRes int descriptionColorId,

View file

@ -30,7 +30,7 @@ public class BottomSheetItemWithDescription extends SimpleBottomSheetItem {
int position,
Drawable icon,
Drawable background,
String title,
CharSequence title,
@ColorRes int titleColorId,
CharSequence description,
@ColorRes int descriptionColorId,

View file

@ -17,7 +17,7 @@ public class SimpleBottomSheetItem extends BaseBottomSheetItem {
private Drawable background;
private Drawable icon;
protected String title;
protected CharSequence title;
@ColorRes
protected int titleColorId = INVALID_ID;
@ -32,7 +32,7 @@ public class SimpleBottomSheetItem extends BaseBottomSheetItem {
int position,
Drawable icon,
Drawable background,
String title,
CharSequence title,
@ColorRes int titleColorId) {
super(customView, layoutId, tag, disabled, onClickListener, position);
this.icon = icon;
@ -83,7 +83,7 @@ public class SimpleBottomSheetItem extends BaseBottomSheetItem {
protected Drawable icon;
protected Drawable background;
protected String title;
protected CharSequence title;
@ColorRes
protected int titleColorId = INVALID_ID;
@ -97,7 +97,7 @@ public class SimpleBottomSheetItem extends BaseBottomSheetItem {
return this;
}
public Builder setTitle(String title) {
public Builder setTitle(CharSequence title) {
this.title = title;
return this;
}

View file

@ -0,0 +1,147 @@
package net.osmand.plus.dialogs;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.text.SpannableString;
import net.osmand.AndroidUtils;
import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.R;
import net.osmand.plus.base.MenuBottomSheetDialogFragment;
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem;
import net.osmand.plus.helpers.FontCache;
import net.osmand.plus.widgets.style.CustomTypefaceSpan;
import org.apache.commons.logging.Log;
import static net.osmand.plus.OsmandPlugin.PLUGIN_ID_KEY;
public class PluginDisabledBottomSheet extends MenuBottomSheetDialogFragment {
public static final String TAG = PluginDisabledBottomSheet.class.getName();
private static final Log LOG = PlatformUtil.getLog(PluginDisabledBottomSheet.class);
private String pluginId;
@Override
public void createMenuItems(Bundle savedInstanceState) {
final OsmandApplication app = getMyApplication();
Context context = getContext();
if (context == null || app == null) {
return;
}
if (savedInstanceState != null) {
pluginId = savedInstanceState.getString(PLUGIN_ID_KEY);
} else {
Bundle args = getArguments();
if (args != null) {
pluginId = args.getString(PLUGIN_ID_KEY);
}
}
OsmandPlugin plugin = OsmandPlugin.getPlugin(pluginId);
if (plugin == null) {
return;
}
BaseBottomSheetItem titleItem = new TitleItem.Builder()
.setTitle(getString(R.string.plugin_disabled))
.setLayoutId(R.layout.bottom_sheet_item_title_big)
.create();
items.add(titleItem);
Typeface typeface = FontCache.getRobotoMedium(getContext());
SpannableString pluginTitleSpan = new SpannableString(plugin.getName());
pluginTitleSpan.setSpan(new CustomTypefaceSpan(typeface), 0, pluginTitleSpan.length(), 0);
BaseBottomSheetItem pluginTitle = new SimpleBottomSheetItem.Builder()
.setTitle(pluginTitleSpan)
.setTitleColorId(nightMode ? R.color.active_color_primary_dark : R.color.active_color_primary_light)
.setIcon(getContentIcon(R.drawable.ic_extension_dark))
.setLayoutId(R.layout.bottom_sheet_item_simple_56dp)
.create();
items.add(pluginTitle);
BaseBottomSheetItem descrItem = new SimpleBottomSheetItem.Builder()
.setTitle(getString(R.string.plugin_disabled_descr))
.setLayoutId(R.layout.bottom_sheet_item_title_long)
.create();
items.add(descrItem);
}
@Override
protected int getDismissButtonTextId() {
return R.string.shared_string_close;
}
@Override
protected int getRightBottomButtonTextId() {
return R.string.open_settings;
}
@Override
protected void onRightBottomButtonClick() {
OsmandApplication app = getMyApplication();
Intent intent = getPluginSettingsIntent();
if (app != null && AndroidUtils.isIntentSafe(app, intent)) {
startActivity(intent);
}
dismiss();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(PLUGIN_ID_KEY, pluginId);
}
private Intent getPluginSettingsIntent() {
Intent intent = null;
OsmandApplication app = getMyApplication();
OsmandPlugin plugin = OsmandPlugin.getPlugin(pluginId);
if (plugin != null && app != null) {
String installedPackage = null;
if (OsmandPlugin.isPackageInstalled(plugin.getComponentId1(), app)) {
installedPackage = plugin.getComponentId1();
}
if (OsmandPlugin.isPackageInstalled(plugin.getComponentId2(), app)) {
installedPackage = plugin.getComponentId2();
}
if (installedPackage != null) {
intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromParts("package", installedPackage, null);
intent.setData(uri);
}
}
return intent;
}
public static void showInstance(@NonNull FragmentManager fm, String pluginId, Boolean usedOnMap) {
try {
Bundle args = new Bundle();
args.putString(PLUGIN_ID_KEY, pluginId);
PluginDisabledBottomSheet dialog = new PluginDisabledBottomSheet();
dialog.setArguments(args);
dialog.setUsedOnMap(usedOnMap);
dialog.show(fm, PluginDisabledBottomSheet.TAG);
} catch (RuntimeException e) {
LOG.error("showInstance", e);
}
}
}

View file

@ -2,9 +2,11 @@ package net.osmand.plus.dialogs;
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.text.SpannableString;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
@ -29,7 +31,9 @@ import net.osmand.plus.download.DownloadIndexesThread;
import net.osmand.plus.download.DownloadValidationManager;
import net.osmand.plus.download.IndexItem;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.helpers.FontCache;
import net.osmand.plus.settings.BaseSettingsFragment;
import net.osmand.plus.widgets.style.CustomTypefaceSpan;
import org.apache.commons.logging.Log;
@ -79,8 +83,12 @@ public class PluginInstalledBottomSheetDialog extends MenuBottomSheetDialogFragm
.create();
items.add(titleItem);
Typeface typeface = FontCache.getRobotoMedium(getContext());
SpannableString pluginTitleSpan = new SpannableString(plugin.getName());
pluginTitleSpan.setSpan(new CustomTypefaceSpan(typeface), 0, pluginTitleSpan.length(), 0);
BaseBottomSheetItem pluginTitle = new SimpleBottomSheetItem.Builder()
.setTitle(plugin.getName())
.setTitle(pluginTitleSpan)
.setTitleColorId(nightMode ? R.color.active_color_primary_dark : R.color.active_color_primary_light)
.setIcon(getContentIcon(R.drawable.ic_extension_dark))
.setLayoutId(R.layout.bottom_sheet_item_simple_56dp)

View file

@ -89,8 +89,8 @@ public class SRTMPlugin extends OsmandPlugin {
}
@Override
protected boolean checkPluginPackage(OsmandApplication app) {
return super.checkPluginPackage(app) || InAppPurchaseHelper.isSubscribedToLiveUpdates(app);
protected boolean pluginAvailable(OsmandApplication app) {
return super.pluginAvailable(app) || InAppPurchaseHelper.isSubscribedToLiveUpdates(app);
}
@Override