下面列出了怎么用android.app.RemoteAction的API类实例代码及写法,或者点击链接到github查看源代码。
private TextClassification(
@Nullable String text,
@Nullable Drawable legacyIcon,
@Nullable String legacyLabel,
@Nullable Intent legacyIntent,
@Nullable OnClickListener legacyOnClickListener,
@NonNull List<RemoteAction> actions,
@NonNull Map<String, Float> entityConfidence,
@Nullable String id) {
mText = text;
mLegacyIcon = legacyIcon;
mLegacyLabel = legacyLabel;
mLegacyIntent = legacyIntent;
mLegacyOnClickListener = legacyOnClickListener;
mActions = Collections.unmodifiableList(actions);
mEntityConfidence = new EntityConfidence(entityConfidence);
mId = id;
}
private TextClassification(Parcel in) {
mText = in.readString();
mActions = in.createTypedArrayList(RemoteAction.CREATOR);
if (!mActions.isEmpty()) {
final RemoteAction action = mActions.get(0);
mLegacyIcon = maybeLoadDrawable(action.getIcon());
mLegacyLabel = action.getTitle().toString();
mLegacyOnClickListener = createIntentOnClickListener(mActions.get(0).getActionIntent());
} else {
mLegacyIcon = null;
mLegacyLabel = null;
mLegacyOnClickListener = null;
}
mLegacyIntent = null; // mLegacyIntent is not parcelled.
mEntityConfidence = EntityConfidence.CREATOR.createFromParcel(in);
mId = in.readString();
}
/**
* Update the state of pause/resume action item in Picture-in-Picture mode.
*
* @param iconId The icon to be used.
* @param title The title text.
* @param controlType The type of the action. either {@link #CONTROL_TYPE_PLAY} or {@link
* #CONTROL_TYPE_PAUSE}.
* @param requestCode The request code for the {@link PendingIntent}.
*/
void updatePictureInPictureActions(
@DrawableRes int iconId, String title, int controlType, int requestCode) {
final ArrayList<RemoteAction> actions = new ArrayList<>();
// This is the PendingIntent that is invoked when a user clicks on the action item.
// You need to use distinct request codes for play and pause, or the PendingIntent won't
// be properly updated.
final PendingIntent intent =
PendingIntent.getBroadcast(
AndroidOPiPActivity.this,
requestCode,
new Intent(ACTION_MEDIA_CONTROL).putExtra(EXTRA_CONTROL_TYPE, controlType),
0);
final Icon icon = Icon.createWithResource(AndroidOPiPActivity.this, iconId);
actions.add(new RemoteAction(icon, title, title, intent));
mPictureInPictureParamsBuilder.setActions(actions);
// This is how you can update action items (or aspect ratio) for Picture-in-Picture mode.
// Note this call can happen even when the app is not in PiP mode. In that case, the
// arguments will be used for at the next call of #enterPictureInPictureMode.
setPictureInPictureParams(mPictureInPictureParamsBuilder.build());
}
/**
* Update the state of pause/resume action item in Picture-in-Picture mode.
*
* @param iconId The icon to be used.
* @param title The title text.
* @param controlType The type of the action. either {@link #CONTROL_TYPE_PLAY} or {@link
* #CONTROL_TYPE_PAUSE}.
* @param requestCode The request code for the {@link PendingIntent}.
*/
void updatePictureInPictureActions(
@DrawableRes int iconId, String title, int controlType, int requestCode) {
final ArrayList<RemoteAction> actions = new ArrayList<>();
// This is the PendingIntent that is invoked when a user clicks on the action item.
// You need to use distinct request codes for play and pause, or the PendingIntent won't
// be properly updated.
final PendingIntent intent =
PendingIntent.getBroadcast(
MainActivity.this,
requestCode,
new Intent(ACTION_MEDIA_CONTROL).putExtra(EXTRA_CONTROL_TYPE, controlType),
0);
final Icon icon = Icon.createWithResource(MainActivity.this, iconId);
actions.add(new RemoteAction(icon, title, title, intent));
// Another action item. This is a fixed action.
actions.add(
new RemoteAction(
Icon.createWithResource(MainActivity.this, R.drawable.ic_info_24dp),
getString(R.string.info),
getString(R.string.info_description),
PendingIntent.getActivity(
MainActivity.this,
REQUEST_INFO,
new Intent(
Intent.ACTION_VIEW,
Uri.parse(getString(R.string.info_uri))),
0)));
mPictureInPictureParamsBuilder.setActions(actions);
// This is how you can update action items (or aspect ratio) for Picture-in-Picture mode.
// Note this call can happen even when the app is not in PiP mode. In that case, the
// arguments will be used for at the next call of #enterPictureInPictureMode.
setPictureInPictureParams(mPictureInPictureParamsBuilder.build());
}
/**
* Sets the current set of actions.
*/
void setActions(List<RemoteAction> actions) {
mActions.clear();
if (actions != null) {
mActions.addAll(actions);
}
notifyActionsChanged(mActions);
}
/**
* Notifies listeners that the PIP actions have changed.
*/
private void notifyActionsChanged(List<RemoteAction> actions) {
if (mPinnedStackListener != null) {
try {
mPinnedStackListener.onActionsChanged(new ParceledListSlice(actions));
} catch (RemoteException e) {
Slog.e(TAG_WM, "Error delivering actions changed event.", e);
}
}
}
void dump(String prefix, PrintWriter pw) {
pw.println(prefix + "PinnedStackController");
pw.print(prefix + " defaultBounds=");
getDefaultBounds(INVALID_SNAP_FRACTION).printShortString(pw);
pw.println();
mService.getStackBounds(WINDOWING_MODE_PINNED, ACTIVITY_TYPE_STANDARD, mTmpRect);
pw.print(prefix + " movementBounds="); getMovementBounds(mTmpRect).printShortString(pw);
pw.println();
pw.println(prefix + " mIsImeShowing=" + mIsImeShowing);
pw.println(prefix + " mImeHeight=" + mImeHeight);
pw.println(prefix + " mIsShelfShowing=" + mIsShelfShowing);
pw.println(prefix + " mShelfHeight=" + mShelfHeight);
pw.println(prefix + " mReentrySnapFraction=" + mReentrySnapFraction);
pw.println(prefix + " mIsMinimized=" + mIsMinimized);
if (mActions.isEmpty()) {
pw.println(prefix + " mActions=[]");
} else {
pw.println(prefix + " mActions=[");
for (int i = 0; i < mActions.size(); i++) {
RemoteAction action = mActions.get(i);
pw.print(prefix + " Action[" + i + "]: ");
action.dump("", pw);
}
pw.println(prefix + " ]");
}
pw.println(prefix + " mDisplayInfo=" + mDisplayInfo);
}
/**
* Sets the current picture-in-picture actions.
*/
public void setPictureInPictureActions(List<RemoteAction> actions) {
synchronized (mWindowMap) {
if (!mService.mSupportsPictureInPicture || mContainer == null) {
return;
}
mContainer.getDisplayContent().getPinnedStackController().setActions(actions);
}
}
/**
* Adds an action that may be performed on the classified text. Actions should be added in
* order of likelihood that the user will use them, with the most likely action being added
* first.
*/
@NonNull
public Builder addAction(@NonNull RemoteAction action) {
Preconditions.checkArgument(action != null);
mActions.add(action);
return this;
}
@Nullable
RemoteAction asRemoteAction(Context context) {
final PackageManager pm = context.getPackageManager();
final ResolveInfo resolveInfo = pm.resolveActivity(mIntent, 0);
final String packageName = resolveInfo != null && resolveInfo.activityInfo != null
? resolveInfo.activityInfo.packageName : null;
Icon icon = null;
boolean shouldShowIcon = false;
if (packageName != null && !"android".equals(packageName)) {
// There is a default activity handling the intent.
mIntent.setComponent(new ComponentName(packageName, resolveInfo.activityInfo.name));
if (resolveInfo.activityInfo.getIconResource() != 0) {
icon = Icon.createWithResource(
packageName, resolveInfo.activityInfo.getIconResource());
shouldShowIcon = true;
}
}
if (icon == null) {
// RemoteAction requires that there be an icon.
icon = Icon.createWithResource("android",
com.android.internal.R.drawable.ic_more_items);
}
final PendingIntent pendingIntent =
TextClassification.createPendingIntent(context, mIntent, mRequestCode);
if (pendingIntent == null) {
return null;
}
final RemoteAction action = new RemoteAction(icon, mTitle, mDescription, pendingIntent);
action.setShouldShowIcon(shouldShowIcon);
return action;
}
/**
* Update the state of pause/resume action item in Picture-in-Picture mode.
*
* @param iconId The icon to be used.
* @param title The title text.
* @param controlType The type of the action. either {@link #CONTROL_TYPE_PLAY} or {@link
* #CONTROL_TYPE_PAUSE}.
* @param requestCode The request code for the {@link PendingIntent}.
*/
void updatePictureInPictureActions(
@DrawableRes int iconId, String title, int controlType, int requestCode) {
final ArrayList<RemoteAction> actions = new ArrayList<>();
// This is the PendingIntent that is invoked when a user clicks on the action item.
// You need to use distinct request codes for play and pause, or the PendingIntent won't
// be properly updated.
final PendingIntent intent =
PendingIntent.getBroadcast(
MainActivity.this,
requestCode,
new Intent(ACTION_MEDIA_CONTROL).putExtra(EXTRA_CONTROL_TYPE, controlType),
0);
final Icon icon = Icon.createWithResource(MainActivity.this, iconId);
actions.add(new RemoteAction(icon, title, title, intent));
// Another action item. This is a fixed action.
actions.add(
new RemoteAction(
Icon.createWithResource(MainActivity.this, R.drawable.ic_info_24dp),
getString(R.string.info),
getString(R.string.info_description),
PendingIntent.getActivity(
MainActivity.this,
REQUEST_INFO,
new Intent(
Intent.ACTION_VIEW,
Uri.parse(getString(R.string.info_uri))),
0)));
mPictureInPictureParamsBuilder.setActions(actions);
// This is how you can update action items (or aspect ratio) for Picture-in-Picture mode.
// Note this call can happen even when the app is not in PiP mode. In that case, the
// arguments will be used for at the next call of #enterPictureInPictureMode.
setPictureInPictureParams(mPictureInPictureParamsBuilder.build());
}
void setPictureInPictureActions(List<RemoteAction> actions) {
getWindowContainerController().setPictureInPictureActions(actions);
}
private TextClassification createClassificationResult(
TextClassifierImplNative.ClassificationResult[] classifications,
String text, int start, int end, @Nullable Instant referenceTime) {
final String classifiedText = text.substring(start, end);
final TextClassification.Builder builder = new TextClassification.Builder()
.setText(classifiedText);
final int size = classifications.length;
TextClassifierImplNative.ClassificationResult highestScoringResult = null;
float highestScore = Float.MIN_VALUE;
for (int i = 0; i < size; i++) {
builder.setEntityType(classifications[i].getCollection(),
classifications[i].getScore());
if (classifications[i].getScore() > highestScore) {
highestScoringResult = classifications[i];
highestScore = classifications[i].getScore();
}
}
boolean isPrimaryAction = true;
for (LabeledIntent labeledIntent : IntentFactory.create(
mContext, referenceTime, highestScoringResult, classifiedText)) {
final RemoteAction action = labeledIntent.asRemoteAction(mContext);
if (action == null) {
continue;
}
if (isPrimaryAction) {
// For O backwards compatibility, the first RemoteAction is also written to the
// legacy API fields.
builder.setIcon(action.getIcon().loadDrawable(mContext));
builder.setLabel(action.getTitle().toString());
builder.setIntent(labeledIntent.getIntent());
builder.setOnClickListener(TextClassification.createIntentOnClickListener(
TextClassification.createPendingIntent(mContext,
labeledIntent.getIntent(), labeledIntent.getRequestCode())));
isPrimaryAction = false;
}
builder.addAction(action);
}
return builder.setId(createId(text, start, end)).build();
}
private synchronized void updatePictureInPictureParams(@Nullable Rational aspect) {
if (aspect == null) {
aspect = ScreenUtils.getDefaultDisplayRational(this);
}
final List<RemoteAction> remoteActions = new ArrayList<>();
if (mShareIntent != null) {
final RemoteAction shareAction = new RemoteAction(
Icon.createWithResource(this, R.drawable.ic_share_white_24dp),
getString(R.string.action_share_screenshot),
getString(R.string.action_share_screenshot),
PendingIntent.getBroadcast(this, 0,
new Intent(ACTION_SHARE), PendingIntent.FLAG_UPDATE_CURRENT)
);
remoteActions.add(shareAction);
}
if (mDeleteIntent != null) {
final RemoteAction deleteAction = new RemoteAction(
Icon.createWithResource(this, R.drawable.ic_delete_white_24dp),
getString(R.string.action_delete_screenshot),
getString(R.string.action_delete_screenshot),
PendingIntent.getBroadcast(this, 0,
new Intent(ACTION_DELETE), PendingIntent.FLAG_UPDATE_CURRENT)
);
remoteActions.add(deleteAction);
}
if (mEditIntent != null) {
final RemoteAction editAction = new RemoteAction(
Icon.createWithResource(this, R.drawable.ic_edit_white_24dp),
getString(R.string.action_edit),
getString(R.string.action_edit),
PendingIntent.getBroadcast(this, 0,
new Intent(ACTION_EDIT), PendingIntent.FLAG_UPDATE_CURRENT)
);
remoteActions.add(editAction);
}
mPIPParams = new PictureInPictureParams.Builder()
.setAspectRatio(aspect)
.setActions(remoteActions)
.build();
if (isInPictureInPictureMode()) {
setPictureInPictureParams(mPIPParams);
}
}
/**
* Returns a list of actions that may be performed on the text. The list is ordered based on
* the likelihood that a user will use the action, with the most likely action appearing first.
*/
public List<RemoteAction> getActions() {
return mActions;
}