Android Open Source - iosched2011 Vendors Fragment






From Project

Back to project page iosched2011.

License

The source code is released under:

Apache License

If you think the Android project iosched2011 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2011 Google Inc.// ww  w  .j  a va 2 s. c o m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.apps.iosched.ui;

import com.google.android.apps.iosched.R;
import com.google.android.apps.iosched.provider.ScheduleContract;
import com.google.android.apps.iosched.util.ActivityHelper;
import com.google.android.apps.iosched.util.AnalyticsUtils;
import com.google.android.apps.iosched.util.NotifyingAsyncQueryHandler;

import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.BaseColumns;
import android.support.v4.app.ListFragment;
import android.text.Spannable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;

import static com.google.android.apps.iosched.util.UIUtils.buildStyledSnippet;

/**
 * A {@link ListFragment} showing a list of sandbox comapnies.
 */
public class VendorsFragment extends ListFragment implements
        NotifyingAsyncQueryHandler.AsyncQueryListener {

    private static final String STATE_CHECKED_POSITION = "checkedPosition";

    private Uri mTrackUri;
    private Cursor mCursor;
    private CursorAdapter mAdapter;
    private int mCheckedPosition = -1;
    private boolean mHasSetEmptyText = false;

    private NotifyingAsyncQueryHandler mHandler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mHandler = new NotifyingAsyncQueryHandler(getActivity().getContentResolver(), this);
        reloadFromArguments(getArguments());
    }

    public void reloadFromArguments(Bundle arguments) {
        // Teardown from previous arguments
        if (mCursor != null) {
            getActivity().stopManagingCursor(mCursor);
            mCursor = null;
        }

        mCheckedPosition = -1;
        setListAdapter(null);

        mHandler.cancelOperation(SearchQuery._TOKEN);
        mHandler.cancelOperation(VendorsQuery._TOKEN);

        // Load new arguments
        final Intent intent = BaseActivity.fragmentArgumentsToIntent(arguments);
        final Uri vendorsUri = intent.getData();
        final int vendorQueryToken;

        if (vendorsUri == null) {
            return;
        }

        String[] projection;
        if (!ScheduleContract.Vendors.isSearchUri(vendorsUri)) {
            mAdapter = new VendorsAdapter(getActivity());
            projection = VendorsQuery.PROJECTION;
            vendorQueryToken = VendorsQuery._TOKEN;

        } else {
            Log.d("VendorsFragment/reloadFromArguments", "A search URL definitely gets passed in.");
            mAdapter = new SearchAdapter(getActivity());
            projection = SearchQuery.PROJECTION;
            vendorQueryToken = SearchQuery._TOKEN;
        }

        setListAdapter(mAdapter);

        // Start background query to load vendors
        mHandler.startQuery(vendorQueryToken, null, vendorsUri, projection, null, null,
                ScheduleContract.Vendors.DEFAULT_SORT);

        // If caller launched us with specific track hint, pass it along when
        // launching vendor details. Also start a query to load the track info.
        mTrackUri = intent.getParcelableExtra(SessionDetailFragment.EXTRA_TRACK);
        if (mTrackUri != null) {
            mHandler.startQuery(TracksQuery._TOKEN, mTrackUri, TracksQuery.PROJECTION);
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        if (savedInstanceState != null) {
            mCheckedPosition = savedInstanceState.getInt(STATE_CHECKED_POSITION, -1);
        }

        if (!mHasSetEmptyText) {
            // Could be a bug, but calling this twice makes it become visible when it shouldn't
            // be visible.
            setEmptyText(getString(R.string.empty_vendors));
            mHasSetEmptyText = true;
        }
    }


    /** {@inheritDoc} */
    public void onQueryComplete(int token, Object cookie, Cursor cursor) {
        if (getActivity() == null) {
            return;
        }

        if (token == VendorsQuery._TOKEN || token == SearchQuery._TOKEN) {
            onVendorsOrSearchQueryComplete(cursor);
        } else if (token == TracksQuery._TOKEN) {
            onTrackQueryComplete(cursor);
        } else {
            cursor.close();
        }
    }

    /**
     * Handle {@link VendorsQuery} {@link Cursor}.
     */
    private void onVendorsOrSearchQueryComplete(Cursor cursor) {
        // TODO(romannurik): stopManagingCursor on detach (throughout app)
        mCursor = cursor;
        getActivity().startManagingCursor(mCursor);
        mAdapter.changeCursor(mCursor);
        if (mCheckedPosition >= 0 && getView() != null) {
            getListView().setItemChecked(mCheckedPosition, true);
        }
    }

    /**
     * Handle {@link TracksQuery} {@link Cursor}.
     */
    private void onTrackQueryComplete(Cursor cursor) {
        try {
            if (!cursor.moveToFirst()) {
                return;
            }

            // Use found track to build title-bar
            ActivityHelper activityHelper = ((BaseActivity) getActivity()).getActivityHelper();
            String trackName = cursor.getString(TracksQuery.TRACK_NAME);
            activityHelper.setActionBarTitle(trackName);
            activityHelper.setActionBarColor(cursor.getInt(TracksQuery.TRACK_COLOR));

            AnalyticsUtils.getInstance(getActivity()).trackPageView("/Sandbox/Track/" + trackName);

        } finally {
            cursor.close();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getActivity().getContentResolver().registerContentObserver(
                ScheduleContract.Vendors.CONTENT_URI, true, mVendorChangesObserver);
        if (mCursor != null) {
            mCursor.requery();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        getActivity().getContentResolver().unregisterContentObserver(mVendorChangesObserver);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(STATE_CHECKED_POSITION, mCheckedPosition);
    }

    /** {@inheritDoc} */
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Launch viewer for specific vendor.
        final Cursor cursor = (Cursor)mAdapter.getItem(position);
        final String vendorId = cursor.getString(VendorsQuery.VENDOR_ID);
        final Uri vendorUri = ScheduleContract.Vendors.buildVendorUri(vendorId);
        ((BaseActivity) getActivity()).openActivityOrFragment(new Intent(Intent.ACTION_VIEW,
                vendorUri));

        getListView().setItemChecked(position, true);
        mCheckedPosition = position;
    }

    public void clearCheckedPosition() {
        if (mCheckedPosition >= 0) {
            getListView().setItemChecked(mCheckedPosition, false);
            mCheckedPosition = -1;
        }
    }

    /**
     * {@link CursorAdapter} that renders a {@link VendorsQuery}.
     */
    private class VendorsAdapter extends CursorAdapter {
        public VendorsAdapter(Context context) {
            super(context, null);
        }

        /** {@inheritDoc} */
        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            return getActivity().getLayoutInflater().inflate(R.layout.list_item_vendor_oneline,
                    parent, false);
        }

        /** {@inheritDoc} */
        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            ((TextView) view.findViewById(R.id.vendor_name)).setText(
                    cursor.getString(VendorsQuery.NAME));

            final boolean starred = cursor.getInt(VendorsQuery.STARRED) != 0;
            view.findViewById(R.id.star_button).setVisibility(
                    starred ? View.VISIBLE : View.INVISIBLE);
        }
    }

    /**
     * {@link CursorAdapter} that renders a {@link SearchQuery}.
     */
    private class SearchAdapter extends CursorAdapter {
        public SearchAdapter(Context context) {
            super(context, null);
        }

        /** {@inheritDoc} */
        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            return getActivity().getLayoutInflater().inflate(R.layout.list_item_vendor, parent,
                    false);
        }

        /** {@inheritDoc} */ 
        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            ((TextView) view.findViewById(R.id.vendor_name)).setText(cursor
                    .getString(SearchQuery.NAME));

            final String snippet = cursor.getString(SearchQuery.SEARCH_SNIPPET);
            final Spannable styledSnippet = buildStyledSnippet(snippet);
            ((TextView) view.findViewById(R.id.vendor_location)).setText(styledSnippet);

            final boolean starred = cursor.getInt(VendorsQuery.STARRED) != 0;
            view.findViewById(R.id.star_button).setVisibility(
                    starred ? View.VISIBLE : View.INVISIBLE);
        }
    }

    private ContentObserver mVendorChangesObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
            if (mCursor != null) {
                mCursor.requery();
            }
        }
    };

    /**
     * {@link com.google.android.apps.iosched.provider.ScheduleContract.Vendors} query parameters.
     */
    private interface VendorsQuery {
        int _TOKEN = 0x1;

        String[] PROJECTION = {
                BaseColumns._ID,
                ScheduleContract.Vendors.VENDOR_ID,
                ScheduleContract.Vendors.VENDOR_NAME,
                ScheduleContract.Vendors.VENDOR_LOCATION,
                ScheduleContract.Vendors.VENDOR_STARRED,
        };

        int _ID = 0;
        int VENDOR_ID = 1;
        int NAME = 2;
        int LOCATION = 3;
        int STARRED = 4;
    }

    /**
     * {@link com.google.android.apps.iosched.provider.ScheduleContract.Tracks} query parameters.
     */
    private interface TracksQuery {
        int _TOKEN = 0x2;

        String[] PROJECTION = {
                ScheduleContract.Tracks.TRACK_NAME,
                ScheduleContract.Tracks.TRACK_COLOR,
        };

        int TRACK_NAME = 0;
        int TRACK_COLOR = 1;
    }

    /** {@link com.google.android.apps.iosched.provider.ScheduleContract.Vendors} search query
     * parameters. */
    private interface SearchQuery {
        int _TOKEN = 0x3;

        String[] PROJECTION = {
                BaseColumns._ID,
                ScheduleContract.Vendors.VENDOR_ID,
                ScheduleContract.Vendors.VENDOR_NAME,
                ScheduleContract.Vendors.SEARCH_SNIPPET,
                ScheduleContract.Vendors.VENDOR_STARRED,
        };

        int _ID = 0;
        int VENDOR_ID = 1;
        int NAME = 2;
        int SEARCH_SNIPPET = 3;
        int STARRED = 4;
    }
}




