OsmAnd/plugins/Osmand-SRTMPlugin/src/net/osmand/srtmPlugin/SRTMPluginActivity.java

86 lines
3 KiB
Java
Raw Normal View History

2012-12-11 00:41:08 +01:00
package net.osmand.srtmPlugin;
2016-08-14 23:41:23 +02:00
2012-12-11 00:41:08 +01:00
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
2016-12-09 17:15:08 +01:00
import android.content.Context;
2012-12-11 00:41:08 +01:00
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
2016-12-09 17:15:08 +01:00
import android.text.Html;
import android.view.View;
import android.widget.TextView;
import net.osmand.srtmPlugin.paid.R;
import java.lang.reflect.Method;
2012-12-11 00:41:08 +01:00
public class SRTMPluginActivity extends Activity {
private static final String OSMAND_COMPONENT = "net.osmand"; //$NON-NLS-1$
private static final String OSMAND_COMPONENT_PLUS = "net.osmand.plus"; //$NON-NLS-1$
2016-12-11 15:23:56 +01:00
private static final String OSMAND_ACTIVITY = "net.osmand.plus.activities.MapActivity"; //$NON-NLS-1$
2012-12-11 00:41:08 +01:00
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
2016-12-09 17:15:08 +01:00
TextView descriptionTextView = (TextView) findViewById(R.id.descriptionTextView);
descriptionTextView.setText(Html.fromHtml(getString(R.string.plugin_description)));
2012-12-11 00:41:08 +01:00
Intent intentPlus = new Intent();
intentPlus.setComponent(new ComponentName(OSMAND_COMPONENT_PLUS, OSMAND_ACTIVITY));
intentPlus.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
ResolveInfo resolved = getPackageManager().resolveActivity(intentPlus, PackageManager.MATCH_DEFAULT_ONLY);
if(resolved != null) {
2016-12-09 17:15:08 +01:00
logEvent(this, "open_osmand_plus");
2012-12-11 00:41:08 +01:00
stopService(intentPlus);
startActivity(intentPlus);
2016-12-09 17:15:08 +01:00
finish();
2012-12-11 00:41:08 +01:00
} else {
Intent intentNormal = new Intent();
intentNormal.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
intentNormal.setComponent(new ComponentName(OSMAND_COMPONENT, OSMAND_ACTIVITY));
resolved = getPackageManager().resolveActivity(intentNormal, PackageManager.MATCH_DEFAULT_ONLY);
if (resolved != null) {
2016-12-09 17:15:08 +01:00
logEvent(this, "open_osmand");
2012-12-11 00:41:08 +01:00
stopService(intentNormal);
startActivity(intentNormal);
2016-12-09 17:15:08 +01:00
finish();
2012-12-11 00:41:08 +01:00
} else {
2016-12-09 17:15:08 +01:00
logEvent(this, "open_dialog");
findViewById(R.id.buyButton).setOnClickListener(new View.OnClickListener() {
2012-12-11 00:41:08 +01:00
@Override
2016-12-09 17:15:08 +01:00
public void onClick(View v) {
String appName = OSMAND_COMPONENT;
logEvent(SRTMPluginActivity.this, "open_play_store_" + appName);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:" + appName));
2012-12-11 00:41:08 +01:00
try {
2016-12-09 17:15:08 +01:00
//stopService(intent);
2012-12-11 00:41:08 +01:00
startActivity(intent);
2016-12-09 17:15:08 +01:00
finish();
2012-12-11 00:41:08 +01:00
} catch (ActivityNotFoundException e) {
2016-12-09 17:15:08 +01:00
// ignore
2012-12-11 00:41:08 +01:00
}
}
});
}
}
}
2016-12-09 17:15:08 +01:00
public void logEvent(Activity ctx, String event) {
try {
Class<?> cl = Class.forName("com.google.firebase.analytics.FirebaseAnalytics");
Method mm = cl.getMethod("getInstance", Context.class);
Object inst = mm.invoke(null, ctx == null ? this : ctx);
Method log = cl.getMethod("logEvent", String.class, Bundle.class);
log.invoke(inst, event, new Bundle());
} catch (Exception e) {
e.printStackTrace();
}
}
2012-12-11 00:41:08 +01:00
}