Android Open Source - Icinga-Mobile Main Activity






From Project

Back to project page Icinga-Mobile.

License

The source code is released under:

GNU General Public License

If you think the Android project Icinga-Mobile listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package mhst.dreamteam;
/* w  w  w  .j  ava 2s . c o  m*/
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.ContentResolver;
import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import mhst.dreamteam.IcingaClient.GlobalConst;
import mhst.dreamteam.IcingaClient.Icinga.IcingaUdt;
import mhst.dreamteam.UI.LoginActivity;
import mhst.dreamteam.IcingaClient.SessionMng.Session;
import mhst.dreamteam.UI.HostlistFragment;
import mhst.dreamteam.UI.OverviewFragment;
import mhst.dreamteam.UI.ServicelistFragment;

/**
 * This is where our program starts
 *
 * @author MinhNN
 */
public class MainActivity extends Activity {
    private Session currentSession;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    private ListView mDrawerList;
    private String[] menuItems;
    private int[] menuNumbers;

    private final int UP_OK = 0;
    private final int DOWN_WARNING = 1;
    private final int UNREACHABLE_CRITICAL = 2;
    private final int PENDING_UNKNOWN = 3;

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

        currentSession = Session.getInstance();

        initSession();

        // Log in if not, required
        if (!currentSession.isLogin()) {
            Intent i = new Intent(MainActivity.this, LoginActivity.class);
            startActivityForResult(i, GlobalConst.REQUESTCODE_REQUIRE_LOGIN);
        }

        setContentView(R.layout.activity_main);

