Example usage for android.os Bundle setClassLoader

List of usage examples for android.os Bundle setClassLoader

Introduction

In this page you can find the example usage for android.os Bundle setClassLoader.

Prototype

@Override
public void setClassLoader(ClassLoader loader) 

Source Link

Document

Changes the ClassLoader this Bundle uses when instantiating objects.

Usage

From source file:com.android.mail.browse.ConversationPagerAdapter.java

@Override
public void restoreState(Parcelable state, ClassLoader loader) {
    super.restoreState(state, loader);
    if (state != null) {
        Bundle b = (Bundle) state;
        b.setClassLoader(loader);
        final boolean detached = b.getBoolean(BUNDLE_DETACHED_MODE);
        setDetachedMode(detached);/*from w w w.  j a v a  2 s .  co m*/

        // save off the bundle in case it later needs to be consulted for fragments-to-kill
        mRestoredState = b;
    }
    LogUtils.d(LOG_TAG, "OUT PagerAdapter.restoreState. this=%s", this);
}

From source file:com.bilibili.lib.pageradapter.IDFragmentStatePagerAdapter.java

@Override
public Object instantiateItem(ViewGroup container, int position) {
    final int id = getItemId(position);
    // find in restored fragments cache
    Fragment f = mFragments.get(id);/*from w w  w.  j a  v a 2 s.  c  o m*/
    if (f != null) {
        return f;
    }

    if (mCurrentTransaction == null) {
        // will commit() when finishUpdate() call
        mCurrentTransaction = mFragmentManager.beginTransaction();
    }

    Fragment fragment = getItem(id);
    if (DEBUG) {
        Log.v(TAG, "Adding item #" + position + ": id =" + id + ", f=" + fragment);
    }
    Fragment.SavedState fss = mSavedState.get(id);
    if (fss != null) {
        Bundle state;
        try {
            state = Reflection.on(Fragment.SavedState.class).fieldValue(fss, "mState");
        } catch (NoSuchFieldException e) {
            if (DEBUG)
                Log.w(TAG, "No field name 'mState'!");
            state = null;
        }
        if (state != null) {
            state.setClassLoader(fragment.getClass().getClassLoader());
        }
        fragment.setInitialSavedState(fss);
    }
    fragment.setMenuVisibility(false);
    fragment.setUserVisibleHint(false);
    mFragments.put(id, fragment);
    mCurrentTransaction.add(container.getId(), fragment);

    return fragment;
}

From source file:com.first3.viz.download.DownloadManager.java

private Resource getResourceFromMsg(Message msg) {
    Bundle b = msg.getData();
    b.setClassLoader(getClassLoader());
    Resource r = b.getParcelable(RESOURCE);
    return r;/*from ww w .  j  av a  2  s  .co m*/
}

From source file:by.zatta.pilight.MainActivity.java

/**
 * Send data to the service/*  w ww  .j  a  v a2s  . c o m*/
 *
 * @param intvaluetosend The data to send
 */
private void sendMessageToService(String switchCommand) {
    Log.v(TAG, "switchCommand: " + switchCommand);
    if (mIsBound) {
        if (mServiceMessenger != null) {
            try {
                Bundle bundle = new Bundle();
                bundle.setClassLoader(this.getClassLoader());
                bundle.putString("command", switchCommand);
                Message msg_string = Message.obtain(null, ConnectionService.MSG_SWITCH_DEVICE);
                msg_string.setData(bundle);
                msg_string.replyTo = mMessenger;
                mServiceMessenger.send(msg_string);
            } catch (RemoteException e) {
            }
        }
    }
}

From source file:com.solera.defrag.ViewStack.java

/**
 * @param view the view to return the parameters from.
 * @return the start parameters of the view/presenter
 *///from   www  . ja  va  2s.c om
@Nullable
public Bundle getParameters(@NonNull Object view) {
    final Iterator<ViewStackEntry> viewStackEntryIterator = viewStack.descendingIterator();
    while (viewStackEntryIterator.hasNext()) {
        final ViewStackEntry viewStackEntry = viewStackEntryIterator.next();
        if (view == viewStackEntry.mViewReference.get()) {
            Bundle bundle = viewStackEntry.mParameters;
            bundle.setClassLoader(view.getClass().getClassLoader());
            return bundle;
        }
    }
    return null;
}

From source file:com.bmd.android.collection.SimpleArrayMapTest.java

