OsmAnd/plugins/Osmand-ParkingPlugin/src/net/osmand/parkingPlugin/ParkingPluginActivity.java

84 lines
3 KiB
Java
Raw Normal View History

package net.osmand.parkingPlugin;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
2016-12-09 17:10:52 +01:00
import android.content.Context;
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:10:52 +01:00
import android.text.Html;
import android.view.View;
import android.widget.TextView;
import java.lang.reflect.Method;
public class ParkingPluginActivity 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$
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
2016-12-09 17:10:52 +01:00
TextView descriptionTextView = (TextView) findViewById(R.id.descriptionTextView);
descriptionTextView.setText(Html.fromHtml(getString(R.string.plugin_description)));
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:10:52 +01:00
logEvent(this, "open_osmand_plus");
stopService(intentPlus);
startActivity(intentPlus);
2016-12-09 17:10:52 +01:00
finish();
} 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:10:52 +01:00
logEvent(this, "open_osmand");
stopService(intentNormal);
startActivity(intentNormal);
2016-12-09 17:10:52 +01:00
finish();
} else {
2016-12-09 17:10:52 +01:00
logEvent(this, "open_dialog");
findViewById(R.id.buyButton).setOnClickListener(new View.OnClickListener() {
@Override
2016-12-09 17:10:52 +01:00
public void onClick(View v) {
String appName = OSMAND_COMPONENT;
logEvent(ParkingPluginActivity.this, "open_play_store_" + appName);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:" + appName));
try {
2016-12-09 17:10:52 +01:00
//stopService(intent);
startActivity(intent);
2016-12-09 17:10:52 +01:00
finish();
} catch (ActivityNotFoundException e) {
2016-12-09 17:10:52 +01:00
// ignore
}
}
});
}
}
}
2016-12-09 17:10:52 +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();
}
}
}