org.akop.crosswords.activity.CrosswordActivity.java Source code

Java tutorial

Introduction

Here is the source code for org.akop.crosswords.activity.CrosswordActivity.java

Source

// Copyright (c) 2014-2015 Akop Karapetyan
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package org.akop.crosswords.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import org.akop.crosswords.Crosswords;
import org.akop.crosswords.R;
import org.akop.crosswords.Storage;
import org.akop.crosswords.fragment.CrosswordFragment;
import org.akop.crosswords.utility.WordReferenceMovementMethod;
import org.akop.xross.object.Crossword;
import org.akop.xross.utility.ReferenceScanner;

import java.util.List;

public class CrosswordActivity extends ActionBarActivity implements CrosswordFragment.OnUpdateSelectedWordListener {
    public static final int CROSSWORD_ID_NONE = -1;

    private Crossword mCrossword;

    private TextView mWordHint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_crossword);

        long crosswordId = CROSSWORD_ID_NONE;

        Intent intent = getIntent();
        if (intent != null) {
            crosswordId = intent.getLongExtra("crosswordId", CROSSWORD_ID_NONE);
        }

        WordReferenceMovementMethod rm = new WordReferenceMovementMethod();
        rm.setOnWordReferenceSelectedListener(new WordReferenceMovementMethod.OnWordReferenceSelectedListener() {
            @Override
            public void onWordReferenceSelected(int direction, int number) {
                if (mCrossword != null) {
                    Crossword.Word word = mCrossword.findWord(direction, number);
                    if (word != null) {
                        CharSequence hint = getHintText(word, mCrossword, false);
                        Toast.makeText(CrosswordActivity.this, hint, Toast.LENGTH_LONG).show();
                    }
                }
            }
        });

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        if (savedInstanceState == null) {
            if (crosswordId == CROSSWORD_ID_NONE) {
                Crosswords.logv("Missing crossword id");
            } else {
                mCrossword = Storage.getInstance().getPuzzle(crosswordId);
            }

            if (mCrossword != null) {
                CrosswordFragment fragment = new CrosswordFragment();
                Bundle args = CrosswordFragment.createArgs(crosswordId, mCrossword);

                fragment.setArguments(args);
                FragmentManager fm = getSupportFragmentManager();
                fm.beginTransaction().add(R.id.container, fragment).commit();
            }
        } else {
            mCrossword = savedInstanceState.getParcelable("crossword");
        }

        if (mCrossword == null) {
            Crosswords.logv("No matching crossword found");
            finish();
            return;
        }

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setCustomView(R.layout.template_crossword_actionbar_view);

        View customView = actionBar.getCustomView();
        mWordHint = (TextView) customView.findViewById(R.id.word_hint);
        mWordHint.setMovementMethod(rm);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putParcelable("crossword", mCrossword);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void updateSelectedWord(Crossword.Word word, Crossword crossword) {
        if (word == null) {
            mWordHint.setText("");
            return;
        }

        CharSequence hintText = getHintText(word, crossword, true);
        mWordHint.setText(hintText);
    }

    private CharSequence getHintText(Crossword.Word word, Crossword crossword, boolean linkReferences) {
        String hint = word.getHint();

        if (linkReferences) {
            List<ReferenceScanner.WordReference> refs = ReferenceScanner.findReferences(word, crossword);

            if (refs.size() > 0) {
                StringBuilder sb = new StringBuilder();
                int start = 0;
                for (ReferenceScanner.WordReference ref : refs) {
                    int refStart = ref.getStart();
                    int refEnd = ref.getEnd();

                    sb.append(hint, start, refStart);
                    sb.append("<a href=\"");
                    sb.append(WordReferenceMovementMethod.createReferenceLink(ref));
                    sb.append("\">");
                    sb.append(TextUtils.htmlEncode(hint.substring(refStart, refEnd)));
                    sb.append("</a>");

                    start = refEnd;
                }
                sb.append(hint, start, hint.length());

                hint = sb.toString();
            }
        }

        String directionLabel = "";
        if (word.getDirection() == Crossword.Word.DIR_ACROSS) {
            directionLabel = getString(R.string.across);
        } else if (word.getDirection() == Crossword.Word.DIR_DOWN) {
            directionLabel = getString(R.string.down);
        }

        return Html.fromHtml(getString(R.string.hint_format_f, word.getNumber(), directionLabel, hint));
    }

    public static void launch(Context context, long crosswordId) {
        Intent intent = new Intent(context, CrosswordActivity.class);
        intent.putExtra("crosswordId", crosswordId);
        context.startActivity(intent);
    }
}