package io.jawg.osmcontributor.flickr.ui;
/**
* Copyright (C) 2016 eBusiness Information
*
* This file is part of OSM Contributor.
*
* OSM Contributor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OSM Contributor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OSM Contributor. If not, see <http://www.gnu.org/licenses/>.
*/
import android.app.DialogFragment;
import android.app.Fragment;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import org.greenrobot.eventbus.EventBus;
import java.lang.reflect.Field;
import butterknife.BindView;
import butterknife.ButterKnife;
import io.jawg.osmcontributor.R;
import io.jawg.osmcontributor.flickr.event.AuthorizeGrantedEvent;
/**
* @author Tommy Buonomo on 21/07/16.
*/
@SuppressWarnings("all")
public class FlickrConnectionDialog extends DialogFragment {
private static final String TAG = "FlickrFragment";
private boolean viewPositionFixed;
@BindView(R.id.dialog_flickr_connection)
WebView webView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.dialog_flickr_connection, container, false);
ButterKnife.bind(this, rootView);
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (!viewPositionFixed) {
webView.setMinimumHeight(rootView.getHeight());
viewPositionFixed = true;
}
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
// If last view, close dialog and continue. On mobile there is no callback URL !
if (url.contains("oauth_verifier") && !url.contains("AND")) {
EventBus.getDefault().post(new AuthorizeGrantedEvent(url));
dismiss();
}
return true;
}
});
webView.loadUrl(getArguments().getString("code"));
return rootView;
}
@Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}