public void testParcelable() {

    final ParcelableObjectSparseObjectEntry[] parcelableArray = AndroidCollections.iterate(mArray).only()
            .values("4", "1").toParcelableArray(ParcelableObjectSparseObjectEntry.class);

    assertThat(parcelableArray).hasSize(2);
    assertThat(parcelableArray[0].getKey()).isEqualTo(1);
    assertThat(parcelableArray[0].getValue()).isEqualTo("1");
    assertThat(parcelableArray[1].getKey()).isEqualTo(4);
    assertThat(parcelableArray[1].getValue()).isEqualTo("4");

    final ArrayList<ParcelableObjectSparseObjectEntry<Integer, String>> parcelableList = AndroidCollections
            .iterate(mArray).but().keys(Arrays.asList(1, 2, 3)).reverse().toParcelableList();

    assertThat(parcelableList).hasSize(2);
    assertThat(parcelableList.get(0).getKey()).isEqualTo(4);
    assertThat(parcelableList.get(0).getValue()).isEqualTo("4");
    assertThat(parcelableList.get(1).getKey()).isEqualTo(0);
    assertThat(parcelableList.get(1).getValue()).isEqualTo("0");
    assertThat(parcelableList).containsExactly(SparseEntries.parcelableEntry(Integer.valueOf(4), "4"),
            SparseEntries.parcelableEntry(Integer.valueOf(0), "0"));

    final Bundle bundle = new Bundle();
    bundle.putParcelableArray("array", parcelableArray);
    bundle.putParcelableArrayList("list", parcelableList);

    final Parcel parcel = Parcel.obtain();
    bundle.writeToParcel(parcel, 0);/* ww w.  j a v  a2 s.co m*/

    parcel.setDataPosition(0);

    final Bundle out = parcel.readBundle();
    out.setClassLoader(AndroidCollections.class.getClassLoader());

    assertThat(out.getParcelableArray("array")).isEqualTo(parcelableArray);
    assertThat(out.getParcelableArrayList("list")).isEqualTo(new ArrayList<Parcelable>(parcelableList));

    final ArrayList<ParcelableObjectSparseObjectEntry<Integer, String>> filledList = new ArrayList<ParcelableObjectSparseObjectEntry<Integer, String>>(
            2);

    AndroidCollections.iterate(mArray).but().keys(1, 2, 3).reverse().fillParcelable(filledList);

    assertThat(filledList).isEqualTo(parcelableList);

    final ParcelableObjectSparseObjectEntry[] filledArray = new ParcelableObjectSparseObjectEntry[2];

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(Integer.valueOf(2), "2"));

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray, 1);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(Integer.valueOf(2), "2"));
    assertThat(filledArray[0]).isEqualTo(filledArray[1]);

    try {

        AndroidCollections.iterate(mArray).fillParcelable(filledArray);

        fail();

    } catch (final IndexOutOfBoundsException e) {

    }

    final Parcelable[] parcelables = new Parcelable[2];

    AndroidCollections.iterate(mArray).only().to(1).fillParcelable(parcelables);

    assertThat(parcelables).containsExactly(filledArray);
}

From source file:com.bmd.android.collection.SparseArrayCompatTest.java

public void testParcelable() {

    final ParcelableIntSparseObjectEntry[] parcelableArray = AndroidCollections.iterate(mArray).only()
            .values("4", "1").toParcelableArray(ParcelableIntSparseObjectEntry.class);

    assertThat(parcelableArray).hasSize(2);
    assertThat(parcelableArray[0].getKey()).isEqualTo(1);
    assertThat(parcelableArray[0].getValue()).isEqualTo("1");
    assertThat(parcelableArray[1].getKey()).isEqualTo(4);
    assertThat(parcelableArray[1].getValue()).isEqualTo("4");

    final ArrayList<ParcelableIntSparseObjectEntry<String>> parcelableList = AndroidCollections.iterate(mArray)
            .but().keys(Arrays.asList(1, 2, 3)).reverse().toParcelableList();

    assertThat(parcelableList).hasSize(2);
    assertThat(parcelableList.get(0).getKey()).isEqualTo(4);
    assertThat(parcelableList.get(0).getValue()).isEqualTo("4");
    assertThat(parcelableList.get(1).getKey()).isEqualTo(0);
    assertThat(parcelableList.get(1).getValue()).isEqualTo("0");
    assertThat(parcelableList).containsExactly(SparseEntries.parcelableEntry(4, "4"),
            SparseEntries.parcelableEntry(0, "0"));

    final Bundle bundle = new Bundle();
    bundle.putParcelableArray("array", parcelableArray);
    bundle.putParcelableArrayList("list", parcelableList);

    final Parcel parcel = Parcel.obtain();
    bundle.writeToParcel(parcel, 0);// w w  w .j av  a2  s  .com

    parcel.setDataPosition(0);

    final Bundle out = parcel.readBundle();
    out.setClassLoader(AndroidCollections.class.getClassLoader());

    assertThat(out.getParcelableArray("array")).isEqualTo(parcelableArray);
    assertThat(out.getParcelableArrayList("list")).isEqualTo(new ArrayList<Parcelable>(parcelableList));

    final ArrayList<ParcelableIntSparseObjectEntry<String>> filledList = new ArrayList<ParcelableIntSparseObjectEntry<String>>(
            2);

    AndroidCollections.iterate(mArray).but().keys(1, 2, 3).reverse().fillParcelable(filledList);

    assertThat(filledList).isEqualTo(parcelableList);

    final ParcelableIntSparseObjectEntry[] filledArray = new ParcelableIntSparseObjectEntry[2];

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(2, "2"));

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray, 1);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(2, "2"));
    assertThat(filledArray[0]).isEqualTo(filledArray[1]);

    try {

        AndroidCollections.iterate(mArray).fillParcelable(filledArray);

        fail();

    } catch (final IndexOutOfBoundsException e) {

    }

    final Parcelable[] parcelables = new Parcelable[2];

    AndroidCollections.iterate(mArray).only().to(1).fillParcelable(parcelables);

    assertThat(parcelables).containsExactly(filledArray);
}

