at.entenbaer.gui.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for at.entenbaer.gui.MainActivity.java

Source

/**
 * This file is part of TexturePoemApp.
 * Created by Matthias Perkonigg, 2015.
 * TexturePoemApp 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.
 *   
 *  TexturePoemApp 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 TexturePoemApp.  If not, see <http://www.gnu.org/licenses/>.
 */

package at.entenbaer.gui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.widget.DrawerLayout;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import at.entenbaer.texturepoemapp.R;

/**
 * Activity that is the entry point of Texture Poem App
 * Handles fragments, holds the navigation drawer
 */
public class MainActivity extends Activity implements ListView.OnItemClickListener,
        CameraViewFragment.OnPictureSavedListener, PictureViewerFragment.OnBackListener {

    private ListView drawerList;
    private DrawerLayout navDrawer;
    CameraViewFragment cvFragment;
    PictureViewerFragment picFragment;
    SharedPreferences sharedPref;

    ArrayList<DrawerItem> drawerItems = new ArrayList<DrawerItem>();

    @Override
    public void pictureSaved(String s) {
        picFragment = new PictureViewerFragment();
        picFragment.setPicture(s);
        getFragmentManager().beginTransaction().replace(R.id.camera_view, picFragment).commit();
    }

    @Override
    public void backPressed() {
        getFragmentManager().beginTransaction().remove(cvFragment).commit();
        cvFragment = new CameraViewFragment();
        getFragmentManager().beginTransaction().replace(R.id.camera_view, cvFragment).commit();
    }

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

        sharedPref = PreferenceManager.getDefaultSharedPreferences(this);

        if (!sharedPref.contains("firstRun")) {
            //start update indent
            Intent intent = new Intent(this, UpdateActivity.class);
            startActivity(intent);

        }

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);

        drawerItems.add(new DrawerItem(getString(R.string.settings), R.drawable.ic_setting));
        drawerItems.add(new DrawerItem(getString(R.string.realtimeMode), R.drawable.ic_time));

        drawerList = (ListView) findViewById(R.id.left_drawer);

        navDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        DrawerAdapter adapter = new DrawerAdapter(this, drawerItems);
        drawerList.setAdapter(adapter);

        drawerList.setOnItemClickListener(this);

        cvFragment = new CameraViewFragment();

        getFragmentManager().beginTransaction().replace(R.id.camera_view, cvFragment).commit();
    }

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

    @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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
        case 0:
            Intent intent = new Intent(this, SettingsActivity.class);
            this.startActivity(intent);
            break;
        case 1:
            if (!cvFragment.realTimeMode) {
                cvFragment.activeRealTimeMode();
                navDrawer.closeDrawers();
                drawerItems.get(1).icon = R.drawable.ic_time_active;
                DrawerAdapter adapter = new DrawerAdapter(this, drawerItems);
                drawerList.setAdapter(adapter);
            } else {
                cvFragment.deactivateRealTimeMode();
                navDrawer.closeDrawers();
                drawerItems.get(1).icon = R.drawable.ic_time;
                DrawerAdapter adapter = new DrawerAdapter(this, drawerItems);
                drawerList.setAdapter(adapter);
            }
            break;
        }
    }

    class DrawerItem {
        String text;
        int icon;

        public DrawerItem(String text, int icon) {
            this.text = text;
            this.icon = icon;
        }
    }

    class DrawerAdapter extends BaseAdapter {
        Context context;
        ArrayList<DrawerItem> drawerItems;

        public DrawerAdapter(Context context, ArrayList<DrawerItem> drawerItems) {
            this.context = context;
            this.drawerItems = drawerItems;
        }

        @Override
        public int getCount() {
            return drawerItems.size();
        }

        @Override
        public Object getItem(int position) {
            return drawerItems.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v;

            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = inflater.inflate(R.layout.tpa_drawer_item, null);
            } else {
                v = convertView;
            }

            TextView itemName = (TextView) v.findViewById(R.id.drawer_itemname);
            ImageView icon = (ImageView) v.findViewById(R.id.drawer_icon);

            itemName.setText(drawerItems.get(position).text);
            icon.setImageResource(drawerItems.get(position).icon);

            return v;
        }
    }

}