Update API demo to show new clip data types and conversions.
Change-Id: I32e2951c53ecedc75d1459666ced9f54b54cf6ba
This commit is contained in:
@@ -21,12 +21,15 @@
|
|||||||
com.example.android.apis.content.ClipboardSample
|
com.example.android.apis.content.ClipboardSample
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:orientation="vertical">
|
android:orientation="vertical">
|
||||||
|
<LinearLayout android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="vertical">
|
||||||
|
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<LinearLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="horizontal">
|
android:orientation="horizontal">
|
||||||
@@ -40,12 +43,11 @@
|
|||||||
android:id="@+id/styled_text"
|
android:id="@+id/styled_text"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:gravity="center_horizontal"
|
|
||||||
android:textStyle="normal" />
|
android:textStyle="normal" />
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<LinearLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="horizontal">
|
android:orientation="horizontal">
|
||||||
@@ -59,12 +61,29 @@
|
|||||||
android:id="@+id/plain_text"
|
android:id="@+id/plain_text"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:gravity="center_horizontal"
|
|
||||||
android:textStyle="normal" />
|
android:textStyle="normal" />
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="horizontal">
|
||||||
|
|
||||||
|
<Button android:id="@+id/copy_html_text"
|
||||||
|
android:layout_width="wrap_content" android:layout_height="wrap_content"
|
||||||
|
android:onClick="pasteHtmlText"
|
||||||
|
android:text="@string/copy_text" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/html_text"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:textStyle="normal" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="horizontal">
|
android:orientation="horizontal">
|
||||||
@@ -81,27 +100,66 @@
|
|||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
<Spinner android:id="@+id/clip_type"
|
<LinearLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="8dp"
|
||||||
|
android:orientation="horizontal">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:textAppearance="?android:attr/textAppearanceMedium"
|
||||||
|
android:text="Data type: " />
|
||||||
|
|
||||||
|
<Spinner android:id="@+id/clip_type"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
android:drawSelectorOnTop="true"
|
android:drawSelectorOnTop="true"
|
||||||
android:prompt="@string/clip_type_prompt"
|
android:prompt="@string/clip_type_prompt"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<TextView
|
</LinearLayout>
|
||||||
android:id="@+id/clip_mime_types"
|
|
||||||
|
<LinearLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_weight="0"
|
android:layout_marginTop="4dp"
|
||||||
android:textStyle="normal"
|
android:orientation="horizontal">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:textAppearance="?android:attr/textAppearanceMedium"
|
||||||
|
android:text="MIME types: " />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/clip_mime_types"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:background="#ff303030"
|
||||||
|
android:padding="4dp"
|
||||||
|
android:textAppearance="?android:attr/textAppearanceMedium"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<EditText
|
</LinearLayout>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="4dp"
|
||||||
|
android:textAppearance="?android:attr/textAppearanceMedium"
|
||||||
|
android:text="Data content:" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
android:id="@+id/clip_text"
|
android:id="@+id/clip_text"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="0px"
|
android:layout_height="wrap_content"
|
||||||
android:layout_weight="1"
|
android:background="#ff303030"
|
||||||
android:textStyle="normal"
|
android:padding="4dp"
|
||||||
|
android:textAppearance="?android:attr/textAppearanceMedium"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
</ScrollView>
|
||||||
@@ -42,8 +42,12 @@
|
|||||||
<string-array name="clip_data_types">
|
<string-array name="clip_data_types">
|
||||||
<item>No data in clipboard</item>
|
<item>No data in clipboard</item>
|
||||||
<item>Text clip</item>
|
<item>Text clip</item>
|
||||||
|
<item>HTML Text clip</item>
|
||||||
<item>Intent clip</item>
|
<item>Intent clip</item>
|
||||||
<item>Uri clip</item>
|
<item>Uri clip</item>
|
||||||
|
<item>Coerce to text</item>
|
||||||
|
<item>Coerce to styled text</item>
|
||||||
|
<item>Coerce to HTML text</item>
|
||||||
</string-array>
|
</string-array>
|
||||||
|
|
||||||
<!-- Used in App/SearchInvoke.java -->
|
<!-- Used in App/SearchInvoke.java -->
|
||||||
|
|||||||
@@ -26,10 +26,10 @@ import android.content.Intent;
|
|||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.text.method.LinkMovementMethod;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.AdapterView;
|
import android.widget.AdapterView;
|
||||||
import android.widget.ArrayAdapter;
|
import android.widget.ArrayAdapter;
|
||||||
import android.widget.EditText;
|
|
||||||
import android.widget.Spinner;
|
import android.widget.Spinner;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.AdapterView.OnItemSelectedListener;
|
import android.widget.AdapterView.OnItemSelectedListener;
|
||||||
@@ -39,15 +39,17 @@ public class ClipboardSample extends Activity {
|
|||||||
|
|
||||||
Spinner mSpinner;
|
Spinner mSpinner;
|
||||||
TextView mMimeTypes;
|
TextView mMimeTypes;
|
||||||
EditText mEditText;
|
TextView mDataText;
|
||||||
|
|
||||||
CharSequence mStyledText;
|
CharSequence mStyledText;
|
||||||
String mPlainText;
|
String mPlainText;
|
||||||
|
String mHtmlText;
|
||||||
|
String mHtmlPlainText;
|
||||||
|
|
||||||
ClipboardManager.OnPrimaryClipChangedListener mPrimaryChangeListener
|
ClipboardManager.OnPrimaryClipChangedListener mPrimaryChangeListener
|
||||||
= new ClipboardManager.OnPrimaryClipChangedListener() {
|
= new ClipboardManager.OnPrimaryClipChangedListener() {
|
||||||
public void onPrimaryClipChanged() {
|
public void onPrimaryClipChanged() {
|
||||||
updateClipData();
|
updateClipData(true);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -70,6 +72,11 @@ public class ClipboardSample extends Activity {
|
|||||||
tv = (TextView)findViewById(R.id.plain_text);
|
tv = (TextView)findViewById(R.id.plain_text);
|
||||||
tv.setText(mPlainText);
|
tv.setText(mPlainText);
|
||||||
|
|
||||||
|
mHtmlText = "<b>Link:</b> <a href=\"http://www.android.com\">Android</a>";
|
||||||
|
mHtmlPlainText = "Link: http://www.android.com";
|
||||||
|
tv = (TextView)findViewById(R.id.html_text);
|
||||||
|
tv.setText(mHtmlText);
|
||||||
|
|
||||||
mSpinner = (Spinner) findViewById(R.id.clip_type);
|
mSpinner = (Spinner) findViewById(R.id.clip_type);
|
||||||
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
|
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
|
||||||
this, R.array.clip_data_types, android.R.layout.simple_spinner_item);
|
this, R.array.clip_data_types, android.R.layout.simple_spinner_item);
|
||||||
@@ -79,16 +86,17 @@ public class ClipboardSample extends Activity {
|
|||||||
new OnItemSelectedListener() {
|
new OnItemSelectedListener() {
|
||||||
public void onItemSelected(
|
public void onItemSelected(
|
||||||
AdapterView<?> parent, View view, int position, long id) {
|
AdapterView<?> parent, View view, int position, long id) {
|
||||||
|
updateClipData(false);
|
||||||
}
|
}
|
||||||
public void onNothingSelected(AdapterView<?> parent) {
|
public void onNothingSelected(AdapterView<?> parent) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
mMimeTypes = (TextView)findViewById(R.id.clip_mime_types);
|
mMimeTypes = (TextView)findViewById(R.id.clip_mime_types);
|
||||||
mEditText = (EditText)findViewById(R.id.clip_text);
|
mDataText = (TextView)findViewById(R.id.clip_text);
|
||||||
|
|
||||||
mClipboard.addPrimaryClipChangedListener(mPrimaryChangeListener);
|
mClipboard.addPrimaryClipChangedListener(mPrimaryChangeListener);
|
||||||
updateClipData();
|
updateClipData(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -105,6 +113,10 @@ public class ClipboardSample extends Activity {
|
|||||||
mClipboard.setPrimaryClip(ClipData.newPlainText("Styled Text", mPlainText));
|
mClipboard.setPrimaryClip(ClipData.newPlainText("Styled Text", mPlainText));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void pasteHtmlText(View button) {
|
||||||
|
mClipboard.setPrimaryClip(ClipData.newHtmlText("HTML Text", mHtmlPlainText, mHtmlText));
|
||||||
|
}
|
||||||
|
|
||||||
public void pasteIntent(View button) {
|
public void pasteIntent(View button) {
|
||||||
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.android.com/"));
|
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.android.com/"));
|
||||||
mClipboard.setPrimaryClip(ClipData.newIntent("VIEW intent", intent));
|
mClipboard.setPrimaryClip(ClipData.newIntent("VIEW intent", intent));
|
||||||
@@ -114,31 +126,74 @@ public class ClipboardSample extends Activity {
|
|||||||
mClipboard.setPrimaryClip(ClipData.newRawUri("URI", Uri.parse("http://www.android.com/")));
|
mClipboard.setPrimaryClip(ClipData.newRawUri("URI", Uri.parse("http://www.android.com/")));
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateClipData() {
|
void updateClipData(boolean updateType) {
|
||||||
ClipData clip = mClipboard.getPrimaryClip();
|
ClipData clip = mClipboard.getPrimaryClip();
|
||||||
String[] mimeTypes = clip != null ? clip.getDescription().filterMimeTypes("*/*") : null;
|
String[] mimeTypes = clip != null ? clip.getDescription().filterMimeTypes("*/*") : null;
|
||||||
mMimeTypes.setText("");
|
|
||||||
if (mimeTypes != null) {
|
if (mimeTypes != null) {
|
||||||
|
mMimeTypes.setText("");
|
||||||
for (int i=0; i<mimeTypes.length; i++) {
|
for (int i=0; i<mimeTypes.length; i++) {
|
||||||
mMimeTypes.append(mimeTypes[i]);
|
if (i > 0) {
|
||||||
mMimeTypes.append("\n");
|
mMimeTypes.append("\n");
|
||||||
}
|
}
|
||||||
|
mMimeTypes.append(mimeTypes[i]);
|
||||||
}
|
}
|
||||||
if (clip == null) {
|
} else {
|
||||||
mSpinner.setSelection(0);
|
mMimeTypes.setText("NULL");
|
||||||
mEditText.setText("");
|
}
|
||||||
} else if (clip.getItemAt(0).getText() != null) {
|
|
||||||
mSpinner.setSelection(1);
|
if (updateType) {
|
||||||
mEditText.setText(clip.getItemAt(0).getText());
|
if (clip != null) {
|
||||||
} else if (clip.getItemAt(0).getIntent() != null) {
|
ClipData.Item item = clip.getItemAt(0);
|
||||||
|
if (item.getHtmlText() != null) {
|
||||||
mSpinner.setSelection(2);
|
mSpinner.setSelection(2);
|
||||||
mEditText.setText(clip.getItemAt(0).getIntent().toUri(0));
|
} else if (item.getText() != null) {
|
||||||
} else if (clip.getItemAt(0).getUri() != null) {
|
mSpinner.setSelection(1);
|
||||||
|
} else if (item.getIntent() != null) {
|
||||||
mSpinner.setSelection(3);
|
mSpinner.setSelection(3);
|
||||||
mEditText.setText(clip.getItemAt(0).getUri().toString());
|
} else if (item.getUri() != null) {
|
||||||
|
mSpinner.setSelection(4);
|
||||||
} else {
|
} else {
|
||||||
mSpinner.setSelection(0);
|
mSpinner.setSelection(0);
|
||||||
mEditText.setText("Clip containing no data");
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
mSpinner.setSelection(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (clip != null) {
|
||||||
|
ClipData.Item item = clip.getItemAt(0);
|
||||||
|
switch (mSpinner.getSelectedItemPosition()) {
|
||||||
|
case 0:
|
||||||
|
mDataText.setText("(No data)");
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
mDataText.setText(item.getText());
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
mDataText.setText(item.getHtmlText());
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
mDataText.setText(item.getIntent().toUri(0));
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
mDataText.setText(item.getUri().toString());
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
mDataText.setText(item.coerceToText(this));
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
mDataText.setText(item.coerceToStyledText(this));
|
||||||
|
break;
|
||||||
|
case 7:
|
||||||
|
mDataText.setText(item.coerceToHtmlText(this));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
mDataText.setText("Unknown option: " + mSpinner.getSelectedItemPosition());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
mDataText.setText("(NULL clip)");
|
||||||
|
}
|
||||||
|
mDataText.setMovementMethod(LinkMovementMethod.getInstance());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user