diff options
author | Tor Norbye <tnorbye@google.com> | 2012-05-25 17:05:55 -0700 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2012-06-04 16:57:34 -0700 |
commit | 56afaff9c34a60c0e850324938d101a0509f8a38 (patch) | |
tree | 8a945bf07d4cb162d2c1b0c1d13989ccaa55da41 /templates/activities/BlankActivity/root/src/app_package | |
parent | 6de0364094c5780cf39f4805ac507374c382da2f (diff) | |
download | sdk-56afaff9c34a60c0e850324938d101a0509f8a38.zip sdk-56afaff9c34a60c0e850324938d101a0509f8a38.tar.gz sdk-56afaff9c34a60c0e850324938d101a0509f8a38.tar.bz2 |
Allow project templates to be bundled separately
This changeset moves the various FreeMarker templates out of the ADT
plugin and over into the tools install area. The code to instantiate
templates is simplified a bit now that it no longer needs to both
handle files on disk and files read out of the .jar.
There's a new first page to the various template wizards which is
shown if the tools are not up to date (specifically, if the
$SDK/tools/templates/ directory does not exist). This page explains
that the tools have to be updated, and the Next button is disabled
until they are up to date.
This CL also contains some other tweaks suggested by Roman to the
activity-to-layout name mapping and misc code changes.
Change-Id: I3bc65f54a6b79bbeedfb917a9d34ec0d312f3526
Diffstat (limited to 'templates/activities/BlankActivity/root/src/app_package')
4 files changed, 362 insertions, 0 deletions
diff --git a/templates/activities/BlankActivity/root/src/app_package/DropdownActivity.java.ftl b/templates/activities/BlankActivity/root/src/app_package/DropdownActivity.java.ftl new file mode 100644 index 0000000..98c1a8f --- /dev/null +++ b/templates/activities/BlankActivity/root/src/app_package/DropdownActivity.java.ftl @@ -0,0 +1,98 @@ +package ${packageName}; + +import android.app.ActionBar; +import android.app.FragmentTransaction; +import android.content.Context; +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; +import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TextView; + +public class ${activityClass} extends FragmentActivity implements ActionBar.OnNavigationListener { + + private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item"; + + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.${layoutName}); + + // Set up the action bar. + final ActionBar actionBar = getActionBar(); + actionBar.setDisplayShowTitleEnabled(false); + actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); + + // Set up the dropdown list navigation in the action bar. + actionBar.setListNavigationCallbacks( + // Specify a SpinnerAdapter to populate the dropdown list. + new ArrayAdapter( + actionBar.getThemedContext(), + android.R.layout.simple_list_item_1, + android.R.id.text1, + new String[]{ + getString(R.string.title_section1), + getString(R.string.title_section2), + getString(R.string.title_section3), + }), + this); + } + + @Override + public void onRestoreInstanceState(Bundle savedInstanceState) { + if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) { + getActionBar().setSelectedNavigationItem( + savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM)); + } + } + + @Override + public void onSaveInstanceState(Bundle outState) { + outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, + getActionBar().getSelectedNavigationIndex()); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.${menuName}, menu); + return true; + } + + @Override + public boolean onNavigationItemSelected(int position, long id) { + // When the given tab is selected, show the tab contents in the container + Fragment fragment = new DummySectionFragment(); + Bundle args = new Bundle(); + args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1); + fragment.setArguments(args); + getSupportFragmentManager().beginTransaction() + .replace(R.id.container, fragment) + .commit(); + return true; + } + + /** + * A dummy fragment representing a section of the app, but that simply displays dummy text. + */ + public static class DummySectionFragment extends Fragment { + public DummySectionFragment() { + } + + public static final String ARG_SECTION_NUMBER = "section_number"; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + TextView textView = new TextView(getActivity()); + textView.setGravity(Gravity.CENTER); + Bundle args = getArguments(); + textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER))); + return textView; + } + } +} diff --git a/templates/activities/BlankActivity/root/src/app_package/SimpleActivity.java.ftl b/templates/activities/BlankActivity/root/src/app_package/SimpleActivity.java.ftl new file mode 100644 index 0000000..1ebc0fa --- /dev/null +++ b/templates/activities/BlankActivity/root/src/app_package/SimpleActivity.java.ftl @@ -0,0 +1,19 @@ +package ${packageName}; + +import android.os.Bundle; +import android.app.Activity; +import android.view.Menu; + +public class ${activityClass} extends Activity { + + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.${layoutName}); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.${menuName}, menu); + return true; + } +} diff --git a/templates/activities/BlankActivity/root/src/app_package/TabsActivity.java.ftl b/templates/activities/BlankActivity/root/src/app_package/TabsActivity.java.ftl new file mode 100644 index 0000000..ab11a7f --- /dev/null +++ b/templates/activities/BlankActivity/root/src/app_package/TabsActivity.java.ftl @@ -0,0 +1,94 @@ +package ${packageName}; + +import android.app.ActionBar; +import android.app.FragmentTransaction; +import android.content.Context; +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; +import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +public class ${activityClass} extends FragmentActivity implements ActionBar.TabListener { + + private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item"; + + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.${layoutName}); + + // Set up the action bar. + final ActionBar actionBar = getActionBar(); + actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); + + // For each of the sections in the app, add a tab to the action bar. + actionBar.addTab(actionBar.newTab().setText(R.string.title_section1).setTabListener(this)); + actionBar.addTab(actionBar.newTab().setText(R.string.title_section2).setTabListener(this)); + actionBar.addTab(actionBar.newTab().setText(R.string.title_section3).setTabListener(this)); + } + + @Override + public void onRestoreInstanceState(Bundle savedInstanceState) { + if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) { + getActionBar().setSelectedNavigationItem( + savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM)); + } + } + + @Override + public void onSaveInstanceState(Bundle outState) { + outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, + getActionBar().getSelectedNavigationIndex()); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.${menuName}, menu); + return true; + } + + @Override + public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { + } + + @Override + public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { + // When the given tab is selected, show the tab contents in the container + Fragment fragment = new DummySectionFragment(); + Bundle args = new Bundle(); + args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, tab.getPosition() + 1); + fragment.setArguments(args); + getSupportFragmentManager().beginTransaction() + .replace(R.id.container, fragment) + .commit(); + } + + @Override + public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { + } + + /** + * A dummy fragment representing a section of the app, but that simply displays dummy text. + */ + public static class DummySectionFragment extends Fragment { + public DummySectionFragment() { + } + + public static final String ARG_SECTION_NUMBER = "section_number"; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + TextView textView = new TextView(getActivity()); + textView.setGravity(Gravity.CENTER); + Bundle args = getArguments(); + textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER))); + return textView; + } + } +} diff --git a/templates/activities/BlankActivity/root/src/app_package/TabsAndPagerActivity.java.ftl b/templates/activities/BlankActivity/root/src/app_package/TabsAndPagerActivity.java.ftl new file mode 100644 index 0000000..eb47519 --- /dev/null +++ b/templates/activities/BlankActivity/root/src/app_package/TabsAndPagerActivity.java.ftl @@ -0,0 +1,151 @@ +package ${packageName}; + +import android.app.ActionBar; +import android.app.FragmentTransaction; +import android.content.Context; +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentPagerAdapter; +import android.support.v4.view.ViewPager; +import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +public class ${activityClass} extends FragmentActivity<#if navType?contains("tabs")> implements ActionBar.TabListener</#if> { + + /** + * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the + * sections. We use a {@link android.support.v4.app.FragmentPagerAdapter} derivative, which will + * keep every loaded fragment in memory. If this becomes too memory intensive, it may be best + * to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}. + */ + SectionsPagerAdapter mSectionsPagerAdapter; + + /** + * The {@link ViewPager} that will host the section contents. + */ + ViewPager mViewPager; + + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.${layoutName}); + + // Create the adapter that will return a fragment for each of the three primary sections + // of the app. + mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); + + <#if navType?contains("tabs")> + // Set up the action bar. + final ActionBar actionBar = getActionBar(); + actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); + </#if> + + // Set up the ViewPager with the sections adapter. + mViewPager = (ViewPager) findViewById(R.id.pager); + mViewPager.setAdapter(mSectionsPagerAdapter); + + <#if navType?contains("tabs")> + // When swiping between different sections, select the corresponding tab. + // We can also use ActionBar.Tab#select() to do this if we have a reference to the + // Tab. + mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { + @Override + public void onPageSelected(int position) { + actionBar.setSelectedNavigationItem(position); + } + }); + + // For each of the sections in the app, add a tab to the action bar. + for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { + // Create a tab with text corresponding to the page title defined by the adapter. + // Also specify this Activity object, which implements the TabListener interface, as the + // listener for when this tab is selected. + actionBar.addTab( + actionBar.newTab() + .setText(mSectionsPagerAdapter.getPageTitle(i)) + .setTabListener(this)); + } + </#if> + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.${menuName}, menu); + return true; + } + <#if navType?contains("tabs")> + @Override + public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { + } + + @Override + public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { + // When the given tab is selected, switch to the corresponding page in the ViewPager. + mViewPager.setCurrentItem(tab.getPosition()); + } + + @Override + public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { + } + </#if> + + /** + * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary + * sections of the app. + */ + public class SectionsPagerAdapter extends FragmentPagerAdapter { + + public SectionsPagerAdapter(FragmentManager fm) { + super(fm); + } + + @Override + public Fragment getItem(int i) { + Fragment fragment = new DummySectionFragment(); + Bundle args = new Bundle(); + args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1); + fragment.setArguments(args); + return fragment; + } + + @Override + public int getCount() { + return 3; + } + + @Override + public CharSequence getPageTitle(int position) { + switch (position) { + case 0: return getString(R.string.title_section1).toUpperCase(); + case 1: return getString(R.string.title_section2).toUpperCase(); + case 2: return getString(R.string.title_section3).toUpperCase(); + } + return null; + } + } + + /** + * A dummy fragment representing a section of the app, but that simply displays dummy text. + */ + public static class DummySectionFragment extends Fragment { + public DummySectionFragment() { + } + + public static final String ARG_SECTION_NUMBER = "section_number"; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + TextView textView = new TextView(getActivity()); + textView.setGravity(Gravity.CENTER); + Bundle args = getArguments(); + textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER))); + return textView; + } + } +} |