Android Open Source - PrincePolo Main Activity






From Project

Back to project page PrincePolo.

License

The source code is released under:

GNU General Public License

If you think the Android project PrincePolo 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 se.agile.activities;
//from w w  w  . j a  v  a  2 s  .  c om
import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import se.agile.asynctasks.RequestAccessToken;
import se.agile.model.NotificationHandler;
import se.agile.model.Preferences;
import se.agile.model.TemporaryStorage;
import se.agile.navigator.NavDrawerItem;
import se.agile.navigator.NavDrawerListAdapter;
import se.agile.princepolo.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class MainActivity extends Activity {
  private DrawerLayout mDrawerLayout;
  private ListView mDrawerList;
  private ActionBarDrawerToggle mDrawerToggle;

  // drawer title
  private CharSequence mDrawerTitle;

  // app title
  private CharSequence mTitle;

  // slide menu items
  // private String[] navMenuTitles;
  // private TypedArray navMenuIcons;
  private ArrayList<NavDrawerItem> navDrawerItems;
  private NavDrawerListAdapter adapter;
  private static String logTag;
  private static MainActivity activity;

  private NotificationHandler notificationHandler;

  // For the GetCommitHistory
  ScheduledExecutorService scheduler = Executors
      .newSingleThreadScheduledExecutor();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Preferences.initializePreferences(this);
    TemporaryStorage.workingFiles = Preferences.getWorkingFiles();
    activity = this;
    logTag = getResources().getString(R.string.logtag_main);

    mTitle = mDrawerTitle = getTitle();
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
    navDrawerItems = new ArrayList<NavDrawerItem>();
    // nagivagion drawer items added to array
    for (VIEW view : VIEW.values()) {
      navDrawerItems.add(view.getNavDrawerItem());
    }
    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

    // setting the navigation drawer list adapter
    adapter = new NavDrawerListAdapter(getApplicationContext(),
        navDrawerItems);
    mDrawerList.setAdapter(adapter);

    // enabling action bar app icon and behaving it as toggle button
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
        R.drawable.ic_drawer, // nav menu toggle icon
        R.string.app_name, // nav drawer open - description for
                  // accessibility
        R.string.app_name // nav drawer close - description for
                  // accessibility
    ) {
      public void onDrawerClosed(View view) {
        getActionBar().setTitle(mTitle);
        // calling onPrepareOptionsMenu() to show action bar icons
        invalidateOptionsMenu();
      }

      public void onDrawerOpened(View drawerView) {
        getActionBar().setTitle(mDrawerTitle);
        // calling onPrepareOptionsMenu() to hide action bar icons
        invalidateOptionsMenu();
      }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {

      displayView(VIEW.REPOSITORY_OVERVIEW);
    }
    notificationHandler = new NotificationHandler(this);
    notificationHandler.start();

    // Opens up the menu from the left when the app is openeds
    // mDrawerLayout.openDrawer(Gravity.LEFT);

  }

  /**
   * Slide menu item click listener
   * */
  private class SlideMenuClickListener implements
      ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
      // display view for selected nav drawer item
      displayView(VIEW.getVIEW(position));
    }
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    return false;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // toggle nav drawer on selecting action bar app icon/title
    if (mDrawerToggle.onOptionsItemSelected(item)) {
      return true;
    }
    // Handle action bar actions click
    switch (item.getItemId()) {
    case R.id.action_settings:
      return true;
    default:
      return super.onOptionsItemSelected(item);
    }
  }

  /* *
   * Called when invalidateOptionsMenu() is triggered
   */
  @Override
  public boolean onPrepareOptionsMenu(Menu menu) {
    // if nav drawer is opened, hide the action items
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
  }

  /**
   * Here you can add a new view. (Don't forget to add the title and the icon
   * in /res/values/strings.xml) (And in the MainActivity.displayView(VIEW
   * view))
   * 
   * @author Jacob
   * 
   */
  public enum VIEW {
    // The titleIconArrayIndex is to make sure you get the icon and title
    // you specified when first creating the view.
    // The position is now easy to change.
    // But if you change the position you also have to change the order that
    // they are specified! position 0 -> Specified first (in this enum).
    REPOSITORY_OVERVIEW(0, false, "", 0), 
    NOTIFICATIONS(1, true, "0", 1), 
    PLANNING_POKER(2, false, "", 2), 
    PERSONAL_NOTES(3, false, "", 3), 
    BRANCHES(4, false, "", 4),
    COLLABORATORS(5, false, "", 5), 
    SELECT_REPOSITORY(6, false, "", 6),
    CONNECT_TO_GITHUB(7, false, "", 7), 
    SETTINGS(8, false, "", 8), 
    SELECT_WORKING_FILES(9, false,"", 9), 
    SIGNOUT(10, false, "", 10);

    private final int position, titleIconArrayIndex;
    private boolean isCounterVisible;
    private String count;

    private static final String[] navMenuTitles = activity.getResources()
        .getStringArray(R.array.nav_drawer_items);
    private static final TypedArray navMenuIcons = activity.getResources()
        .obtainTypedArray(R.array.nav_drawer_icons);

    private VIEW(final int position, boolean isCounterVisible,
        String count, int titleIconArrayIndex) {
      this.position = position;
      this.isCounterVisible = isCounterVisible;
      this.count = count;
      this.titleIconArrayIndex = titleIconArrayIndex;
    }

    public int getPosition() {
      return position;
    }

    public String getTitle() {
      return navMenuTitles[titleIconArrayIndex];
    }

    public int getIconValue() {
      return navMenuIcons.getResourceId(titleIconArrayIndex, -1);
    }

    public NavDrawerItem getNavDrawerItem() {
      if (isCounterVisible) {
        return new NavDrawerItem(navMenuTitles[titleIconArrayIndex],
            navMenuIcons.getResourceId(titleIconArrayIndex, -1),
            isCounterVisible, count);
      } else {
        return new NavDrawerItem(navMenuTitles[titleIconArrayIndex],
            navMenuIcons.getResourceId(titleIconArrayIndex, -1));
      }

    }

    public static VIEW getVIEW(int position) {
      for (VIEW view : values()) {
        if (view.getPosition() == position) {
          return view;
        }
      }
      return null;
    }

    public static VIEW getView(String name) {
      for (VIEW view : values()) {
        if (view.name().equals(name)) {
          return view;
        }
      }
      return null;
    }

    public String getName() {
      return this.name();
    }
  }

  // displays the fragment view for selected fragment
  protected void displayView(VIEW view) {
    Fragment fragment = null;
    switch (view) {
    case REPOSITORY_OVERVIEW:
      fragment = new RepositoryOverviewFragment();
      break;
    case NOTIFICATIONS:
      fragment = new NotificationFragmentSwitcher();
      break;
    case PLANNING_POKER:
      fragment = new PokerFragment();
      break;
    case PERSONAL_NOTES:
      fragment = new PersonalNotesFragment();
      break;
    case BRANCHES:
      fragment = new BranchesFragment();
      break;
    case COLLABORATORS:
      fragment = new CollaboratorsFragment();
      break;
    case SELECT_REPOSITORY:
      fragment = new SelectRepositoryFragment();
      break;
    case CONNECT_TO_GITHUB:
      fragment = new ConnectToGitHubFragment();
      break;
    case SETTINGS:
      fragment = new SettingsFragment();
      break;
    case SELECT_WORKING_FILES:
      fragment = new SelectWorkingFilesOverviewFragment();
      break;
    case SIGNOUT:
      fragment = new SignOutFragment();
      break;

    default:
      break;
    }

    if (fragment != null) {
      FragmentManager fragmentManager = getFragmentManager();
      fragmentManager.beginTransaction()
          .replace(R.id.frame_container, fragment).commit();

      // update selected item and title, then close the drawer
      mDrawerList.setItemChecked(view.getPosition(), true);
      mDrawerList.setSelection(view.getPosition());
      setTitle(view.getTitle());
      mDrawerLayout.closeDrawer(mDrawerList);
    } else {
      // error in creating fragment
      Log.e("MainActivity", "Error in creating fragment");
    }
  }

  @Override
  public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
  }

  /**
   * When using the ActionBarDrawerToggle, you must call it during
   * onPostCreate() and onConfigurationChanged()...
   */

  @Override
  protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
  }

  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggls
    mDrawerToggle.onConfigurationChanged(newConfig);
  }

  public static boolean isNetworkConnected() {
    if (activity != null) {
      ConnectivityManager cm = (ConnectivityManager) activity
          .getSystemService(Context.CONNECTIVITY_SERVICE);
      return (cm.getActiveNetworkInfo() != null);
    } else {
      Log.e(logTag,
          "isNetworkConnected: Couldn't check connection proberly");
      return false;
    }
  }

  public static void hasNoInternetConnection(Context context) {
    final Context finalContext = context;
    new AlertDialog.Builder(context)
        .setTitle("No Internet Access")
        .setMessage("Connect to Internet and then try again")
        .setPositiveButton("Retry",
            new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog,
                  int whichButton) {
                if (MainActivity.isNetworkConnected()) {
                  if (Preferences.getAccessToken().equals("")) {
                    Intent intent = new Intent(activity,
                        LoginActivity.class);
                    activity.startActivity(intent);
                    activity.overridePendingTransition(0, 0);
                    activity.displayView(VIEW.SETTINGS);
                  }
                } else {
                  hasNoInternetConnection(finalContext);
                }
              }
            })
        .setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog,
                  int whichButton) {
                activity.displayView(VIEW.CONNECT_TO_GITHUB);
              }
            }).show();
  }

  public static void hasNoSelectedRepository(Context context) {
    final Context finalContext = context;
    new AlertDialog.Builder(context).setTitle("No Selected Repository")
        .setMessage("Select a Repository")
        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton) {
            activity.displayView(VIEW.SELECT_REPOSITORY);
          }
        }).show();
  }

  /**
   * Check that we have an access token and a selected repository.
   */
  private void initialCheck() {
    Log.d(logTag, "initialCheck");
    if (!Preferences.isConnectedToGitHub()
        && !RequestAccessToken.isRequestingAccessToken()) {
      if (isNetworkConnected()) {
        Log.d(logTag, "start login");
        Intent intent = new Intent(MainActivity.this,
            LoginActivity.class);
        startActivity(intent);
        overridePendingTransition(0, 0);
      } else {
        hasNoInternetConnection(activity);
      }
    } else if (!Preferences.hasSelectedRepository()) {
      Log.d(logTag, "Select repo");
      displayView(VIEW.SELECT_REPOSITORY);
    }
  }

  @Override
  public void onResume() {
    initialCheck();
    super.onResume();
  }

  @Override
  public void onNewIntent(Intent intent) {
    if (intent.getAction().equals(VIEW.NOTIFICATIONS.getName())) {
      displayView(VIEW.NOTIFICATIONS);
    }
    super.onNewIntent(intent);
  }
  @Override
  public void onStop(){
    Preferences.setWorkingFiles(TemporaryStorage.workingFiles);
    super.onStop();
  }
}




