Android Open Source - SeeKampf Main Activity






From Project

Back to project page SeeKampf.

License

The source code is released under:

GNU General Public License

If you think the Android project SeeKampf 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 net.avedo.seekampf.core;
/*from ww w  .  j a  v a 2 s  .  com*/
import net.avedo.seekampf.R;
import net.avedo.seekampf.fragments.AboutFragment;
import net.avedo.seekampf.fragments.AllianceListFragment;
import net.avedo.seekampf.fragments.AuctionListFragment;
import net.avedo.seekampf.fragments.HomeFragment;
import net.avedo.seekampf.fragments.MessageListFragment;
import net.avedo.seekampf.fragments.OceanFragment;
import net.avedo.seekampf.fragments.PlayerListFragment;
import net.avedo.seekampf.fragments.SettingsFragment;
import net.avedo.seekampf.utils.Constants;
import net.avedo.seekampf.utils.Interfaces;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
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.BaseAdapter;
import android.widget.ListView;
import android.widget.ShareActionProvider;
import android.widget.TextView;

public class MainActivity extends VolleyActivity implements Interfaces.OnFragmentActionListener {
  private DrawerLayout drawer;
  private ListView drawerList;
  private int drawerTitle = R.string.app_name;
  private ActionBarDrawerToggle drawerToggle;
  private int title = R.string.home;
  
  private ShareActionProvider shareActionProvider;

    @Override
  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // Set the default preferences if not already set, ...
        PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
        
        // ... setup the navigation ...
    setupNavigation();
    
    // ... and navigate to the home page.
    navigateToItem(Constants.NAVIGATION_HOME, false);
    
    // Show the change log if necessary.
    ChangeLog log = new ChangeLog(this);
    
    if(log.firstRunAfterUpdate() || log.firstRunAfterInstallation()) {
      log.getDialog().show();
    }
  }

    @Override
  protected void onPostCreate(Bundle paramBundle) {
    super.onPostCreate(paramBundle);
    this.drawerToggle.syncState();
  }

    @Override
  public void onFragmentAction(int paramInt, Bundle paramBundle) {
  }

    @Override
  public void onFragmentRequest(int paramInt, Bundle paramBundle) {
  }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate menu resource file, ...
        getMenuInflater().inflate(R.menu.main, menu);

        // ... locate the ShareActionProvider menu item ...
        MenuItem item = menu.findItem(R.id.menu_item_share);

        // ... and fetch and store the ShareActionProvider.
        shareActionProvider = (ShareActionProvider) item.getActionProvider();
        
        // Finally setup the share intent ...
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, getResources().getString(R.string.shareMsg));
        
        // ... and assign it to the action provider.
        shareActionProvider.setShareIntent(shareIntent);

        return true;
    }

    @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Check if the ActionBarDrawerToggle can handle this menu item.
      if (drawerToggle.onOptionsItemSelected(item)) {
        return true;
      }
      
      return super.onOptionsItemSelected(item);
  }

    @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    this.drawerToggle.onConfigurationChanged(newConfig);
  }

    private void setupNavigation() {
    // Fetch the drawer ...
    this.drawer = ((DrawerLayout) findViewById(R.id.drawerLayout));
    
    // ... and initialize the drawer toggle.
    this.drawerToggle = new ActionBarDrawerToggle(this, this.drawer,
        R.drawable.ic_drawer, R.string.menu, R.string.app_name) {
      
      public void onDrawerClosed(View paramAnonymousView) {
        super.onDrawerClosed(paramAnonymousView);
        getActionBar().setTitle(title);
      }

      public void onDrawerOpened(View paramAnonymousView) {
        super.onDrawerOpened(paramAnonymousView);
        getActionBar().setTitle(drawerTitle);
      }
    };
    
    // Set the drawer listener ...
    this.drawer.setDrawerListener(this.drawerToggle);
    
    // ... and some actionBar properties.
    getActionBar().setHomeButtonEnabled(true);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    
    // Finally fetch the ListView object, ...
    this.drawerList = ((ListView) findViewById(R.id.navigationLst));
    
    // ... assign the list adapter ...
    this.drawerList.setAdapter(new MenuAdapter(this));
    
    // ... as well as the onClick listener.
    this.drawerList.setOnItemClickListener(new NavigationItemClickListener());
  }

  private void setContentFrame(int resId, Fragment fragment,
      boolean addToBackStack) {
    // Setup a new fragment transaction, ...
    FragmentTransaction ft = getFragmentManager().beginTransaction();

    // ... and replace the current fragment with the given one.
    ft.replace(R.id.mainFrm, fragment);

    // Then add the fragment to the backstack.
    if (addToBackStack) {
      ft.addToBackStack(null);
    }

    ft.commit();

    // Finally save the title ...
    title = resId;

    // ... and update it within the actionBar.
    getActionBar().setTitle(resId);
  }

  private void navigateToItem(int position, boolean addToBackStack) {
    Fragment fragment;
    switch (position) {
    case Constants.NAVIGATION_HOME:
      fragment = new HomeFragment();
          setContentFrame(R.string.home, fragment, addToBackStack);
      break;
    case Constants.NAVIGATION_AUCTION_HOUSE:
      fragment = new AuctionListFragment();
          setContentFrame(R.string.auctionHouse, fragment, addToBackStack);
      break;
    case Constants.NAVIGATION_PLAYERS:
      fragment = new PlayerListFragment();
          setContentFrame(R.string.players, fragment, addToBackStack);
      break;
    case Constants.NAVIGATION_ISLANDS:
      fragment = new OceanFragment();
          setContentFrame(R.string.islands, fragment, addToBackStack);
      break;
    case Constants.NAVIGATION_ALLIANCES:
      fragment = new AllianceListFragment();
          setContentFrame(R.string.alliances, fragment, addToBackStack);
      break;
    case Constants.NAVIGATION_MESSAGES:
      fragment = new MessageListFragment();
          setContentFrame(R.string.messages, fragment, addToBackStack);
      break;
    case Constants.NAVIGATION_SETTINGS:
      fragment = new SettingsFragment();
          setContentFrame(R.string.settings, fragment, addToBackStack);
      break;
    case Constants.NAVIGATION_ABOUT:
      fragment = new AboutFragment();
          setContentFrame(R.string.about, fragment, addToBackStack);
          break;
    default:
      return;
    }
    
    drawerList.setItemChecked(position, true);
    drawer.closeDrawer(drawerList);
  }

  private class NavigationItemClickListener implements
      AdapterView.OnItemClickListener {
    private NavigationItemClickListener() {}

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      navigateToItem(position, true);
    }
  }
  
  private class MenuAdapter extends BaseAdapter {
    private LayoutInflater layoutInflater;

    private int[] menuDrawables;
    private String[] menuItems;
    
    public MenuAdapter(Context context) {
      // Fetch the application resource handle ...
      Resources res = context.getResources();
      
      // ... and fetch the menu entries ...
      this.menuItems = res.getStringArray(R.array.navigation_items);
      
      // ... and their drawables.
      TypedArray icons = res.obtainTypedArray(R.array.navigation_drawables);
      
      int len = icons.length();
      
      this.menuDrawables = new int[len];
      for (int i = 0; i < len; i++) {
        this.menuDrawables[i] = icons.getResourceId(i, 0);
      }
      
      icons.recycle();
      
      // Finally  initialize the layout inflater.
      layoutInflater = (LayoutInflater) context
          .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
      return menuItems.length;
    }

    @Override
    public Object getItem(int position) {
      return menuItems[position];
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
          if (convertView == null) {
            convertView = (View) layoutInflater.inflate(R.layout.navigation_row, parent, false);
      }
          
          // Fetch the TxetView object, ...
        TextView menuItem = (TextView) convertView.findViewById(R.id.navigationItem);
        
        // ... assign the menu item text ...
        menuItem.setText(menuItems[position]);
        
        // ... and the compound drawable.
        menuItem.setCompoundDrawablesWithIntrinsicBounds(menuDrawables[position], 0, 0, 0);
        
        // Finally adjust the compound drawable padding.
        menuItem.setCompoundDrawablePadding(7);
          
      return convertView;
    }
  }
}




