com.github.longkai.zhihu.ui.AnswerActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.github.longkai.zhihu.ui.AnswerActivity.java

Source

/*
 * The MIT License (MIT)
 * Copyright (c) 2013 longkai()
 * The software shall be used for good, not evil.
 */
package com.github.longkai.zhihu.ui;

import android.content.AsyncQueryHandler;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.github.longkai.zhihu.R;
import com.github.longkai.zhihu.ZhihuApp;
import com.github.longkai.zhihu.provider.ZhihuProvider;
import com.github.longkai.zhihu.util.Constants;
import com.github.longkai.zhihu.util.Utils;

import static com.github.longkai.zhihu.util.Constants.ANSWER;
import static com.github.longkai.zhihu.util.Constants.ANSWER_ID;
import static com.github.longkai.zhihu.util.Constants.AVATAR;
import static com.github.longkai.zhihu.util.Constants.DESCRIPTION;
import static com.github.longkai.zhihu.util.Constants.ITEMS;
import static com.github.longkai.zhihu.util.Constants.ITEM_PROJECTION;
import static com.github.longkai.zhihu.util.Constants.LAST_ALTER_DATE;
import static com.github.longkai.zhihu.util.Constants.NICK;
import static com.github.longkai.zhihu.util.Constants.QUESTION_ID;
import static com.github.longkai.zhihu.util.Constants.STATUS;
import static com.github.longkai.zhihu.util.Constants.TITLE;
import static com.github.longkai.zhihu.util.Constants.UID;

/**
 * ?
 *
 * @User longkai
 * @Date 13-11-11
 * @Mail im.longkai@gmail.com
 */
public class AnswerActivity extends ActionBarActivity {

    /**
     * id
     */
    private long qid;
    /**
     * serverid
     */
    private long aid;
    /**
     * ?id
     */
    private long id;
    /**
     * 
     */
    private String questionTitle;
    /**
     * ?
     */
    private String answerDigest;
    /**
     * id
     */
    private String uid;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.answer);

        final TextView title = (TextView) findViewById(android.R.id.title);
        final WebView desc = (WebView) findViewById(R.id.description);
        final TextView nick = (TextView) findViewById(R.id.nick);
        final ImageView avatar = (ImageView) findViewById(R.id.avatar);
        final TextView status = (TextView) findViewById(R.id.status);
        final WebView answer = (WebView) findViewById(R.id.content);
        final TextView last_alter_date = (TextView) findViewById(R.id.last_alter_date);

        id = getIntent().getLongExtra(ANSWER_ID, 0);
        new AsyncQueryHandler(getContentResolver()) {
            @Override
            protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
                if (cursor.moveToNext()) {
                    // 
                    aid = cursor.getLong(cursor.getColumnIndex(ANSWER_ID));
                    qid = cursor.getLong(cursor.getColumnIndex(QUESTION_ID));
                    questionTitle = cursor.getString(cursor.getColumnIndex(TITLE));
                    title.setText(questionTitle);
                    String description = cursor.getString(cursor.getColumnIndex(DESCRIPTION));
                    if (TextUtils.isEmpty(description)) {
                        desc.setVisibility(View.GONE);
                    } else {
                        desc.loadDataWithBaseURL(null, description, "text/html", "utf-8", null);
                        desc.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                        desc.setBackgroundColor(getResources().getColor(R.color.bgcolor));
                    }

                    // 
                    uid = cursor.getString(cursor.getColumnIndex(UID));
                    nick.setText(cursor.getString(cursor.getColumnIndex(NICK)));
                    String src = cursor.getString(cursor.getColumnIndex(AVATAR));
                    ZhihuApp.getImageLoader().get(src,
                            ImageLoader.getImageListener(avatar, R.drawable.ic_launcher, R.drawable.ic_launcher));
                    status.setText(cursor.getString(cursor.getColumnIndex(STATUS)));

                    // 
                    String content = cursor.getString(cursor.getColumnIndex(ANSWER));
                    answerDigest = content.length() > 50 ? content.substring(0, 50) : content;
                    answer.loadDataWithBaseURL(null, content, "text/html", "utf-8", null);
                    answer.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                    answer.setBackgroundColor(getResources().getColor(R.color.bgcolor));

                    last_alter_date.setText(DateUtils
                            .getRelativeTimeSpanString(cursor.getLong(cursor.getColumnIndex(LAST_ALTER_DATE))));

                    cursor.close();
                }
            }
        }.startQuery(0, null, Uri.parse(ZhihuProvider.BASE_URI + Constants.ITEMS + "/" + id), ITEM_PROJECTION, null,
                null, null);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.answer, menu);
        MenuItem share = menu.findItem(R.id.share);
        ShareActionProvider provider = (ShareActionProvider) MenuItemCompat.getActionProvider(share);
        provider.setOnShareTargetSelectedListener(new ShareActionProvider.OnShareTargetSelectedListener() {
            @Override
            public boolean onShareTargetSelected(ShareActionProvider shareActionProvider, Intent intent) {
                startActivity(intent);
                return true;
            }
        });
        provider.setShareIntent(Utils.share(this, questionTitle, answerDigest));
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            break;
        case R.id.author: // web?
            Utils.viewUserInfo(this, uid);
            break;
        case R.id.view_at_web: // web
            Utils.viewOnWeb(this, Uri.parse("http://www.zhihu.com/question/" + qid + "/answer/" + aid));
            break;
        case R.id.view_all: // web
            Utils.viewOnWeb(this, Uri.parse("http://www.zhihu.com/question/" + qid));
            break;
        default:
            throw new RuntimeException("no this option!");
        }
        return super.onOptionsItemSelected(item);
    }

}