com.gnufabio.costituzione.DetailedActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.gnufabio.costituzione.DetailedActivity.java

Source

/*
 * Costituzione Italiana 4.0 
 * Copyright (C) 2012 
 *    Gnufabio <me@gnufabio.com>, Enrix835 <enrico.trt@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.gnufabio.costituzione;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ShareActionProvider;
import android.widget.Toast;

import com.gnufabio.costituzione.components.DetailedFragmentAdapter;
import com.gnufabio.costituzione.components.Note;
import com.gnufabio.costituzione.components.SingleEntry;
import com.gnufabio.costituzione.util.DatabaseHandler;
import com.gnufabio.costituzione.util.LoadViewPagerTask;
import com.gnufabio.costituzione.util.NotesDatabaseHandler;
import com.gnufabio.costituzione.util.Util;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import java.util.List;

public class DetailedActivity extends BasicActivity implements OnPageChangeListener {
    SingleEntry mEntry;
    Note mNote;
    ViewPager vp;
    ShareActionProvider mShareProvider;
    public int id, position;
    boolean mIsTablet;
    Context mContext;

    SharedPreferences mPrefs;

    public List<SingleEntry> list;
    public List<Note> listNotes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detailed_container);

        mPrefs = getSharedPreferences(Constants.PREF_KEY, MODE_APPEND);
        AdView mAdView = (AdView) findViewById(R.id.adView);

        if (!mPrefs.getBoolean(Constants.INAPP_ADS_ID, false)) {
            AdRequest adRequest = new AdRequest.Builder().build();
            mAdView.loadAd(adRequest);
        } else {
            mAdView.setVisibility(View.GONE);
        }

        mContext = this;

        mIsTablet = Util.isTabletDevice(this);
        heuristicSetTytle(getResources().getConfiguration());

        vp = (ViewPager) findViewById(R.id.pager);

        if (savedInstanceState == null) {
            Bundle data = getIntent().getExtras();

            id = data.getInt("id");
            String idlist = data.getString("idlist");

            LoadViewPagerTask task = new LoadViewPagerTask(this, id, idlist, vp);
            task.execute();

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.actionbar_details, menu);

        MenuItem shareItem = menu.findItem(R.id.menu_item_share);
        mShareProvider = (ShareActionProvider) shareItem.getActionProvider();

        if (list == null || listNotes == null)
            return false;

        mEntry = list.get(vp.getCurrentItem());
        mShareProvider.setShareIntent(getShareIntent());
        mNote = listNotes.get(vp.getCurrentItem());

        updateFavoriteButton(menu.findItem(R.id.favourite_status));

        return true;
    }

    @SuppressWarnings("static-access")
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch (item.getItemId()) {

        case R.id.favourite_status:
            DatabaseHandler dbh = new DatabaseHandler(this);
            dbh.open(true);
            mEntry.setIsFavourite(!mEntry.isFavourite());
            dbh.updateFavoriteStatus(mEntry);
            dbh.close();

            new Toast(this).makeText(this, mEntry.isFavourite() ? R.string.added_to_fav : R.string.removed_from_fav,
                    Toast.LENGTH_LONG).show();
            updateFavoriteButton(item);
            break;
        case R.id.menu_edit_note:
            AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle(R.string.dialog_update_note);

            final EditText input = new EditText(this);
            String nota = mNote.getTesto();
            input.setText(nota.equalsIgnoreCase("@Null") ? "" : nota);
            alert.setView(input);

            alert.setPositiveButton(mContext.getResources().getString(android.R.string.ok),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            int pos = vp.getCurrentItem();
                            String value = input.getText().toString();
                            NotesDatabaseHandler ndbh = new NotesDatabaseHandler(mContext);
                            ndbh.open(true);
                            mNote.setTesto(value);
                            ndbh.updateNote(mNote);
                            ndbh.close();
                            listNotes.remove(pos);
                            listNotes.add(pos, mNote);
                            DetailedFragmentAdapter adapter = (DetailedFragmentAdapter) vp.getAdapter();
                            adapter.getItem(pos).setNoteText(value);
                            adapter.updateNote(pos, mNote);
                            vp.setAdapter(adapter);
                            vp.setCurrentItem(pos, false);
                        }
                    });

            alert.setNegativeButton(mContext.getResources().getString(android.R.string.cancel), null);
            alert.show();
            break;
        }
        return true;
    }

    public Intent getShareIntent() {
        String text = mEntry.getTitle() + ": " + mEntry.getBody();
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/*");
        shareIntent.putExtra(Intent.EXTRA_TEXT, text);
        return shareIntent;
    }

    public void updateFavoriteButton(MenuItem favorite) {
        boolean isFav = mEntry.isFavourite();

        int icon = isFav ? R.drawable.is_favorite_light : R.drawable.is_not_favorite_light;
        int title = isFav ? R.string.remove_from_fav : R.string.add_to_fav;

        favorite.setIcon(icon).setTitle(title);
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    @Override
    public void onPageSelected(int arg0) {
        invalidateOptionsMenu();
    }

    @SuppressWarnings("deprecation")
    public void heuristicSetTytle(Configuration config) {
        if (mIsTablet) {
            setTitle(R.string.app_name);
        } else {
            int orientation = config.orientation;
            switch (orientation) {
            case Configuration.ORIENTATION_LANDSCAPE:
            case Configuration.ORIENTATION_SQUARE:
                setTitle(R.string.app_name);
                break;
            case Configuration.ORIENTATION_PORTRAIT:
                setTitle(null);
                break;
            default:
                setTitle(R.string.app_name);
                break;
            }
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        heuristicSetTytle(newConfig);

    }

}