com.sweetiepiggy.littlepro.BrowseQuizzesActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.sweetiepiggy.littlepro.BrowseQuizzesActivity.java

Source

/*
Copyright (C) 2014-2015 Sweetie Piggy Apps <sweetiepiggyapps@gmail.com>
    
This file is part of Little Pro.
    
Little Pro 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.
    
Little Pro 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 Little Pro; if not, see <http://www.gnu.org/licenses/>.
*/

package com.sweetiepiggy.littlepro;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.Toast;

public class BrowseQuizzesActivity extends LockedActionBarActivity
        implements DownloadQuizListTask.OnDownloadedListener {
    private static final String FRAGMENT_TAG = "fragment_tag";

    private DownloadQuizListTask mDownloadQuizListTask = null;
    private BrowseQuizzesFragment mBrowseQuizzesFragment = null;
    private String mEmail = null;
    private long mCourseId = -1;
    private long mSubjectId = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_container);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        if (savedInstanceState == null) {
            mBrowseQuizzesFragment = new BrowseQuizzesFragment();
            getSupportFragmentManager().beginTransaction()
                    .add(android.R.id.content, mBrowseQuizzesFragment, FRAGMENT_TAG).commit();

            Bundle bundle = getIntent().getExtras();
            if (bundle != null) {
                loadState(bundle);
            }
        } else {
            mBrowseQuizzesFragment = (BrowseQuizzesFragment) getSupportFragmentManager()
                    .findFragmentByTag(FRAGMENT_TAG);
            loadState(savedInstanceState);
        }

        mDownloadQuizListTask = new DownloadQuizListTask(this, this, mCourseId, mSubjectId);
        mDownloadQuizListTask.execute();
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        savedInstanceState.putString("email", mEmail);
        savedInstanceState.putLong("courseId", mCourseId);
        savedInstanceState.putLong("subjectId", mSubjectId);
        super.onSaveInstanceState(savedInstanceState);
    }

    private void loadState(Bundle bundle) {
        mEmail = bundle.getString("email");
        mCourseId = bundle.getLong("courseId", -1);
        mSubjectId = bundle.getLong("subjectId", -1);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        loadState(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        if (mDownloadQuizListTask != null) {
            mDownloadQuizListTask.cancel(true);
        }
        super.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_refresh, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_refresh:
            if (mDownloadQuizListTask != null) {
                mDownloadQuizListTask.cancel(true);
            }
            mDownloadQuizListTask = new DownloadQuizListTask(this, this, mCourseId, mSubjectId);
            mDownloadQuizListTask.execute();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    public void onDownloaded(List<String> quizNames, List<Long> quizIds) {
        if (getApplicationContext() != null) {
            mBrowseQuizzesFragment.notifyDataSetChanged(quizNames, quizIds, mEmail, mCourseId, mSubjectId);
        }
    }

    public static class BrowseQuizzesFragment extends ListFragment {
        private List<String> mQuizNames = null;
        private List<Long> mQuizIds = null;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            if (container == null) {
                return null;
            }

            View view = inflater.inflate(R.layout.fragment_browse_quizzes, container, false);
            return view;
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
        }

        public void notifyDataSetChanged(List<String> quizNames, List<Long> quizIds, final String email,
                final long courseId, final long subjectId) {
            if (quizNames != null && quizIds != null) {
                mQuizNames = quizNames;
                mQuizIds = quizIds;
                ListAdapter adapter = new ArrayAdapter(getActivity().getApplicationContext(),
                        R.layout.simple_list_item_1, android.R.id.text1, mQuizNames);
                setListAdapter(adapter);

                getListView().setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
                        long quizId = mQuizIds.get(pos);
                        Intent intent = new Intent(getActivity().getApplicationContext(), QuizActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("email", email);
                        bundle.putLong("courseId", courseId);
                        bundle.putLong("subjectId", subjectId);
                        bundle.putLong("quizId", quizId);
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                });
            }
        }
    }
}