Android Open Source - utexas-utilities Tablet Delegate






From Project

Back to project page utexas-utilities.

License

The source code is released under:

Apache License

If you think the Android project utexas-utilities 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 com.mapsaurus.paneslayout;
/*from   w  ww .  ja  v  a  2  s.c om*/
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.ViewGroup;

import com.actionbarsherlock.view.MenuItem;
import com.mapsaurus.paneslayout.PanesLayout.PaneView;
import com.mapsaurus.paneslayout.PanesSizer.PaneSizer;

import java.util.ArrayList;

public class TabletDelegate extends ActivityDelegate implements PanesLayout.OnIndexChangedListener  {

  private PaneSizer mPaneSizer;

  public void setPaneSizer(PaneSizer sizer) {
    panesLayout.setPaneSizer(sizer);
    mPaneSizer = sizer;
  }

  /**
   * This provides all the logic for displaying and moving panes.
   */
  protected PanesLayout panesLayout;

  public TabletDelegate(PanesActivity a) {
    super(a);
  }

  /**
   * Enable the action-bar home button if appropriate
   */
  @Override
  public void onIndexChanged(int firstIndex, int lastIndex,
      int firstCompleteIndex, int lastCompleteIndex) {
    if (firstCompleteIndex == 0)
      getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    else getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  }

  /**
   * On action-bar home button selected, slide to the menu
   */
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
      panesLayout.setIndex(0);
      return true;
    }
    return false;
  }

  /**
   * Handle back press
   */
  @Override
  public boolean onBackPressed() {
    if (panesLayout.onBackPressed()) return true;
    return false;
  }

  /**
   * Save the state of the panes
   */
  @Override
  public void onSaveInstanceState(Bundle savedInstanceState) {
    int[] panesType = new int[panesLayout.getNumPanes()];
    boolean[] panesFocused = new boolean[panesLayout.getNumPanes()];
    for (int i = 0; i < panesLayout.getNumPanes(); i ++) {
      PaneView p = panesLayout.getPane(i);
      panesType[i] = p.type;
      panesFocused[i] = p.focused;
    }

    savedInstanceState.putIntArray("PanesLayout_panesType", panesType);
    savedInstanceState.putBooleanArray("PanesLayout_panesFocused", panesFocused);
    savedInstanceState.putInt("PanesLayout_currentIndex", panesLayout.getCurrentIndex());
  }

  @Override
  public void onDestroy() {
    panesLayout.setOnIndexChangedListener(null);
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    if (findViewById(R.id.content_frame) == null) {
      setContentView(R.layout.panes_layout);
    } else {
      View.inflate(getActivity(), R.layout.panes_layout,
          (ViewGroup) findViewById(R.id.content_frame));
    }

    panesLayout = (PanesLayout) findViewById(R.id.panes);
    panesLayout.setOnIndexChangedListener(this);

    if (savedInstanceState != null) {
      int[] panesType = savedInstanceState.getIntArray("PanesLayout_panesType");
      boolean[] panesFocused = savedInstanceState.getBooleanArray("PanesLayout_panesFocused");
      int currentIndex = savedInstanceState.getInt("PanesLayout_currentIndex");
      for (int i = 0; i < panesType.length; i ++) {
        panesLayout.addPane(panesType[i], panesFocused[i]);
      }
      panesLayout.setIndex(currentIndex);
    }

    if (savedInstanceState != null) {
      FragmentManager fm = getSupportFragmentManager();

      for (int index = 0; index < panesLayout.getNumPanes(); index ++) {
        int id = panesLayout.getPane(index).getInnerId();
        Fragment f = fm.findFragmentById(id);

        fragmentStack.add(f);
      }
    }
  }

  /* *********************************************************************
   * Methods for dealing with adding/removing fragments.
   * ********************************************************************* */

  /**
   * Save the fragment stack manually. The reason to do this is because sometimes when
   * we need to retrieve a fragment, that fragment has not yet been added.
   */
  private ArrayList<Fragment> fragmentStack = new ArrayList<Fragment>();

  private void clearFragments(int index) {
    while (index < fragmentStack.size())
      fragmentStack.remove(index);

    ArrayList<PaneView> panes = panesLayout.removePanes(index);
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    for (PaneView pane : panes) {
      int id = pane.getInnerId();
      Fragment f = fm.findFragmentById(id);
      if (f != null)
        ft.remove(f);
    }

    ft.commit();
  }

  private void addFragment(Fragment f, int index) {
    if (f == null) return;
    
    clearFragments(index + 1);

    int type = 0;
    boolean focused = false;

    PaneSizer paneSizer = mPaneSizer;
    if (paneSizer != null) {
      type = paneSizer.getType(f);
      focused = paneSizer.getFocused(f);
    }

    PaneView p = panesLayout.getPane(index);
    if (p != null && p.type == type && p.focused == focused) {
    } else {
      clearFragments(index);
      p = panesLayout.addPane(type, focused);
      if (p.index != index)
        throw new IllegalStateException("Added pane has wrong index");
    }

    panesLayout.setIndex(index);

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.replace(p.getInnerId(), f);
    ft.commit();

    fragmentStack.add(index, f);
  }

  @Override
  public void addFragment(Fragment oldFragment, Fragment newFragment) {
    if (newFragment != null) {
      int index = 1; // by default, add after menu

      int oldIndex = getIndex(oldFragment);
      if (oldIndex != -1) index = oldIndex + 1;

      addFragment(newFragment, index);
    } else {
      panesLayout.setIndex(1);
    }
  }

  @Override
  public void clearFragments() {
    clearFragments(0);
  }

  @Override
  public void setMenuFragment(Fragment menuFragment) {
    addFragment(menuFragment, 0);
  }

  private int getIndex(Fragment f) {
    for (int i = 0; i < fragmentStack.size(); i ++) {
      if (fragmentStack.get(i) == f)
        return i;
    }
    return -1;
  }

  public Fragment getFragment(int index) {
    if (index < fragmentStack.size() && index >= 0)
      return fragmentStack.get(index);

    // PaneView pane = panesLayout.getPane(index);
    // FragmentManager fm = getSupportFragmentManager();
    // Fragment f = fm.findFragmentById(pane.getInnerId());

    return null;
  }

  @Override
  public Fragment getMenuFragment() {
    return getFragment(0);
  }

  @Override
  public Fragment getTopFragment() {
    return getFragment(panesLayout.getNumPanes() - 1);
  }

  @Override
  public void showMenu() {
    panesLayout.setIndex(0);
  }

  @Override
  public void showContent() {
    if(panesLayout.getNumPanes() > 1)
      panesLayout.setIndex(1);
    else
      throw new IllegalStateException("You can't call showContent()" +
          " when there's no content to show.");
  }
}




