net.vivekiyer.GAL.CorporateContactRecord.java Source code

Java tutorial

Introduction

Here is the source code for net.vivekiyer.GAL.CorporateContactRecord.java

Source

/* Copyright 2010 Vivek Iyer
 * 
 * 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 net.vivekiyer.GAL;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.support.v4.app.FragmentActivity;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SearchView;

/**
 * @author Vivek Iyer 
 * This class is used to display a Contact object in a list.
 * The class takes a parceled Contact object and displays the
 * DisplayName. It also allows the user to save the contact
 */
/**
 * @author vivek
 * 
 */
public class CorporateContactRecord extends FragmentActivity {

    private SearchView searchView;

    @TargetApi(11)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_fragment);

        final Bundle b = getIntent().getExtras();

        Contact contact = b.getParcelable("net.vivekiyer.GAL"); //$NON-NLS-1$

        CorporateContactRecordFragment contacts = (CorporateContactRecordFragment) getSupportFragmentManager()
                .findFragmentById(R.id.contact_fragment);
        contacts.setIsDualFrame(false);
        contacts.setContact(contact);

        if (!Utility.isPreHoneycomb()) {
            final ActionBar actionBar = getActionBar();
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    /**
     * @param menu
     * @param v
     * @param menuInfo
     * 
     *            Create a context menu for the list view Depending upon the
     *            item selected, shows the user different options
     */

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
     * 
     * Displays the menu when the user clicks the Options button In our case the
     * menu contains only one button - save
     */
    @TargetApi(11)
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        final MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.contacts_menu, menu);

        // Get the SearchView and set the searchable configuration for Honeycomb
        // and above
        if (!Utility.isPreHoneycomb()) {
            final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            final ComponentName component = getComponentName();
            final SearchableInfo searchableInfo = searchManager.getSearchableInfo(component);
            searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
            searchView.setSearchableInfo(searchableInfo);
        }

        return true;
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
     * 
     * Launches the preferences pane when the user clicks the settings option
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case android.R.id.home:
            // app icon in action bar clicked; go home
            final Intent intent = new Intent(this, net.vivekiyer.GAL.CorporateAddressBook.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;
        case R.id.menu_search:
            return this.onSearchRequested();
        case R.id.settings:
            CorporateAddressBook.showConfiguration(this);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @TargetApi(11)
    @Override
    public boolean onSearchRequested() {
        if (!Utility.isPreHoneycomb()) {
            if (searchView != null) {
                searchView.setFocusable(true);
                searchView.setIconified(false);
                searchView.requestFocusFromTouch();
                return true;
            } else {
                Debug.Log("Running HC+ without SearchView"); //$NON-NLS-1$
                return false;
            }
        }
        return super.onSearchRequested();
    }

}