com.licubeclub.zionhs.PostViewActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.licubeclub.zionhs.PostViewActivity.java

Source

/*
 * Zion High School Application for Android
 * Copyright (C) 2013-2015 The Zion High School Application for Android Open Source Project
 *
 * This program 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
 * any later version.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.licubeclub.zionhs;

import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.ShareActionProvider;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.widget.Toolbar;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;

public class PostViewActivity extends ActionBarActivity {
    String URL;
    WebView WV;
    String data;
    private final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_view);
        WV = (WebView) findViewById(R.id.webView);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        String Title = getIntent().getStringExtra("title");
        String Date = getIntent().getStringExtra("date");
        String Author = getIntent().getStringExtra("author");
        URL = getIntent().getStringExtra("URL");
        toolbar.setTitle(Title);
        toolbar.setSubtitle(Author + " - " + Date);
        setSupportActionBar(toolbar);
        networkTask();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_post_view, menu);
        //   
        MenuItem menuItem = menu.findItem(R.id.action_share);

        // ShareActionProvider 
        ShareActionProvider mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);

        //  ?  Intent     .
        if (mShareActionProvider != null) {
            mShareActionProvider.setShareIntent(createShareIntent());
        } else {
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.open) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(URL));
            startActivity(intent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private Intent createShareIntent() {
        //? ACTION_SEND  .
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        //Flag   ?.   ?   ? ? Activity  ,
        // ? ? Activity    ?
        //FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET  ?.
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        // ? .  ? .
        shareIntent.setType("text/plain");

        // ?? Extra  ?.
        shareIntent.putExtra(Intent.EXTRA_TEXT, URL);
        return shareIntent;
    }

    private void networkTask() {

        final Handler mHandler = new Handler();

        new Thread() {
            public void run() {

                try {
                    Document doc = Jsoup.connect(URL).get();
                    Element element = doc.select("td").get(4);
                    data = element.getAllElements().toString();

                } catch (IOException e) {
                    e.printStackTrace();
                }

                mHandler.post(new Runnable() {
                    public void run() {
                        WV.getSettings().setJavaScriptEnabled(true);
                        WV.setWebViewClient(new WebViewClient() {
                            public void onReceivedError(WebView view, int errorCode, String description,
                                    String failingUrl) {
                                Toast.makeText(PostViewActivity.this, description, Toast.LENGTH_SHORT).show();
                            }
                        });
                        //                        WV.loadData(data,"text/html","utf-8");
                        if (data == null) {
                            data = getResources().getString(R.string.nodata);
                        } else if (data.equals("<td class=\"writeBody writeContent\"></td>")) {
                            data = getResources().getString(R.string.nodata);
                        } else {
                        }
                        Log.d("DATA", data);
                        WV.loadDataWithBaseURL(null, data, "text/html", "utf-8", null);
                        handler.sendEmptyMessage(0);
                    }
                });

            }
        }.start();

    }
}