com.chute.components.android.imagesharer.activity.DialogActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.chute.components.android.imagesharer.activity.DialogActivity.java

Source

/**
 * The MIT License (MIT)
    
Copyright (c) 2013 Chute
    
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.chute.components.android.imagesharer.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.chute.components.android.imagesharer.R;
import com.chute.components.android.imagesharer.dialog.DialogFragmentListener;
import com.chute.components.android.imagesharer.dialog.FacebookFragmentDialog;
import com.chute.components.android.imagesharer.dialog.TwitterFragmentDialog;
import com.chute.components.android.imagesharer.intent.DialogActivityIntentWrapper;
import com.chute.components.android.imagesharer.util.Constants;

public class DialogActivity extends FragmentActivity implements DialogFragmentListener {

    public static final String TAG = DialogActivity.class.getSimpleName();
    private WebView webView;
    private ProgressBar progressBar;
    private DialogActivityIntentWrapper wrapper;
    private int dialogType;
    private String shareUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        wrapper = new DialogActivityIntentWrapper(getIntent());
        shareUrl = wrapper.getAssetShareUrl();
        dialogType = wrapper.getDialogType();
        if (dialogType == Constants.DIALOG_FACEBOOK) {
            showFacebookDialog(shareUrl);
        } else if (dialogType == Constants.DIALOG_TWITTER) {
            showTwitterDialog(shareUrl);
        }
    }

    private class BaseWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            progressBar.setVisibility(View.VISIBLE);
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            progressBar.setVisibility(View.GONE);
            super.onPageFinished(view, url);
        }
    }

    private void showFacebookDialog(String url) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.dialogs, FacebookFragmentDialog.newInstance(shareUrl),
                Constants.FRAGMENT_FACEBOOK_TAG);
        fragmentTransaction.commit();
    }

    private void showTwitterDialog(String url) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.dialogs, TwitterFragmentDialog.newInstance(shareUrl),
                Constants.FRAGMENT_TWITTER_TAG);
        fragmentTransaction.commit();
    }

    @Override
    public View getWebView() {
        webView = new WebView(getApplicationContext());
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.setWebViewClient(new BaseWebViewClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.setKeepScreenOn(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setUserAgentString(
                "Mozilla/5.0 (Linux; U; Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        FrameLayout frameLayout = new FrameLayout(getApplicationContext());
        frameLayout.setMinimumHeight(150);
        progressBar = new ProgressBar(getApplicationContext());
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(100, 100);
        layoutParams.gravity = Gravity.CENTER;
        progressBar.setLayoutParams(layoutParams);
        frameLayout.addView(webView);
        frameLayout.addView(progressBar);
        return frameLayout;
    }

    @Override
    public void loadUrl(String url) {
        webView.loadUrl(url);

    }

}