com.github.longkai.zhihu.util.Utils.java Source code

Java tutorial

Introduction

Here is the source code for com.github.longkai.zhihu.util.Utils.java

Source

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

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.provider.BaseColumns;

import com.github.longkai.zhihu.R;
import com.github.longkai.zhihu.provider.ZhihuProvider;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.github.longkai.zhihu.util.Constants.ANSWER;
import static com.github.longkai.zhihu.util.Constants.ANSWERED;
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.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.STARRED;
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.TOPICS;
import static com.github.longkai.zhihu.util.Constants.TOPIC_AVATAR;
import static com.github.longkai.zhihu.util.Constants.TOPIC_DESCRIPTION;
import static com.github.longkai.zhihu.util.Constants.TOPIC_ID;
import static com.github.longkai.zhihu.util.Constants.TOPIC_NAME;
import static com.github.longkai.zhihu.util.Constants.UID;
import static com.github.longkai.zhihu.util.Constants.VIEWED;
import static com.github.longkai.zhihu.util.Constants.VOTE;
import static com.github.longkai.zhihu.util.Constants.VOTERS;

/**
 * 
 *
 * @User longkai
 * @Date 13-11-11
 * @Mail im.longkai@gmail.com
 */
public class Utils {

    public static final String TAG = "Utils";

    /**
     * view user' s infomation on the web.
     *
     * @param context
     * @param id
     */
    public static void viewUserInfo(Context context, String id) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("http://www.zhihu.com/people/" + id));
        context.startActivity(intent);
    }

    /**
     * ??
     * @param context
     * @param subject 
     * @param content 
     * @return intent
     */
    public static Intent share(Context context, String subject, String content) {
        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("text/plain");
        share.putExtra(Intent.EXTRA_SUBJECT, subject);
        share.putExtra(Intent.EXTRA_TEXT, content + context.getString(R.string.share_from));
        return share;
    }

    public static void viewOnWeb(Context context, Uri uri) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(uri);
        context.startActivity(intent);
    }

    /**
     * ?URI
     * @param path
     * @return URI
     */
    public static Uri parseUri(String path) {
        return Uri.parse(ZhihuProvider.BASE_URI + path);
    }

    /**
     * page == 1 
     *
     * @param page
     * @return
     */
    public static String url(int page) {
        return "http://www.zhihu.com/reader/json/" + page + "?r=" + System.currentTimeMillis();
    }

    /**
     * web??
     */
    public static String refreshUrl() {
        return url(1);
    }

    /**
     * sql
     * @param str
     * @return '%str%'
     */
    public static String like(String str) {
        return "'%" + str + "%'";
    }

    public static void createTables(SQLiteDatabase db) {
        StringBuilder sql = new StringBuilder();
        // ?
        // _id ??id
        sql.append("CREATE TABLE ").append(ITEMS).append("(").append(BaseColumns._ID).append(" int,")
                .append(QUESTION_ID).append(" int NOT NULL,").append(TITLE).append(" text NOT NULL,")
                .append(DESCRIPTION).append(" text,").append(STARRED).append(" int,").append(ANSWERED)
                .append(" int,").append(VIEWED).append(" int,").append(TOPICS).append(" text,")

                .append(ANSWER_ID).append(" int NOT NULL,").append(ANSWER).append(" text,").append(VOTE)
                .append(" int PRIMARY KEY,").append(LAST_ALTER_DATE).append(" int,").append(VOTERS).append(" text,")

                .append(UID).append(" text,").append(NICK).append(" text,").append(STATUS).append(" text,")
                .append(AVATAR).append(" text").append(")");

        db.execSQL(sql.toString());

        // ?string builder
        sql.setLength(0);

        // ??
        sql.append("CREATE TABLE ").append(TOPICS).append("(").append(TOPIC_ID).append(" int PRIMARY KEY,")
                .append(TOPIC_NAME).append(" text,").append(TOPIC_DESCRIPTION).append(" text,").append(TOPIC_AVATAR)
                .append(" text").append(")");

        db.execSQL(sql.toString());
    }

    public static void dropTables(SQLiteDatabase db) {
        db.execSQL("DROP TABLE IF EXISTS " + TOPICS);
        db.execSQL("DROP TABLE IF EXISTS " + ITEMS);
    }

    /**
     * Android`_id`??
     * @param id
     * @return _id=xx
     */
    public static String queryById(String id) {
        return queryByKey(BaseColumns._ID, id);
    }

    /**
     * key=value???
     * @param key
     * @param value
     * @return key=value
     */
    public static String queryByKey(String key, Object value) {
        if (value instanceof String) {
            return key + "='" + value + "'";
        }
        return key + "=" + value;
    }

    /**
     * ???ContentValues?
     * @param jsonArray data
     * @return itemstopicscontent values map
     */
    public static Map<String, ContentValues[]> process(JSONArray jsonArray) {
        ContentValues[] items = new ContentValues[jsonArray.length()];
        List<ContentValues> topics = new ArrayList<ContentValues>();
        ContentValues item;
        ContentValues topic;
        // ??
        JSONArray array;
        JSONArray user;
        JSONArray question;
        JSONArray _topics;
        JSONArray _topic;
        // ??id??(?
        // ??= =)123,321,456,
        StringBuilder topicIds = new StringBuilder();
        // ??item???
        long millis = System.currentTimeMillis();

        for (int i = 0; i < items.length; i++) {
            array = jsonArray.optJSONArray(i);
            item = new ContentValues();

            item.put(BaseColumns._ID, millis--);

            // 
            item.put(STATUS, array.optString(1));
            item.put(ANSWER, array.optString(2));
            item.put(VOTE, array.optInt(3));
            item.put(LAST_ALTER_DATE, array.optLong(4) * 1000);
            item.put(ANSWER_ID, array.optLong(5));

            // 
            user = array.optJSONArray(6);
            if (user != null) {
                item.put(NICK, user.optString(0));
                item.put(UID, user.optString(1));
                item.put(AVATAR, user.optString(2));
            }

            // 
            question = array.optJSONArray(7);
            if (question != null) {
                item.put(TITLE, question.optString(1, null));
                item.put(DESCRIPTION, question.optString(2));
                item.put(QUESTION_ID, question.optLong(3));
                item.put(STARRED, question.optLong(5));
                item.put(VIEWED, question.optLong(6));
            }

            // ?
            topicIds.setLength(0);
            if (!question.isNull(7)) {
                _topics = question.optJSONArray(7);
                for (int j = 0; j < _topics.length(); j++) {
                    _topic = _topics.optJSONArray(j);
                    topic = new ContentValues(4);
                    topic.put(TOPIC_NAME, _topic.optString(1, null));
                    topic.put(TOPIC_DESCRIPTION, _topic.optString(2));
                    topic.put(TOPIC_AVATAR, _topic.optString(3));
                    topic.put(TOPIC_ID, _topic.optLong(4));
                    // todo ?????replace?
                    // ?????????
                    topics.add(topic);

                    // ?item?
                    topicIds.append(_topic.optLong(4)).append(",");
                }
            }
            item.put(TOPICS, topicIds.toString());

            // ?json
            item.put(VOTERS, array.isNull(8) ? null : array.optJSONArray(8).toString());

            items[i] = item;
        }

        Map<String, ContentValues[]> map = new HashMap<String, ContentValues[]>(2);
        map.put(ITEMS, items);
        map.put(TOPICS, topics.toArray(new ContentValues[0]));
        return map;
    }

}