165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/*
265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Copyright (C) 2014 The Android Open Source Project
365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Licensed under the Apache License, Version 2.0 (the "License");
565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * you may not use this file except in compliance with the License.
665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * You may obtain a copy of the License at
765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *      http://www.apache.org/licenses/LICENSE-2.0
965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
1065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Unless required by applicable law or agreed to in writing, software
1165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * distributed under the License is distributed on an "AS IS" BASIS,
1265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * See the License for the specific language governing permissions and
1465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * limitations under the License.
1565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
1665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepackage com.android.tv.settings.util;
1865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.graphics.Bitmap;
2065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.Bundle;
2165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.IBinder;
2265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.RemoteException;
2365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.util.Log;
2465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.lang.reflect.Method;
2665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane// This is a helper class to allow for light-weight transmission of Bitmap data through
2865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane// an intent extra.
2965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepublic class ActivityTransitionBitmapHelper {
3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String TAG = "ActivityTransitionBitmapHelper";
3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_BINDER =
3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            "com.android.tv.settings.util.extra_binder";
3465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static Method sPutBinder;
3565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static Method sGetBinder;
3665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    static {
3865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        // TODO: switch to not use reflection when everybody is building against the right target
3965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        try {
4065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            sPutBinder = Bundle.class.getDeclaredMethod("putBinder", String.class, IBinder.class);
4165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } catch (Exception e) {
4265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            Log.e(TAG, e.getMessage());
4365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
4465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        try {
4565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            sGetBinder = Bundle.class.getDeclaredMethod("getBinder", String.class);
4665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } catch (Exception e) {
4765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            Log.e(TAG, e.getMessage());
4865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
4965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
5065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static Bitmap getBitmapFromBinderBundle(Bundle bundle) {
5265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (bundle.containsKey(EXTRA_BINDER)) {
5365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            IActivityTransitionBitmapProvider provider = null;
5565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            IBinder binder = null;
5665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (sGetBinder != null) {
5765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                try {
5865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    binder = (IBinder) sGetBinder.invoke(bundle, EXTRA_BINDER);
5965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                } catch (Exception e) {
6065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    Log.e(TAG, e.getMessage());
6165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
6265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
6365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
6465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (binder != null) {
6565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                provider = IActivityTransitionBitmapProvider.
6665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        Stub.asInterface(binder);
6765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
6865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (provider != null) {
6965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                try {
7065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return provider.getTransitionBitmap();
7165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                } catch (RemoteException e) {
7265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    Log.d(TAG, "The remote process is not accessible, maybe it was killed.", e);
7365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
7465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
7565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
7665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return null;
7765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
7865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static Bundle bitmapAsBinderBundle(Bitmap bitmap) {
8065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        ActivityTransitionBitmapProvider provider = new ActivityTransitionBitmapProvider(bitmap);
8165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        Bundle bundle = new Bundle();
8265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
8365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (sPutBinder != null) {
8465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            try {
8565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                sPutBinder.invoke(bundle, EXTRA_BINDER, provider);
8665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            } catch (Exception e) {
8765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                Log.e(TAG, "Error invoking binder. ", e);
8865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
8965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
9065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
9165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return bundle;
9265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
9365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
9465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static class ActivityTransitionBitmapProvider extends
9565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            IActivityTransitionBitmapProvider.Stub {
9665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        private Bitmap mBitmap;
9765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
9865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public ActivityTransitionBitmapProvider(Bitmap bitmap) {
9965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mBitmap = bitmap;
10065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
10165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
10265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        @Override
10365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public Bitmap getTransitionBitmap() {
10465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            Bitmap b = mBitmap;
10565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
10665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            // We don't want to hold on to the bitmap object longer than necessary
10765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mBitmap = null;
10865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return b;
10965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
11065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
11165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane}
112