Java Source Code List

com.google.android.apps.iosched.io.LocalBlocksHandler.java
com.google.android.apps.iosched.io.LocalExecutor.java
com.google.android.apps.iosched.io.LocalRoomsHandler.java
com.google.android.apps.iosched.io.LocalSearchSuggestHandler.java
com.google.android.apps.iosched.io.LocalSessionsHandler.java
com.google.android.apps.iosched.io.LocalTracksHandler.java
com.google.android.apps.iosched.io.RemoteExecutor.java
com.google.android.apps.iosched.io.RemoteSessionsHandler.java
com.google.android.apps.iosched.io.RemoteSpeakersHandler.java
com.google.android.apps.iosched.io.RemoteVendorsHandler.java
com.google.android.apps.iosched.io.RemoteWorksheetsHandler.java
com.google.android.apps.iosched.io.SessionsHandlerTest.java
com.google.android.apps.iosched.io.StubHttpClient.java
com.google.android.apps.iosched.io.XmlHandler.java
com.google.android.apps.iosched.provider.ScheduleContract.java
com.google.android.apps.iosched.provider.ScheduleDatabase.java
com.google.android.apps.iosched.provider.ScheduleProvider.java
com.google.android.apps.iosched.service.SyncService.java
com.google.android.apps.iosched.ui.BaseActivity.java
com.google.android.apps.iosched.ui.BaseMultiPaneActivity.java
com.google.android.apps.iosched.ui.BaseSinglePaneActivity.java
com.google.android.apps.iosched.ui.BulletinActivity.java
com.google.android.apps.iosched.ui.BulletinFragment.java
com.google.android.apps.iosched.ui.DashboardFragment.java
com.google.android.apps.iosched.ui.HomeActivity.java
com.google.android.apps.iosched.ui.MapFragment.java
com.google.android.apps.iosched.ui.ScheduleFragment.java
com.google.android.apps.iosched.ui.SearchActivity.java
com.google.android.apps.iosched.ui.SessionDetailFragment.java
com.google.android.apps.iosched.ui.SessionsFragment.java
com.google.android.apps.iosched.ui.StarredActivity.java
com.google.android.apps.iosched.ui.TagStreamActivity.java
com.google.android.apps.iosched.ui.TagStreamFragment.java
com.google.android.apps.iosched.ui.TracksAdapter.java
com.google.android.apps.iosched.ui.TracksFragment.java
com.google.android.apps.iosched.ui.VendorDetailFragment.java
com.google.android.apps.iosched.ui.VendorsFragment.java
com.google.android.apps.iosched.ui.WhatsOnFragment.java
com.google.android.apps.iosched.ui.phone.MapActivity.java
com.google.android.apps.iosched.ui.phone.ScheduleActivity.java
com.google.android.apps.iosched.ui.phone.SessionDetailActivity.java
com.google.android.apps.iosched.ui.phone.SessionsActivity.java
com.google.android.apps.iosched.ui.phone.TracksActivity.java
com.google.android.apps.iosched.ui.phone.VendorDetailActivity.java
com.google.android.apps.iosched.ui.phone.VendorsActivity.java
com.google.android.apps.iosched.ui.tablet.MapMultiPaneActivity.java
com.google.android.apps.iosched.ui.tablet.NowPlayingMultiPaneActivity.java
com.google.android.apps.iosched.ui.tablet.ScheduleMultiPaneActivity.java
com.google.android.apps.iosched.ui.tablet.SessionsMultiPaneActivity.java
com.google.android.apps.iosched.ui.tablet.TracksDropdownFragment.java
com.google.android.apps.iosched.ui.tablet.VendorsMultiPaneActivity.java
com.google.android.apps.iosched.ui.widget.BezelImageView.java
com.google.android.apps.iosched.ui.widget.BlockView.java
com.google.android.apps.iosched.ui.widget.BlocksLayout.java
com.google.android.apps.iosched.ui.widget.DashboardLayout.java
com.google.android.apps.iosched.ui.widget.ObservableScrollView.java
com.google.android.apps.iosched.ui.widget.TimeRulerView.java
com.google.android.apps.iosched.ui.widget.Workspace.java
com.google.android.apps.iosched.util.ActivityHelperHoneycomb.java
com.google.android.apps.iosched.util.ActivityHelper.java
com.google.android.apps.iosched.util.AnalyticsUtils.java
com.google.android.apps.iosched.util.BitmapUtils.java
com.google.android.apps.iosched.util.CatchNotesHelper.java
com.google.android.apps.iosched.util.DetachableResultReceiver.java
com.google.android.apps.iosched.util.EulaHelper.java
com.google.android.apps.iosched.util.FractionalTouchDelegate.java
com.google.android.apps.iosched.util.Lists.java
com.google.android.apps.iosched.util.Maps.java
com.google.android.apps.iosched.util.MotionEventUtils.java
com.google.android.apps.iosched.util.NotifyingAsyncQueryHandler.java
com.google.android.apps.iosched.util.ParserUtils.java
com.google.android.apps.iosched.util.ReflectionUtils.java
com.google.android.apps.iosched.util.SelectionBuilder.java
com.google.android.apps.iosched.util.Sets.java
com.google.android.apps.iosched.util.SimpleMenuItem.java
com.google.android.apps.iosched.util.SimpleMenu.java
com.google.android.apps.iosched.util.SpreadsheetEntryTest.java
com.google.android.apps.iosched.util.SpreadsheetEntry.java
com.google.android.apps.iosched.util.UIUtils.java
com.google.android.apps.iosched.util.WorksheetEntry.java