<commit_before>/*
 * Copyright (C) 2012 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.btmura.android.reddit.provider;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.JsonReader;

import com.btmura.android.reddit.BuildConfig;
import com.btmura.android.reddit.database.Kinds;
import com.btmura.android.reddit.database.MessageActions;
import com.btmura.android.reddit.database.Messages;
import com.btmura.android.reddit.database.Sessions;
import com.btmura.android.reddit.database.SharedColumns;
import com.btmura.android.reddit.net.RedditApi;
import com.btmura.android.reddit.net.Urls;
import com.btmura.android.reddit.util.Array;
import com.btmura.android.reddit.util.JsonParser;

class MessageListing extends JsonParser implements Listing {

    public static final String TAG = "MessageListing";

    private static final String[] MERGE_PROJECTION = {
            MessageActions._ID,
            MessageActions.COLUMN_ACTION,
            MessageActions.COLUMN_TEXT,
    };

    private static final int MERGE_ID = 0;
    private static final int MERGE_ACTION = 1;
    private static final int MERGE_TEXT = 2;

    private static final String MERGE_SELECTION = MessageActions.COLUMN_ACCOUNT + "=? AND "
            + MessageActions.COLUMN_PARENT_THING_ID + "=?";

    private final int sessionType;
    private final String accountName;
    private final String thingId;
    private final int filter;
    private final String more;
    private final boolean mark;
    private final String cookie;
    private final SQLiteOpenHelper dbHelper;
    private final ArrayList<ContentValues> values = new ArrayList<ContentValues>(30);

    private long networkTimeMs;
    private long parseTimeMs;

    private String moreThingId;

    /** Returns a listing of messages for inbox or sent. */
    static MessageListing newInstance(SQLiteOpenHelper dbHelper, String accountName, int filter,
            String more, boolean mark, String cookie) {
        return new MessageListing(dbHelper, Sessions.TYPE_MESSAGES, accountName,
                null, filter, more, mark, cookie);
    }

    /** Returns an instance for a message thread. */
    static MessageListing newThreadInstance(SQLiteOpenHelper dbHelper, String accountName,
            String thingId, String cookie) {
        return new MessageListing(dbHelper, Sessions.TYPE_MESSAGE_THREAD, accountName,
                thingId, 0, null, false, cookie);
    }

    private MessageListing(SQLiteOpenHelper dbHelper, int sessionType, String accountName,
            String thingId, int filter, String more, boolean mark, String cookie) {
        this.dbHelper = dbHelper;
        this.accountName = accountName;
        this.thingId = thingId;
        this.filter = filter;
        this.more = more;
        this.mark = mark;
        this.cookie = cookie;
        this.sessionType = sessionType;
    }

    @Override
    public String getSessionTag() {
        return thingId;
    }

    @Override
    public int getSessionType() {
        return sessionType;
    }

    public ArrayList<ContentValues> getValues() throws IOException {
        long t1 = System.currentTimeMillis();
        HttpURLConnection conn = RedditApi.connect(getUrl(), cookie, true, false);
        InputStream input = null;
        try {
            input = new BufferedInputStream(conn.getInputStream());
            long t2 = System.currentTimeMillis();

            JsonReader reader = new JsonReader(new InputStreamReader(input));
            parseListingObject(reader);
            if (BuildConfig.DEBUG) {
                long t3 = System.currentTimeMillis();<commit_msg>Fix exceptions when loading messages<commit_after>/*
 * Copyright (C) 2012 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.btmura.android.reddit.provider;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.JsonReader;

import com.btmura.android.reddit.BuildConfig;
import com.btmura.android.reddit.database.Kinds;
import com.btmura.android.reddit.database.MessageActions;
import com.btmura.android.reddit.database.Messages;
import com.btmura.android.reddit.database.Sessions;
import com.btmura.android.reddit.database.SharedColumns;
import com.btmura.android.reddit.net.RedditApi;
import com.btmura.android.reddit.net.Urls;
import com.btmura.android.reddit.util.Array;
import com.btmura.android.reddit.util.JsonParser;

class MessageListing extends JsonParser implements Listing {

    public static final String TAG = "MessageListing";

    private static final String[] MERGE_PROJECTION = {
            MessageActions._ID,
            MessageActions.COLUMN_ACTION,
            MessageActions.COLUMN_TEXT,
    };

    private static final int MERGE_ID = 0;
    private static final int MERGE_ACTION = 1;
    private static final int MERGE_TEXT = 2;

    private static final String MERGE_SELECTION = MessageActions.COLUMN_ACCOUNT + "=? AND "
            + MessageActions.COLUMN_PARENT_THING_ID + "=?";

    private final SQLiteOpenHelper dbHelper;
    private final int sessionType;
    private final String sessionTag;
    private final String accountName;
    private final String thingId;
    private final int filter;
    private final String more;
    private final boolean mark;
    private final String cookie;

    private final ArrayList<ContentValues> values = new ArrayList<ContentValues>(30);
    private long networkTimeMs;
    private long parseTimeMs;
    private String moreThingId;

    /** Returns a listing of messages for inbox or sent. */
    static MessageListing newInstance(SQLiteOpenHelper dbHelper, String accountName, int filter,
            String more, boolean mark, String cookie) {
        return new MessageListing(dbHelper, Sessions.TYPE_MESSAGES, accountName,
                accountName, null, filter, more, mark, cookie);
    }

    /** Returns an instance for a message thread. */
    static MessageListing newThreadInstance(SQLiteOpenHelper dbHelper, String accountName,
            String thingId, String cookie) {
        return new MessageListing(dbHelper, Sessions.TYPE_MESSAGE_THREAD, thingId,
                accountName, thingId, 0, null, false, cookie);
    }

    private MessageListing(SQLiteOpenHelper dbHelper, int sessionType, String sessionTag,
            String accountName, String thingId, int filter, String more, boolean mark,
            String cookie) {
        this.dbHelper = dbHelper;
        this.sessionType = sessionType;
        this.sessionTag = sessionTag;
        this.accountName = accountName;
        this.thingId = thingId;
        this.filter = filter;
        this.more = more;
        this.mark = mark;
        this.cookie = cookie;
    }

    @Override
    public String getSessionTag() {
        return sessionTag;
    }

    @Override
    public int getSessionType() {
        return sessionType;
    }

    public ArrayList<ContentValues> getValues() throws IOException {
        long t1 = System.currentTimeMillis();
        HttpURLConnection conn = RedditApi.connect(getUrl(), cookie, true, false);
        InputStream input = null;
        try {
            input = new BufferedInputStream(conn.getInputStream());
            long t2 = System.currentTimeMillis();

            JsonReader reader = new JsonReader(new InputStreamReader(input));
            parseListingObject(reader);
            if (BuildConfig.DEBUG) {
                long t3 = System.currentTimeMillis();
{'fraction_non_alphanumeric': 0.07914911996544649, 'fraction_numerical': 0.0036713097937587733, 'mean_word_length': 3.348356807511737, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '414611', 'n_tokens_mistral': 2496, 'n_tokens_neox': 2452, 'n_words': 745}