mirror of
https://codeberg.org/comaps/comaps
synced 2025-12-21 05:43:37 +00:00
[android] Get rid of MapFragment
Signed-off-by: Andrei Shkrob <github@shkrob.dev>
This commit is contained in:
@@ -21,7 +21,6 @@ public final class Map
|
||||
void report();
|
||||
}
|
||||
|
||||
public static final String ARG_LAUNCH_BY_DEEP_LINK = "launch_by_deep_link";
|
||||
private static final String TAG = Map.class.getSimpleName();
|
||||
|
||||
// Should correspond to android::MultiTouchAction from Framework.cpp
|
||||
@@ -220,11 +219,10 @@ public final class Map
|
||||
mMapRenderingListener.onRenderingRestored();
|
||||
}
|
||||
|
||||
public void onSurfaceDestroyed(boolean activityIsChangingConfigurations, boolean isAdded)
|
||||
public void onSurfaceDestroyed(boolean activityIsChangingConfigurations)
|
||||
{
|
||||
Logger.d(TAG, "mSurfaceCreated = " + mSurfaceCreated + ", mSurfaceAttached = " + mSurfaceAttached
|
||||
+ ", isAdded = " + isAdded);
|
||||
if (!mSurfaceCreated || !mSurfaceAttached || !isAdded)
|
||||
Logger.d(TAG, "mSurfaceCreated = " + mSurfaceCreated + ", mSurfaceAttached = " + mSurfaceAttached);
|
||||
if (!mSurfaceCreated || !mSurfaceAttached)
|
||||
return;
|
||||
|
||||
nativeDetachSurface(!activityIsChangingConfigurations);
|
||||
@@ -261,7 +259,7 @@ public final class Map
|
||||
nativeSetRenderingInitializationFinishedListener(null);
|
||||
}
|
||||
|
||||
public void onPause(final Context context)
|
||||
public void onPause()
|
||||
{
|
||||
mUiThemeOnPause = Config.UiTheme.getCurrent();
|
||||
|
||||
|
||||
102
android/sdk/src/main/java/app/organicmaps/sdk/MapController.java
Normal file
102
android/sdk/src/main/java/app/organicmaps/sdk/MapController.java
Normal file
@@ -0,0 +1,102 @@
|
||||
package app.organicmaps.sdk;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.lifecycle.DefaultLifecycleObserver;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import app.organicmaps.sdk.location.LocationHelper;
|
||||
import app.organicmaps.sdk.util.log.Logger;
|
||||
|
||||
public class MapController implements DefaultLifecycleObserver
|
||||
{
|
||||
private static final String TAG = MapController.class.getSimpleName();
|
||||
|
||||
private final MapView mMapView;
|
||||
private final Map mMap;
|
||||
|
||||
@Nullable
|
||||
private Runnable mOnSurfaceDestroyedListener = null;
|
||||
|
||||
public MapController(@NonNull MapView mapView, @NonNull LocationHelper locationHelper,
|
||||
@NonNull MapRenderingListener mapRenderingListener,
|
||||
@NonNull Map.CallbackUnsupported callbackUnsupported, boolean launchByDeepLink)
|
||||
{
|
||||
mMapView = mapView;
|
||||
mMap = mMapView.getMap();
|
||||
mMap.onCreate(launchByDeepLink);
|
||||
mMap.setLocationHelper(locationHelper);
|
||||
mMap.setMapRenderingListener(mapRenderingListener);
|
||||
mMap.setCallbackUnsupported(callbackUnsupported);
|
||||
}
|
||||
|
||||
public MapView getView()
|
||||
{
|
||||
return mMapView;
|
||||
}
|
||||
|
||||
public boolean isRenderingActive()
|
||||
{
|
||||
return Map.isEngineCreated() && mMap.isContextCreated();
|
||||
}
|
||||
|
||||
public void updateCompassOffset(int offsetX, int offsetY)
|
||||
{
|
||||
mMap.updateCompassOffset(mMapView.getContext(), offsetX, offsetY, true);
|
||||
}
|
||||
|
||||
public void updateBottomWidgetsOffset(int offsetX, int offsetY)
|
||||
{
|
||||
mMap.updateBottomWidgetsOffset(mMapView.getContext(), offsetX, offsetY);
|
||||
}
|
||||
|
||||
public void updateMyPositionRoutingOffset(int offsetY)
|
||||
{
|
||||
mMap.updateMyPositionRoutingOffset(offsetY);
|
||||
}
|
||||
|
||||
public void setOnDestroyListener(@NonNull Runnable task)
|
||||
{
|
||||
mOnSurfaceDestroyedListener = task;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart(@NonNull LifecycleOwner owner)
|
||||
{
|
||||
Logger.d(TAG);
|
||||
mMap.onStart();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume(@NonNull LifecycleOwner owner)
|
||||
{
|
||||
Logger.d(TAG);
|
||||
mMap.onResume();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause(@NonNull LifecycleOwner owner)
|
||||
{
|
||||
Logger.d(TAG);
|
||||
mMap.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop(@NonNull LifecycleOwner owner)
|
||||
{
|
||||
Logger.d(TAG);
|
||||
mMap.onStop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy(@NonNull LifecycleOwner owner)
|
||||
{
|
||||
Logger.d(TAG);
|
||||
mMap.setMapRenderingListener(null);
|
||||
mMap.setCallbackUnsupported(null);
|
||||
if (mOnSurfaceDestroyedListener != null)
|
||||
{
|
||||
mOnSurfaceDestroyedListener.run();
|
||||
mOnSurfaceDestroyedListener = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
177
android/sdk/src/main/java/app/organicmaps/sdk/MapView.java
Normal file
177
android/sdk/src/main/java/app/organicmaps/sdk/MapView.java
Normal file
@@ -0,0 +1,177 @@
|
||||
package app.organicmaps.sdk;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.ContextWrapper;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.view.SurfaceView;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.core.content.res.ConfigurationHelper;
|
||||
import app.organicmaps.sdk.display.DisplayType;
|
||||
import app.organicmaps.sdk.util.Utils;
|
||||
import app.organicmaps.sdk.util.log.Logger;
|
||||
|
||||
public class MapView extends SurfaceView
|
||||
{
|
||||
private static final String TAG = MapView.class.getSimpleName();
|
||||
|
||||
private class SurfaceHolderCallback implements SurfaceHolder.Callback
|
||||
{
|
||||
@Override
|
||||
public void surfaceCreated(@NonNull SurfaceHolder holder)
|
||||
{
|
||||
Logger.d(TAG);
|
||||
mMap.onSurfaceCreated(MapView.this.getContext(), holder.getSurface(), holder.getSurfaceFrame(),
|
||||
ConfigurationHelper.getDensityDpi(MapView.this.getResources()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height)
|
||||
{
|
||||
Logger.d(TAG);
|
||||
mMap.onSurfaceChanged(MapView.this.getContext(), holder.getSurface(), holder.getSurfaceFrame(),
|
||||
holder.isCreating());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceDestroyed(@NonNull SurfaceHolder holder)
|
||||
{
|
||||
Logger.d(TAG);
|
||||
mMap.onSurfaceDestroyed(isHostActivityChangingConfigurations());
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private final Map mMap;
|
||||
|
||||
public MapView(Context context)
|
||||
{
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
public MapView(Context context, AttributeSet attrs)
|
||||
{
|
||||
this(context, attrs, 0);
|
||||
}
|
||||
|
||||
public MapView(Context context, AttributeSet attrs, int defStyleAttr)
|
||||
{
|
||||
this(context, attrs, defStyleAttr, 0);
|
||||
}
|
||||
|
||||
public MapView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
|
||||
{
|
||||
super(context, attrs, defStyleAttr, defStyleRes);
|
||||
mMap = new Map(DisplayType.Device);
|
||||
super.getHolder().addCallback(new SurfaceHolderCallback());
|
||||
}
|
||||
|
||||
public final void onDraw(@NonNull Canvas canvas)
|
||||
{
|
||||
super.onDraw(canvas);
|
||||
if (isInEditMode())
|
||||
drawMapPreview(canvas);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTouchEvent(@NonNull MotionEvent event)
|
||||
{
|
||||
int action = event.getActionMasked();
|
||||
int pointerIndex = event.getActionIndex();
|
||||
switch (action)
|
||||
{
|
||||
case MotionEvent.ACTION_POINTER_UP -> action = Map.NATIVE_ACTION_UP;
|
||||
case MotionEvent.ACTION_UP ->
|
||||
{
|
||||
action = Map.NATIVE_ACTION_UP;
|
||||
pointerIndex = 0;
|
||||
}
|
||||
case MotionEvent.ACTION_POINTER_DOWN -> action = Map.NATIVE_ACTION_DOWN;
|
||||
case MotionEvent.ACTION_DOWN ->
|
||||
{
|
||||
action = Map.NATIVE_ACTION_DOWN;
|
||||
pointerIndex = 0;
|
||||
}
|
||||
case MotionEvent.ACTION_MOVE ->
|
||||
{
|
||||
action = Map.NATIVE_ACTION_MOVE;
|
||||
pointerIndex = Map.INVALID_POINTER_MASK;
|
||||
}
|
||||
case MotionEvent.ACTION_CANCEL -> action = Map.NATIVE_ACTION_CANCEL;
|
||||
}
|
||||
Map.onTouch(action, event, pointerIndex);
|
||||
performClick();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean performClick()
|
||||
{
|
||||
super.performClick();
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final SurfaceHolder getHolder()
|
||||
{
|
||||
throw new UnsupportedOperationException("Not supported.");
|
||||
}
|
||||
|
||||
@NonNull
|
||||
Map getMap()
|
||||
{
|
||||
return mMap;
|
||||
}
|
||||
|
||||
/// The function is called only in the design mode of Android Studio.
|
||||
private void drawMapPreview(@NonNull Canvas canvas)
|
||||
{
|
||||
final int w = getWidth();
|
||||
final int h = getHeight();
|
||||
|
||||
// Background
|
||||
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
paint.setStyle(Paint.Style.FILL);
|
||||
if (Utils.isDarkMode(getContext()))
|
||||
paint.setColor(Color.rgb(30, 30, 30));
|
||||
else
|
||||
paint.setColor(Color.rgb(245, 242, 230));
|
||||
canvas.drawRect(0, 0, w, h, paint);
|
||||
|
||||
// Grid lines (lat/lon)
|
||||
paint.setColor(Color.LTGRAY);
|
||||
paint.setStrokeWidth(2f);
|
||||
final int step = Math.min(w, h) / 6;
|
||||
for (int i = 0; i < Math.max(w, h); i += step)
|
||||
{
|
||||
if (i < w)
|
||||
canvas.drawLine(i, 0, i, h, paint);
|
||||
if (i < h)
|
||||
canvas.drawLine(0, i, w, i, paint);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isHostActivityChangingConfigurations()
|
||||
{
|
||||
Activity activity = findActivity(getContext());
|
||||
return activity != null && activity.isChangingConfigurations();
|
||||
}
|
||||
|
||||
private static Activity findActivity(Context context)
|
||||
{
|
||||
while (context instanceof ContextWrapper)
|
||||
{
|
||||
if (context instanceof Activity)
|
||||
{
|
||||
return (Activity) context;
|
||||
}
|
||||
context = ((ContextWrapper) context).getBaseContext();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import android.content.Intent;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.content.res.Configuration;
|
||||
import android.content.res.Resources;
|
||||
import android.os.Build;
|
||||
import android.text.TextUtils;
|
||||
@@ -346,4 +347,10 @@ public class Utils
|
||||
{
|
||||
return context.getResources().getDimensionPixelSize(id);
|
||||
}
|
||||
|
||||
public static boolean isDarkMode(@NonNull Context context)
|
||||
{
|
||||
return (context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK)
|
||||
== Configuration.UI_MODE_NIGHT_YES;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user