Android Open Source - Android_Yellow_Pages_App_Content_Provider Main Activity






From Project

Back to project page Android_Yellow_Pages_App_Content_Provider.

License

The source code is released under:

Apache License

If you think the Android project Android_Yellow_Pages_App_Content_Provider 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

package com.tddrampup.activities;
/*ww w .ja va  2 s  . c o m*/
import android.app.ProgressDialog;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import com.tddrampup.R;
import com.tddrampup.contentProviders.YellowContentProvider;
import com.tddrampup.databases.ListingsTable;
import com.tddrampup.databases.ListingsTableHelper;
import com.tddrampup.databases.SearchTable;
import com.tddrampup.databases.SearchTableHelper;
import com.tddrampup.fragments.DetailFragment;
import com.tddrampup.fragments.GoogleMapFragment;
import com.tddrampup.fragments.HomeFragment;
import com.tddrampup.fragments.ListFragment;
import com.tddrampup.models.Listing;
import com.tddrampup.serviceLayers.VolleyServiceLayer;
import com.tddrampup.serviceLayers.VolleyServiceLayerCallback;

import java.util.List;

import roboguice.activity.RoboFragmentActivity;

public class MainActivity extends RoboFragmentActivity implements HomeFragment.onItemClickedListener, ListFragment.onListViewItemClickedListener {

    private VolleyServiceLayer volleyServiceLayer;
    private ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        volleyServiceLayer = new VolleyServiceLayer(getApplicationContext());
        mProgressDialog = new ProgressDialog(this);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().add(R.id.main_activity, new HomeFragment(), "MY_HOME_FRAGMENT").commit();
        }
    }

    @Override
    public void onListButtonClicked(){
        ListFragment listFragment = new ListFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.main_activity, listFragment, "MY_LIST_FRAGMENT");
        transaction.addToBackStack(null);
        transaction.commit();
    }

    @Override
    public void onMapButtonClicked(){
        GoogleMapFragment googleMapFragment = new GoogleMapFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.main_activity, googleMapFragment, "MY_GOOGLE_MAP_FRAGMENT");
        transaction.addToBackStack(null);
        transaction.commit();
    }

    @Override
    public void onSearchButtonClicked() {
        EditText whatEditText = (EditText) findViewById(R.id.what_editText);
        EditText whereEditText = (EditText) findViewById(R.id.where_editText);

        String what = whatEditText.getText().toString();
        String where = whereEditText.getText().toString();

        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow((whatEditText.isSelected() ? whatEditText : whereEditText).getWindowToken(), 0);

        if(!what.isEmpty() && !where.isEmpty()) {
            ListFragment searchFragment = new ListFragment(what, where);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.main_activity, searchFragment, "MY_SEARCH_FRAGMENT");
            transaction.addToBackStack(null);
            transaction.commit();
        } else {
            Toast.makeText(this, "Please fill in both fields!", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onListViewItemClicked(int position, boolean isSearchQuery) {
        Cursor cursor;
        int listingIdIndex;
        int websiteUrlIndex;

        if (isSearchQuery) {
            cursor = getContentResolver().query(YellowContentProvider.CONTENT_URI_SEARCH_LISTINGS, SearchTableHelper.searchTableItemClickProjection, null, null, null);
            listingIdIndex = cursor.getColumnIndex(SearchTable.COLUMN_LISTING_ID);
            websiteUrlIndex = cursor.getColumnIndex(SearchTable.COLUMN_MERCHANT_URL);
        } else {
            cursor = getContentResolver().query(YellowContentProvider.CONTENT_URI_LISTINGS, ListingsTableHelper.listingsTableItemClickProjection, null, null, null);
            listingIdIndex = cursor.getColumnIndex(ListingsTable.COLUMN_LISTING_ID);
            websiteUrlIndex = cursor.getColumnIndex(ListingsTable.COLUMN_MERCHANT_URL);
        }
        cursor.moveToPosition(position);
        String listingId = cursor.getString(listingIdIndex);
        String websiteUrl = cursor.getString(websiteUrlIndex);
        cursor.close();

        if (websiteUrl.isEmpty()) {
            showLoading();
            volleyServiceLayer.volleyServiceLayerCallback = new Callback();
            volleyServiceLayer.GetListing(listingId, isSearchQuery);
        } else {
            launchDetailFragment(listingId, isSearchQuery);
        }
    }

    public class Callback implements VolleyServiceLayerCallback {
        public void listCallbackCall(List<Listing> listings) {
            // do nothing
        }

        @Override
        public void itemCallbackCall(Listing listing, Boolean isSearchQuery) {
            hideLoading();
            String listingId = listing.getId();
            if (isSearchQuery) {
                SearchTableHelper.updateListing(listing, getApplicationContext());
            } else {
                ListingsTableHelper.updateListing(listing, getApplicationContext());
            }
            launchDetailFragment(listingId, isSearchQuery);
        }
    }

    private void launchDetailFragment(String listingId, Boolean isSearchQuery) {
        DetailFragment detailFragment = new DetailFragment(listingId, isSearchQuery);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.main_activity, detailFragment, "MY_DETAIL_FRAGMENT");
        transaction.addToBackStack(null);
        transaction.commit();
    }

    private void showLoading() {
        mProgressDialog.setTitle("Loading:");
        mProgressDialog.setMessage("Fetching details...");
        mProgressDialog.show();
    }

    private void hideLoading() {
        mProgressDialog.dismiss();
    }

    public boolean isProgressDialogShowing() {
        return mProgressDialog.isShowing();
    }
}




Java Source Code List

android.UnusedStub.java
com.tddrampup.ApplicationModule.java
com.tddrampup.YellowApplication.java
com.tddrampup.activities.MainActivity.java
com.tddrampup.contentProviders.YellowContentProvider.java
com.tddrampup.databases.ListingsTableHelper.java
com.tddrampup.databases.ListingsTable.java
com.tddrampup.databases.PreviousQueryTableHelper.java
com.tddrampup.databases.PreviousQueryTable.java
com.tddrampup.databases.SearchTableHelper.java
com.tddrampup.databases.SearchTable.java
com.tddrampup.databases.YellowDatabaseHelper.java
com.tddrampup.factories.CameraUpdateFactoryWrapperInterface.java
com.tddrampup.factories.CameraUpdateFactoryWrapper.java
com.tddrampup.factories.MarkerOptionsFactoryWrapperInterface.java
com.tddrampup.factories.MarkerOptionsFactoryWrapper.java
com.tddrampup.fragments.DetailFragment.java
com.tddrampup.fragments.GoogleMapFragment.java
com.tddrampup.fragments.HomeFragment.java
com.tddrampup.fragments.ListFragment.java
com.tddrampup.models.Address.java
com.tddrampup.models.GeoCode.java
com.tddrampup.models.Keywords.java
com.tddrampup.models.Listing.java
com.tddrampup.models.Phone.java
com.tddrampup.models.Products.java
com.tddrampup.models.Profile.java
com.tddrampup.models.Summary.java
com.tddrampup.models.YellowResponse.java
com.tddrampup.serviceLayers.VolleyServiceLayerCallback.java
com.tddrampup.serviceLayers.VolleyServiceLayer.java
com.tddrampup.views.MapView.java