Java Source Code List

se.agile.activities.AboutFragment.java
se.agile.activities.BranchFragment.java
se.agile.activities.BranchesFragment.java
se.agile.activities.CardFragment.java
se.agile.activities.CollaboratorsFragment.java
se.agile.activities.CommitFragment.java
se.agile.activities.ConflictFragment.java
se.agile.activities.ConnectToGitHubFragment.java
se.agile.activities.LoginActivity.java
se.agile.activities.MainActivity.java
se.agile.activities.NotificationFragmentSwitcher.java
se.agile.activities.NotificationsFragment.java
se.agile.activities.PersonalNotesFragment.java
se.agile.activities.PokerFragment.java
se.agile.activities.RepositoryOverviewFragment.java
se.agile.activities.SelectRepositoryFragment.java
se.agile.activities.SelectWorkingFilesActivity.java
se.agile.activities.SelectWorkingFilesFragment.java
se.agile.activities.SelectWorkingFilesOverviewFragment.java
se.agile.activities.SettingsFragment.java
se.agile.activities.SignOutFragment.java
se.agile.asynctasks.RequestAccessToken.java
se.agile.asynctasks.RequestAllCommitsForBranch.java
se.agile.asynctasks.RequestAllFilesForBranch.java
se.agile.asynctasks.RequestBranch.java
se.agile.asynctasks.RequestBranches.java
se.agile.asynctasks.RequestFiles.java
se.agile.asynctasks.RequestFullCommit.java
se.agile.asynctasks.RequestListenerAdapter.java
se.agile.asynctasks.RequestListener.java
se.agile.asynctasks.RequestRepositories.java
se.agile.asynctasks.RequestShortCommit.java
se.agile.asynctasks.RequestTask.java
se.agile.asynctasks.RequestUser.java
se.agile.controller.MainActivityController.java
se.agile.controller.settingsFragmentController.java
se.agile.githubdata.Branch.java
se.agile.githubdata.Commit.java
se.agile.githubdata.Directory.java
se.agile.githubdata.File.java
se.agile.githubdata.Folder.java
se.agile.githubdata.GitHubDataInterface.java
se.agile.githubdata.Repository.java
se.agile.githubdata.User.java
se.agile.model.BranchNotification.java
se.agile.model.BranchSelectionModel.java
se.agile.model.CommitNotification.java
se.agile.model.ConflictNotification.java
se.agile.model.CreateNotificationActivity.java
se.agile.model.DirectoryListArrayAdapter.java
se.agile.model.InteractiveArrayAdapter.java
se.agile.model.JSONParser.java
se.agile.model.NotificationDialog.java
se.agile.model.NotificationHandler.java
se.agile.model.NotificationListArrayAdapter.java
se.agile.model.NotificationListener.java
se.agile.model.Notification.java
se.agile.model.PreferenceListener.java
se.agile.model.Preferences.java
se.agile.model.TemporaryStorage.java
se.agile.model.Tuple.java
se.agile.model.WorkingFileListArrayAdapter.java
se.agile.navigator.NavDrawerItem.java
se.agile.navigator.NavDrawerListAdapter.java