        menuItems = getResources().getStringArray(R.array.side_menu_item);
        menuNumbers = new int[menuItems.length];
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.lv_nav_drawer);

        ArrayAdapter<String> nav_adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.layout_side_menu_item, menuItems) {
            @Override
            @SuppressWarnings("ViewHolder")
            public View getView(int position, View convertView, ViewGroup parent) {
                convertView = getLayoutInflater().inflate(R.layout.layout_side_menu_item, parent, false);

                TextView tvTitle = (TextView) convertView.findViewById(R.id.tvSideMenuItemTitle);
                TextView tvNumber = (TextView) convertView.findViewById(R.id.tvSideMenuItemNumber);

                tvTitle.setText(menuItems[position]);
                if (menuNumbers[position] != 0) {
                    tvNumber.setText(menuItems[position]);
                }

                return convertView;
            }
        };

        mDrawerList.setAdapter(nav_adapter);

        mDrawerList.setOnItemClickListener(new ListView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectItem(position, null);
            }
        });

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_launcher, R.string.app_name, R.string.app_name) {
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                invalidateOptionsMenu();
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                invalidateOptionsMenu();
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);
        getActionBar().setHomeButtonEnabled(true);

    }

    private void selectItem(int pos) {

        Intent i = getIntent();
        int reqCode = i.getIntExtra("RequestCode", -1);
        if (reqCode == 0) { // Host request
            int state = i.getIntExtra("RequestInfo", -1);
            if (state != -1) {
                Bundle b = new Bundle();
                switch (state) {
                    case UP_OK:
                        b.putString("Request", IcingaUdt.getTemplate(IcingaUdt.ICINGA_TEMPLATE_MAINACTIVITY_OKHOST, 0, -1, null));
                        b.putString("TitleEx", "[UP]");
                        break;
                    case DOWN_WARNING:
                        b.putString("Request", IcingaUdt.getTemplate(IcingaUdt.ICINGA_TEMPLATE_MAINACTIVITY_DOWNHOST, 0, -1, null));
                        b.putString("TitleEx", "[DWN]");
                        break;
                    case UNREACHABLE_CRITICAL:
                        b.putString("Request", IcingaUdt.getTemplate(IcingaUdt.ICINGA_TEMPLATE_MAINACTIVITY_UNREACHABLEHOST, 0, -1, null));
                        b.putString("TitleEx", "[UNR]");
                        break;
                    case PENDING_UNKNOWN:
                        b.putString("Request", IcingaUdt.getTemplate(IcingaUdt.ICINGA_TEMPLATE_MAINACTIVITY_PENDINGHOST, 0, -1, null));
                        b.putString("TitleEx", "[PEN]");
                        break;
                }
                selectItem(1, b);
            }
        } else if (reqCode == 1) { // Service request
            int state = i.getIntExtra("RequestInfo", -1);
            if (state != -1) {
                Bundle b = new Bundle();
                switch (state) {
                    case UP_OK:
                        b.putString("Request", IcingaUdt.getTemplate(IcingaUdt.ICINGA_TEMPLATE_MAINACTIVITY_OKSERVICE, 0, -1, null));
                        b.putString("TitleEx", "[OK]");
                        break;
                    case DOWN_WARNING:
                        b.putString("Request", IcingaUdt.getTemplate(IcingaUdt.ICINGA_TEMPLATE_MAINACTIVITY_WARNINGSERVICE, 0, -1, null));
                        b.putString("TitleEx", "[WRN]");
                        break;
                    case UNREACHABLE_CRITICAL:
                        b.putString("Request", IcingaUdt.getTemplate(IcingaUdt.ICINGA_TEMPLATE_MAINACTIVITY_CRITICALSERVICE, 0, -1, null));
                        b.putString("TitleEx", "[CRT]");
                        break;
                    case PENDING_UNKNOWN:
                        b.putString("Request", IcingaUdt.getTemplate(IcingaUdt.ICINGA_TEMPLATE_MAINACTIVITY_UNKNOWSERVICE, 0, -1, null));
                        b.putString("TitleEx", "[UNK]");
                        break;
                }
                selectItem(2, b);
            }
        } else {
            selectItem(pos, null);
        }
    }

    private void initSession() {
        ContentResolver resolver = getContentResolver();
        CursorLoader loader = new CursorLoader(this, Uri.parse(GlobalConst.CONTENT_SESSION_URI), null, null, null, null);
        Cursor c = loader.loadInBackground();
        if (c != null) {
            c.moveToFirst();
            while (c.moveToNext()) {
                if (!isNullOrEmpty(c.getString(c.getColumnIndex("Cookie")))) {
                    currentSession.isLogin(true);
                    currentSession.setServer(c.getString(c.getColumnIndex("Server")));
                    currentSession.setCookie(c.getString(c.getColumnIndex("Cookie")));
                }
            }
        }
    }

    private boolean isNullOrEmpty(String s) {
        return ((s == null) || (s.isEmpty()));
    }

    private void selectItem(int pos, Bundle b) {
        Fragment fragToChange;
        boolean notFoundFragment = true;
        FragmentManager fragMng = getFragmentManager();
        fragToChange = fragMng.findFragmentByTag(menuItems[pos]);
        switch (pos) {
            case 0: // Overview
                if ((notFoundFragment = (fragToChange == null))) {
                    fragToChange = new OverviewFragment();
                }
                break;
            case 1: // Host
                if ((notFoundFragment = (fragToChange == null))) {
                    fragToChange = new HostlistFragment();
                }
                break;
            case 2: // Service
                if ((notFoundFragment = (fragToChange == null))) {
                    fragToChange = new ServicelistFragment();
                }
                break;
        }

        if (fragToChange != null) {
            if (b != null) {
                fragToChange.setArguments(b);
            }
            FragmentTransaction fragTrans = fragMng.beginTransaction();
            fragTrans.replace(R.id.main_content, fragToChange);
            if (notFoundFragment) {
                fragTrans.addToBackStack(menuItems[pos]);
            }
            fragTrans.commit();
        }

        mDrawerList.setItemChecked(pos, true);
        mDrawerLayout.closeDrawers();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case GlobalConst.REQUESTCODE_REQUIRE_LOGIN:
                // if user not log in, just exit; otherwise, update main activity view
                if (resultCode != GlobalConst.RETURNCODE_SUCCESS) {
                    finish();
                } else {
                    selectItem(0); // Overview
                }
                break;
        }
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    @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 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.

        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        } else {
            super.onOptionsItemSelected(item);
            switch (item.getItemId()) {
                case R.id.action_logout:
                    currentSession.doLogout();
                    Intent i = new Intent("mhst.dreamteam.intent.SESSION_CHANGED");
                    sendBroadcast(i);
                    i = new Intent(MainActivity.this, LoginActivity.class);
                    startActivityForResult(i, GlobalConst.REQUESTCODE_REQUIRE_LOGIN);
                    break;
            }
        }

        return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
    }
}




