activities.PaintingActivity.java Source code

Java tutorial

Introduction

Here is the source code for activities.PaintingActivity.java

Source

package activities;

import android.app.ActionBar;
import android.app.Dialog;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.flurry.android.FlurryAgent;

import colorlife.arbiec.com.colorlife.R;
import colorlife.arbiec.com.colorlife.SobreAppActivity;
import fragments.CoachMarkFragment;
import fragments.MenuLateralFragment;

public class PaintingActivity extends PaintActivity implements MenuLateralFragment.MenuLateralListener {

    // Constante para hacer referencia al valor de app abierta por primera vez
    private static final String FIRSTLAUNCH_PREF = "firstLaunch";
    private static final int NUM_PAGES = 2;

    // Eventos Flurry
    private static final String FLURRY_EV_DECORADOR = "Home - Decorador";
    private static final String FLURRY_EV_CALCULADORA = "Calculadora";
    private static final String FLURRY_EV_TIENDAS = "Tiendas";
    private static final String FLURRY_EV_SOBRE = "Sobre App";
    private static final String FLURRY_EV_TERMINOS = "Terminos y Condiciones";

    private SharedPreferences sharedPreferences;
    /**
     * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
     */
    private MenuLateralFragment mMenuLateralFragment;

    private CharSequence mTitle;

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

        mMenuLateralFragment = (MenuLateralFragment) getFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mMenuLateralFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));

        //Set up spinners
        //        Spinner bucketSpinner = (Spinner)findViewById(R.id.topbarpainting_bucket);
        //        ArrayAdapter adapter = ArrayAdapter.createFromResource(PaintingActivity.this, R.array.bucket_options_spinner, R.layout.bucket_spinner_item);
        //        adapter.setDropDownViewResource(R.layout.bucket_spinner_dropdown_item);
        //        bucketSpinner.setAdapter(adapter);

        //Set up the firstLaunch
        setUpFirstLauch();

    }

    /*Mtodo Delegate del contract MenuLateralListener*/

    @Override
    public void onMenuLateralItemSelected(int position) {
        String[] sections = getResources().getStringArray(R.array.menu_lateral_secciones);

        // update the main content by replacing fragments
        FragmentManager fragmentManager = getFragmentManager();
        Intent i = null;
        switch (position) {
        case 0: //HOME
            FlurryAgent.logEvent(FLURRY_EV_DECORADOR);
            break;
        case 1: //Calculadora
            FlurryAgent.logEvent(FLURRY_EV_CALCULADORA);
            i = new Intent(PaintingActivity.this, CalculadoraActivity.class);
            startActivity(i);
            break;
        case 2: //Tiendas comex
            FlurryAgent.logEvent(FLURRY_EV_TIENDAS);
            i = new Intent(PaintingActivity.this, StoreFinderActivity.class);
            startActivity(i);

            break;
        case 3: //Tutorial
            i = new Intent(PaintingActivity.this, TutorialActivity.class);
            startActivity(i);
            break;
        case 4: //Terminos y condiciones
            FlurryAgent.logEvent(FLURRY_EV_TERMINOS);
            i = new Intent(PaintingActivity.this, TerminosActivity.class);
            startActivity(i);
            break;
        case 5: //Sobre la app
            FlurryAgent.logEvent(FLURRY_EV_SOBRE);
            i = new Intent(PaintingActivity.this, SobreAppActivity.class);
            startActivity(i);
        default:

            break;
        }

    }

    @Override
    public String getCBLicenseKey() {
        return "571579bc32ed4761a84ae8375b12a977";
    }

    public void onSectionAttached(int position) {
        mTitle = getResources().getStringArray(R.array.menu_lateral_secciones)[position];
    }

    public void restoreActionBar() {
        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayUseLogoEnabled(true);
        actionBar.setLogo(R.drawable.navbar_logo);
        actionBar.setIcon(R.drawable.ic_drawer);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mMenuLateralFragment.isDrawerOpen()) {
            // Only show items in the action bar relevant to this screen
            // if the drawer is not showing. Otherwise, let the drawer
            // decide what to show in the action bar.
            getMenuInflater().inflate(R.menu.painting, menu);
            restoreActionBar();
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        switch (id) {

        case R.id.menuitem_guardar_imagen:
            super.saveImage();
            break;
        case R.id.menuitem_info:
            showCoachMark();

            break;
        case R.id.menuitem_calculadora:
            this.onMenuLateralItemSelected(1);
            FlurryAgent.logEvent(FLURRY_EV_CALCULADORA);
            break;
        case R.id.menuitem_camara:
            super.cameraClicked();
            break;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onStart() {
        super.onStart();
        FlurryAgent.onStartSession(this, getResources().getString(R.string.flurry_app_id));
    }

    @Override
    protected void onStop() {
        super.onStop();
        FlurryAgent.onEndSession(this);
    }

    private void showCoachMark() {
        CoachMarkDialogFragment dialog = new CoachMarkDialogFragment();
        dialog.show(getSupportFragmentManager(), "coachmarkDialog");
    }

    private void setUpFirstLauch() {
        sharedPreferences = getPreferences(0);

        boolean alreadyLaunched = sharedPreferences.getBoolean(FIRSTLAUNCH_PREF, false);

        if (!alreadyLaunched) {
            Intent openTutorial = new Intent(getApplicationContext(), TutorialActivity.class);
            startActivity(openTutorial);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean(FIRSTLAUNCH_PREF, true);
            editor.commit();
        }

    }

    private class CoachMarkDialogFragment extends DialogFragment {

        public static final String COACHMARK_INDEX = "coach-index";
        private int _index;

        @NonNull
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {

            return super.onCreateDialog(savedInstanceState);
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {

            setStyle(android.R.style.Theme_Holo_Dialog_NoActionBar, android.R.style.Theme_Holo_Dialog_NoActionBar);

            super.onCreate(savedInstanceState);

        }

        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                @Nullable Bundle savedInstanceState) {
            getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            getDialog().setCancelable(true);
            View rootView = inflater.inflate(R.layout.dialog_coachmark_content, container, false);
            //Coachmark images
            ViewPager pager = (ViewPager) rootView.findViewById(R.id.dialogpager);
            pager.setBackground(new ColorDrawable(Color.TRANSPARENT));

            CoachMarkPageAdapter viewpagerAdapter = new CoachMarkPageAdapter(getChildFragmentManager());
            pager.setAdapter(viewpagerAdapter);
            return rootView;
        }

    }

    private class CoachMarkPageAdapter extends FragmentStatePagerAdapter {

        public CoachMarkPageAdapter(android.support.v4.app.FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return CoachMarkFragment.newInstance(i);
        }

        @Override
        public int getCount() {
            return NUM_PAGES;
        }

    }

}