Java Source Code List

net.avedo.seekampf.BuildConfig.java
net.avedo.seekampf.core.ChangeLog.java
net.avedo.seekampf.core.CustomAdapter.java
net.avedo.seekampf.core.MainActivity.java
net.avedo.seekampf.core.OceanView.java
net.avedo.seekampf.core.OverScrollerCompat.java
net.avedo.seekampf.core.RestDetailsActivity.java
net.avedo.seekampf.core.ScaleGestureDetectorCompat.java
net.avedo.seekampf.core.VolleyActivity.java
net.avedo.seekampf.core.Zoomer.java
net.avedo.seekampf.fragments.AboutFragment.java
net.avedo.seekampf.fragments.AllianceListFragment.java
net.avedo.seekampf.fragments.AuctionListFragment.java
net.avedo.seekampf.fragments.HomeDetailsFragment.java
net.avedo.seekampf.fragments.HomeFragment.java
net.avedo.seekampf.fragments.IslandDetailsFragment.java
net.avedo.seekampf.fragments.IslandListFragment.java
net.avedo.seekampf.fragments.MessageDetailsFragment.java
net.avedo.seekampf.fragments.MessageListFragment.java
net.avedo.seekampf.fragments.OceanFragment.java
net.avedo.seekampf.fragments.PlayerListFragment.java
net.avedo.seekampf.fragments.RestDetailsFragment.java
net.avedo.seekampf.fragments.RestListFragment.java
net.avedo.seekampf.fragments.SettingsFragment.java
net.avedo.seekampf.models.Alliance.java
net.avedo.seekampf.models.Auction.java
net.avedo.seekampf.models.BaseModel.java
net.avedo.seekampf.models.Island.java
net.avedo.seekampf.models.Message.java
net.avedo.seekampf.models.Player.java
net.avedo.seekampf.utils.AuthGsonRequest.java
net.avedo.seekampf.utils.Constants.java
net.avedo.seekampf.utils.Interfaces.java
net.avedo.seekampf.utils.VolleyErrorHelper.java