From source file:com.bmd.android.collection.SupportLongSparseArrayTest.java

public void testParcelable() {

    final ParcelableLongSparseObjectEntry[] parcelableArray = AndroidCollections.iterate(mArray).only()
            .values("4", "1").toParcelableArray(ParcelableLongSparseObjectEntry.class);

    assertThat(parcelableArray).hasSize(2);
    assertThat(parcelableArray[0].getKey()).isEqualTo(1);
    assertThat(parcelableArray[0].getValue()).isEqualTo("1");
    assertThat(parcelableArray[1].getKey()).isEqualTo(4);
    assertThat(parcelableArray[1].getValue()).isEqualTo("4");

    final ArrayList<ParcelableLongSparseObjectEntry<String>> parcelableList = AndroidCollections.iterate(mArray)
            .but().keys(Arrays.asList(1L, 2L, 3L)).reverse().toParcelableList();

    assertThat(parcelableList).hasSize(2);
    assertThat(parcelableList.get(0).getKey()).isEqualTo(4);
    assertThat(parcelableList.get(0).getValue()).isEqualTo("4");
    assertThat(parcelableList.get(1).getKey()).isEqualTo(0);
    assertThat(parcelableList.get(1).getValue()).isEqualTo("0");
    assertThat(parcelableList).containsExactly(SparseEntries.parcelableEntry(4L, "4"),
            SparseEntries.parcelableEntry(0L, "0"));

    final Bundle bundle = new Bundle();
    bundle.putParcelableArray("array", parcelableArray);
    bundle.putParcelableArrayList("list", parcelableList);

    final Parcel parcel = Parcel.obtain();
    bundle.writeToParcel(parcel, 0);//from   w ww . j av  a 2s.c  om

    parcel.setDataPosition(0);

    final Bundle out = parcel.readBundle();
    out.setClassLoader(AndroidCollections.class.getClassLoader());

    assertThat(out.getParcelableArray("array")).isEqualTo(parcelableArray);
    assertThat(out.getParcelableArrayList("list")).isEqualTo(new ArrayList<Parcelable>(parcelableList));

    final ArrayList<ParcelableLongSparseObjectEntry<String>> filledList = new ArrayList<ParcelableLongSparseObjectEntry<String>>(
            2);

    AndroidCollections.iterate(mArray).but().keys(1, 2, 3).reverse().fillParcelable(filledList);

    assertThat(filledList).isEqualTo(parcelableList);

    final ParcelableLongSparseObjectEntry[] filledArray = new ParcelableLongSparseObjectEntry[2];

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(2L, "2"));

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray, 1);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(2L, "2"));
    assertThat(filledArray[0]).isEqualTo(filledArray[1]);

    try {

        AndroidCollections.iterate(mArray).fillParcelable(filledArray);

        fail();

    } catch (final IndexOutOfBoundsException e) {

    }

    final Parcelable[] parcelables = new Parcelable[2];

    AndroidCollections.iterate(mArray).only().to(1).fillParcelable(parcelables);

    assertThat(parcelables).containsExactly(filledArray);
}

