Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 43c97043 authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Adding shared element transitions support in AppWidgets

Bug: 113071278
Test: atest frameworks/base/core/tests/coretests/src/android/widget/RemoteViewsTest.java
Change-Id: I0d7698faa8d9e61913b55d34de8b7cfbb32863b1
parent 7cc7662d
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -54196,6 +54196,7 @@ package android.widget {
    method public void setLong(int, java.lang.String, long);
    method public void setOnClickFillInIntent(int, android.content.Intent);
    method public void setOnClickPendingIntent(int, android.app.PendingIntent);
    method public void setOnClickResponse(int, android.widget.RemoteViews.RemoteResponse);
    method public void setPendingIntentTemplate(int, android.app.PendingIntent);
    method public void setProgressBar(int, int, int, boolean);
    method public void setRelativeScrollPosition(int, int);
@@ -54216,6 +54217,7 @@ package android.widget {
    method public void showPrevious(int);
    method public void writeToParcel(android.os.Parcel, int);
    field public static final android.os.Parcelable.Creator<android.widget.RemoteViews> CREATOR;
    field public static final java.lang.String EXTRA_SHARED_ELEMENT_BOUNDS = "android.widget.extra.SHARED_ELEMENT_BOUNDS";
  }
  public static class RemoteViews.ActionException extends java.lang.RuntimeException {
@@ -54223,6 +54225,13 @@ package android.widget {
    ctor public RemoteViews.ActionException(java.lang.String);
  }
  public static class RemoteViews.RemoteResponse {
    ctor public RemoteViews.RemoteResponse();
    method public android.widget.RemoteViews.RemoteResponse addSharedElement(int, java.lang.String);
    method public static android.widget.RemoteViews.RemoteResponse fromFillInIntent(android.content.Intent);
    method public static android.widget.RemoteViews.RemoteResponse fromPendingIntent(android.app.PendingIntent);
  }
  public static abstract class RemoteViews.RemoteView implements java.lang.annotation.Annotation {
  }
+0 −1
Original line number Diff line number Diff line
@@ -1455,7 +1455,6 @@ Landroid/webkit/IWebViewUpdateService;->getCurrentWebViewPackageName()Ljava/lang
Landroid/webkit/IWebViewUpdateService;->getValidWebViewPackages()[Landroid/webkit/WebViewProviderInfo;
Landroid/webkit/IWebViewUpdateService;->isFallbackPackage(Ljava/lang/String;)Z
Landroid/widget/RelativeLayout$DependencyGraph$Node;-><init>()V
Landroid/widget/RemoteViews$OnClickHandler;-><init>()V
Landroid/widget/ScrollBarDrawable;-><init>()V
Lcom/android/ims/ImsConfigListener$Stub;-><init>()V
Lcom/android/ims/internal/IImsCallSession$Stub;-><init>()V
+42 −0
Original line number Diff line number Diff line
@@ -17,8 +17,12 @@
package android.appwidget;

import android.annotation.UnsupportedAppUsage;
import android.app.Activity;
import android.app.ActivityOptions;
import android.content.ComponentName;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
@@ -30,6 +34,7 @@ import android.os.CancellationSignal;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Pair;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.LayoutInflater;
@@ -44,6 +49,8 @@ import android.widget.RemoteViews.OnClickHandler;
import android.widget.RemoteViewsAdapter.RemoteAdapterConnectionCallback;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;

/**
@@ -651,4 +658,39 @@ public class AppWidgetHostView extends FrameLayout {
        super.onInitializeAccessibilityNodeInfoInternal(info);
        info.setClassName(AppWidgetHostView.class.getName());
    }

    /** @hide */
    public ActivityOptions createSharedElementActivityOptions(
            int[] sharedViewIds, String[] sharedViewNames, Intent fillInIntent) {
        Context parentContext = getContext();
        while ((parentContext instanceof ContextWrapper)
                && !(parentContext instanceof Activity)) {
            parentContext = ((ContextWrapper) parentContext).getBaseContext();
        }
        if (!(parentContext instanceof Activity)) {
            return null;
        }

        List<Pair<View, String>> sharedElements = new ArrayList<>();
        Bundle extras = new Bundle();

        for (int i = 0; i < sharedViewIds.length; i++) {
            View view = findViewById(sharedViewIds[i]);
            if (view != null) {
                sharedElements.add(Pair.create(view, sharedViewNames[i]));

                extras.putParcelable(sharedViewNames[i], RemoteViews.getSourceBounds(view));
            }
        }

        if (!sharedElements.isEmpty()) {
            fillInIntent.putExtra(RemoteViews.EXTRA_SHARED_ELEMENT_BOUNDS, extras);
            final ActivityOptions opts = ActivityOptions.makeSceneTransitionAnimation(
                    (Activity) parentContext,
                    sharedElements.toArray(new Pair[sharedElements.size()]));
            opts.setPendingIntentLaunchFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            return opts;
        }
        return null;
    }
}
+290 −189

File changed.

Preview size limit exceeded, changes collapsed.

+39 −0
Original line number Diff line number Diff line
@@ -16,11 +16,14 @@

package android.widget;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import android.app.ActivityOptions;
import android.app.PendingIntent;
import android.appwidget.AppWidgetHostView;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
@@ -444,4 +447,40 @@ public class RemoteViewsTest {
        }
        return found[0];
    }

    @Test
    public void sharedElement_pendingIntent_notifyParent() throws Exception {
        RemoteViews views = new RemoteViews(mPackage, R.layout.remote_views_test);
        PendingIntent pi = PendingIntent.getBroadcast(mContext, 0,
                new Intent("android.widget.RemoteViewsTest_shared_element"),
                PendingIntent.FLAG_ONE_SHOT);
        views.setOnClickResponse(R.id.image, RemoteViews.RemoteResponse.fromPendingIntent(pi)
                .addSharedElement(0, "e0")
                .addSharedElement(1, "e1")
                .addSharedElement(2, "e2"));

        WidgetContainer container = new WidgetContainer(mContext);
        container.addView(new RemoteViews(views).apply(mContext, container));
        container.findViewById(R.id.image).performClick();

        assertArrayEquals(container.mSharedViewIds, new int[] {0, 1, 2});
        assertArrayEquals(container.mSharedViewNames, new String[] {"e0", "e1", "e2"});
    }

    private class WidgetContainer extends AppWidgetHostView {
        int[] mSharedViewIds;
        String[] mSharedViewNames;

        WidgetContainer(Context context) {
            super(context);
        }

        @Override
        public ActivityOptions createSharedElementActivityOptions(
                int[] sharedViewIds, String[] sharedViewNames, Intent fillInIntent) {
            mSharedViewIds = sharedViewIds;
            mSharedViewNames = sharedViewNames;
            return null;
        }
    }
}
Loading