forked from organicmaps/organicmaps
[android] Added catalog webview activity
This commit is contained in:
parent
b1fc7a742c
commit
699ce5f655
8 changed files with 189 additions and 17 deletions
7
android/res/layout/fragment_bookmarks_catalog.xml
Normal file
7
android/res/layout/fragment_bookmarks_catalog.xml
Normal file
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<WebView
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/webview"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
</WebView>
|
|
@ -0,0 +1,33 @@
|
|||
package com.mapswithme.maps.auth;
|
||||
|
||||
import android.support.annotation.IdRes;
|
||||
import android.view.View;
|
||||
import android.webkit.WebView;
|
||||
|
||||
import com.mapswithme.maps.R;
|
||||
import com.mapswithme.maps.base.BaseMwmFragment;
|
||||
import com.mapswithme.maps.base.OnBackPressListener;
|
||||
import com.mapswithme.util.Utils;
|
||||
|
||||
public class BaseWebViewMwmFragment extends BaseMwmFragment implements OnBackPressListener
|
||||
{
|
||||
@Override
|
||||
public boolean onBackPressed()
|
||||
{
|
||||
View root;
|
||||
WebView webView = null;
|
||||
boolean goBackAllowed = (root = getView()) != null
|
||||
&& (webView = Utils.castTo(root.findViewById(getWebViewResId()))) != null
|
||||
&& webView.canGoBack();
|
||||
if (goBackAllowed){
|
||||
webView.goBack();
|
||||
}
|
||||
return goBackAllowed;
|
||||
}
|
||||
|
||||
@IdRes
|
||||
protected int getWebViewResId()
|
||||
{
|
||||
return R.id.webview;
|
||||
}
|
||||
}
|
|
@ -15,11 +15,9 @@ import android.webkit.WebViewClient;
|
|||
|
||||
import com.mapswithme.maps.Framework;
|
||||
import com.mapswithme.maps.R;
|
||||
import com.mapswithme.maps.base.BaseMwmFragment;
|
||||
import com.mapswithme.maps.base.OnBackPressListener;
|
||||
import com.mapswithme.util.UiUtils;
|
||||
|
||||
public class PhoneAuthFragment extends BaseMwmFragment implements OnBackPressListener
|
||||
public class PhoneAuthFragment extends BaseWebViewMwmFragment
|
||||
{
|
||||
private static final String REDIRECT_URL = "http://localhost";
|
||||
|
||||
|
@ -44,7 +42,7 @@ public class PhoneAuthFragment extends BaseMwmFragment implements OnBackPressLis
|
|||
{
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
|
||||
mWebView = view.findViewById(R.id.webview);
|
||||
mWebView = view.findViewById(getWebViewResId());
|
||||
mProgress = view.findViewById(R.id.progress);
|
||||
mWebView.setWebViewClient(new WebViewClient()
|
||||
{
|
||||
|
@ -77,14 +75,4 @@ public class PhoneAuthFragment extends BaseMwmFragment implements OnBackPressLis
|
|||
mWebView.getSettings().setJavaScriptEnabled(true);
|
||||
mWebView.loadUrl(Framework.nativeGetPhoneAuthUrl(REDIRECT_URL));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onBackPressed()
|
||||
{
|
||||
if (!mWebView.canGoBack())
|
||||
return false;
|
||||
|
||||
mWebView.goBack();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ import android.support.v4.app.Fragment;
|
|||
|
||||
import com.mapswithme.util.Utils;
|
||||
|
||||
public class BaseMwmFragment extends Fragment
|
||||
public class BaseMwmFragment extends Fragment implements OnBackPressListener
|
||||
{
|
||||
|
||||
@Override
|
||||
|
@ -32,6 +32,12 @@ public class BaseMwmFragment extends Fragment
|
|||
|
||||
public BaseMwmFragmentActivity getMwmActivity()
|
||||
{
|
||||
return (BaseMwmFragmentActivity) getActivity();
|
||||
return Utils.castTo(getActivity());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onBackPressed()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ import android.support.annotation.NonNull;
|
|||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.StyleRes;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.view.MenuItem;
|
||||
|
@ -221,6 +222,37 @@ public abstract class BaseMwmFragmentActivity extends AppCompatActivity
|
|||
setSupportActionBar(getToolbar());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed()
|
||||
{
|
||||
FragmentManager manager = getSupportFragmentManager();
|
||||
if (getFragmentClass() == null)
|
||||
{
|
||||
super.onBackPressed();
|
||||
return;
|
||||
}
|
||||
String name = getFragmentClass().getName();
|
||||
Fragment fragment = manager.findFragmentByTag(name);
|
||||
|
||||
if (fragment == null)
|
||||
{
|
||||
super.onBackPressed();
|
||||
return;
|
||||
}
|
||||
|
||||
boolean processed = onBackPressedInternal(fragment);
|
||||
if (processed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
super.onBackPressed();
|
||||
}
|
||||
|
||||
protected boolean onBackPressedInternal(@NonNull @SuppressWarnings("unused") Fragment currentFragment)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override to set custom content view.
|
||||
* @return layout resId.
|
||||
|
@ -252,7 +284,6 @@ public abstract class BaseMwmFragmentActivity extends AppCompatActivity
|
|||
.replace(resId, fragment, name)
|
||||
.commitAllowingStateLoss();
|
||||
getSupportFragmentManager().executePendingTransactions();
|
||||
|
||||
if (completionListener != null)
|
||||
completionListener.run();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package com.mapswithme.maps.bookmarks;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.app.Fragment;
|
||||
|
||||
import com.mapswithme.maps.base.BaseToolbarActivity;
|
||||
import com.mapswithme.maps.base.OnBackPressListener;
|
||||
import com.mapswithme.util.Utils;
|
||||
|
||||
public class BookmarksCatalogActivity extends BaseToolbarActivity
|
||||
{
|
||||
@Override
|
||||
protected Class<? extends Fragment> getFragmentClass()
|
||||
{
|
||||
return BookmarksCatalogFragment.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean onBackPressedInternal(@NonNull Fragment currentFragment)
|
||||
{
|
||||
return Utils.<OnBackPressListener>castTo(currentFragment).onBackPressed();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
package com.mapswithme.maps.bookmarks;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.webkit.WebSettings;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
|
||||
import com.mapswithme.maps.R;
|
||||
import com.mapswithme.maps.auth.BaseWebViewMwmFragment;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class BookmarksCatalogFragment extends BaseWebViewMwmFragment
|
||||
{
|
||||
public static final String EXTRA_BOOKMARKS_CATALOG_URL = "bookmarks_catalog_url";
|
||||
@NonNull
|
||||
private String mCatalogUrl;
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState)
|
||||
{
|
||||
super.onCreate(savedInstanceState);
|
||||
Bundle args = getArguments();
|
||||
if ((args == null || (mCatalogUrl = args.getString(EXTRA_BOOKMARKS_CATALOG_URL)) == null)
|
||||
&& (mCatalogUrl = getActivity().getIntent()
|
||||
.getStringExtra(EXTRA_BOOKMARKS_CATALOG_URL)) == null)
|
||||
{
|
||||
throw new IllegalArgumentException("Catalog url not found in bundle");
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater,
|
||||
@Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState)
|
||||
{
|
||||
View root = inflater.inflate(R.layout.bookmarks_catalog_frag, container, false);
|
||||
WebView webView = root.findViewById(getWebViewResId());
|
||||
initWebView(webView);
|
||||
webView.loadUrl(mCatalogUrl);
|
||||
return root;
|
||||
}
|
||||
|
||||
private void initWebView(WebView webView)
|
||||
{
|
||||
webView.setWebViewClient(new WebViewBookmarksCatalogClient());
|
||||
final WebSettings webSettings = webView.getSettings();
|
||||
webSettings.setJavaScriptEnabled(true);
|
||||
}
|
||||
|
||||
private static class WebViewBookmarksCatalogClient extends WebViewClient
|
||||
{
|
||||
@Override
|
||||
public boolean shouldOverrideUrlLoading(WebView view, String url)
|
||||
{
|
||||
try
|
||||
{
|
||||
return requestArchive(view, url);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
return super.shouldOverrideUrlLoading(view, url);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean requestArchive(@NonNull WebView view, @NonNull String url) throws IOException
|
||||
{
|
||||
/* BookmarksDownloadManager dm = BookmarksDownloadManager.from(view.getContext());
|
||||
dm.enqueueRequest(url);*/
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -52,6 +52,11 @@ public class Utils
|
|||
private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC);
|
||||
private static final String TAG = "Utils";
|
||||
|
||||
public static <T> T castTo(Object object)
|
||||
{
|
||||
return (T) object;
|
||||
}
|
||||
|
||||
public interface Proc<T>
|
||||
{
|
||||
void invoke(@NonNull T param);
|
||||
|
|
Loading…
Add table
Reference in a new issue