com.renard.documentview.TableOfContentsActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.renard.documentview.TableOfContentsActivity.java

Source

/*
 * Copyright (C) 2012,2013 Renard Wellnitz.
 * 
 * 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.renard.documentview;

import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.renard.documentview.SimpleDocumentAdapter.DocumentViewHolder;
import com.renard.documentview.SimpleDocumentAdapter.ViewBinder;
import com.renard.ocr.DocumentContentProvider;
import com.renard.ocr.DocumentContentProvider.Columns;
import com.renard.ocr.R;
import com.renard.ocr.cropimage.MonitoredActivity;
import com.renard.ocr.help.HintDialog;

public class TableOfContentsActivity extends MonitoredActivity
        implements LoaderManager.LoaderCallbacks<Cursor>, OnItemClickListener {
    private final static String[] PROJECTION = { Columns.ID, Columns.TITLE, Columns.OCR_TEXT, Columns.CREATED };

    public final static String EXTRA_DOCUMENT_ID = "document_id";
    public final static String EXTRA_DOCUMENT_POS = "document_pos";
    private ListView mList;
    private static final int HINT_DIALOG_ID = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.table_of_contents_activity);
        if (getIntent() == null || getIntent().getData() == null) {
            finish();
            return;
        }
        mList = (ListView) findViewById(R.id.list);
        mList.setOnItemClickListener(this);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportLoaderManager().initLoader(0, null, this);
        initAppIcon(this, -1);
    }

    @Override
    protected Dialog onCreateDialog(int id, Bundle args) {
        switch (id) {
        case HINT_DIALOG_ID:
            return HintDialog.createDialog(this, R.string.toc_help_title, "file:///android_res/raw/toc_help.html");
        }
        return super.onCreateDialog(id, args);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
        final Uri documentUri = getIntent().getData();
        final String selection = DocumentContentProvider.Columns.PARENT_ID + "=? OR " + Columns.ID + "=?";
        final String[] args = new String[] { documentUri.getLastPathSegment(), documentUri.getLastPathSegment() };
        return new CursorLoader(this, DocumentContentProvider.CONTENT_URI, PROJECTION, selection, args,
                "created ASC");
    }

    @Override
    public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {

        final SimpleDocumentAdapter adapter = new SimpleDocumentAdapter(this, R.layout.table_of_contents_element,
                cursor, new ViewBinder() {

                    @Override
                    public void bind(View v, DocumentViewHolder holder, String title, CharSequence formattedDate,
                            String text, int position, final int id) {
                        holder.date.setText(formattedDate);
                        holder.text.setText(title);
                        final String pageNo = String.valueOf(position + 1);
                        holder.mPageNumber.setText(pageNo);
                    }

                });
        mList.setAdapter(adapter);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent data = new Intent();
        data.putExtra(EXTRA_DOCUMENT_ID, (int) id);
        data.putExtra(EXTRA_DOCUMENT_POS, position);
        setResult(RESULT_OK, data);
        finish();
    }
}