From source file:com.bmd.android.collection.LongSparseArrayTest.java

public void testParcelable() {

    if (VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN) {

        return;/*from   w w  w .  ja  va2  s . co  m*/
    }

    final ParcelableLongSparseObjectEntry[] parcelableArray = AndroidCollections.iterate(mArray).only()
            .values("4", "1").toParcelableArray(ParcelableLongSparseObjectEntry.class);

    assertThat(parcelableArray).hasSize(2);
    assertThat(parcelableArray[0].getKey()).isEqualTo(1);
    assertThat(parcelableArray[0].getValue()).isEqualTo("1");
    assertThat(parcelableArray[1].getKey()).isEqualTo(4);
    assertThat(parcelableArray[1].getValue()).isEqualTo("4");

    final ArrayList<ParcelableLongSparseObjectEntry<String>> parcelableList = AndroidCollections.iterate(mArray)
            .but().keys(Arrays.asList(1L, 2L, 3L)).reverse().toParcelableList();

    assertThat(parcelableList).hasSize(2);
    assertThat(parcelableList.get(0).getKey()).isEqualTo(4);
    assertThat(parcelableList.get(0).getValue()).isEqualTo("4");
    assertThat(parcelableList.get(1).getKey()).isEqualTo(0);
    assertThat(parcelableList.get(1).getValue()).isEqualTo("0");
    assertThat(parcelableList).containsExactly(SparseEntries.parcelableEntry(4L, "4"),
            SparseEntries.parcelableEntry(0L, "0"));

    final Bundle bundle = new Bundle();
    bundle.putParcelableArray("array", parcelableArray);
    bundle.putParcelableArrayList("list", parcelableList);

    final Parcel parcel = Parcel.obtain();
    bundle.writeToParcel(parcel, 0);

    parcel.setDataPosition(0);

    final Bundle out = parcel.readBundle();
    out.setClassLoader(AndroidCollections.class.getClassLoader());

    assertThat(out.getParcelableArray("array")).isEqualTo(parcelableArray);
    assertThat(out.getParcelableArrayList("list")).isEqualTo(new ArrayList<Parcelable>(parcelableList));

    final ArrayList<ParcelableLongSparseObjectEntry<String>> filledList = new ArrayList<ParcelableLongSparseObjectEntry<String>>(
            2);

    AndroidCollections.iterate(mArray).but().keys(1, 2, 3).reverse().fillParcelable(filledList);

    assertThat(filledList).isEqualTo(parcelableList);

    final ParcelableLongSparseObjectEntry[] filledArray = new ParcelableLongSparseObjectEntry[2];

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(2L, "2"));

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray, 1);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(2L, "2"));
    assertThat(filledArray[0]).isEqualTo(filledArray[1]);

    try {

        AndroidCollections.iterate(mArray).fillParcelable(filledArray);

        fail();

    } catch (final IndexOutOfBoundsException e) {

    }

    final Parcelable[] parcelables = new Parcelable[2];

    AndroidCollections.iterate(mArray).only().to(1).fillParcelable(parcelables);

    assertThat(parcelables).containsExactly(filledArray);
}

From source file:org.opensilk.music.library.provider.LibraryProvider.java