Java Source Code List

mhst.dreamteam.ApplicationContext.java
mhst.dreamteam.ApplicationTest.java
mhst.dreamteam.ApplicationTest.java
mhst.dreamteam.MainActivity.java
mhst.dreamteam.IcingaClient.GlobalConfig.java
mhst.dreamteam.IcingaClient.GlobalConst.java
mhst.dreamteam.IcingaClient.Controller.NetControllerTest.java
mhst.dreamteam.IcingaClient.Controller.NetController.java
mhst.dreamteam.IcingaClient.Icinga.IcingaApiConst.java
mhst.dreamteam.IcingaClient.Icinga.IcingaApi.java
mhst.dreamteam.IcingaClient.Icinga.IcingaConst.java
mhst.dreamteam.IcingaClient.Icinga.IcingaExecutor.java
mhst.dreamteam.IcingaClient.Icinga.IcingaParam.java
mhst.dreamteam.IcingaClient.Icinga.IcingaUdt.java
mhst.dreamteam.IcingaClient.Icinga.package-info.java
mhst.dreamteam.IcingaClient.Interface.OnCompleteListener.java
mhst.dreamteam.IcingaClient.Interface.OnPieChartClickListener.java
mhst.dreamteam.IcingaClient.Json.JsonHelperTest.java
mhst.dreamteam.IcingaClient.Json.JsonHelper.java
mhst.dreamteam.IcingaClient.Misc.CookieMng.java
mhst.dreamteam.IcingaClient.Misc.CookieTest.java
mhst.dreamteam.IcingaClient.SessionMng.LogInTest.java
mhst.dreamteam.IcingaClient.SessionMng.Login.java
mhst.dreamteam.IcingaClient.SessionMng.Logout.java
mhst.dreamteam.IcingaClient.SessionMng.Session.java
mhst.dreamteam.IcingaService.ApplicationContext.java
mhst.dreamteam.IcingaService.DataUpdater.java
mhst.dreamteam.IcingaService.MessageReveicer.java
mhst.dreamteam.IcingaService.NotiBuilder.java
mhst.dreamteam.IcingaService.SQLHelper.java
mhst.dreamteam.IcingaService.SessionProvider.java
mhst.dreamteam.UI.Color.java
mhst.dreamteam.UI.GradientLine.java
mhst.dreamteam.UI.HostDetailsFragment.java
mhst.dreamteam.UI.HostlistAdapter.java
mhst.dreamteam.UI.HostlistFragment.java
mhst.dreamteam.UI.LoginActivity.java
mhst.dreamteam.UI.OverviewFragment.java
mhst.dreamteam.UI.PieGraph.java
mhst.dreamteam.UI.ProgressDialog.java
mhst.dreamteam.UI.ServiceDetailsFragment.java
mhst.dreamteam.UI.ServicelistAdapter.java
mhst.dreamteam.UI.ServicelistFragment.java
org.json.CDL.java
org.json.CookieList.java
org.json.Cookie.java
org.json.HTTPTokener.java
org.json.HTTP.java
org.json.JSONArray.java
org.json.JSONException.java
org.json.JSONML.java
org.json.JSONObject.java
org.json.JSONString.java
org.json.JSONStringer.java
org.json.JSONTokener.java
org.json.JSONWriter.java
org.json.Kim.java
org.json.Property.java
org.json.XMLTokener.java
org.json.XML.java
org.json.zip.BitInputStream.java
org.json.zip.BitOutputStream.java
org.json.zip.BitReader.java
org.json.zip.BitWriter.java
org.json.zip.Huff.java
org.json.zip.JSONzip.java
org.json.zip.Keep.java
org.json.zip.None.java
org.json.zip.PostMortem.java
org.json.zip.Unzipper.java
org.json.zip.Zipper.java