Java Source Code List

com.google.maps.android.ui.MyIconGenerator.java
com.mapsaurus.paneslayout.ActivityDelegate.java
com.mapsaurus.paneslayout.FragmentLauncher.java
com.mapsaurus.paneslayout.MenuItemWrapper.java
com.mapsaurus.paneslayout.MySlidingPaneLayout.java
com.mapsaurus.paneslayout.PanesActivity.java
com.mapsaurus.paneslayout.PanesLayout.java
com.mapsaurus.paneslayout.PanesSizer.java
com.mapsaurus.paneslayout.PhoneDelegate.java
com.mapsaurus.paneslayout.SimpleScrollView.java
com.mapsaurus.paneslayout.TabletDelegate.java
com.nasageek.utexasutilities.AsyncTask.java
com.nasageek.utexasutilities.AttachmentDownloadService.java
com.nasageek.utexasutilities.AuthCookie.java
com.nasageek.utexasutilities.BlackboardDashboardXmlParser.java
com.nasageek.utexasutilities.BuildingDatabase.java
com.nasageek.utexasutilities.BuildingProvider.java
com.nasageek.utexasutilities.BuildingSaxHandler.java
com.nasageek.utexasutilities.ChangeLog.java
com.nasageek.utexasutilities.ChangeableContextTask.java
com.nasageek.utexasutilities.CourseMapSaxHandler.java
com.nasageek.utexasutilities.FixedListPreference.java
com.nasageek.utexasutilities.LoginWebViewClient.java
com.nasageek.utexasutilities.MarkerManager.java
com.nasageek.utexasutilities.MyPair.java
com.nasageek.utexasutilities.MyScrollView.java
com.nasageek.utexasutilities.PlacemarkSaxHandler.java
com.nasageek.utexasutilities.PnaAuthCookie.java
com.nasageek.utexasutilities.RouteSaxHandler.java
com.nasageek.utexasutilities.SecurePreferences.java
com.nasageek.utexasutilities.TempLoginException.java
com.nasageek.utexasutilities.UTilitiesApplication.java
com.nasageek.utexasutilities.UtdAuthCookie.java
com.nasageek.utexasutilities.Utility.java
com.nasageek.utexasutilities.WrappedViewPager.java
com.nasageek.utexasutilities.WrappingSlidingDrawer.java
com.nasageek.utexasutilities.activities.AboutMeActivity.java
com.nasageek.utexasutilities.activities.BalanceActivity.java
com.nasageek.utexasutilities.activities.BlackboardPanesActivity.java
com.nasageek.utexasutilities.activities.CampusMapActivity.java
com.nasageek.utexasutilities.activities.DataUsageActivity.java
com.nasageek.utexasutilities.activities.LoginActivity.java
com.nasageek.utexasutilities.activities.MenuActivity.java
com.nasageek.utexasutilities.activities.NutritionInfoActivity.java
com.nasageek.utexasutilities.activities.Preferences.java
com.nasageek.utexasutilities.activities.ScheduleActivity.java
com.nasageek.utexasutilities.activities.UTilitiesActivity.java
com.nasageek.utexasutilities.adapters.BBClassAdapter.java
com.nasageek.utexasutilities.adapters.CourseMapAdapter.java
com.nasageek.utexasutilities.adapters.MultiPanePagerAdapter.java
com.nasageek.utexasutilities.adapters.MyFragmentPagerAdapter.java
com.nasageek.utexasutilities.adapters.ScheduleClassAdapter.java
com.nasageek.utexasutilities.adapters.TransactionAdapter.java
com.nasageek.utexasutilities.fragments.ActionModeFragment.java
com.nasageek.utexasutilities.fragments.BlackboardAnnouncementsFragment.java
com.nasageek.utexasutilities.fragments.BlackboardCourseListFragment.java
com.nasageek.utexasutilities.fragments.BlackboardCourseMapFragment.java
com.nasageek.utexasutilities.fragments.BlackboardDashboardFragment.java
com.nasageek.utexasutilities.fragments.BlackboardDownloadableItemFragment.java
com.nasageek.utexasutilities.fragments.BlackboardExternalItemFragment.java
com.nasageek.utexasutilities.fragments.BlackboardFragment.java
com.nasageek.utexasutilities.fragments.BlackboardGradesFragment.java
com.nasageek.utexasutilities.fragments.BlackboardPagerFragment.java
com.nasageek.utexasutilities.fragments.CourseScheduleFragment.java
com.nasageek.utexasutilities.fragments.DatePickerFragment.java
com.nasageek.utexasutilities.fragments.DoubleDatePickerDialogFragment.java
com.nasageek.utexasutilities.fragments.ExamScheduleFragment.java
com.nasageek.utexasutilities.fragments.MenuFragment.java
com.nasageek.utexasutilities.fragments.NutritionInfoFragment.java
com.nasageek.utexasutilities.fragments.PickCalendarDialogFragment.java
com.nasageek.utexasutilities.fragments.TransactionsFragment.java
com.nasageek.utexasutilities.model.BBClass.java
com.nasageek.utexasutilities.model.Building.java
com.nasageek.utexasutilities.model.Classtime.java
com.nasageek.utexasutilities.model.CourseMapItem.java
com.nasageek.utexasutilities.model.FeedItem.java
com.nasageek.utexasutilities.model.Placemark.java
com.nasageek.utexasutilities.model.Transaction.java
com.nasageek.utexasutilities.model.UTClass.java
com.viewpagerindicator.MyTabPageIndicator.java