@Override
public final Bundle call(String method, String arg, Bundle extras) {

    final Bundle ok = new Bundle();
    ok.putBoolean(Extras.OK, true);//from   w w w  . j a v  a2  s.  com

    if (method == null)
        method = "";
    switch (method) {
    case LibraryMethods.QUERY: {
        extras.setClassLoader(getClass().getClassLoader());

        final IBinder binder = getBinderCallbackFromBundle(extras);
        if (binder == null || !binder.isBinderAlive()) {
            //this is mostly for the null, if the binder is dead then
            //sending them a reason is moot. but we check the binder here
            //so we dont have to do it 50 times below and we can be pretty
            //sure the linkToDeath will succeed
            ok.putBoolean(Extras.OK, false);
            writeCause(ok, new LibraryException(BAD_BINDER, null));
            return ok;
        }

        final Uri uri = extras.getParcelable(Extras.URI);
        final List<String> pathSegments = uri.getPathSegments();
        if (pathSegments.size() < 3 || pathSegments.size() > 5) {
            Log.e(TAG, "Wrong number of path segments: uri=" + uri);
            ok.putBoolean(Extras.OK, false);
            writeCause(ok, new LibraryException(ILLEGAL_URI, new IllegalArgumentException(uri.toString())));
            return ok;
        }

        final String library = pathSegments.get(0);
        final String identity;
        if (pathSegments.size() > 3) {
            identity = pathSegments.get(3);
        } else {
            identity = null;
        }

        final Bundle args = new Bundle();
        args.putParcelable(Extras.URI, uri);
        String sortOrder = extras.getString(Extras.SORTORDER);
        args.putString(Extras.SORTORDER, sortOrder != null ? sortOrder : BundleableSortOrder.A_Z);

        switch (mMatcher.match(uri)) {
        case M_ALBUMS: {
            final BundleableSubscriber<Album> subscriber = new BundleableSubscriber<>(binder);
            queryAlbumsInternal(library, subscriber, args);
            break;
        }
        case M_ALBUM: {
            final BundleableSubscriber<Album> subscriber = new BundleableSubscriber<>(binder);
            getAlbumInternal(library, identity, subscriber, args);
            break;
        }
        case M_ALBUM_TRACKS: {
            final BundleableSubscriber<Track> subscriber = new BundleableSubscriber<>(binder);
            getAlbumTracksInternal(library, identity, subscriber, args);
            break;
        }
        case M_ARTISTS: {
            final BundleableSubscriber<Artist> subscriber = new BundleableSubscriber<>(binder);
            queryArtistsInternal(library, subscriber, args);
            break;
        }
        case M_ARTIST: {
            final BundleableSubscriber<Artist> subscriber = new BundleableSubscriber<>(binder);
            getArtistInternal(library, identity, subscriber, args);
            break;
        }
        case M_ARTIST_ALBUMS: {
            final BundleableSubscriber<Album> subscriber = new BundleableSubscriber<>(binder);
            getArtistAlbumsInternal(library, identity, subscriber, args);
            break;
        }
        case M_ARTIST_TRACKS: {
            final BundleableSubscriber<Track> subscriber = new BundleableSubscriber<>(binder);
            getArtistTracksInternal(library, identity, subscriber, args);
            break;
        }
        case M_FOLDERS:
        case M_FOLDER: {
            final BundleableSubscriber<Bundleable> subscriber = new BundleableSubscriber<>(binder);
            browseFoldersInternal(library, identity, subscriber, args);
            break;
        }
        case M_GENRES: {
            final BundleableSubscriber<Genre> subscriber = new BundleableSubscriber<>(binder);
            queryGenresInternal(library, subscriber, args);
            break;
        }
        case M_GENRE: {
            final BundleableSubscriber<Genre> subscriber = new BundleableSubscriber<>(binder);
            getGenreInternal(library, identity, subscriber, args);
            break;
        }
        case M_GENRE_ALBUMS: {
            final BundleableSubscriber<Album> subscriber = new BundleableSubscriber<>(binder);
            getGenreAlbumsInternal(library, identity, subscriber, args);
            break;
        }
        case M_GENRE_TRACKS: {
            final BundleableSubscriber<Track> subscriber = new BundleableSubscriber<>(binder);
            getGenreTracksInternal(library, identity, subscriber, args);
            break;
        }
        case M_PLAYLISTS: {
            final BundleableSubscriber<Playlist> subscriber = new BundleableSubscriber<>(binder);
            queryPlaylistsInternal(library, subscriber, args);
            break;
        }
        case M_PLAYLIST: {
            final BundleableSubscriber<Playlist> subscriber = new BundleableSubscriber<>(binder);
            getPlaylistInternal(library, identity, subscriber, args);
            break;
        }
        case M_PLAYLIST_TRACKS: {
            final BundleableSubscriber<Track> subscriber = new BundleableSubscriber<>(binder);
            getPlaylistTracksInternal(library, identity, subscriber, args);
            break;
        }
        case M_TRACKS: {
            final BundleableSubscriber<Track> subscriber = new BundleableSubscriber<>(binder);
            queryTracksInternal(library, subscriber, args);
            break;
        }
        case M_TRACK: {
            final BundleableSubscriber<Track> subscriber = new BundleableSubscriber<>(binder);
            getTrackInternal(library, identity, subscriber, args);
            break;
        }
        default: {
            ok.putBoolean(Extras.OK, false);
            writeCause(ok, new LibraryException(ILLEGAL_URI, new IllegalArgumentException(uri.toString())));
        }
        }
        return ok;
    }
    case LibraryMethods.LIBRARYCONF:
        return getLibraryConfig().dematerialize();
    default:
        Log.e(TAG, "Unknown method " + method);
        ok.putBoolean(Extras.OK, false);
        writeCause(ok, new LibraryException(METHOD_NOT_IMPLEMENTED, new UnsupportedOperationException(method)));
        return ok;
    }
}