org.maikelwever.droidpile.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for org.maikelwever.droidpile.MainActivity.java

Source

/*
 * This file is part of Droidpile: a Mailpile client for Android.
 *
 * You should have recieved a copy of the LICENSE file along with Droidpile.
 * If not: see <https://github.com/maikelwever/droidpile/blob/master/LICENSE>
 */
package org.maikelwever.droidpile;

import android.app.NotificationManager;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.widget.SearchView;
import com.crashlytics.android.Crashlytics;

import org.maikelwever.droidpile.adapters.TabsPagerAdapter;
import org.maikelwever.droidpile.background.BootReciever;

public class MainActivity extends SherlockFragmentActivity {

    private ViewPager viewPager;
    private TabsPagerAdapter pagerAdapter;
    public static String[] tabs = { "Inbox", "Drafts", "Sent", "Outbox", };

    public MainActivity() {
        instance = this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // This is enables on releases. TODO (reminder)
        //Crashlytics.start(this);

        setContentView(R.layout.activity_main);
        final ActionBar actionBar = getSupportActionBar();

        viewPager = (ViewPager) findViewById(R.id.pager);
        pagerAdapter = new TabsPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.TabListener tabListener = new ActionBar.TabListener() {
            @Override
            public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
                viewPager.setCurrentItem(tab.getPosition());
            }

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

            }

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

            }
        };

        viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                getSupportActionBar().setSelectedNavigationItem(position);
            }
        });

        actionBar.setHomeButtonEnabled(false);
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(tabListener));
        }

        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancel(1234);

    }

    @Override
    public void onStart() {
        super.onStart();

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

        if (preferences.getString("server_hostname", "").isEmpty()) {
            Toast.makeText(getApplicationContext(), getString(R.string.settings_unset_message), Toast.LENGTH_LONG)
                    .show();

            startActivity(new Intent(getBaseContext(), SettingsActivity.class));
        }

        BootReciever.registerWithAlarmManager(getContext());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getSupportMenuInflater().inflate(R.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();
        if (id == R.id.action_settings) {
            startActivity(new Intent(getBaseContext(), SettingsActivity.class));
            return true;
        } else if (id == R.id.action_search) {
            onSearchRequested();
            return true;
        } else if (id == R.id.action_send_new) {
            startActivity(new Intent(getBaseContext(), SendMailActivity.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private static MainActivity instance;

    public static Context getContext() {
        return instance;
    }
}