app.hacked.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for app.hacked.MainActivity.java

Source

/*
 * Copyright (C) 2013 - Gareth Llewellyn
 *
 * This file is part of the Unofficial HackedIO Android App https://github.com/NetworksAreMadeOfString/HackedIO-Android-App
 *
 * This app is unofficial. The "Hacked.io" logo is copyright (and maybe even a trademark of) Telefnica UK Limited.
 * The author of this app is not affiliated with Telefnica UK Limited, The Lab ( https://thelab.o2.com/ ) or
 * Geeks of London ( http://geeksoflondon.com/ ) although the author <strong>is</strong> an attendee.
 *
 * 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 app.hacked;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.android.gcm.GCMRegistrar;
import org.json.JSONObject;

import java.util.Locale;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener, ScheduleListFragment.Callbacks,
        ProjectListFragment.Callbacks, ChallengeListFragment.Callbacks {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
     * will keep every loaded fragment in memory. If this becomes too memory
     * intensive, it may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;

    /**
     * Whether or not the activity is in two-pane mode, i.e. running on a tablet
     * device.
     */
    private boolean mTwoPane;

    /**
     * Stores the instance of GCM registration ID
     */
    String regId = "";

    /**
     *
     */
    RequestQueue queue = null;

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

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        //getActionBar().setIcon(R.drawable.ic_ab_logo);

        actionBar.setSubtitle("This app is UNOFFICIAL");

        queue = Volley.newRequestQueue(this);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // When swiping between different sections, select the corresponding
        // tab. We can also use ActionBar.Tab#select() to do this if we have
        // a reference to the Tab.
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by
            // the adapter. Also specify this Activity object, which implements
            // the TabListener interface, as the callback (listener) for when
            // this tab is selected.
            actionBar
                    .addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
        }

        //GCM stuff
        GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);
        regId = GCMRegistrar.getRegistrationId(this);

        if (regId.equals("")) {
            Log.v("GCM", "Registering");
            GCMRegistrar.register(this, API.SENDER_ID);
        } else {
            Log.v("GCM", "Already registered");
        }

        //Log.e("GCMID",GCMRegistrar.getRegistrationId(this));

        JSONObject post = new JSONObject();
        try {
            post.put("gcmid", regId);
            post.put("deviceid", Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID));
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "An Error Was encountered connecting to the GCM cloud. Chat will be unavailable",
                    Toast.LENGTH_LONG).show();
            return;
        }

        JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,
                "http://hackedioapp.networksaremadeofstring.co.uk/registergcm.php", post,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.e("response", response.toString());
                        try {
                            if (response.has("success") && response.getBoolean("success")) {
                                //Alls cool
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub
                    }
                });

        queue.add(jsObjRequest);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in
        // the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    /**
     * Defines what we do when a schedule item is clicked.
     *
     * @param SchItem The selected schedule item
     */
    @Override
    public void onScheduleItemSelected(ScheduleItem SchItem) {
        if (findViewById(R.id.ScheduleDetails) != null) {
            mTwoPane = true;
            //((ScheduleListFragment) getSupportFragmentManager().findFragmentById(R.id.pager)).setActivateOnItemClick(true);

            Bundle arguments = new Bundle();
            arguments.putString(ScheduleItemDetailFragment.SCHEDULETITLE, SchItem.Title);
            arguments.putString(ScheduleItemDetailFragment.SCHEDULEID, SchItem.ID);
            arguments.putString(ScheduleItemDetailFragment.SCHEDULEFULLDATAHTML, SchItem.fullDetails);
            arguments.putString(ScheduleItemDetailFragment.SCHEDULESTARTTIME, SchItem.StartDate);
            arguments.putString(ScheduleItemDetailFragment.SCHEDULEDESC, SchItem.itemDesc);
            arguments.putBoolean(ScheduleItemDetailFragment.ISDAYTITLE, SchItem.isDay);
            arguments.putString(ScheduleItemDetailFragment.SCHEDULEENDTIME, SchItem.EndDate);
            arguments.putBoolean(ScheduleItemDetailFragment.ARG_2PANE, true);

            ScheduleItemDetailFragment fragment = new ScheduleItemDetailFragment();
            fragment.setArguments(arguments);
            getSupportFragmentManager().beginTransaction().replace(R.id.ScheduleDetails, fragment).commit();
        } else {
            mTwoPane = false;

            Intent detailIntent = new Intent(this, ScheduleListItemDetailActivity.class);

            detailIntent.putExtra(ScheduleItemDetailFragment.SCHEDULETITLE, SchItem.Title);
            detailIntent.putExtra(ScheduleItemDetailFragment.SCHEDULEID, SchItem.ID);
            detailIntent.putExtra(ScheduleItemDetailFragment.SCHEDULEFULLDATAHTML, SchItem.fullDetails);
            detailIntent.putExtra(ScheduleItemDetailFragment.SCHEDULESTARTTIME, SchItem.StartDate);
            detailIntent.putExtra(ScheduleItemDetailFragment.SCHEDULEDESC, SchItem.itemDesc);
            detailIntent.putExtra(ScheduleItemDetailFragment.ISDAYTITLE, SchItem.isDay);
            detailIntent.putExtra(ScheduleItemDetailFragment.SCHEDULEENDTIME, SchItem.EndDate);
            detailIntent.putExtra(ScheduleItemDetailFragment.ARG_2PANE, true);

            startActivity(detailIntent);
        }
    }

    @Override
    public void onProjectItemSelected(Project project) {
        if (findViewById(R.id.ProjectDetails) != null) {
            mTwoPane = true;

            Bundle arguments = new Bundle();
            arguments.putInt(ProjectDetailsFragment.PROJECTID, project.ID);
            arguments.putString(ProjectDetailsFragment.NAME, project.Name);
            arguments.putString(ProjectDetailsFragment.SYNOPSIS, project.Synopsis);
            arguments.putString(ProjectDetailsFragment.DESCRIPTION, project.Description);
            arguments.putString(ProjectDetailsFragment.LOCATION, project.Location);
            arguments.putString(ProjectDetailsFragment.TECHNOLOGIES, project.Technologies);
            arguments.putInt(ProjectDetailsFragment.POPULARITY, project.Popularity);
            //arguments.putString(ProjectDetailsFragment.TEAMMEMBERS, project.getTeamMembers());
            arguments.putBoolean(ProjectDetailsFragment.ARG_2PANE, true);
            arguments.putParcelableArrayList(ProjectDetailsFragment.TEAMMEMBERS, project.TeamMembers);

            ProjectDetailsFragment fragment = new ProjectDetailsFragment();
            fragment.setArguments(arguments);
            fragment.setHasOptionsMenu(true);
            getSupportFragmentManager().beginTransaction().replace(R.id.ProjectDetails, fragment).commit();
        } else {
            mTwoPane = false;

            Intent detailIntent = new Intent(this, ProjectDetailsActivity.class);
            detailIntent.putExtra(ProjectDetailsFragment.PROJECTID, project.ID);
            detailIntent.putExtra(ProjectDetailsFragment.NAME, project.Name);
            detailIntent.putExtra(ProjectDetailsFragment.SYNOPSIS, project.Synopsis);
            detailIntent.putExtra(ProjectDetailsFragment.DESCRIPTION, project.Description);
            detailIntent.putExtra(ProjectDetailsFragment.LOCATION, project.Location);
            detailIntent.putExtra(ProjectDetailsFragment.TECHNOLOGIES, project.Technologies);
            detailIntent.putExtra(ProjectDetailsFragment.POPULARITY, project.Popularity);
            //detailIntent.putExtra(ProjectDetailsFragment.TEAMMEMBERS, project.getTeamMembers());
            detailIntent.putParcelableArrayListExtra(ProjectDetailsFragment.TEAMMEMBERS, project.TeamMembers);
            detailIntent.putExtra(ProjectDetailsFragment.ARG_2PANE, false);
            startActivity(detailIntent);
        }
    }

    @Override
    public void onChallengeItemSelected(ChallengeItem challenge) {
        if (findViewById(R.id.ChallengeDetails) != null) {
            mTwoPane = true;
            Bundle arguments = new Bundle();
            arguments.putString(ChallengeDetailsFragment.TITLE, challenge.ChallengeTitle);
            arguments.putString(ChallengeDetailsFragment.DETAILS, challenge.ChallengeContent);
            arguments.putBoolean(ProjectDetailsFragment.ARG_2PANE, true);
            ChallengeDetailsFragment fragment = new ChallengeDetailsFragment();
            fragment.setArguments(arguments);
            getSupportFragmentManager().beginTransaction().replace(R.id.ChallengeDetails, fragment).commit();
        } else {
            mTwoPane = false;
            Intent detailIntent = new Intent(this, ChallengeDetailsActivity.class);
            detailIntent.putExtra(ChallengeDetailsFragment.TITLE, challenge.ChallengeTitle);
            detailIntent.putExtra(ChallengeDetailsFragment.DETAILS, challenge.ChallengeContent);
            detailIntent.putExtra(ProjectDetailsFragment.ARG_2PANE, false);
            startActivity(detailIntent);
        }
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;

            switch (position) {
            case 0: {
                fragment = new ScheduleListFragment();
            }
                break;

            case 1: {
                fragment = new ProjectListFragment();
                fragment.setHasOptionsMenu(true);
            }
                break;

            case 2: {
                fragment = new ChallengeListFragment();
            }
                break;

            case 3: {
                fragment = new ChatFragment();
                fragment.setHasOptionsMenu(true);
            }
                break;

            case 4: {
                fragment = new WorkShopsFragment();
                //fragment.setHasOptionsMenu(true);
            }
                break;

            /*default:
            {
                // getItem is called to instantiate the fragment for the given page.
                // Return a DummySectionFragment (defined as a static inner class
                // below) with the page number as its lone argument.
                fragment = new DummySectionFragment();
                Bundle args = new Bundle();
                args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
                fragment.setArguments(args);
            }*/
            }
            return fragment;
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
            case 0:
                return "SCHEDULE";
            case 1:
                return "PROJECTS LIST";
            case 2:
                return "CHALLENGES";
            case 3:
                return "CHAT";
            case 4:
                return "WORKSHOPS";
            }
            return null;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_scavenge_game: {
            Intent gameIntent = new Intent(this, ScavengerHunt.class);
            startActivity(gameIntent);
            return true;
        }
        }

        return false;
    }
}