Extract support lib samples out of ApiDemos and in to their own app.
Change-Id: I2ca344e5f1b44c4825de938c9a4f9375a523ede5
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/Shakespeare.java b/samples/Support4Demos/src/com/example/android/supportv4/Shakespeare.java
new file mode 100644
index 0000000..17608d4
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/Shakespeare.java
@@ -0,0 +1,223 @@
+package com.example.android.supportv4;
+
+public final class Shakespeare {
+ /**
+ * Our data, part 1.
+ */
+ public static final String[] TITLES =
+ {
+ "Henry IV (1)",
+ "Henry V",
+ "Henry VIII",
+ "Richard II",
+ "Richard III",
+ "Merchant of Venice",
+ "Othello",
+ "King Lear"
+ };
+
+ /**
+ * Our data, part 2.
+ */
+ public static final String[] DIALOGUE =
+ {
+ "So shaken as we are, so wan with care," +
+ "Find we a time for frighted peace to pant," +
+ "And breathe short-winded accents of new broils" +
+ "To be commenced in strands afar remote." +
+ "No more the thirsty entrance of this soil" +
+ "Shall daub her lips with her own children's blood;" +
+ "Nor more shall trenching war channel her fields," +
+ "Nor bruise her flowerets with the armed hoofs" +
+ "Of hostile paces: those opposed eyes," +
+ "Which, like the meteors of a troubled heaven," +
+ "All of one nature, of one substance bred," +
+ "Did lately meet in the intestine shock" +
+ "And furious close of civil butchery" +
+ "Shall now, in mutual well-beseeming ranks," +
+ "March all one way and be no more opposed" +
+ "Against acquaintance, kindred and allies:" +
+ "The edge of war, like an ill-sheathed knife," +
+ "No more shall cut his master. Therefore, friends," +
+ "As far as to the sepulchre of Christ," +
+ "Whose soldier now, under whose blessed cross" +
+ "We are impressed and engaged to fight," +
+ "Forthwith a power of English shall we levy;" +
+ "Whose arms were moulded in their mothers' womb" +
+ "To chase these pagans in those holy fields" +
+ "Over whose acres walk'd those blessed feet" +
+ "Which fourteen hundred years ago were nail'd" +
+ "For our advantage on the bitter cross." +
+ "But this our purpose now is twelve month old," +
+ "And bootless 'tis to tell you we will go:" +
+ "Therefore we meet not now. Then let me hear" +
+ "Of you, my gentle cousin Westmoreland," +
+ "What yesternight our council did decree" +
+ "In forwarding this dear expedience.",
+
+ "Hear him but reason in divinity," +
+ "And all-admiring with an inward wish" +
+ "You would desire the king were made a prelate:" +
+ "Hear him debate of commonwealth affairs," +
+ "You would say it hath been all in all his study:" +
+ "List his discourse of war, and you shall hear" +
+ "A fearful battle render'd you in music:" +
+ "Turn him to any cause of policy," +
+ "The Gordian knot of it he will unloose," +
+ "Familiar as his garter: that, when he speaks," +
+ "The air, a charter'd libertine, is still," +
+ "And the mute wonder lurketh in men's ears," +
+ "To steal his sweet and honey'd sentences;" +
+ "So that the art and practic part of life" +
+ "Must be the mistress to this theoric:" +
+ "Which is a wonder how his grace should glean it," +
+ "Since his addiction was to courses vain," +
+ "His companies unletter'd, rude and shallow," +
+ "His hours fill'd up with riots, banquets, sports," +
+ "And never noted in him any study," +
+ "Any retirement, any sequestration" +
+ "From open haunts and popularity.",
+
+ "I come no more to make you laugh: things now," +
+ "That bear a weighty and a serious brow," +
+ "Sad, high, and working, full of state and woe," +
+ "Such noble scenes as draw the eye to flow," +
+ "We now present. Those that can pity, here" +
+ "May, if they think it well, let fall a tear;" +
+ "The subject will deserve it. Such as give" +
+ "Their money out of hope they may believe," +
+ "May here find truth too. Those that come to see" +
+ "Only a show or two, and so agree" +
+ "The play may pass, if they be still and willing," +
+ "I'll undertake may see away their shilling" +
+ "Richly in two short hours. Only they" +
+ "That come to hear a merry bawdy play," +
+ "A noise of targets, or to see a fellow" +
+ "In a long motley coat guarded with yellow," +
+ "Will be deceived; for, gentle hearers, know," +
+ "To rank our chosen truth with such a show" +
+ "As fool and fight is, beside forfeiting" +
+ "Our own brains, and the opinion that we bring," +
+ "To make that only true we now intend," +
+ "Will leave us never an understanding friend." +
+ "Therefore, for goodness' sake, and as you are known" +
+ "The first and happiest hearers of the town," +
+ "Be sad, as we would make ye: think ye see" +
+ "The very persons of our noble story" +
+ "As they were living; think you see them great," +
+ "And follow'd with the general throng and sweat" +
+ "Of thousand friends; then in a moment, see" +
+ "How soon this mightiness meets misery:" +
+ "And, if you can be merry then, I'll say" +
+ "A man may weep upon his wedding-day.",
+
+ "First, heaven be the record to my speech!" +
+ "In the devotion of a subject's love," +
+ "Tendering the precious safety of my prince," +
+ "And free from other misbegotten hate," +
+ "Come I appellant to this princely presence." +
+ "Now, Thomas Mowbray, do I turn to thee," +
+ "And mark my greeting well; for what I speak" +
+ "My body shall make good upon this earth," +
+ "Or my divine soul answer it in heaven." +
+ "Thou art a traitor and a miscreant," +
+ "Too good to be so and too bad to live," +
+ "Since the more fair and crystal is the sky," +
+ "The uglier seem the clouds that in it fly." +
+ "Once more, the more to aggravate the note," +
+ "With a foul traitor's name stuff I thy throat;" +
+ "And wish, so please my sovereign, ere I move," +
+ "What my tongue speaks my right drawn sword may prove.",
+
+ "Now is the winter of our discontent" +
+ "Made glorious summer by this sun of York;" +
+ "And all the clouds that lour'd upon our house" +
+ "In the deep bosom of the ocean buried." +
+ "Now are our brows bound with victorious wreaths;" +
+ "Our bruised arms hung up for monuments;" +
+ "Our stern alarums changed to merry meetings," +
+ "Our dreadful marches to delightful measures." +
+ "Grim-visaged war hath smooth'd his wrinkled front;" +
+ "And now, instead of mounting barded steeds" +
+ "To fright the souls of fearful adversaries," +
+ "He capers nimbly in a lady's chamber" +
+ "To the lascivious pleasing of a lute." +
+ "But I, that am not shaped for sportive tricks," +
+ "Nor made to court an amorous looking-glass;" +
+ "I, that am rudely stamp'd, and want love's majesty" +
+ "To strut before a wanton ambling nymph;" +
+ "I, that am curtail'd of this fair proportion," +
+ "Cheated of feature by dissembling nature," +
+ "Deformed, unfinish'd, sent before my time" +
+ "Into this breathing world, scarce half made up," +
+ "And that so lamely and unfashionable" +
+ "That dogs bark at me as I halt by them;" +
+ "Why, I, in this weak piping time of peace," +
+ "Have no delight to pass away the time," +
+ "Unless to spy my shadow in the sun" +
+ "And descant on mine own deformity:" +
+ "And therefore, since I cannot prove a lover," +
+ "To entertain these fair well-spoken days," +
+ "I am determined to prove a villain" +
+ "And hate the idle pleasures of these days." +
+ "Plots have I laid, inductions dangerous," +
+ "By drunken prophecies, libels and dreams," +
+ "To set my brother Clarence and the king" +
+ "In deadly hate the one against the other:" +
+ "And if King Edward be as true and just" +
+ "As I am subtle, false and treacherous," +
+ "This day should Clarence closely be mew'd up," +
+ "About a prophecy, which says that 'G'" +
+ "Of Edward's heirs the murderer shall be." +
+ "Dive, thoughts, down to my soul: here" +
+ "Clarence comes.",
+
+ "To bait fish withal: if it will feed nothing else," +
+ "it will feed my revenge. He hath disgraced me, and" +
+ "hindered me half a million; laughed at my losses," +
+ "mocked at my gains, scorned my nation, thwarted my" +
+ "bargains, cooled my friends, heated mine" +
+ "enemies; and what's his reason? I am a Jew. Hath" +
+ "not a Jew eyes? hath not a Jew hands, organs," +
+ "dimensions, senses, affections, passions? fed with" +
+ "the same food, hurt with the same weapons, subject" +
+ "to the same diseases, healed by the same means," +
+ "warmed and cooled by the same winter and summer, as" +
+ "a Christian is? If you prick us, do we not bleed?" +
+ "if you tickle us, do we not laugh? if you poison" +
+ "us, do we not die? and if you wrong us, shall we not" +
+ "revenge? If we are like you in the rest, we will" +
+ "resemble you in that. If a Jew wrong a Christian," +
+ "what is his humility? Revenge. If a Christian" +
+ "wrong a Jew, what should his sufferance be by" +
+ "Christian example? Why, revenge. The villany you" +
+ "teach me, I will execute, and it shall go hard but I" +
+ "will better the instruction.",
+
+ "Virtue! a fig! 'tis in ourselves that we are thus" +
+ "or thus. Our bodies are our gardens, to the which" +
+ "our wills are gardeners: so that if we will plant" +
+ "nettles, or sow lettuce, set hyssop and weed up" +
+ "thyme, supply it with one gender of herbs, or" +
+ "distract it with many, either to have it sterile" +
+ "with idleness, or manured with industry, why, the" +
+ "power and corrigible authority of this lies in our" +
+ "wills. If the balance of our lives had not one" +
+ "scale of reason to poise another of sensuality, the" +
+ "blood and baseness of our natures would conduct us" +
+ "to most preposterous conclusions: but we have" +
+ "reason to cool our raging motions, our carnal" +
+ "stings, our unbitted lusts, whereof I take this that" +
+ "you call love to be a sect or scion.",
+
+ "Blow, winds, and crack your cheeks! rage! blow!" +
+ "You cataracts and hurricanoes, spout" +
+ "Till you have drench'd our steeples, drown'd the cocks!" +
+ "You sulphurous and thought-executing fires," +
+ "Vaunt-couriers to oak-cleaving thunderbolts," +
+ "Singe my white head! And thou, all-shaking thunder," +
+ "Smite flat the thick rotundity o' the world!" +
+ "Crack nature's moulds, an germens spill at once," +
+ "That make ingrateful man!"
+ };
+}
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/Support4Demos.java b/samples/Support4Demos/src/com/example/android/supportv4/Support4Demos.java
new file mode 100644
index 0000000..af445db
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/Support4Demos.java
@@ -0,0 +1,149 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.supportv4;
+
+import android.app.ListActivity;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.os.Bundle;
+import android.view.View;
+import android.widget.ListView;
+import android.widget.SimpleAdapter;
+
+import java.text.Collator;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class Support4Demos extends ListActivity {
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ Intent intent = getIntent();
+ String path = intent.getStringExtra("com.example.android.apis.Path");
+
+ if (path == null) {
+ path = "";
+ }
+
+ setListAdapter(new SimpleAdapter(this, getData(path),
+ android.R.layout.simple_list_item_1, new String[] { "title" },
+ new int[] { android.R.id.text1 }));
+ getListView().setTextFilterEnabled(true);
+ }
+
+ protected List<Map<String, Object>> getData(String prefix) {
+ List<Map<String, Object>> myData = new ArrayList<Map<String, Object>>();
+
+ Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
+ mainIntent.addCategory("com.example.android.supportv4.SUPPORT4_SAMPLE_CODE");
+
+ PackageManager pm = getPackageManager();
+ List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
+
+ if (null == list)
+ return myData;
+
+ String[] prefixPath;
+ String prefixWithSlash = prefix;
+
+ if (prefix.equals("")) {
+ prefixPath = null;
+ } else {
+ prefixPath = prefix.split("/");
+ prefixWithSlash = prefix + "/";
+ }
+
+ int len = list.size();
+
+ Map<String, Boolean> entries = new HashMap<String, Boolean>();
+
+ for (int i = 0; i < len; i++) {
+ ResolveInfo info = list.get(i);
+ CharSequence labelSeq = info.loadLabel(pm);
+ String label = labelSeq != null
+ ? labelSeq.toString()
+ : info.activityInfo.name;
+
+ if (prefixWithSlash.length() == 0 || label.startsWith(prefixWithSlash)) {
+
+ String[] labelPath = label.split("/");
+
+ String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length];
+
+ if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) {
+ addItem(myData, nextLabel, activityIntent(
+ info.activityInfo.applicationInfo.packageName,
+ info.activityInfo.name));
+ } else {
+ if (entries.get(nextLabel) == null) {
+ addItem(myData, nextLabel, browseIntent(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel));
+ entries.put(nextLabel, true);
+ }
+ }
+ }
+ }
+
+ Collections.sort(myData, sDisplayNameComparator);
+
+ return myData;
+ }
+
+ private final static Comparator<Map<String, Object>> sDisplayNameComparator =
+ new Comparator<Map<String, Object>>() {
+ private final Collator collator = Collator.getInstance();
+
+ public int compare(Map<String, Object> map1, Map<String, Object> map2) {
+ return collator.compare(map1.get("title"), map2.get("title"));
+ }
+ };
+
+ protected Intent activityIntent(String pkg, String componentName) {
+ Intent result = new Intent();
+ result.setClassName(pkg, componentName);
+ return result;
+ }
+
+ protected Intent browseIntent(String path) {
+ Intent result = new Intent();
+ result.setClass(this, Support4Demos.class);
+ result.putExtra("com.example.android.apis.Path", path);
+ return result;
+ }
+
+ protected void addItem(List<Map<String, Object>> data, String name, Intent intent) {
+ Map<String, Object> temp = new HashMap<String, Object>();
+ temp.put("title", name);
+ temp.put("intent", intent);
+ data.add(temp);
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ protected void onListItemClick(ListView l, View v, int position, long id) {
+ Map<String, Object> map = (Map<String, Object>)l.getItemAtPosition(position);
+
+ Intent intent = (Intent) map.get("intent");
+ startActivity(intent);
+ }
+}
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/_package.html b/samples/Support4Demos/src/com/example/android/supportv4/_package.html
new file mode 100644
index 0000000..4657c4c
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/_package.html
@@ -0,0 +1,24 @@
+<html>
+<head>
+<link rel="stylesheet" type="text/css" href="assets/style.css" />
+<script type="text/javascript" src="http://www.corp.google.com/style/prettify.js"></script>
+<script src="http://www.corp.google.com/eng/techpubs/include/navbar.js" type="text/javascript"></script>
+
+
+
+</head>
+
+<body>
+
+<p>
+Examples of how to use support library APIs. See:
+
+<ol>
+ <li> <a href="com.example.android.supportv4.app">sdk.app</a> for examples
+ of using the application package support APIs.
+</ol>
+</p>
+
+
+</body>
+</html>
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentAlertDialogSupport.java b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentAlertDialogSupport.java
new file mode 100644
index 0000000..f35e021
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentAlertDialogSupport.java
@@ -0,0 +1,110 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.supportv4.app;
+
+import com.example.android.supportv4.R;
+
+import android.support.v4.app.DialogFragment;
+import android.support.v4.app.FragmentActivity;
+
+import android.app.AlertDialog;
+import android.app.Dialog;
+import android.content.DialogInterface;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.widget.Button;
+import android.widget.TextView;
+
+/**
+ * Demonstrates how to show an AlertDialog that is managed by a Fragment.
+ */
+public class FragmentAlertDialogSupport extends FragmentActivity {
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.fragment_dialog);
+
+ View tv = findViewById(R.id.text);
+ ((TextView)tv).setText("Example of displaying an alert dialog with a DialogFragment");
+
+ // Watch for button clicks.
+ Button button = (Button)findViewById(R.id.show);
+ button.setOnClickListener(new OnClickListener() {
+ public void onClick(View v) {
+ showDialog();
+ }
+ });
+ }
+
+//BEGIN_INCLUDE(activity)
+ void showDialog() {
+ DialogFragment newFragment = MyAlertDialogFragment.newInstance(
+ R.string.alert_dialog_two_buttons_title);
+ newFragment.show(getSupportFragmentManager(), "dialog");
+ }
+
+ public void doPositiveClick() {
+ // Do stuff here.
+ Log.i("FragmentAlertDialog", "Positive click!");
+ }
+
+ public void doNegativeClick() {
+ // Do stuff here.
+ Log.i("FragmentAlertDialog", "Negative click!");
+ }
+//END_INCLUDE(activity)
+
+//BEGIN_INCLUDE(dialog)
+ public static class MyAlertDialogFragment extends DialogFragment {
+
+ public static MyAlertDialogFragment newInstance(int title) {
+ MyAlertDialogFragment frag = new MyAlertDialogFragment();
+ Bundle args = new Bundle();
+ args.putInt("title", title);
+ frag.setArguments(args);
+ return frag;
+ }
+
+ @Override
+ public Dialog onCreateDialog(Bundle savedInstanceState) {
+ int title = getArguments().getInt("title");
+
+ return new AlertDialog.Builder(getActivity())
+ .setIcon(R.drawable.alert_dialog_icon)
+ .setTitle(title)
+ .setPositiveButton(R.string.alert_dialog_ok,
+ new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog, int whichButton) {
+ ((FragmentAlertDialogSupport)getActivity()).doPositiveClick();
+ }
+ }
+ )
+ .setNegativeButton(R.string.alert_dialog_cancel,
+ new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog, int whichButton) {
+ ((FragmentAlertDialogSupport)getActivity()).doNegativeClick();
+ }
+ }
+ )
+ .create();
+ }
+ }
+//END_INCLUDE(dialog)
+}
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentArgumentsSupport.java b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentArgumentsSupport.java
new file mode 100644
index 0000000..8def8af
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentArgumentsSupport.java
@@ -0,0 +1,113 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.supportv4.app;
+
+import com.example.android.supportv4.R;
+
+import android.support.v4.app.FragmentActivity;
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentTransaction;
+
+import android.app.Activity;
+import android.content.res.TypedArray;
+import android.os.Bundle;
+import android.util.AttributeSet;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.TextView;
+
+/**
+ * Demonstrates a fragment that can be configured through both Bundle arguments
+ * and layout attributes.
+ */
+public class FragmentArgumentsSupport extends FragmentActivity {
+//BEGIN_INCLUDE(create)
+ @Override protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.fragment_arguments_support);
+
+ if (savedInstanceState == null) {
+ // First-time init; create fragment to embed in activity.
+ FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
+ Fragment newFragment = MyFragment.newInstance("From Arguments");
+ ft.add(R.id.created, newFragment);
+ ft.commit();
+ }
+ }
+//END_INCLUDE(create)
+
+//BEGIN_INCLUDE(fragment)
+ public static class MyFragment extends Fragment {
+ CharSequence mLabel;
+
+ /**
+ * Create a new instance of MyFragment that will be initialized
+ * with the given arguments.
+ */
+ static MyFragment newInstance(CharSequence label) {
+ MyFragment f = new MyFragment();
+ Bundle b = new Bundle();
+ b.putCharSequence("label", label);
+ f.setArguments(b);
+ return f;
+ }
+
+ /**
+ * Parse attributes during inflation from a view hierarchy into the
+ * arguments we handle.
+ */
+ @Override public void onInflate(Activity activity, AttributeSet attrs,
+ Bundle savedInstanceState) {
+ super.onInflate(activity, attrs, savedInstanceState);
+
+ TypedArray a = activity.obtainStyledAttributes(attrs,
+ R.styleable.FragmentArguments);
+ mLabel = a.getText(R.styleable.FragmentArguments_android_label);
+ a.recycle();
+ }
+
+ /**
+ * During creation, if arguments have been supplied to the fragment
+ * then parse those out.
+ */
+ @Override public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ Bundle args = getArguments();
+ if (args != null) {
+ CharSequence label = args.getCharSequence("label");
+ if (label != null) {
+ mLabel = label;
+ }
+ }
+ }
+
+ /**
+ * Create the view for this fragment, using the arguments given to it.
+ */
+ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ View v = inflater.inflate(R.layout.hello_world, container, false);
+ View tv = v.findViewById(R.id.text);
+ ((TextView)tv).setText(mLabel != null ? mLabel : "(no label)");
+ tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
+ return v;
+ }
+ }
+//END_INCLUDE(fragment)
+}
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentContextMenuSupport.java b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentContextMenuSupport.java
new file mode 100644
index 0000000..e52e732
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentContextMenuSupport.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.supportv4.app;
+
+import com.example.android.supportv4.R;
+
+import android.os.Bundle;
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentActivity;
+import android.util.Log;
+import android.view.ContextMenu;
+import android.view.ContextMenu.ContextMenuInfo;
+import android.view.LayoutInflater;
+import android.view.Menu;
+import android.view.MenuItem;
+import android.view.View;
+import android.view.ViewGroup;
+
+/**
+ * Demonstration of displaying a context menu from a fragment.
+ */
+public class FragmentContextMenuSupport extends FragmentActivity {
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ // Create the list fragment and add it as our sole content.
+ ContextMenuFragment content = new ContextMenuFragment();
+ getSupportFragmentManager().beginTransaction().add(
+ android.R.id.content, content).commit();
+ }
+
+ public static class ContextMenuFragment extends Fragment {
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ View root = inflater.inflate(R.layout.fragment_context_menu, container, false);
+ registerForContextMenu(root.findViewById(R.id.long_press));
+ return root;
+ }
+
+ @Override
+ public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
+ super.onCreateContextMenu(menu, v, menuInfo);
+ menu.add(Menu.NONE, R.id.a_item, Menu.NONE, "Menu A");
+ menu.add(Menu.NONE, R.id.b_item, Menu.NONE, "Menu B");
+ }
+
+ @Override
+ public boolean onContextItemSelected(MenuItem item) {
+ switch (item.getItemId()) {
+ case R.id.a_item:
+ Log.i("ContextMenu", "Item 1a was chosen");
+ return true;
+ case R.id.b_item:
+ Log.i("ContextMenu", "Item 1b was chosen");
+ return true;
+ }
+ return super.onContextItemSelected(item);
+ }
+ }
+}
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentDialogOrActivitySupport.java b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentDialogOrActivitySupport.java
new file mode 100644
index 0000000..06b2730
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentDialogOrActivitySupport.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.supportv4.app;
+
+import com.example.android.supportv4.R;
+
+import android.os.Bundle;
+import android.support.v4.app.DialogFragment;
+import android.support.v4.app.FragmentActivity;
+import android.support.v4.app.FragmentTransaction;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.view.ViewGroup;
+import android.widget.Button;
+import android.widget.TextView;
+
+public class FragmentDialogOrActivitySupport extends FragmentActivity {
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.fragment_dialog_or_activity);
+
+ if (savedInstanceState == null) {
+ // First-time init; create fragment to embed in activity.
+//BEGIN_INCLUDE(embed)
+ FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
+ DialogFragment newFragment = MyDialogFragment.newInstance();
+ ft.add(R.id.embedded, newFragment);
+ ft.commit();
+//END_INCLUDE(embed)
+ }
+
+ // Watch for button clicks.
+ Button button = (Button)findViewById(R.id.show_dialog);
+ button.setOnClickListener(new OnClickListener() {
+ public void onClick(View v) {
+ showDialog();
+ }
+ });
+ }
+
+//BEGIN_INCLUDE(show_dialog)
+ void showDialog() {
+ // Create the fragment and show it as a dialog.
+ DialogFragment newFragment = MyDialogFragment.newInstance();
+ newFragment.show(getSupportFragmentManager(), "dialog");
+ }
+//END_INCLUDE(show_dialog)
+
+//BEGIN_INCLUDE(dialog)
+ public static class MyDialogFragment extends DialogFragment {
+ static MyDialogFragment newInstance() {
+ return new MyDialogFragment();
+ }
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ View v = inflater.inflate(R.layout.hello_world, container, false);
+ View tv = v.findViewById(R.id.text);
+ ((TextView)tv).setText("This is an instance of MyDialogFragment");
+ return v;
+ }
+ }
+//END_INCLUDE(dialog)
+}
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentDialogSupport.java b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentDialogSupport.java
new file mode 100644
index 0000000..b387bfc
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentDialogSupport.java
@@ -0,0 +1,168 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.supportv4.app;
+
+import com.example.android.supportv4.R;
+
+import android.support.v4.app.DialogFragment;
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentActivity;
+import android.support.v4.app.FragmentTransaction;
+
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.view.ViewGroup;
+import android.widget.Button;
+import android.widget.TextView;
+
+public class FragmentDialogSupport extends FragmentActivity {
+ int mStackLevel = 0;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.fragment_dialog);
+
+ View tv = findViewById(R.id.text);
+ ((TextView)tv).setText("Example of displaying dialogs with a DialogFragment. "
+ + "Press the show button below to see the first dialog; pressing "
+ + "successive show buttons will display other dialog styles as a "
+ + "stack, with dismissing or back going to the previous dialog.");
+
+ // Watch for button clicks.
+ Button button = (Button)findViewById(R.id.show);
+ button.setOnClickListener(new OnClickListener() {
+ public void onClick(View v) {
+ showDialog();
+ }
+ });
+
+ if (savedInstanceState != null) {
+ mStackLevel = savedInstanceState.getInt("level");
+ }
+ }
+
+ @Override
+ public void onSaveInstanceState(Bundle outState) {
+ super.onSaveInstanceState(outState);
+ outState.putInt("level", mStackLevel);
+ }
+
+//BEGIN_INCLUDE(add_dialog)
+ void showDialog() {
+ mStackLevel++;
+
+ // DialogFragment.show() will take care of adding the fragment
+ // in a transaction. We also want to remove any currently showing
+ // dialog, so make our own transaction and take care of that here.
+ FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
+ Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
+ if (prev != null) {
+ ft.remove(prev);
+ }
+ ft.addToBackStack(null);
+
+ // Create and show the dialog.
+ DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);
+ newFragment.show(ft, "dialog");
+ }
+//END_INCLUDE(add_dialog)
+
+ static String getNameForNum(int num) {
+ switch ((num-1)%6) {
+ case 1: return "STYLE_NO_TITLE";
+ case 2: return "STYLE_NO_FRAME";
+ case 3: return "STYLE_NO_INPUT (this window can't receive input, so "
+ + "you will need to press the bottom show button)";
+ case 4: return "STYLE_NORMAL with dark fullscreen theme";
+ case 5: return "STYLE_NORMAL with light theme";
+ case 6: return "STYLE_NO_TITLE with light theme";
+ case 7: return "STYLE_NO_FRAME with light theme";
+ case 8: return "STYLE_NORMAL with light fullscreen theme";
+ }
+ return "STYLE_NORMAL";
+ }
+
+//BEGIN_INCLUDE(dialog)
+ public static class MyDialogFragment extends DialogFragment {
+ int mNum;
+
+ /**
+ * Create a new instance of MyDialogFragment, providing "num"
+ * as an argument.
+ */
+ static MyDialogFragment newInstance(int num) {
+ MyDialogFragment f = new MyDialogFragment();
+
+ // Supply num input as an argument.
+ Bundle args = new Bundle();
+ args.putInt("num", num);
+ f.setArguments(args);
+
+ return f;
+ }
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ mNum = getArguments().getInt("num");
+
+ // Pick a style based on the num.
+ int style = DialogFragment.STYLE_NORMAL, theme = 0;
+ switch ((mNum-1)%6) {
+ case 1: style = DialogFragment.STYLE_NO_TITLE; break;
+ case 2: style = DialogFragment.STYLE_NO_FRAME; break;
+ case 3: style = DialogFragment.STYLE_NO_INPUT; break;
+ case 4: style = DialogFragment.STYLE_NORMAL; break;
+ case 5: style = DialogFragment.STYLE_NO_TITLE; break;
+ case 6: style = DialogFragment.STYLE_NO_FRAME; break;
+ case 7: style = DialogFragment.STYLE_NORMAL; break;
+ }
+ switch ((mNum-1)%6) {
+ case 2: theme = android.R.style.Theme_Panel; break;
+ case 4: theme = android.R.style.Theme; break;
+ case 5: theme = android.R.style.Theme_Light; break;
+ case 6: theme = android.R.style.Theme_Light_Panel; break;
+ case 7: theme = android.R.style.Theme_Light; break;
+ }
+ setStyle(style, theme);
+ }
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ View v = inflater.inflate(R.layout.fragment_dialog, container, false);
+ View tv = v.findViewById(R.id.text);
+ ((TextView)tv).setText("Dialog #" + mNum + ": using style "
+ + getNameForNum(mNum));
+
+ // Watch for button clicks.
+ Button button = (Button)v.findViewById(R.id.show);
+ button.setOnClickListener(new OnClickListener() {
+ public void onClick(View v) {
+ // When button is clicked, call up to owning activity.
+ ((FragmentDialogSupport)getActivity()).showDialog();
+ }
+ });
+
+ return v;
+ }
+ }
+//END_INCLUDE(dialog)
+}
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentHideShowSupport.java b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentHideShowSupport.java
new file mode 100644
index 0000000..e0fcbdb
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentHideShowSupport.java
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.supportv4.app;
+
+import com.example.android.supportv4.R;
+
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentActivity;
+import android.support.v4.app.FragmentManager;
+import android.support.v4.app.FragmentTransaction;
+
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.view.View.OnClickListener;
+import android.widget.Button;
+import android.widget.TextView;
+
+/**
+ * Demonstration of hiding and showing fragments.
+ */
+public class FragmentHideShowSupport extends FragmentActivity {
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.fragment_hide_show_support);
+
+ // The content view embeds two fragments; now retrieve them and attach
+ // their "hide" button.
+ FragmentManager fm = getSupportFragmentManager();
+ addShowHideListener(R.id.frag1hide, fm.findFragmentById(R.id.fragment1));
+ addShowHideListener(R.id.frag2hide, fm.findFragmentById(R.id.fragment2));
+ }
+
+ void addShowHideListener(int buttonId, final Fragment fragment) {
+ final Button button = (Button)findViewById(buttonId);
+ button.setOnClickListener(new OnClickListener() {
+ public void onClick(View v) {
+ FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
+ ft.setCustomAnimations(android.R.anim.fade_in,
+ android.R.anim.fade_out);
+ if (fragment.isHidden()) {
+ ft.show(fragment);
+ button.setText("Hide");
+ } else {
+ ft.hide(fragment);
+ button.setText("Show");
+ }
+ ft.commit();
+ }
+ });
+ }
+
+ public static class FirstFragment extends Fragment {
+ TextView mTextView;
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ View v = inflater.inflate(R.layout.labeled_text_edit, container, false);
+ View tv = v.findViewById(R.id.msg);
+ ((TextView)tv).setText("The fragment saves and restores this text.");
+
+ // Retrieve the text editor, and restore the last saved state if needed.
+ mTextView = (TextView)v.findViewById(R.id.saved);
+ if (savedInstanceState != null) {
+ mTextView.setText(savedInstanceState.getCharSequence("text"));
+ }
+ return v;
+ }
+
+ @Override
+ public void onSaveInstanceState(Bundle outState) {
+ super.onSaveInstanceState(outState);
+
+ // Remember the current text, to restore if we later restart.
+ outState.putCharSequence("text", mTextView.getText());
+ }
+ }
+
+ public static class SecondFragment extends Fragment {
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ View v = inflater.inflate(R.layout.labeled_text_edit, container, false);
+ View tv = v.findViewById(R.id.msg);
+ ((TextView)tv).setText("The TextView saves and restores this text.");
+
+ // Retrieve the text editor and tell it to save and restore its state.
+ // Note that you will often set this in the layout XML, but since
+ // we are sharing our layout with the other fragment we will customize
+ // it here.
+ ((TextView)v.findViewById(R.id.saved)).setSaveEnabled(true);
+ return v;
+ }
+ }
+}
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentLayoutSupport.java b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentLayoutSupport.java
new file mode 100644
index 0000000..972f55c
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentLayoutSupport.java
@@ -0,0 +1,223 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.supportv4.app;
+
+import com.example.android.supportv4.R;
+import com.example.android.supportv4.Shakespeare;
+
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentActivity;
+import android.support.v4.app.FragmentTransaction;
+import android.support.v4.app.ListFragment;
+
+import android.content.Intent;
+import android.content.res.Configuration;
+import android.os.Bundle;
+import android.util.TypedValue;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.ListView;
+import android.widget.ScrollView;
+import android.widget.TextView;
+
+/**
+ * Demonstration of using fragments to implement different activity layouts.
+ * This sample provides a different layout (and activity flow) when run in
+ * landscape.
+ */
+public class FragmentLayoutSupport extends FragmentActivity {
+
+//BEGIN_INCLUDE(main)
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ setContentView(R.layout.fragment_layout_support);
+ }
+//END_INCLUDE(main)
+
+ /**
+ * This is a secondary activity, to show what the user has selected
+ * when the screen is not large enough to show it all in one activity.
+ */
+//BEGIN_INCLUDE(details_activity)
+ public static class DetailsActivity extends FragmentActivity {
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ if (getResources().getConfiguration().orientation
+ == Configuration.ORIENTATION_LANDSCAPE) {
+ // If the screen is now in landscape mode, we can show the
+ // dialog in-line with the list so we don't need this activity.
+ finish();
+ return;
+ }
+
+ if (savedInstanceState == null) {
+ // During initial setup, plug in the details fragment.
+ DetailsFragment details = new DetailsFragment();
+ details.setArguments(getIntent().getExtras());
+ getSupportFragmentManager().beginTransaction().add(
+ android.R.id.content, details).commit();
+ }
+ }
+ }
+//END_INCLUDE(details_activity)
+
+ /**
+ * This is the "top-level" fragment, showing a list of items that the
+ * user can pick. Upon picking an item, it takes care of displaying the
+ * data to the user as appropriate based on the currrent UI layout.
+ */
+//BEGIN_INCLUDE(titles)
+ public static class TitlesFragment extends ListFragment {
+ boolean mDualPane;
+ int mCurCheckPosition = 0;
+
+ @Override
+ public void onActivityCreated(Bundle savedInstanceState) {
+ super.onActivityCreated(savedInstanceState);
+
+ // Populate list with our static array of titles.
+ setListAdapter(new ArrayAdapter<String>(getActivity(),
+ R.layout.simple_list_item_checkable_1,
+ android.R.id.text1, Shakespeare.TITLES));
+
+ // Check to see if we have a frame in which to embed the details
+ // fragment directly in the containing UI.
+ View detailsFrame = getActivity().findViewById(R.id.details);
+ mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
+
+ if (savedInstanceState != null) {
+ // Restore last state for checked position.
+ mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
+ }
+
+ if (mDualPane) {
+ // In dual-pane mode, the list view highlights the selected item.
+ getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
+ // Make sure our UI is in the correct state.
+ showDetails(mCurCheckPosition);
+ }
+ }
+
+ @Override
+ public void onSaveInstanceState(Bundle outState) {
+ super.onSaveInstanceState(outState);
+ outState.putInt("curChoice", mCurCheckPosition);
+ }
+
+ @Override
+ public void onListItemClick(ListView l, View v, int position, long id) {
+ showDetails(position);
+ }
+
+ /**
+ * Helper function to show the details of a selected item, either by
+ * displaying a fragment in-place in the current UI, or starting a
+ * whole new activity in which it is displayed.
+ */
+ void showDetails(int index) {
+ mCurCheckPosition = index;
+
+ if (mDualPane) {
+ // We can display everything in-place with fragments, so update
+ // the list to highlight the selected item and show the data.
+ getListView().setItemChecked(index, true);
+
+ // Check what fragment is currently shown, replace if needed.
+ DetailsFragment details = (DetailsFragment)
+ getFragmentManager().findFragmentById(R.id.details);
+ if (details == null || details.getShownIndex() != index) {
+ // Make new fragment to show this selection.
+ details = DetailsFragment.newInstance(index);
+
+ // Execute a transaction, replacing any existing fragment
+ // with this one inside the frame.
+ FragmentTransaction ft = getFragmentManager().beginTransaction();
+ ft.replace(R.id.details, details);
+ ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
+ ft.commit();
+ }
+
+ } else {
+ // Otherwise we need to launch a new activity to display
+ // the dialog fragment with selected text.
+ Intent intent = new Intent();
+ intent.setClass(getActivity(), DetailsActivity.class);
+ intent.putExtra("index", index);
+ startActivity(intent);
+ }
+ }
+ }
+//END_INCLUDE(titles)
+
+ /**
+ * This is the secondary fragment, displaying the details of a particular
+ * item.
+ */
+//BEGIN_INCLUDE(details)
+ public static class DetailsFragment extends Fragment {
+ /**
+ * Create a new instance of DetailsFragment, initialized to
+ * show the text at 'index'.
+ */
+ public static DetailsFragment newInstance(int index) {
+ DetailsFragment f = new DetailsFragment();
+
+ // Supply index input as an argument.
+ Bundle args = new Bundle();
+ args.putInt("index", index);
+ f.setArguments(args);
+
+ return f;
+ }
+
+ public int getShownIndex() {
+ return getArguments().getInt("index", 0);
+ }
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ if (container == null) {
+ // We have different layouts, and in one of them this
+ // fragment's containing frame doesn't exist. The fragment
+ // may still be created from its saved state, but there is
+ // no reason to try to create its view hierarchy because it
+ // won't be displayed. Note this is not needed -- we could
+ // just run the code below, where we would create and return
+ // the view hierarchy; it would just never be used.
+ return null;
+ }
+
+ ScrollView scroller = new ScrollView(getActivity());
+ TextView text = new TextView(getActivity());
+ int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
+ 4, getActivity().getResources().getDisplayMetrics());
+ text.setPadding(padding, padding, padding, padding);
+ scroller.addView(text);
+ text.setText(Shakespeare.DIALOGUE[getShownIndex()]);
+ return scroller;
+ }
+ }
+//END_INCLUDE(details)
+}
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentListArraySupport.java b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentListArraySupport.java
new file mode 100644
index 0000000..7efae2c
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentListArraySupport.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.supportv4.app;
+
+import com.example.android.supportv4.Shakespeare;
+
+import android.support.v4.app.FragmentActivity;
+import android.support.v4.app.ListFragment;
+
+import android.os.Bundle;
+import android.util.Log;
+import android.view.View;
+import android.widget.ArrayAdapter;
+import android.widget.ListView;
+
+/**
+ * Demonstration of using ListFragment to show a list of items
+ * from a canned array.
+ */
+public class FragmentListArraySupport extends FragmentActivity {
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ // Create the list fragment and add it as our sole content.
+ if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
+ ArrayListFragment list = new ArrayListFragment();
+ getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit();
+ }
+ }
+
+ public static class ArrayListFragment extends ListFragment {
+
+ @Override
+ public void onActivityCreated(Bundle savedInstanceState) {
+ super.onActivityCreated(savedInstanceState);
+ setListAdapter(new ArrayAdapter<String>(getActivity(),
+ android.R.layout.simple_list_item_1, Shakespeare.TITLES));
+ }
+
+ @Override
+ public void onListItemClick(ListView l, View v, int position, long id) {
+ Log.i("FragmentList", "Item clicked: " + id);
+ }
+ }
+}
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentMenuSupport.java b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentMenuSupport.java
new file mode 100644
index 0000000..37a132d
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentMenuSupport.java
@@ -0,0 +1,139 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.supportv4.app;
+
+import com.example.android.supportv4.R;
+
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentActivity;
+import android.support.v4.app.FragmentManager;
+import android.support.v4.app.FragmentTransaction;
+import android.support.v4.view.MenuCompat;
+
+import android.os.Bundle;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.MenuItem;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.widget.CheckBox;
+
+/**
+ * Demonstrates how fragments can participate in the options menu.
+ */
+public class FragmentMenuSupport extends FragmentActivity {
+ Fragment mFragment1;
+ Fragment mFragment2;
+ CheckBox mCheckBox1;
+ CheckBox mCheckBox2;
+
+ // Update fragment visibility when check boxes are changed.
+ final OnClickListener mClickListener = new OnClickListener() {
+ public void onClick(View v) {
+ updateFragmentVisibility();
+ }
+ };
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.fragment_menu);
+
+ // Make sure the two menu fragments are created.
+ FragmentManager fm = getSupportFragmentManager();
+ FragmentTransaction ft = fm.beginTransaction();
+ mFragment1 = fm.findFragmentByTag("f1");
+ if (mFragment1 == null) {
+ mFragment1 = new MenuFragment();
+ ft.add(mFragment1, "f1");
+ }
+ mFragment2 = fm.findFragmentByTag("f2");
+ if (mFragment2 == null) {
+ mFragment2 = new Menu2Fragment();
+ ft.add(mFragment2, "f2");
+ }
+ ft.commit();
+
+ // Watch check box clicks.
+ mCheckBox1 = (CheckBox)findViewById(R.id.menu1);
+ mCheckBox1.setOnClickListener(mClickListener);
+ mCheckBox2 = (CheckBox)findViewById(R.id.menu2);
+ mCheckBox2.setOnClickListener(mClickListener);
+
+ // Make sure fragments start out with correct visibility.
+ updateFragmentVisibility();
+ }
+
+ @Override
+ protected void onRestoreInstanceState(Bundle savedInstanceState) {
+ super.onRestoreInstanceState(savedInstanceState);
+ // Make sure fragments are updated after check box view state is restored.
+ updateFragmentVisibility();
+ }
+
+ // Update fragment visibility based on current check box state.
+ void updateFragmentVisibility() {
+ FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
+ if (mCheckBox1.isChecked()) ft.show(mFragment1);
+ else ft.hide(mFragment1);
+ if (mCheckBox2.isChecked()) ft.show(mFragment2);
+ else ft.hide(mFragment2);
+ ft.commit();
+ }
+
+ /**
+ * A fragment that displays a menu. This fragment happens to not
+ * have a UI (it does not implement onCreateView), but it could also
+ * have one if it wanted.
+ */
+ public static class MenuFragment extends Fragment {
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setHasOptionsMenu(true);
+ }
+
+ @Override
+ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
+ MenuItem item;
+ item = menu.add("Menu 1a");
+ MenuCompat.setShowAsAction(item, MenuItem.SHOW_AS_ACTION_IF_ROOM);
+ item = menu.add("Menu 1b");
+ MenuCompat.setShowAsAction(item, MenuItem.SHOW_AS_ACTION_IF_ROOM);
+ }
+ }
+
+ /**
+ * Second fragment with a menu.
+ */
+ public static class Menu2Fragment extends Fragment {
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setHasOptionsMenu(true);
+ }
+
+ @Override
+ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
+ MenuItem item;
+ item = menu.add("Menu 2");
+ MenuCompat.setShowAsAction(item, MenuItem.SHOW_AS_ACTION_IF_ROOM);
+ }
+ }
+}
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentPagerSupport.java b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentPagerSupport.java
new file mode 100644
index 0000000..a3d89bb
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentPagerSupport.java
@@ -0,0 +1,129 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.supportv4.app;
+
+import com.example.android.supportv4.R;
+import com.example.android.supportv4.Shakespeare;
+
+import android.os.Bundle;
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentActivity;
+import android.support.v4.app.FragmentPager;
+import android.support.v4.app.FragmentTransaction;
+import android.support.v4.app.ListFragment;
+import android.util.Log;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.view.View.OnClickListener;
+import android.widget.ArrayAdapter;
+import android.widget.Button;
+import android.widget.ListView;
+import android.widget.TextView;
+
+public class FragmentPagerSupport extends FragmentActivity
+ implements FragmentPager.Adapter {
+ static final int NUM_ITEMS = 10;
+
+ FragmentPager mPager;
+ int mCurPos;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.fragment_pager);
+
+ mPager = (FragmentPager)findViewById(R.id.pager);
+ mPager.setAdapter(this);
+
+ // Watch for button clicks.
+ Button button = (Button)findViewById(R.id.new_fragment);
+ button.setOnClickListener(new OnClickListener() {
+ public void onClick(View v) {
+ mCurPos++;
+ if (mCurPos < NUM_ITEMS) {
+ mPager.setCurrentItem(mCurPos);
+ } else {
+ mCurPos--;
+ }
+ }
+ });
+ }
+
+ @Override
+ public int getCount() {
+ return NUM_ITEMS;
+ }
+
+ @Override
+ public Fragment getItem(int position) {
+ return ArrayListFragment.newInstance(position);
+ }
+
+ public static class ArrayListFragment extends ListFragment {
+ int mNum;
+
+ /**
+ * Create a new instance of CountingFragment, providing "num"
+ * as an argument.
+ */
+ static ArrayListFragment newInstance(int num) {
+ ArrayListFragment f = new ArrayListFragment();
+
+ // Supply num input as an argument.
+ Bundle args = new Bundle();
+ args.putInt("num", num);
+ f.setArguments(args);
+
+ return f;
+ }
+
+ /**
+ * When creating, retrieve this instance's number from its arguments.
+ */
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ mNum = getArguments() != null ? getArguments().getInt("num") : 1;
+ }
+
+ /**
+ * The Fragment's UI is just a simple text view showing its
+ * instance number.
+ */
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
+ View tv = v.findViewById(R.id.text);
+ ((TextView)tv).setText("Fragment #" + mNum);
+ return v;
+ }
+
+ @Override
+ public void onActivityCreated(Bundle savedInstanceState) {
+ super.onActivityCreated(savedInstanceState);
+ setListAdapter(new ArrayAdapter<String>(getActivity(),
+ android.R.layout.simple_list_item_1, Shakespeare.TITLES));
+ }
+
+ @Override
+ public void onListItemClick(ListView l, View v, int position, long id) {
+ Log.i("FragmentList", "Item clicked: " + id);
+ }
+ }
+}
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentReceiveResultSupport.java b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentReceiveResultSupport.java
new file mode 100644
index 0000000..d360cf9
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentReceiveResultSupport.java
@@ -0,0 +1,134 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.supportv4.app;
+
+import com.example.android.supportv4.R;
+
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentActivity;
+import android.support.v4.app.FragmentTransaction;
+
+import android.content.Intent;
+import android.os.Bundle;
+import android.text.Editable;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.view.ViewGroup;
+import android.widget.Button;
+import android.widget.FrameLayout;
+import android.widget.TextView;
+
+public class FragmentReceiveResultSupport extends FragmentActivity {
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
+ ViewGroup.LayoutParams.MATCH_PARENT,
+ ViewGroup.LayoutParams.MATCH_PARENT);
+ FrameLayout frame = new FrameLayout(this);
+ frame.setId(R.id.simple_fragment);
+ setContentView(frame, lp);
+
+ if (savedInstanceState == null) {
+ // Do first time initialization -- add fragment.
+ Fragment newFragment = new ReceiveResultFragment();
+ FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
+ ft.add(R.id.simple_fragment, newFragment).commit();
+ }
+ }
+
+ public static class ReceiveResultFragment extends Fragment {
+ // Definition of the one requestCode we use for receiving resuls.
+ static final private int GET_CODE = 0;
+
+ private TextView mResults;
+
+ private OnClickListener mGetListener = new OnClickListener() {
+ public void onClick(View v) {
+ // Start the activity whose result we want to retrieve. The
+ // result will come back with request code GET_CODE.
+ Intent intent = new Intent(getActivity(), SendResult.class);
+ startActivityForResult(intent, GET_CODE);
+ }
+ };
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ }
+
+ @Override
+ public void onSaveInstanceState(Bundle outState) {
+ super.onSaveInstanceState(outState);
+ }
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ View v = inflater.inflate(R.layout.receive_result, container, false);
+
+ // Retrieve the TextView widget that will display results.
+ mResults = (TextView)v.findViewById(R.id.results);
+
+ // This allows us to later extend the text buffer.
+ mResults.setText(mResults.getText(), TextView.BufferType.EDITABLE);
+
+ // Watch for button clicks.
+ Button getButton = (Button)v.findViewById(R.id.get);
+ getButton.setOnClickListener(mGetListener);
+
+ return v;
+ }
+
+ /**
+ * This method is called when the sending activity has finished, with the
+ * result it supplied.
+ */
+ @Override
+ public void onActivityResult(int requestCode, int resultCode, Intent data) {
+ // You can use the requestCode to select between multiple child
+ // activities you may have started. Here there is only one thing
+ // we launch.
+ if (requestCode == GET_CODE) {
+
+ // We will be adding to our text.
+ Editable text = (Editable)mResults.getText();
+
+ // This is a standard resultCode that is sent back if the
+ // activity doesn't supply an explicit result. It will also
+ // be returned if the activity failed to launch.
+ if (resultCode == RESULT_CANCELED) {
+ text.append("(cancelled)");
+
+ // Our protocol with the sending activity is that it will send
+ // text in 'data' as its result.
+ } else {
+ text.append("(okay ");
+ text.append(Integer.toString(resultCode));
+ text.append(") ");
+ if (data != null) {
+ text.append(data.getAction());
+ }
+ }
+
+ text.append("\n");
+ }
+ }
+ }
+}
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentRetainInstanceSupport.java b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentRetainInstanceSupport.java
new file mode 100644
index 0000000..0ff34dd
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentRetainInstanceSupport.java
@@ -0,0 +1,233 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.supportv4.app;
+
+import com.example.android.supportv4.R;
+
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentActivity;
+import android.support.v4.app.FragmentManager;
+
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.view.View.OnClickListener;
+import android.widget.Button;
+import android.widget.ProgressBar;
+
+/**
+ * This example shows how you can use a Fragment to easily propagate state
+ * (such as threads) across activity instances when an activity needs to be
+ * restarted due to, for example, a configuration change. This is a lot
+ * easier than using the raw Activity.onRetainNonConfiguratinInstance() API.
+ */
+public class FragmentRetainInstanceSupport extends FragmentActivity {
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ // First time init, create the UI.
+ if (savedInstanceState == null) {
+ getSupportFragmentManager().beginTransaction().add(android.R.id.content,
+ new UiFragment()).commit();
+ }
+ }
+
+ /**
+ * This is a fragment showing UI that will be updated from work done
+ * in the retained fragment.
+ */
+ public static class UiFragment extends Fragment {
+ RetainedFragment mWorkFragment;
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ View v = inflater.inflate(R.layout.fragment_retain_instance, container, false);
+
+ // Watch for button clicks.
+ Button button = (Button)v.findViewById(R.id.restart);
+ button.setOnClickListener(new OnClickListener() {
+ public void onClick(View v) {
+ mWorkFragment.restart();
+ }
+ });
+
+ return v;
+ }
+
+ @Override
+ public void onActivityCreated(Bundle savedInstanceState) {
+ super.onActivityCreated(savedInstanceState);
+
+ FragmentManager fm = getFragmentManager();
+
+ // Check to see if we have retained the worker fragment.
+ mWorkFragment = (RetainedFragment)fm.findFragmentByTag("work");
+
+ // If not retained (or first time running), we need to create it.
+ if (mWorkFragment == null) {
+ mWorkFragment = new RetainedFragment();
+ // Tell it who it is working with.
+ mWorkFragment.setTargetFragment(this, 0);
+ fm.beginTransaction().add(mWorkFragment, "work").commit();
+ }
+ }
+
+ }
+
+ /**
+ * This is the Fragment implementation that will be retained across
+ * activity instances. It represents some ongoing work, here a thread
+ * we have that sits around incrementing a progress indicator.
+ */
+ public static class RetainedFragment extends Fragment {
+ ProgressBar mProgressBar;
+ int mPosition;
+ boolean mReady = false;
+ boolean mQuiting = false;
+
+ /**
+ * This is the thread that will do our work. It sits in a loop running
+ * the progress up until it has reached the top, then stops and waits.
+ */
+ final Thread mThread = new Thread() {
+ @Override
+ public void run() {
+ // We'll figure the real value out later.
+ int max = 10000;
+
+ // This thread runs almost forever.
+ while (true) {
+
+ // Update our shared state with the UI.
+ synchronized (this) {
+ // Our thread is stopped if the UI is not ready
+ // or it has completed its work.
+ while (!mReady || mPosition >= max) {
+ if (mQuiting) {
+ return;
+ }
+ try {
+ wait();
+ } catch (InterruptedException e) {
+ }
+ }
+
+ // Now update the progress. Note it is important that
+ // we touch the progress bar with the lock held, so it
+ // doesn't disappear on us.
+ mPosition++;
+ max = mProgressBar.getMax();
+ mProgressBar.setProgress(mPosition);
+ }
+
+ // Normally we would be doing some work, but put a kludge
+ // here to pretend like we are.
+ synchronized (this) {
+ try {
+ wait(50);
+ } catch (InterruptedException e) {
+ }
+ }
+ }
+ }
+ };
+
+ /**
+ * Fragment initialization. We way we want to be retained and
+ * start our thread.
+ */
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ // Tell the framework to try to keep this fragment around
+ // during a configuration change.
+ setRetainInstance(true);
+
+ // Start up the worker thread.
+ mThread.start();
+ }
+
+ /**
+ * This is called when the Fragment's Activity is ready to go, after
+ * its content view has been installed; it is called both after
+ * the initial fragment creation and after the fragment is re-attached
+ * to a new activity.
+ */
+ @Override
+ public void onActivityCreated(Bundle savedInstanceState) {
+ super.onActivityCreated(savedInstanceState);
+
+ // Retrieve the progress bar from the target's view hierarchy.
+ mProgressBar = (ProgressBar)getTargetFragment().getView().findViewById(
+ R.id.progress_horizontal);
+
+ // We are ready for our thread to go.
+ synchronized (mThread) {
+ mReady = true;
+ mThread.notify();
+ }
+ }
+
+ /**
+ * This is called when the fragment is going away. It is NOT called
+ * when the fragment is being propagated between activity instances.
+ */
+ @Override
+ public void onDestroy() {
+ // Make the thread go away.
+ synchronized (mThread) {
+ mReady = false;
+ mQuiting = true;
+ mThread.notify();
+ }
+
+ super.onDestroy();
+ }
+
+ /**
+ * This is called right before the fragment is detached from its
+ * current activity instance.
+ */
+ @Override
+ public void onDetach() {
+ // This fragment is being detached from its activity. We need
+ // to make sure its thread is not going to touch any activity
+ // state after returning from this function.
+ synchronized (mThread) {
+ mProgressBar = null;
+ mReady = false;
+ mThread.notify();
+ }
+
+ super.onDetach();
+ }
+
+ /**
+ * API for our UI to restart the progress thread.
+ */
+ public void restart() {
+ synchronized (mThread) {
+ mPosition = 0;
+ mThread.notify();
+ }
+ }
+ }
+}
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentStackSupport.java b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentStackSupport.java
new file mode 100644
index 0000000..c4d05fa
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentStackSupport.java
@@ -0,0 +1,125 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.supportv4.app;
+
+import com.example.android.supportv4.R;
+
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentActivity;
+import android.support.v4.app.FragmentTransaction;
+
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.view.View.OnClickListener;
+import android.widget.Button;
+import android.widget.TextView;
+
+public class FragmentStackSupport extends FragmentActivity {
+ int mStackLevel = 1;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.fragment_stack);
+
+ // Watch for button clicks.
+ Button button = (Button)findViewById(R.id.new_fragment);
+ button.setOnClickListener(new OnClickListener() {
+ public void onClick(View v) {
+ addFragmentToStack();
+ }
+ });
+
+ if (savedInstanceState == null) {
+ // Do first time initialization -- add initial fragment.
+ Fragment newFragment = CountingFragment.newInstance(mStackLevel);
+ FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
+ ft.add(R.id.simple_fragment, newFragment).commit();
+ } else {
+ mStackLevel = savedInstanceState.getInt("level");
+ }
+ }
+
+ @Override
+ public void onSaveInstanceState(Bundle outState) {
+ super.onSaveInstanceState(outState);
+ outState.putInt("level", mStackLevel);
+ }
+
+//BEGIN_INCLUDE(add_stack)
+ void addFragmentToStack() {
+ mStackLevel++;
+
+ // Instantiate a new fragment.
+ Fragment newFragment = CountingFragment.newInstance(mStackLevel);
+
+ // Add the fragment to the activity, pushing this transaction
+ // on to the back stack.
+ FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
+ ft.replace(R.id.simple_fragment, newFragment);
+ ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
+ ft.addToBackStack(null);
+ ft.commit();
+ }
+//END_INCLUDE(add_stack)
+
+//BEGIN_INCLUDE(fragment)
+ public static class CountingFragment extends Fragment {
+ int mNum;
+
+ /**
+ * Create a new instance of CountingFragment, providing "num"
+ * as an argument.
+ */
+ static CountingFragment newInstance(int num) {
+ CountingFragment f = new CountingFragment();
+
+ // Supply num input as an argument.
+ Bundle args = new Bundle();
+ args.putInt("num", num);
+ f.setArguments(args);
+
+ return f;
+ }
+
+ /**
+ * When creating, retrieve this instance's number from its arguments.
+ */
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ mNum = getArguments() != null ? getArguments().getInt("num") : 1;
+ }
+
+ /**
+ * The Fragment's UI is just a simple text view showing its
+ * instance number.
+ */
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ View v = inflater.inflate(R.layout.hello_world, container, false);
+ View tv = v.findViewById(R.id.text);
+ ((TextView)tv).setText("Fragment #" + mNum);
+ tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
+ return v;
+ }
+ }
+//END_INCLUDE(fragment)
+}
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/app/LoaderCursorSupport.java b/samples/Support4Demos/src/com/example/android/supportv4/app/LoaderCursorSupport.java
new file mode 100644
index 0000000..07b9309
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/app/LoaderCursorSupport.java
@@ -0,0 +1,160 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.supportv4.app;
+
+import android.support.v4.app.FragmentActivity;
+import android.support.v4.app.FragmentManager;
+import android.support.v4.app.ListFragment;
+import android.support.v4.app.LoaderManager;
+import android.support.v4.content.CursorLoader;
+import android.support.v4.content.Loader;
+import android.support.v4.widget.SimpleCursorAdapter;
+
+import android.database.Cursor;
+import android.net.Uri;
+import android.os.Bundle;
+import android.provider.ContactsContract.Contacts;
+import android.text.TextUtils;
+import android.util.Log;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.View;
+import android.widget.ListView;
+
+/**
+ * Demonstration of the use of a CursorLoader to load and display contacts
+ * data in a fragment.
+ */
+public class LoaderCursorSupport extends FragmentActivity {
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ FragmentManager fm = getSupportFragmentManager();
+
+ // Create the list fragment and add it as our sole content.
+ if (fm.findFragmentById(android.R.id.content) == null) {
+ CursorLoaderListFragment list = new CursorLoaderListFragment();
+ fm.beginTransaction().add(android.R.id.content, list).commit();
+ }
+ }
+
+//BEGIN_INCLUDE(fragment_cursor)
+ public static class CursorLoaderListFragment extends ListFragment
+ implements LoaderManager.LoaderCallbacks<Cursor> {
+
+ // This is the Adapter being used to display the list's data.
+ SimpleCursorAdapter mAdapter;
+
+ // If non-null, this is the current filter the user has provided.
+ String mCurFilter;
+
+ @Override public void onActivityCreated(Bundle savedInstanceState) {
+ super.onActivityCreated(savedInstanceState);
+
+ // Give some text to display if there is no data. In a real
+ // application this would come from a resource.
+ setEmptyText("No phone numbers");
+
+ // We have a menu item to show in action bar.
+ setHasOptionsMenu(true);
+
+ // Create an empty adapter we will use to display the loaded data.
+ mAdapter = new SimpleCursorAdapter(getActivity(),
+ android.R.layout.simple_list_item_2, null,
+ new String[] { Contacts.DISPLAY_NAME, Contacts.CONTACT_STATUS },
+ new int[] { android.R.id.text1, android.R.id.text2 }, 0);
+ setListAdapter(mAdapter);
+
+ // Prepare the loader. Either re-connect with an existing one,
+ // or start a new one.
+ getLoaderManager().initLoader(0, null, this);
+ }
+
+ @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
+ // Place an action bar item for searching.
+ //MenuItem item = menu.add("Search");
+ //item.setIcon(android.R.drawable.ic_menu_search);
+ //item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
+ //SearchView sv = new SearchView(getActivity());
+ //sv.setOnQueryTextListener(this);
+ //item.setActionView(sv);
+ }
+
+ public boolean onQueryTextChange(String newText) {
+ // Called when the action bar search text has changed. Update
+ // the search filter, and restart the loader to do a new query
+ // with this filter.
+ mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;
+ getLoaderManager().restartLoader(0, null, this);
+ return true;
+ }
+
+ @Override public void onListItemClick(ListView l, View v, int position, long id) {
+ // Insert desired behavior here.
+ Log.i("FragmentComplexList", "Item clicked: " + id);
+ }
+
+ // These are the Contacts rows that we will retrieve.
+ static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
+ Contacts._ID,
+ Contacts.DISPLAY_NAME,
+ Contacts.CONTACT_STATUS,
+ Contacts.CONTACT_PRESENCE,
+ Contacts.PHOTO_ID,
+ Contacts.LOOKUP_KEY,
+ };
+
+ public Loader<Cursor> onCreateLoader(int id, Bundle args) {
+ // This is called when a new Loader needs to be created. This
+ // sample only has one Loader, so we don't care about the ID.
+ // First, pick the base URI to use depending on whether we are
+ // currently filtering.
+ Uri baseUri;
+ if (mCurFilter != null) {
+ baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI,
+ Uri.encode(mCurFilter));
+ } else {
+ baseUri = Contacts.CONTENT_URI;
+ }
+
+ // Now create and return a CursorLoader that will take care of
+ // creating a Cursor for the data being displayed.
+ String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
+ + Contacts.HAS_PHONE_NUMBER + "=1) AND ("
+ + Contacts.DISPLAY_NAME + " != '' ))";
+ return new CursorLoader(getActivity(), baseUri,
+ CONTACTS_SUMMARY_PROJECTION, select, null,
+ Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
+ }
+
+ public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
+ // Swap the new cursor in. (The framework will take care of closing the
+ // old cursor once we return.)
+ mAdapter.swapCursor(data);
+ }
+
+ public void onLoaderReset(Loader<Cursor> loader) {
+ // This is called when the last Cursor provided to onLoadFinished()
+ // above is about to be closed. We need to make sure we are no
+ // longer using it.
+ mAdapter.swapCursor(null);
+ }
+ }
+//END_INCLUDE(fragment_cursor)
+}
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/app/LoaderThrottleSupport.java b/samples/Support4Demos/src/com/example/android/supportv4/app/LoaderThrottleSupport.java
new file mode 100644
index 0000000..6693008
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/app/LoaderThrottleSupport.java
@@ -0,0 +1,503 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.supportv4.app;
+
+//BEGIN_INCLUDE(complete)
+import android.support.v4.app.FragmentActivity;
+import android.support.v4.app.FragmentManager;
+import android.support.v4.app.ListFragment;
+import android.support.v4.app.LoaderManager;
+import android.support.v4.content.CursorLoader;
+import android.support.v4.content.Loader;
+import android.support.v4.widget.SimpleCursorAdapter;
+
+import android.content.ContentProvider;
+import android.content.ContentResolver;
+import android.content.ContentUris;
+import android.content.ContentValues;
+import android.content.Context;
+import android.content.UriMatcher;
+import android.database.Cursor;
+import android.database.DatabaseUtils;
+import android.database.SQLException;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteOpenHelper;
+import android.database.sqlite.SQLiteQueryBuilder;
+import android.net.Uri;
+import android.os.AsyncTask;
+import android.os.Bundle;
+import android.provider.BaseColumns;
+import android.text.TextUtils;
+import android.util.Log;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.MenuItem;
+import android.view.View;
+import android.widget.ListView;
+
+import java.util.HashMap;
+
+/**
+ * Demonstration of bottom to top implementation of a content provider holding
+ * structured data through displaying it in the UI, using throttling to reduce
+ * the number of queries done when its data changes.
+ */
+public class LoaderThrottleSupport extends FragmentActivity {
+ // Debugging.
+ static final String TAG = "LoaderThrottle";
+
+ /**
+ * The authority we use to get to our sample provider.
+ */
+ public static final String AUTHORITY = "com.example.android.apis.support.app.LoaderThrottle";
+
+ /**
+ * Definition of the contract for the main table of our provider.
+ */
+ public static final class MainTable implements BaseColumns {
+
+ // This class cannot be instantiated
+ private MainTable() {}
+
+ /**
+ * The table name offered by this provider
+ */
+ public static final String TABLE_NAME = "main";
+
+ /**
+ * The content:// style URL for this table
+ */
+ public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/main");
+
+ /**
+ * The content URI base for a single row of data. Callers must
+ * append a numeric row id to this Uri to retrieve a row
+ */
+ public static final Uri CONTENT_ID_URI_BASE
+ = Uri.parse("content://" + AUTHORITY + "/main/");
+
+ /**
+ * The MIME type of {@link #CONTENT_URI}.
+ */
+ public static final String CONTENT_TYPE
+ = "vnd.android.cursor.dir/vnd.example.api-demos-throttle";
+
+ /**
+ * The MIME type of a {@link #CONTENT_URI} sub-directory of a single row.
+ */
+ public static final String CONTENT_ITEM_TYPE
+ = "vnd.android.cursor.item/vnd.example.api-demos-throttle";
+ /**
+ * The default sort order for this table
+ */
+ public static final String DEFAULT_SORT_ORDER = "data COLLATE LOCALIZED ASC";
+
+ /**
+ * Column name for the single column holding our data.
+ * <P>Type: TEXT</P>
+ */
+ public static final String COLUMN_NAME_DATA = "data";
+ }
+
+ /**
+ * This class helps open, create, and upgrade the database file.
+ */
+ static class DatabaseHelper extends SQLiteOpenHelper {
+
+ private static final String DATABASE_NAME = "loader_throttle.db";
+ private static final int DATABASE_VERSION = 2;
+
+ DatabaseHelper(Context context) {
+
+ // calls the super constructor, requesting the default cursor factory.
+ super(context, DATABASE_NAME, null, DATABASE_VERSION);
+ }
+
+ /**
+ *
+ * Creates the underlying database with table name and column names taken from the
+ * NotePad class.
+ */
+ @Override
+ public void onCreate(SQLiteDatabase db) {
+ db.execSQL("CREATE TABLE " + MainTable.TABLE_NAME + " ("
+ + MainTable._ID + " INTEGER PRIMARY KEY,"
+ + MainTable.COLUMN_NAME_DATA + " TEXT"
+ + ");");
+ }
+
+ /**
+ *
+ * Demonstrates that the provider must consider what happens when the
+ * underlying datastore is changed. In this sample, the database is upgraded the database
+ * by destroying the existing data.
+ * A real application should upgrade the database in place.
+ */
+ @Override
+ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
+
+ // Logs that the database is being upgraded
+ Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ + newVersion + ", which will destroy all old data");
+
+ // Kills the table and existing data
+ db.execSQL("DROP TABLE IF EXISTS notes");
+
+ // Recreates the database with a new version
+ onCreate(db);
+ }
+ }
+
+ /**
+ * A very simple implementation of a content provider.
+ */
+ public static class SimpleProvider extends ContentProvider {
+ // A projection map used to select columns from the database
+ private final HashMap<String, String> mNotesProjectionMap;
+ // Uri matcher to decode incoming URIs.
+ private final UriMatcher mUriMatcher;
+
+ // The incoming URI matches the main table URI pattern
+ private static final int MAIN = 1;
+ // The incoming URI matches the main table row ID URI pattern
+ private static final int MAIN_ID = 2;
+
+ // Handle to a new DatabaseHelper.
+ private DatabaseHelper mOpenHelper;
+
+ /**
+ * Global provider initialization.
+ */
+ public SimpleProvider() {
+ // Create and initialize URI matcher.
+ mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
+ mUriMatcher.addURI(AUTHORITY, MainTable.TABLE_NAME, MAIN);
+ mUriMatcher.addURI(AUTHORITY, MainTable.TABLE_NAME + "/#", MAIN_ID);
+
+ // Create and initialize projection map for all columns. This is
+ // simply an identity mapping.
+ mNotesProjectionMap = new HashMap<String, String>();
+ mNotesProjectionMap.put(MainTable._ID, MainTable._ID);
+ mNotesProjectionMap.put(MainTable.COLUMN_NAME_DATA, MainTable.COLUMN_NAME_DATA);
+ }
+
+ /**
+ * Perform provider creation.
+ */
+ @Override
+ public boolean onCreate() {
+ mOpenHelper = new DatabaseHelper(getContext());
+ // Assumes that any failures will be reported by a thrown exception.
+ return true;
+ }
+
+ /**
+ * Handle incoming queries.
+ */
+ @Override
+ public Cursor query(Uri uri, String[] projection, String selection,
+ String[] selectionArgs, String sortOrder) {
+
+ // Constructs a new query builder and sets its table name
+ SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
+ qb.setTables(MainTable.TABLE_NAME);
+
+ switch (mUriMatcher.match(uri)) {
+ case MAIN:
+ // If the incoming URI is for main table.
+ qb.setProjectionMap(mNotesProjectionMap);
+ break;
+
+ case MAIN_ID:
+ // The incoming URI is for a single row.
+ qb.setProjectionMap(mNotesProjectionMap);
+ qb.appendWhere(MainTable._ID + "=?");
+ selectionArgs = DatabaseUtils.appendSelectionArgs(selectionArgs,
+ new String[] { uri.getLastPathSegment() });
+ break;
+
+ default:
+ throw new IllegalArgumentException("Unknown URI " + uri);
+ }
+
+
+ if (TextUtils.isEmpty(sortOrder)) {
+ sortOrder = MainTable.DEFAULT_SORT_ORDER;
+ }
+
+ SQLiteDatabase db = mOpenHelper.getReadableDatabase();
+
+ Cursor c = qb.query(db, projection, selection, selectionArgs,
+ null /* no group */, null /* no filter */, sortOrder);
+
+ c.setNotificationUri(getContext().getContentResolver(), uri);
+ return c;
+ }
+
+ /**
+ * Return the MIME type for an known URI in the provider.
+ */
+ @Override
+ public String getType(Uri uri) {
+ switch (mUriMatcher.match(uri)) {
+ case MAIN:
+ return MainTable.CONTENT_TYPE;
+ case MAIN_ID:
+ return MainTable.CONTENT_ITEM_TYPE;
+ default:
+ throw new IllegalArgumentException("Unknown URI " + uri);
+ }
+ }
+
+ /**
+ * Handler inserting new data.
+ */
+ @Override
+ public Uri insert(Uri uri, ContentValues initialValues) {
+ if (mUriMatcher.match(uri) != MAIN) {
+ // Can only insert into to main URI.
+ throw new IllegalArgumentException("Unknown URI " + uri);
+ }
+
+ ContentValues values;
+
+ if (initialValues != null) {
+ values = new ContentValues(initialValues);
+ } else {
+ values = new ContentValues();
+ }
+
+ if (values.containsKey(MainTable.COLUMN_NAME_DATA) == false) {
+ values.put(MainTable.COLUMN_NAME_DATA, "");
+ }
+
+ SQLiteDatabase db = mOpenHelper.getWritableDatabase();
+
+ long rowId = db.insert(MainTable.TABLE_NAME, null, values);
+
+ // If the insert succeeded, the row ID exists.
+ if (rowId > 0) {
+ Uri noteUri = ContentUris.withAppendedId(MainTable.CONTENT_ID_URI_BASE, rowId);
+ getContext().getContentResolver().notifyChange(noteUri, null);
+ return noteUri;
+ }
+
+ throw new SQLException("Failed to insert row into " + uri);
+ }
+
+ /**
+ * Handle deleting data.
+ */
+ @Override
+ public int delete(Uri uri, String where, String[] whereArgs) {
+ SQLiteDatabase db = mOpenHelper.getWritableDatabase();
+ String finalWhere;
+
+ int count;
+
+ switch (mUriMatcher.match(uri)) {
+ case MAIN:
+ // If URI is main table, delete uses incoming where clause and args.
+ count = db.delete(MainTable.TABLE_NAME, where, whereArgs);
+ break;
+
+ // If the incoming URI matches a single note ID, does the delete based on the
+ // incoming data, but modifies the where clause to restrict it to the
+ // particular note ID.
+ case MAIN_ID:
+ // If URI is for a particular row ID, delete is based on incoming
+ // data but modified to restrict to the given ID.
+ finalWhere = DatabaseUtils.concatenateWhere(
+ MainTable._ID + " = " + ContentUris.parseId(uri), where);
+ count = db.delete(MainTable.TABLE_NAME, finalWhere, whereArgs);
+ break;
+
+ default:
+ throw new IllegalArgumentException("Unknown URI " + uri);
+ }
+
+ getContext().getContentResolver().notifyChange(uri, null);
+
+ return count;
+ }
+
+ /**
+ * Handle updating data.
+ */
+ @Override
+ public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
+ SQLiteDatabase db = mOpenHelper.getWritableDatabase();
+ int count;
+ String finalWhere;
+
+ switch (mUriMatcher.match(uri)) {
+ case MAIN:
+ // If URI is main table, update uses incoming where clause and args.
+ count = db.update(MainTable.TABLE_NAME, values, where, whereArgs);
+ break;
+
+ case MAIN_ID:
+ // If URI is for a particular row ID, update is based on incoming
+ // data but modified to restrict to the given ID.
+ finalWhere = DatabaseUtils.concatenateWhere(
+ MainTable._ID + " = " + ContentUris.parseId(uri), where);
+ count = db.update(MainTable.TABLE_NAME, values, finalWhere, whereArgs);
+ break;
+
+ default:
+ throw new IllegalArgumentException("Unknown URI " + uri);
+ }
+
+ getContext().getContentResolver().notifyChange(uri, null);
+
+ return count;
+ }
+ }
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ FragmentManager fm = getSupportFragmentManager();
+
+ // Create the list fragment and add it as our sole content.
+ if (fm.findFragmentById(android.R.id.content) == null) {
+ ThrottledLoaderListFragment list = new ThrottledLoaderListFragment();
+ fm.beginTransaction().add(android.R.id.content, list).commit();
+ }
+ }
+
+ public static class ThrottledLoaderListFragment extends ListFragment
+ implements LoaderManager.LoaderCallbacks<Cursor> {
+
+ // Menu identifiers
+ static final int POPULATE_ID = Menu.FIRST;
+ static final int CLEAR_ID = Menu.FIRST+1;
+
+ // This is the Adapter being used to display the list's data.
+ SimpleCursorAdapter mAdapter;
+
+ // If non-null, this is the current filter the user has provided.
+ String mCurFilter;
+
+ // Task we have running to populate the database.
+ AsyncTask<Void, Void, Void> mPopulatingTask;
+
+ @Override public void onActivityCreated(Bundle savedInstanceState) {
+ super.onActivityCreated(savedInstanceState);
+
+ setEmptyText("No data. Select 'Populate' to fill with data from Z to A at a rate of 4 per second.");
+ setHasOptionsMenu(true);
+
+ // Create an empty adapter we will use to display the loaded data.
+ mAdapter = new SimpleCursorAdapter(getActivity(),
+ android.R.layout.simple_list_item_1, null,
+ new String[] { MainTable.COLUMN_NAME_DATA },
+ new int[] { android.R.id.text1 }, 0);
+ setListAdapter(mAdapter);
+
+ // Prepare the loader. Either re-connect with an existing one,
+ // or start a new one.
+ getLoaderManager().initLoader(0, null, this);
+ }
+
+ @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
+ menu.add(Menu.NONE, POPULATE_ID, 0, "Populate")
+ .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
+ menu.add(Menu.NONE, CLEAR_ID, 0, "Clear")
+ .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
+ }
+
+ @Override public boolean onOptionsItemSelected(MenuItem item) {
+ final ContentResolver cr = getActivity().getContentResolver();
+
+ switch (item.getItemId()) {
+ case POPULATE_ID:
+ if (mPopulatingTask != null) {
+ mPopulatingTask.cancel(false);
+ }
+ mPopulatingTask = new AsyncTask<Void, Void, Void>() {
+ @Override protected Void doInBackground(Void... params) {
+ for (char c='Z'; c>='A'; c--) {
+ if (isCancelled()) {
+ break;
+ }
+ StringBuilder builder = new StringBuilder("Data ");
+ builder.append(c);
+ ContentValues values = new ContentValues();
+ values.put(MainTable.COLUMN_NAME_DATA, builder.toString());
+ cr.insert(MainTable.CONTENT_URI, values);
+ // Wait a bit between each insert.
+ try {
+ Thread.sleep(250);
+ } catch (InterruptedException e) {
+ }
+ }
+ return null;
+ }
+ };
+ mPopulatingTask.executeOnExecutor(
+ AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null);
+ return true;
+
+ case CLEAR_ID:
+ if (mPopulatingTask != null) {
+ mPopulatingTask.cancel(false);
+ mPopulatingTask = null;
+ }
+ AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
+ @Override protected Void doInBackground(Void... params) {
+ cr.delete(MainTable.CONTENT_URI, null, null);
+ return null;
+ }
+ };
+ task.execute((Void[])null);
+ return true;
+
+ default:
+ return super.onOptionsItemSelected(item);
+ }
+ }
+
+ @Override public void onListItemClick(ListView l, View v, int position, long id) {
+ // Insert desired behavior here.
+ Log.i(TAG, "Item clicked: " + id);
+ }
+
+ // These are the rows that we will retrieve.
+ static final String[] PROJECTION = new String[] {
+ MainTable._ID,
+ MainTable.COLUMN_NAME_DATA,
+ };
+
+ public Loader<Cursor> onCreateLoader(int id, Bundle args) {
+ CursorLoader cl = new CursorLoader(getActivity(), MainTable.CONTENT_URI,
+ PROJECTION, null, null, null);
+ cl.setUpdateThrottle(2000); // update at most every 2 seconds.
+ return cl;
+ }
+
+ public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
+ mAdapter.swapCursor(data);
+ }
+
+ public void onLoaderReset(Loader<Cursor> loader) {
+ mAdapter.swapCursor(null);
+ }
+ }
+}
+//END_INCLUDE(complete)
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/app/SendResult.java b/samples/Support4Demos/src/com/example/android/supportv4/app/SendResult.java
new file mode 100644
index 0000000..7179505
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/app/SendResult.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2007 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.supportv4.app;
+
+// Need the following import to get access to the app resources, since this
+// class is in a sub-package.
+import com.example.android.supportv4.R;
+
+import android.app.Activity;
+import android.content.Intent;
+import android.os.Bundle;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.widget.Button;
+
+
+/**
+ * Example of receiving a result from another activity.
+ */
+public class SendResult extends Activity
+{
+ /**
+ * Initialization of the Activity after it is first created. Must at least
+ * call {@link android.app.Activity#setContentView setContentView()} to
+ * describe what is to be displayed in the screen.
+ */
+ @Override
+ protected void onCreate(Bundle savedInstanceState)
+ {
+ // Be sure to call the super class.
+ super.onCreate(savedInstanceState);
+
+ // See assets/res/any/layout/hello_world.xml for this
+ // view layout definition, which is being set here as
+ // the content of our screen.
+ setContentView(R.layout.send_result);
+
+ // Watch for button clicks.
+ Button button = (Button)findViewById(R.id.corky);
+ button.setOnClickListener(mCorkyListener);
+ button = (Button)findViewById(R.id.violet);
+ button.setOnClickListener(mVioletListener);
+ }
+
+ private OnClickListener mCorkyListener = new OnClickListener()
+ {
+ public void onClick(View v)
+ {
+ // To send a result, simply call setResult() before your
+ // activity is finished.
+ setResult(RESULT_OK, (new Intent()).setAction("Corky!"));
+ finish();
+ }
+ };
+
+ private OnClickListener mVioletListener = new OnClickListener()
+ {
+ public void onClick(View v)
+ {
+ // To send a result, simply call setResult() before your
+ // activity is finished.
+ setResult(RESULT_OK, (new Intent()).setAction("Violet!"));
+ finish();
+ }
+ };
+}
+
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/app/_index.html b/samples/Support4Demos/src/com/example/android/supportv4/app/_index.html
new file mode 100644
index 0000000..c9e0aea
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/app/_index.html
@@ -0,0 +1,74 @@
+
+<p>This section includes samples showing the use of the application
+package features of the static support library, in particular fragments
+and loaders.</p>
+<ul>
+ <li><a href="#Fragment">Fragment</a></li>
+ <li><a href="#LoaderManager">LoaderManager</a></li>
+</ul>
+
+
+<h3 id="Fragment">Fragment</h3>
+<dl>
+ <dt><a href="FragmentAlertDialogSupport.html">Fragment Alert Dialog</a></dt>
+ <dd>Demonstrates how to use a DialogFragment to show and manage an
+ AlertDialog.</dd>
+
+ <dt><a href="FragmentArgumentsSupport.html">Fragment Arguments</a></dt>
+ <dd>Demonstrates how a fragment can be initialized with arguments,
+ supplying them either as an argument Bundle at runtime or XML attributes
+ in a <fragment> tag.</dd>
+
+ <dt><a href="FragmentContextMenuSupport.html">Fragment Context Menu</a></dt>
+ <dd>Demonstrates how to display and respond to a context menu that is
+ display from a fragment's view hierarchy.</dd>
+
+ <dt><a href="FragmentDialogSupport.html">Fragment Dialog</a></dt>
+ <dd>Demonstrates use of DialogFragment to show various types of dialogs.</dd>
+
+ <dt><a href="FragmentDialogOrActivitySupport.html">Fragment Dialog or Activity</a></dt>
+ <dd>Demonstrates how the same Fragment implementation can be used to provide the UI
+ for either an Activity or Dialog.</dd>
+
+ <dt><a href="FragmentHideShowSupport.html">Fragment Hide Show</a></dt>
+ <dd>Demonstrates hiding and showing fragments.</dd>
+
+ <dt><a href="FragmentLayoutSupport.html">Fragment Layout</a></dt>
+ <dd>Demonstrates use of the <fragment> tag to embed a Fragment in
+ an Activity's content view layout, and making the layout change based on
+ configuration to achieve different UI flows.</dd>
+
+ <dt><a href="FragmentListArraySupport.html">Fragment List Array</a></dt>
+ <dd>Demonstrates use of ListFragment to show the contents of a simple ArrayAdapter.</dd>
+
+ <dt><a href="FragmentMenuSupport.html">Fragment Menu</a></dt>
+ <dd>Demonstrates populating custom menu items from a Fragment.</dd>
+
+ <dt><a href="FragmentReceiveResultSupport.html">Fragment Receive Result</a></dt>
+ <dd>Demonstrates starting a new Activity from a Fragment, and receiving
+ a result back from it.</dd>
+
+ <dt><a href="FragmentRetainInstanceSupport.html">Fragment Retain Instance</a></dt>
+ <dd>Demonstrates a Fragment can be used to easily retain active state across
+ an Activity's configuration change.</dd>
+
+ <dt><a href="FragmentStackSupport.html">Fragment Stack</a></dt>
+ <dd>Demonstrates creating a stack of Fragment instances similar to the
+ traditional stack of activities.</dd>
+
+</dl>
+
+<h3 id="LoaderManager">LoaderManager</h3>
+<dl>
+ <dt><a href="LoaderCursorSupport.html">Loader Cursor</a></dt>
+ <dd>Demonstrates use of LoaderManager to perform a query for a Cursor that
+ populates a ListFragment.</dd>
+
+ <dt><a href="LoaderThrottleSupport.html">Loader Throttle</a></dt>
+ <dd>Complete end-to-end demonstration of a simple content provider that
+ populates data in a list through a cursor loader. The UI allows the list
+ to be populated with a series of items, showing how AsyncTaskLoader's
+ throttling facility can be used to control how much a Loader is refreshed
+ in this case.</dd>
+</dl>
+
\ No newline at end of file
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/view/CheckableFrameLayout.java b/samples/Support4Demos/src/com/example/android/supportv4/view/CheckableFrameLayout.java
new file mode 100644
index 0000000..c559cf6
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/view/CheckableFrameLayout.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.supportv4.view;
+
+import android.content.Context;
+import android.graphics.drawable.ColorDrawable;
+import android.util.AttributeSet;
+import android.widget.Checkable;
+import android.widget.FrameLayout;
+
+public class CheckableFrameLayout extends FrameLayout implements Checkable {
+ private boolean mChecked;
+
+ public CheckableFrameLayout(Context context) {
+ super(context);
+ }
+
+ public CheckableFrameLayout(Context context, AttributeSet attrs) {
+ super(context, attrs);
+ }
+
+ public void setChecked(boolean checked) {
+ mChecked = checked;
+ setBackgroundDrawable(checked ? new ColorDrawable(0xff0000a0) : null);
+ }
+
+ public boolean isChecked() {
+ return mChecked;
+ }
+
+ public void toggle() {
+ setChecked(!mChecked);
+ }
+
+}