Merge remote-tracking branch 'origin/master'

This commit is contained in:
Weblate 2016-11-13 15:43:21 +01:00
commit 714a61cfd8
6 changed files with 233 additions and 10 deletions

View file

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="24dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="@string/install_osmand_q"/>
<Button
android:id="@+id/install_osmand_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Get OsmAnd"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/install_osmand_note"/>
</LinearLayout>
</ScrollView>
</LinearLayout>

View file

@ -1,5 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="install_maps_title">Install maps</string>
<string name="install_maps_desc">You do not have maps in the OsmAnd application. Please download map(s) first and restart the Sample.\n\nDo not forget to set OsmAnd General settings > Data storage folder to Shared memory.</string>
<string name="shared_string_cancel">Cancel</string>
<string name="shared_string_error">Error</string>
<string name="restart_app">Restart app</string>
<string name="install_osmand_title">Install OsmAnd</string>
<string name="install_osmand_q">The OsmAnd application not found. Please install OsmAnd and download maps.</string>
<string name="install_osmand_note">NOTE: After OsmAnd will be installed, you need to set OsmAnd General settings > Data storage folder to Shared memory</string>
<string name="app_name">OsmAnd Core API sample 1 for Android OS</string>
<string name="title_activity_main">OsmAnd Core Sample 1</string>
<string name="zoomIn">Zoom in</string>

View file

@ -1,5 +1,5 @@
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>

View file

@ -0,0 +1,79 @@
package net.osmand.core.samples.android.sample1;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.View;
public class InstallOsmAndAppDialog extends AppCompatDialogFragment {
private static final String TAG = "InstallOsmAndAppDialog";
private static final String OSMAND_PLUS_PACKAGE_NAME = "net.osmand.plus";
private static final String OSMAND_PACKAGE_NAME = "net.osmand";
private static boolean wasChecked = false;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.install_osmand_title);
builder.setNegativeButton(R.string.shared_string_cancel, null);
builder.setPositiveButton(getString(R.string.restart_app), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
SampleUtils.doRestart(getActivity());
}
});
View view = getActivity().getLayoutInflater().inflate(R.layout.install_osmand_dialog, null);
view.findViewById(R.id.install_osmand_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean success = execOsmAndInstall("market://search?q=pname:");
if (!success) {
success = execOsmAndInstall("https://play.google.com/store/apps/details?id=");
}
if (!success) {
execOsmAndInstall("http://osmand.net/apps?id=");
}
}
});
builder.setView(view);
return builder.create();
}
private boolean execOsmAndInstall(String prefix) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(prefix + OSMAND_PACKAGE_NAME));
try {
getActivity().startActivity(intent);
return true;
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
return false;
}
public static boolean showIfNeeded(FragmentManager manager, Context ctx) {
if (wasChecked) {
return false;
}
wasChecked = true;
if (!SampleUtils.isPackageInstalled(OSMAND_PACKAGE_NAME, ctx)
&& !SampleUtils.isPackageInstalled(OSMAND_PLUS_PACKAGE_NAME, ctx)) {
new InstallOsmAndAppDialog().show(manager, TAG);
return true;
} else {
return false;
}
}
}

View file

@ -2,7 +2,7 @@ package net.osmand.core.samples.android.sample1;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager;
@ -14,6 +14,8 @@ import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorListener;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
@ -66,13 +68,14 @@ import net.osmand.search.core.SearchSettings;
import net.osmand.util.MapUtils;
import java.io.File;
import java.io.FilenameFilter;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
public class MainActivity extends Activity {
public class MainActivity extends AppCompatActivity {
private static final String TAG = "OsmAndCoreSample";
private float displayDensityFactor;
@ -113,6 +116,8 @@ public class MainActivity extends Activity {
private SearchListAdapter adapter;
private String queryText = "";
private boolean noMapsFound;
// Germany
private final static float INIT_LAT = 49.353953f;
private final static float INIT_LON = 11.214384f;
@ -144,7 +149,7 @@ public class MainActivity extends Activity {
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == SampleApplication.PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE &&
if (requestCode == SampleApplication.PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getSampleApplication().initPoiTypes();
}
@ -162,8 +167,9 @@ public class MainActivity extends Activity {
// Inflate views
setContentView(R.layout.activity_main);
if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
boolean externalStoragePermissionGranted = ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
if (!externalStoragePermissionGranted) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
SampleApplication.PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
@ -342,6 +348,37 @@ public class MainActivity extends Activity {
}
});
if (!InstallOsmAndAppDialog.showIfNeeded(getSupportFragmentManager(), this)
&& externalStoragePermissionGranted) {
checkMapsInstalled();
}
}
private void checkMapsInstalled() {
File mapsDir = new File(getSampleApplication().getAbsoluteAppPath());
if (mapsDir.exists()) {
File[] maps = mapsDir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
return filename.toLowerCase().endsWith(".obf");
}
});
noMapsFound = maps == null || maps.length == 0;
}
if (noMapsFound) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.install_maps_title);
builder.setMessage(R.string.install_maps_desc);
builder.setNegativeButton(R.string.shared_string_cancel, null);
builder.setPositiveButton(R.string.restart_app, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SampleUtils.doRestart(MainActivity.this);
}
});
builder.create().show();
}
}
private void setupSearch() {
@ -382,7 +419,6 @@ public class MainActivity extends Activity {
@Override
protected void onResume() {
super.onResume();
mapView.handleOnResume();
}
@ -390,14 +426,12 @@ public class MainActivity extends Activity {
protected void onPause() {
saveMapState();
mapView.handleOnPause();
super.onPause();
}
@Override
protected void onDestroy() {
mapView.handleOnDestroy();
super.onDestroy();
}
@ -538,7 +572,7 @@ public class MainActivity extends Activity {
private void runSearch(String text) {
SearchSettings settings = searchUICore.getPhrase().getSettings();
if(settings.getRadiusLevel() != 1){
if (settings.getRadiusLevel() != 1) {
searchUICore.updateSettings(settings.setRadiusLevel(1));
}
searchUICore.search(text, null);

View file

@ -0,0 +1,62 @@
package net.osmand.core.samples.android.sample1;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
public class SampleUtils {
public static void doRestart(Context c) {
boolean res = false;
try {
//check if the context is given
if (c != null) {
//fetch the packagemanager so we can get the default launch activity
// (you can replace this intent with any other activity if you want
PackageManager pm = c.getPackageManager();
//check if we got the PackageManager
if (pm != null) {
//create the intent with the default start activity for your application
Intent mStartActivity = pm.getLaunchIntentForPackage(
c.getPackageName()
);
if (mStartActivity != null) {
mStartActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//create a pending intent so the application is restarted after System.exit(0) was called.
// We use an AlarmManager to call this intent in 100ms
int mPendingIntentId = 84523123;
PendingIntent mPendingIntent = PendingIntent
.getActivity(c, mPendingIntentId, mStartActivity,
PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
//kill the application
res = true;
android.os.Process.killProcess(android.os.Process.myPid());
//System.exit(0);
}
}
}
} catch (Exception ex) {
//ignore
}
if (!res) {
android.os.Process.killProcess(android.os.Process.myPid());
}
}
public static boolean isPackageInstalled(String packageInfo, Context ctx) {
if (packageInfo == null) {
return false;
}
boolean installed = false;
try {
installed = ctx.getPackageManager().getPackageInfo(packageInfo, 0) != null;
} catch (PackageManager.NameNotFoundException e) {
//ignore
}
return installed;
}
}